1 | "use strict";
|
2 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
3 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
4 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
5 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
6 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
7 | };
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | const core_1 = require("@fusion.io/core");
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | let Authenticator = class Authenticator extends core_1.Manager {
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 | constructor() {
|
22 | super(...arguments);
|
23 | this.gatewayConnectors = new Map();
|
24 | }
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | connect(gateway, connector) {
|
32 | this.gatewayConnectors.set(gateway, connector);
|
33 | return this;
|
34 | }
|
35 | |
36 |
|
37 |
|
38 |
|
39 | bootstrap(config) {
|
40 | const standardAdapterConfiguration = Object.entries(config.gateways)
|
41 | .reduce((merged, [gatewayName, { protocol, options }]) => (Object.assign({}, merged, { [gatewayName]: {
|
42 | driver: protocol,
|
43 | options: Object.assign({}, options, {
|
44 |
|
45 |
|
46 | connector: this.gatewayConnectors.get(gatewayName) })
|
47 | } })), {});
|
48 | return this.configure({
|
49 | default: config.default,
|
50 | adapters: standardAdapterConfiguration
|
51 | });
|
52 | }
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 | installAdapter(configuration) {
|
59 | const gateway = super.installAdapter(configuration);
|
60 | if (configuration.options.connector) {
|
61 | let providers = configuration.options.connector(configuration.options);
|
62 | if (!(providers instanceof Array))
|
63 | providers = [providers];
|
64 | providers.forEach(provider => gateway.provider.push(provider));
|
65 | }
|
66 | return gateway;
|
67 | }
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | authenticate(gateway, context) {
|
75 | return this.adapter(gateway).authenticate(context);
|
76 | }
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | guard(gateway) {
|
91 |
|
92 |
|
93 |
|
94 | return (...arg) => {
|
95 |
|
96 |
|
97 | const protocol = this.adapter(gateway).protocol;
|
98 | if ('function' !== typeof protocol.mount) {
|
99 | throw new Error(`The protocol [${protocol.constructor.name}] of the gateway [${gateway}]` +
|
100 | ` does not support mounting to a framework.`);
|
101 | }
|
102 |
|
103 | const consumer = (context) => this.authenticate(gateway, context);
|
104 |
|
105 |
|
106 | const mountFunction = protocol.mount(consumer);
|
107 |
|
108 | return mountFunction(...arg);
|
109 | };
|
110 | }
|
111 | };
|
112 | Authenticator = __decorate([
|
113 | core_1.singleton()
|
114 | ], Authenticator);
|
115 | exports.Authenticator = Authenticator;
|
116 |
|
\ | No newline at end of file |