1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
12 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
13 | };
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | const flatten_1 = __importDefault(require("lodash/flatten"));
|
16 | const omit_1 = __importDefault(require("lodash/omit"));
|
17 | const errors_1 = require("@feathersjs/errors");
|
18 | const debug_1 = __importDefault(require("debug"));
|
19 | const debug = (0, debug_1.default)('@feathersjs/authentication/hooks/authenticate');
|
20 | exports.default = (originalSettings, ...originalStrategies) => {
|
21 | const settings = typeof originalSettings === 'string'
|
22 | ? { strategies: (0, flatten_1.default)([originalSettings, ...originalStrategies]) }
|
23 | : originalSettings;
|
24 | if (!originalSettings || settings.strategies.length === 0) {
|
25 | throw new Error('The authenticate hook needs at least one allowed strategy');
|
26 | }
|
27 | return (context) => __awaiter(void 0, void 0, void 0, function* () {
|
28 | const { app, params, type, path, service } = context;
|
29 | const { strategies } = settings;
|
30 | const { provider, authentication } = params;
|
31 | const authService = app.defaultAuthentication(settings.service);
|
32 | debug(`Running authenticate hook on '${path}'`);
|
33 | if (type && type !== 'before') {
|
34 | throw new errors_1.NotAuthenticated('The authenticate hook must be used as a before hook');
|
35 | }
|
36 | if (!authService || typeof authService.authenticate !== 'function') {
|
37 | throw new errors_1.NotAuthenticated('Could not find a valid authentication service');
|
38 | }
|
39 |
|
40 | if (service === authService) {
|
41 | throw new errors_1.NotAuthenticated('The authenticate hook does not need to be used on the authentication service');
|
42 | }
|
43 | if (params.authenticated === true) {
|
44 | return context;
|
45 | }
|
46 | if (authentication) {
|
47 | const authParams = (0, omit_1.default)(params, 'provider', 'authentication');
|
48 | debug('Authenticating with', authentication, strategies);
|
49 | const authResult = yield authService.authenticate(authentication, authParams, ...strategies);
|
50 | context.params = Object.assign({}, params, (0, omit_1.default)(authResult, 'accessToken'), { authenticated: true });
|
51 | return context;
|
52 | }
|
53 | else if (provider) {
|
54 | throw new errors_1.NotAuthenticated('Not authenticated');
|
55 | }
|
56 | return context;
|
57 | });
|
58 | };
|
59 |
|
\ | No newline at end of file |