UNPKG

stackpress

Version:

Incept is a content management framework.

67 lines (66 loc) 3.07 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.default = OAuth; const helpers_js_1 = require("../helpers.js"); function OAuth(req, res, ctx) { return __awaiter(this, void 0, void 0, function* () { var _a; if (res.body || (res.code && res.code !== 200)) { return; } const view = ctx.config.path('view', {}); const brand = ctx.config.path('brand', {}); const { scopes = {}, endpoints = [] } = ctx.config.path('api'); res.data.set('api', { scopes, endpoints }); res.data.set('view', { base: view.base || '/', props: view.props || {} }); res.data.set('brand', { name: brand.name || 'Stackpress', logo: brand.logo || '/logo.png', icon: brand.icon || '/icon.png', favicon: brand.favicon || '/favicon.ico', }); const id = req.data('client_id'); const redirect = req.data('redirect_uri'); const state = req.data('state'); if (!id || !redirect) { return (0, helpers_js_1.unauthorized)(res); } const session = yield ctx.resolve('me', req); if (!((_a = session.results) === null || _a === void 0 ? void 0 : _a.id)) { const redirect = encodeURIComponent(req.url.pathname + req.url.search); res.redirect(`/auth/signin?redirect_uri=${redirect}`); return; } yield ctx.resolve('application-detail', { id }, res); if (res.code !== 200) { return (0, helpers_js_1.unauthorized)(res); } if (req.method === 'POST') { const { expires = 1000 * 60 * 60 * 24 } = ctx.config('api') || {}; const response = yield ctx.resolve('session-create', Object.assign(Object.assign({}, req.data()), { applicationId: id, profileId: session.results.id, expires: new Date(Date.now() + expires) })); if (res.code !== 200 || !response.results) { return; } const [uri, query] = redirect.split('?'); const params = new URLSearchParams(query); params.set('code', response.results.id); if (state) { params.set('state', state); } res.redirect(`${uri}?${params.toString()}`); return; } }); }