@studyportals/sp-hs-misc
Version:
Miscellaneous code used in HouseStark's projects
200 lines (188 loc) • 8.85 kB
JavaScript
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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());
});
};
/**
* @deprecated Use @studyportals/client-internal-platform-sso
*/
var CognitoAuthenticationServicesProvider = function () {
_createClass(CognitoAuthenticationServicesProvider, [{
key: "cognitoUserPoolId",
get: function get() {
return this._cognitoUserPoolId;
}
}, {
key: "appClientId",
get: function get() {
return this._appClientId;
}
}]);
function CognitoAuthenticationServicesProvider() {
var cognitoUserPoolId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var appClientId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
_classCallCheck(this, CognitoAuthenticationServicesProvider);
this._cognitoUserPoolId = cognitoUserPoolId;
this._appClientId = appClientId;
}
_createClass(CognitoAuthenticationServicesProvider, [{
key: "authenticate",
value: function authenticate(userIdentifier, secret) {
var _this = this;
var lowerCaseUserIdentifier = userIdentifier.toLowerCase();
var userPool = this.createUserPool();
var user = this.createUserFromUserPool(lowerCaseUserIdentifier, userPool);
var authenticationDetails = this.createAuthenticationDetails(lowerCaseUserIdentifier, secret);
return new Promise(function (resolve, reject) {
user.authenticateUser(authenticationDetails, {
onSuccess: function onSuccess(session) {
resolve(_this.createSuccessfulAuthenticationResultFromCognitoUseSession(lowerCaseUserIdentifier, session));
},
onFailure: function onFailure(error) {
if (_this.authenticationFailedBecauseUserDoesNotExist(error)) {
resolve(_this.createResultForUserDoesNotExist());
} else if (_this.authenticationFailedBecauseTheSecretIsIncorrect(error)) {
resolve(_this.createResultForIncorrectSecret());
} else {
reject(error);
}
}
});
});
}
}, {
key: "registerUser",
value: function registerUser(userIdentifier, secret) {
var userPool = this.createUserPool();
var lowerCaseUserIdentifier = userIdentifier.toLowerCase();
return new Promise(function (resolve, reject) {
userPool.signUp(lowerCaseUserIdentifier, secret, [], [], function (err, res) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
}, {
key: "changePassword",
value: function changePassword(userIdentifier, oldSecret, newSecret) {
return __awaiter(this, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
var lowerCaseUserIdentifier, user;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
lowerCaseUserIdentifier = userIdentifier.toLowerCase();
user = this.createUser(lowerCaseUserIdentifier);
return _context.abrupt("return", new Promise(function (resolve, reject) {
user.changePassword(oldSecret, newSecret, function (error, success) {
if (error) {
reject(error);
} else {
resolve();
}
});
}));
case 3:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
}
}, {
key: "createUser",
value: function createUser(username) {
return new CognitoUser({
Username: username,
Pool: this.createUserPool()
});
}
}, {
key: "createUserFromUserPool",
value: function createUserFromUserPool(username, userPool) {
return new CognitoUser({
Username: username,
Pool: userPool
});
}
}, {
key: "createAuthenticationDetails",
value: function createAuthenticationDetails(username, password) {
return new AuthenticationDetails({
Username: username,
Password: password
});
}
}, {
key: "createSuccessfulAuthenticationResultFromCognitoUseSession",
value: function createSuccessfulAuthenticationResultFromCognitoUseSession(username, session) {
var oneHourInMs = 3600000;
var idToken = session.getIdToken();
var idTokenAvailabilityInMs = oneHourInMs;
var idTokenTimeOfCreation = new Date();
var refreshToken = session.getRefreshToken();
var authenticationResult = new SuccessfulAuthenticationResult(username, idToken.getJwtToken(), refreshToken.getToken(), idTokenAvailabilityInMs, idTokenTimeOfCreation);
return authenticationResult;
}
}, {
key: "authenticationFailedBecauseUserDoesNotExist",
value: function authenticationFailedBecauseUserDoesNotExist(error) {
return CognitoAuthenticationServicesProvider.USER_NOT_FOUND_ERROR_CODE === error.code;
}
}, {
key: "authenticationFailedBecauseTheSecretIsIncorrect",
value: function authenticationFailedBecauseTheSecretIsIncorrect(error) {
return CognitoAuthenticationServicesProvider.INCORRECT_SECRET_ERROR_CODE === error.code;
}
}, {
key: "createResultForUserDoesNotExist",
value: function createResultForUserDoesNotExist() {
return new FailedAuthenticationResult(AuthenticationFailureReason.USER_DOES_NOT_EXIST);
}
}, {
key: "createResultForIncorrectSecret",
value: function createResultForIncorrectSecret() {
return new FailedAuthenticationResult(AuthenticationFailureReason.INCORRECT_SECRET);
}
}, {
key: "createUserPool",
value: function createUserPool() {
return new CognitoUserPool({
UserPoolId: this.cognitoUserPoolId,
ClientId: this.appClientId
});
}
}]);
return CognitoAuthenticationServicesProvider;
}();
CognitoAuthenticationServicesProvider.USER_NOT_FOUND_ERROR_CODE = "UserNotFoundException";
//# sourceMappingURL=cognito-authentication-services-provider.class.js.map
CognitoAuthenticationServicesProvider.INCORRECT_SECRET_ERROR_CODE = "NotAuthorizedException";