UNPKG

@minimaltech/node-infra

Version:

Minimal Technology NodeJS Infrastructure - Loopback 4 Framework

69 lines 3.71 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerOAuth2Strategy = exports.defineOAuth2Strategy = void 0; const network_1 = require("../../../helpers/network"); const utilities_1 = require("../../../utilities"); const authentication_1 = require("@loopback/authentication"); const security_1 = require("@loopback/security"); const isEmpty_1 = __importDefault(require("lodash/isEmpty")); class AuthProviderNetworkRequest extends network_1.BaseNetworkRequest { } const defineOAuth2Strategy = (opts) => { class Strategy { constructor() { var _a; this.name = opts.name; const baseURL = opts.baseURL; if (!baseURL || (0, isEmpty_1.default)(baseURL)) { throw (0, utilities_1.getError)({ message: `[RemoteAuthenticationStrategy][DANGER] INVALID baseURL | Missing env: APP_ENV_REMOTE_AUTH_SERVER_URL`, }); } this.authPath = (_a = opts.authPath) !== null && _a !== void 0 ? _a : '/auth/who-am-i'; this.authProvider = new AuthProviderNetworkRequest({ name: AuthProviderNetworkRequest.name, scope: `${Strategy.name}_${opts.name}`, networkOptions: { baseURL }, }); } authenticate(request) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; const networkService = this.authProvider.getNetworkService(); const rs = yield networkService.send({ url: this.authProvider.getRequestUrl({ paths: [this.authPath] }), headers: { Authorization: request.headers['authorization'] }, }); if ((_a = rs === null || rs === void 0 ? void 0 : rs.data) === null || _a === void 0 ? void 0 : _a.error) { throw (0, utilities_1.getError)(rs.data.error); } return Object.assign(Object.assign({}, rs === null || rs === void 0 ? void 0 : rs.data), { [security_1.securityId]: (_c = (_b = rs === null || rs === void 0 ? void 0 : rs.data) === null || _b === void 0 ? void 0 : _b.userId) === null || _c === void 0 ? void 0 : _c.toString() }); }); } } return Strategy; }; exports.defineOAuth2Strategy = defineOAuth2Strategy; const registerOAuth2Strategy = (context, options) => { var _a; const remoteOAuth2Strategy = (0, exports.defineOAuth2Strategy)({ name: options.strategyName, baseURL: options.authenticateUrl, authPath: (_a = options.authenticatePath) !== null && _a !== void 0 ? _a : '/auth/who-am-i', }); (0, authentication_1.registerAuthenticationStrategy)(context, remoteOAuth2Strategy); }; exports.registerOAuth2Strategy = registerOAuth2Strategy; //# sourceMappingURL=oauth2.strategy.js.map