1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
10 | for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
|
11 | };
|
12 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
13 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
14 | };
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | exports.AuthX = void 0;
|
17 | const koa_body_1 = __importDefault(require("koa-body"));
|
18 | const uuid_1 = require("uuid");
|
19 | const graphql_playground_middleware_koa_1 = __importDefault(require("graphql-playground-middleware-koa"));
|
20 | const koa_router_1 = __importDefault(require("koa-router"));
|
21 | const graphql_api_koa_1 = require("graphql-api-koa");
|
22 | const auth_header_1 = require("auth-header");
|
23 | const pg_1 = require("pg");
|
24 | const x_1 = __importDefault(require("./x"));
|
25 | const oauth2_1 = __importDefault(require("./oauth2"));
|
26 | const Config_1 = require("./Config");
|
27 | const graphql_1 = require("./graphql");
|
28 | const getAuthorization_1 = require("./util/getAuthorization");
|
29 | const StrategyCollection_1 = require("./StrategyCollection");
|
30 | const errors_1 = require("./errors");
|
31 | const explanations_1 = require("./explanations");
|
32 | const loader_1 = require("./loader");
|
33 | __exportStar(require("./x"), exports);
|
34 | __exportStar(require("./errors"), exports);
|
35 | __exportStar(require("./loader"), exports);
|
36 | __exportStar(require("./model"), exports);
|
37 | __exportStar(require("./graphql"), exports);
|
38 | __exportStar(require("./Strategy"), exports);
|
39 | __exportStar(require("./StrategyCollection"), exports);
|
40 | __exportStar(require("./Config"), exports);
|
41 | __exportStar(require("./Context"), exports);
|
42 | __exportStar(require("./util/validateIdFormat"), exports);
|
43 | class AuthX extends koa_router_1.default {
|
44 | constructor(config) {
|
45 | Config_1.assertConfig(config);
|
46 | super(config);
|
47 | const explanations = explanations_1.createAuthXExplanations({ [config.realm]: "AuthX" });
|
48 | const strategies = config.strategies instanceof StrategyCollection_1.StrategyCollection
|
49 | ? config.strategies
|
50 | : new StrategyCollection_1.StrategyCollection(config.strategies);
|
51 |
|
52 | this.pool = new pg_1.Pool(config.pg);
|
53 |
|
54 | const contextMiddleware = async (ctx, next) => {
|
55 | const tx = await this.pool.connect();
|
56 | try {
|
57 | let authorization = null;
|
58 | const auth = ctx.request.header.authorization
|
59 | ? auth_header_1.parse(ctx.request.header.authorization)
|
60 | : null;
|
61 |
|
62 | const basic = auth && auth.scheme === "Basic" && typeof auth.token === "string"
|
63 | ? auth.token
|
64 | : null;
|
65 | if (basic) {
|
66 | authorization = await getAuthorization_1.fromBasic(tx, basic);
|
67 |
|
68 |
|
69 |
|
70 | await authorization.invoke(tx, {
|
71 | id: uuid_1.v4(),
|
72 | format: "basic",
|
73 | createdAt: new Date()
|
74 | });
|
75 | }
|
76 |
|
77 | const bearer = auth && auth.scheme === "Bearer" && typeof auth.token === "string"
|
78 | ? auth.token
|
79 | : null;
|
80 | if (bearer) {
|
81 | authorization = await getAuthorization_1.fromBearer(tx, config.publicKeys, bearer);
|
82 |
|
83 |
|
84 | }
|
85 |
|
86 | if (ctx.request.header.authorization && !authorization) {
|
87 | throw new Error("An authorization header must be of either HTTP Basic or Bearer format.");
|
88 | }
|
89 | const context = {
|
90 | ...ctx[x_1.default],
|
91 | ...config,
|
92 | authorization,
|
93 | explanations: explanations,
|
94 | executor: new loader_1.DataLoaderExecutor(this.pool, strategies)
|
95 | };
|
96 | ctx[x_1.default] = context;
|
97 | }
|
98 | finally {
|
99 | tx.release();
|
100 | }
|
101 | await next();
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 | this.post("/graphql", graphql_api_koa_1.errorHandler(), contextMiddleware,
|
107 |
|
108 |
|
109 | async (ctx, next) => {
|
110 | if (!ctx.is("json"))
|
111 | throw new errors_1.UnsupportedMediaTypeError("Requests to the AuthX GraphQL endpoint MUST specify a Content-Type of `application/json`.");
|
112 | await next();
|
113 | }, koa_body_1.default({ multipart: false, urlencoded: false, text: false, json: true }), graphql_api_koa_1.execute({
|
114 | schema: config.processSchema
|
115 | ? config.processSchema(graphql_1.createSchema(strategies))
|
116 | : graphql_1.createSchema(strategies),
|
117 | override: (ctx) => {
|
118 | const contextValue = ctx[x_1.default];
|
119 | return {
|
120 | contextValue
|
121 | };
|
122 | }
|
123 | }));
|
124 |
|
125 |
|
126 |
|
127 | this.all("/graphiql", graphql_playground_middleware_koa_1.default({ endpoint: "/graphql" }));
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | this.post("/", contextMiddleware, koa_body_1.default({ multipart: false, urlencoded: true, text: false, json: true }), oauth2_1.default);
|
140 | }
|
141 | }
|
142 | exports.AuthX = AuthX;
|
143 | exports.default = AuthX;
|
144 |
|
\ | No newline at end of file |