UNPKG

4.39 kBJavaScriptView Raw
1"use strict";
2var __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};
8Object.defineProperty(exports, "__esModule", { value: true });
9const core_1 = require("@fusion.io/core");
10/**
11 * An authenticator service. It its simplest form, it managing
12 * gateways
13 *
14 */
15let Authenticator = class Authenticator extends core_1.Manager {
16 /**
17 * An authenticator service. It its simplest form, it managing
18 * gateways
19 *
20 */
21 constructor() {
22 super(...arguments);
23 this.gatewayConnectors = new Map();
24 }
25 /**
26 * Connect an IDP to the gateway
27 *
28 * @param gateway
29 * @param connector
30 */
31 connect(gateway, connector) {
32 this.gatewayConnectors.set(gateway, connector);
33 return this;
34 }
35 /**
36 *
37 * @param config
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 // We'll allow the consumer code connects
45 // its IDP to the gateway
46 connector: this.gatewayConnectors.get(gatewayName) })
47 } })), {});
48 return this.configure({
49 default: config.default,
50 adapters: standardAdapterConfiguration
51 });
52 }
53 /**
54 * Overrides the install adapter method to connect the gateway with IDP
55 *
56 * @param configuration
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 * Authenticate a context by a given gateway
70 *
71 * @param gateway
72 * @param context
73 */
74 authenticate(gateway, context) {
75 return this.adapter(gateway).authenticate(context);
76 }
77 /**
78 * Returning a connection which can be used to mount to
79 * the transport layer
80 *
81 * Normally, we need to authenticate over Http, then guard() will
82 * return a middleware
83 *
84 * If we authenticate over Socket, then guard() will return the
85 * socket connection middleware
86 *
87 * @param gateway
88 * @return {mount}
89 */
90 guard(gateway) {
91 // First we'll wrap exposed function. By doing this way, the logic for checking
92 // the Gateway existence will be done in the mounted context.
93 // Not the initial setup context.
94 return (...arg) => {
95 // Now we are actually inside the context.
96 // we check for the protocol of the gateway
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 // Create a consumer function which actually run the authentication process
103 const consumer = (context) => this.authenticate(gateway, context);
104 // Get give the consumer to the protocol
105 // and get back the mount function.
106 const mountFunction = protocol.mount(consumer);
107 // Lastly, we'll trigger the mountFunction here here
108 return mountFunction(...arg);
109 };
110 }
111};
112Authenticator = __decorate([
113 core_1.singleton()
114], Authenticator);
115exports.Authenticator = Authenticator;
116//# sourceMappingURL=Authenticator.js.map
\No newline at end of file