UNPKG

@mvx/identity

Version:

identity is oidc for mvc, type-mvc is base on koa. Decorator, Ioc, AOP mvc framework on server.

101 lines (99 loc) 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionStore = void 0; var tslib_1 = require("tslib"); var StateStore_1 = require("./StateStore"); var utils_1 = require("./utils"); /** * Creates an instance of `SessionStore`. * * This is the state store implementation for the OAuth2Strategy used when * the `state` option is enabled. It generates a random state and stores it in * `req.session` and verifies it when the service provider redirects the user * back to the application. * * This state store requires session support. If no session exists, an error * will be thrown. * */ var SessionStore = /** @class */ (function (_super) { tslib_1.__extends(SessionStore, _super); function SessionStore(key) { var _this = _super.call(this) || this; _this.key = key; return _this; } /** * Store request state. * * This implementation simply generates a random string and stores the value in * the session, where it will be used for verification when the user is * redirected back to the application. * */ SessionStore.prototype.store = function (ctx, meta) { return tslib_1.__awaiter(this, void 0, void 0, function () { var key, state; return tslib_1.__generator(this, function (_a) { if (!ctx.session) { throw new Error("OAuth 2.0 authentication requires session support\n when using state. Did you forget to use session middleware?"); } key = this.key; state = utils_1.OIDCUtils.uid(24); if (!ctx.session[key]) { ctx.session[key] = {}; } ctx.session[key].state = state; return [2 /*return*/, state]; }); }); }; /** * Verify request state. * * This implementation simply compares the state parameter in the request to the * value generated earlier and stored in the session. * */ SessionStore.prototype.verify = function (ctx, providedState) { return tslib_1.__awaiter(this, void 0, void 0, function () { var key, state; return tslib_1.__generator(this, function (_a) { if (!ctx.session) { throw new Error("OAuth 2.0 authentication requires session support\n when using state. Did you forget to use koa-session middleware?"); } key = this.key; if (!ctx.session[key]) { return [2 /*return*/, { result: false, message: 'Unable to verify authorization request state.', }]; } state = ctx.session[key].state; if (!state) { return [2 /*return*/, { result: false, message: 'Unable to verify authorization request state.', }]; } delete ctx.session[key].state; if (Object.keys(ctx.session[key]).length === 0) { delete ctx.session[key]; } if (state !== providedState) { return [2 /*return*/, { result: false, message: 'Invalid authorization request state.', }]; } return [2 /*return*/, { result: true, state: state, message: '' }]; }); }); }; SessionStoreAnn = function () { return { "name": "SessionStore" }; }; return SessionStore; }(StateStore_1.StateStore)); exports.SessionStore = SessionStore; //# sourceMappingURL=../sourcemaps/stores/SessionStore.js.map