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