UNPKG

@mvx/identity

Version:

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

89 lines (87 loc) 2.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionStore = void 0; const StateStore_1 = require("./StateStore"); const 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. * */ class SessionStore extends StateStore_1.StateStore { constructor(key) { super(); this.key = key; } /** * 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. * */ async store(ctx, meta) { if (!ctx.session) { throw new Error(`OAuth 2.0 authentication requires session support when using state. Did you forget to use session middleware?`); } const key = this.key; const state = utils_1.OIDCUtils.uid(24); if (!ctx.session[key]) { ctx.session[key] = {}; } ctx.session[key].state = state; 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. * */ async verify(ctx, providedState) { if (!ctx.session) { throw new Error(`OAuth 2.0 authentication requires session support when using state. Did you forget to use koa-session middleware?`); } const key = this.key; if (!ctx.session[key]) { return { result: false, message: 'Unable to verify authorization request state.', }; } const state = ctx.session[key].state; if (!state) { 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 { result: false, message: 'Invalid authorization request state.', }; } return { result: true, state: state, message: '' }; } static ρAnn() { return { "name": "SessionStore" }; } } exports.SessionStore = SessionStore; //# sourceMappingURL=../sourcemaps/stores/SessionStore.js.map