@studyportals/sp-hs-misc
Version:
Miscellaneous code used in HouseStark's projects
120 lines • 5.03 kB
JavaScript
import { CognitoUser, CognitoUserPool, AuthenticationDetails } from 'amazon-cognito-identity-js';
import { AuthenticationFailureReason, } from "../..";
import { FailedAuthenticationResult } from "./failed-authentication-result.class";
import { SuccessfulAuthenticationResult } from "./successful-authentication-result.class";
/**
* @deprecated Use @studyportals/client-internal-platform-sso
*/
class CognitoAuthenticationServicesProvider {
get cognitoUserPoolId() {
return this._cognitoUserPoolId;
}
get appClientId() {
return this._appClientId;
}
constructor(cognitoUserPoolId = "", appClientId = "") {
this._cognitoUserPoolId = cognitoUserPoolId;
this._appClientId = appClientId;
}
authenticate(userIdentifier, secret) {
const lowerCaseUserIdentifier = userIdentifier.toLowerCase();
const userPool = this.createUserPool();
const user = this.createUserFromUserPool(lowerCaseUserIdentifier, userPool);
const authenticationDetails = this.createAuthenticationDetails(lowerCaseUserIdentifier, secret);
return new Promise((resolve, reject) => {
user.authenticateUser(authenticationDetails, {
onSuccess: session => {
resolve(this.createSuccessfulAuthenticationResultFromCognitoUseSession(lowerCaseUserIdentifier, session));
},
onFailure: error => {
if (this.authenticationFailedBecauseUserDoesNotExist(error)) {
resolve(this.createResultForUserDoesNotExist());
}
else if (this.authenticationFailedBecauseTheSecretIsIncorrect(error)) {
resolve(this.createResultForIncorrectSecret());
}
else {
reject(error);
}
}
});
});
}
registerUser(userIdentifier, secret) {
const userPool = this.createUserPool();
const lowerCaseUserIdentifier = userIdentifier.toLowerCase();
return new Promise((resolve, reject) => {
userPool.signUp(lowerCaseUserIdentifier, secret, [], [], (err, res) => {
if (err) {
reject(err);
}
else {
resolve();
}
});
});
}
async changePassword(userIdentifier, oldSecret, newSecret) {
const lowerCaseUserIdentifier = userIdentifier.toLowerCase();
const user = this.createUser(lowerCaseUserIdentifier);
return new Promise((resolve, reject) => {
user.changePassword(oldSecret, newSecret, (error, success) => {
if (error) {
reject(error);
}
else {
resolve();
}
});
});
}
createUser(username) {
return new CognitoUser({
Username: username,
Pool: this.createUserPool()
});
}
createUserFromUserPool(username, userPool) {
return new CognitoUser({
Username: username,
Pool: userPool
});
}
createAuthenticationDetails(username, password) {
return new AuthenticationDetails({
Username: username,
Password: password
});
}
createSuccessfulAuthenticationResultFromCognitoUseSession(username, session) {
const oneHourInMs = 3600000;
const idToken = session.getIdToken();
const idTokenAvailabilityInMs = oneHourInMs;
const idTokenTimeOfCreation = new Date();
const refreshToken = session.getRefreshToken();
const authenticationResult = new SuccessfulAuthenticationResult(username, idToken.getJwtToken(), refreshToken.getToken(), idTokenAvailabilityInMs, idTokenTimeOfCreation);
return authenticationResult;
}
authenticationFailedBecauseUserDoesNotExist(error) {
return CognitoAuthenticationServicesProvider.USER_NOT_FOUND_ERROR_CODE === error.code;
}
authenticationFailedBecauseTheSecretIsIncorrect(error) {
return CognitoAuthenticationServicesProvider.INCORRECT_SECRET_ERROR_CODE === error.code;
}
createResultForUserDoesNotExist() {
return new FailedAuthenticationResult(AuthenticationFailureReason.USER_DOES_NOT_EXIST);
}
createResultForIncorrectSecret() {
return new FailedAuthenticationResult(AuthenticationFailureReason.INCORRECT_SECRET);
}
createUserPool() {
return new CognitoUserPool({
UserPoolId: this.cognitoUserPoolId,
ClientId: this.appClientId
});
}
}
CognitoAuthenticationServicesProvider.USER_NOT_FOUND_ERROR_CODE = "UserNotFoundException";
CognitoAuthenticationServicesProvider.INCORRECT_SECRET_ERROR_CODE = "NotAuthorizedException";
export { CognitoAuthenticationServicesProvider };
//# sourceMappingURL=cognito-authentication-services-provider.class.js.map