@studyportals/sp-hs-misc
Version:
Miscellaneous code used in HouseStark's projects
125 lines • 5.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CognitoAuthenticationServicesProvider = void 0;
const amazon_cognito_identity_js_1 = require("amazon-cognito-identity-js");
const __1 = require("../..");
const failed_authentication_result_class_1 = require("./failed-authentication-result.class");
const successful_authentication_result_class_1 = require("./successful-authentication-result.class");
/**
* @deprecated Use @studyportals/client-internal-platform-sso
*/
class CognitoAuthenticationServicesProvider {
static USER_NOT_FOUND_ERROR_CODE = "UserNotFoundException";
static INCORRECT_SECRET_ERROR_CODE = "NotAuthorizedException";
_cognitoUserPoolId;
_appClientId;
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 amazon_cognito_identity_js_1.CognitoUser({
Username: username,
Pool: this.createUserPool()
});
}
createUserFromUserPool(username, userPool) {
return new amazon_cognito_identity_js_1.CognitoUser({
Username: username,
Pool: userPool
});
}
createAuthenticationDetails(username, password) {
return new amazon_cognito_identity_js_1.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 successful_authentication_result_class_1.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 failed_authentication_result_class_1.FailedAuthenticationResult(__1.AuthenticationFailureReason.USER_DOES_NOT_EXIST);
}
createResultForIncorrectSecret() {
return new failed_authentication_result_class_1.FailedAuthenticationResult(__1.AuthenticationFailureReason.INCORRECT_SECRET);
}
createUserPool() {
return new amazon_cognito_identity_js_1.CognitoUserPool({
UserPoolId: this.cognitoUserPoolId,
ClientId: this.appClientId
});
}
}
exports.CognitoAuthenticationServicesProvider = CognitoAuthenticationServicesProvider;
//# sourceMappingURL=cognito-authentication-services-provider.class.js.map