UNPKG

@studyportals/sp-hs-misc

Version:

Miscellaneous code used in HouseStark's projects

134 lines 6.22 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; 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 { 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(); } }); }); } changePassword(userIdentifier, oldSecret, newSecret) { return __awaiter(this, void 0, void 0, function* () { 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; CognitoAuthenticationServicesProvider.USER_NOT_FOUND_ERROR_CODE = "UserNotFoundException"; CognitoAuthenticationServicesProvider.INCORRECT_SECRET_ERROR_CODE = "NotAuthorizedException"; //# sourceMappingURL=cognito-authentication-services-provider.class.js.map