nestjs-authentication 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/CHANGELOG.md +77 -2
  2. package/README.md +108 -0
  3. package/SECURITY.md +15 -0
  4. package/dist/classes/email.password.user.d.ts +13 -0
  5. package/dist/classes/email.password.user.d.ts.map +1 -0
  6. package/dist/classes/email.password.user.js +3 -0
  7. package/dist/classes/email.password.user.js.map +1 -0
  8. package/dist/constants.d.ts +13 -0
  9. package/dist/constants.d.ts.map +1 -1
  10. package/dist/constants.js +14 -1
  11. package/dist/constants.js.map +1 -1
  12. package/dist/context/auth.context.d.ts +38 -0
  13. package/dist/context/auth.context.d.ts.map +1 -0
  14. package/dist/context/auth.context.js +39 -0
  15. package/dist/context/auth.context.js.map +1 -0
  16. package/dist/context/decorators.d.ts +25 -0
  17. package/dist/context/decorators.d.ts.map +1 -0
  18. package/dist/context/decorators.js +38 -0
  19. package/dist/context/decorators.js.map +1 -0
  20. package/dist/context/index.d.ts +5 -0
  21. package/dist/context/index.d.ts.map +1 -0
  22. package/dist/context/index.js +21 -0
  23. package/dist/context/index.js.map +1 -0
  24. package/dist/context/interceptor.d.ts +15 -0
  25. package/dist/context/interceptor.d.ts.map +1 -0
  26. package/dist/context/interceptor.js +51 -0
  27. package/dist/context/interceptor.js.map +1 -0
  28. package/dist/context/service.d.ts +18 -0
  29. package/dist/context/service.d.ts.map +1 -0
  30. package/dist/context/service.js +45 -0
  31. package/dist/context/service.js.map +1 -0
  32. package/dist/encryptor.d.ts +20 -0
  33. package/dist/encryptor.d.ts.map +1 -1
  34. package/dist/encryptor.js +36 -6
  35. package/dist/encryptor.js.map +1 -1
  36. package/dist/encryptors/index.d.ts +3 -0
  37. package/dist/encryptors/index.d.ts.map +1 -0
  38. package/dist/encryptors/index.js +19 -0
  39. package/dist/encryptors/index.js.map +1 -0
  40. package/dist/encryptors/migrating.d.ts +28 -0
  41. package/dist/encryptors/migrating.d.ts.map +1 -0
  42. package/dist/encryptors/migrating.js +72 -0
  43. package/dist/encryptors/migrating.js.map +1 -0
  44. package/dist/encryptors/scrypt.d.ts +42 -0
  45. package/dist/encryptors/scrypt.d.ts.map +1 -0
  46. package/dist/encryptors/scrypt.js +85 -0
  47. package/dist/encryptors/scrypt.js.map +1 -0
  48. package/dist/errors.d.ts +32 -0
  49. package/dist/errors.d.ts.map +1 -0
  50. package/dist/errors.js +63 -0
  51. package/dist/errors.js.map +1 -0
  52. package/dist/guard.d.ts +115 -15
  53. package/dist/guard.d.ts.map +1 -1
  54. package/dist/guard.js +302 -90
  55. package/dist/guard.js.map +1 -1
  56. package/dist/index.d.ts +10 -4
  57. package/dist/index.d.ts.map +1 -1
  58. package/dist/index.js +15 -5
  59. package/dist/index.js.map +1 -1
  60. package/dist/injection.d.ts +3 -0
  61. package/dist/injection.d.ts.map +1 -0
  62. package/dist/injection.js +9 -0
  63. package/dist/injection.js.map +1 -0
  64. package/dist/interfaces/auth.user.d.ts +16 -5
  65. package/dist/interfaces/auth.user.d.ts.map +1 -1
  66. package/dist/interfaces/auth.user.js +6 -0
  67. package/dist/interfaces/auth.user.js.map +1 -1
  68. package/dist/interfaces/encrypt.d.ts +29 -1
  69. package/dist/interfaces/encrypt.d.ts.map +1 -1
  70. package/dist/interfaces/options.d.ts +52 -5
  71. package/dist/interfaces/options.d.ts.map +1 -1
  72. package/dist/interfaces/provider.d.ts +26 -3
  73. package/dist/interfaces/provider.d.ts.map +1 -1
  74. package/dist/module.d.ts +48 -6
  75. package/dist/module.d.ts.map +1 -1
  76. package/dist/module.js +153 -50
  77. package/dist/module.js.map +1 -1
  78. package/dist/providers/auth.provider.d.ts +12 -4
  79. package/dist/providers/auth.provider.d.ts.map +1 -1
  80. package/dist/providers/auth.provider.js +7 -1
  81. package/dist/providers/auth.provider.js.map +1 -1
  82. package/dist/providers/email.password.auth.provider.d.ts +75 -0
  83. package/dist/providers/email.password.auth.provider.d.ts.map +1 -0
  84. package/dist/providers/email.password.auth.provider.js +120 -0
  85. package/dist/providers/email.password.auth.provider.js.map +1 -0
  86. package/dist/providers/memory.user.provider.d.ts +39 -6
  87. package/dist/providers/memory.user.provider.d.ts.map +1 -1
  88. package/dist/providers/memory.user.provider.js +70 -15
  89. package/dist/providers/memory.user.provider.js.map +1 -1
  90. package/dist/scopes/decorators.d.ts +41 -0
  91. package/dist/scopes/decorators.d.ts.map +1 -0
  92. package/dist/scopes/decorators.js +44 -0
  93. package/dist/scopes/decorators.js.map +1 -0
  94. package/dist/scopes/index.d.ts +4 -0
  95. package/dist/scopes/index.d.ts.map +1 -0
  96. package/dist/scopes/index.js +20 -0
  97. package/dist/scopes/index.js.map +1 -0
  98. package/dist/scopes/service.d.ts +32 -0
  99. package/dist/scopes/service.d.ts.map +1 -0
  100. package/dist/scopes/service.js +90 -0
  101. package/dist/scopes/service.js.map +1 -0
  102. package/dist/scopes/utils.d.ts +8 -0
  103. package/dist/scopes/utils.d.ts.map +1 -0
  104. package/dist/scopes/utils.js +29 -0
  105. package/dist/scopes/utils.js.map +1 -0
  106. package/dist/service.d.ts +25 -6
  107. package/dist/service.d.ts.map +1 -1
  108. package/dist/service.js +36 -5
  109. package/dist/service.js.map +1 -1
  110. package/dist/session/index.d.ts +3 -0
  111. package/dist/session/index.d.ts.map +1 -0
  112. package/dist/session/index.js +19 -0
  113. package/dist/session/index.js.map +1 -0
  114. package/dist/session/manager.d.ts +32 -0
  115. package/dist/session/manager.d.ts.map +1 -0
  116. package/dist/session/manager.js +131 -0
  117. package/dist/session/manager.js.map +1 -0
  118. package/dist/session/request.d.ts +14 -0
  119. package/dist/session/request.d.ts.map +1 -0
  120. package/dist/session/request.js +59 -0
  121. package/dist/session/request.js.map +1 -0
  122. package/dist/strategies/emailpassword.d.ts +20 -7
  123. package/dist/strategies/emailpassword.d.ts.map +1 -1
  124. package/dist/strategies/emailpassword.js +42 -19
  125. package/dist/strategies/emailpassword.js.map +1 -1
  126. package/dist/strategies/extract.d.ts +23 -0
  127. package/dist/strategies/extract.d.ts.map +1 -0
  128. package/dist/strategies/extract.js +57 -0
  129. package/dist/strategies/extract.js.map +1 -0
  130. package/dist/strategies/index.d.ts +8 -0
  131. package/dist/strategies/index.d.ts.map +1 -0
  132. package/dist/strategies/index.js +24 -0
  133. package/dist/strategies/index.js.map +1 -0
  134. package/dist/strategies/interfaces.d.ts +65 -0
  135. package/dist/strategies/interfaces.d.ts.map +1 -0
  136. package/dist/{classes/memory.user.js → strategies/interfaces.js} +1 -1
  137. package/dist/strategies/interfaces.js.map +1 -0
  138. package/dist/strategies/jwt.d.ts +87 -15
  139. package/dist/strategies/jwt.d.ts.map +1 -1
  140. package/dist/strategies/jwt.js +140 -9
  141. package/dist/strategies/jwt.js.map +1 -1
  142. package/dist/strategies/passport.d.ts +22 -0
  143. package/dist/strategies/passport.d.ts.map +1 -0
  144. package/dist/strategies/passport.js +90 -0
  145. package/dist/strategies/passport.js.map +1 -0
  146. package/dist/strategies/registry.d.ts +33 -0
  147. package/dist/strategies/registry.d.ts.map +1 -0
  148. package/dist/strategies/registry.js +67 -0
  149. package/dist/strategies/registry.js.map +1 -0
  150. package/dist/strategies/session.d.ts +18 -0
  151. package/dist/strategies/session.d.ts.map +1 -0
  152. package/dist/strategies/session.js +79 -0
  153. package/dist/strategies/session.js.map +1 -0
  154. package/dist/transports/graphql.d.ts +28 -0
  155. package/dist/transports/graphql.d.ts.map +1 -0
  156. package/dist/transports/graphql.js +59 -0
  157. package/dist/transports/graphql.js.map +1 -0
  158. package/dist/transports/http.d.ts +13 -0
  159. package/dist/transports/http.d.ts.map +1 -0
  160. package/dist/transports/http.js +31 -0
  161. package/dist/transports/http.js.map +1 -0
  162. package/dist/transports/index.d.ts +8 -0
  163. package/dist/transports/index.d.ts.map +1 -0
  164. package/dist/transports/index.js +24 -0
  165. package/dist/transports/index.js.map +1 -0
  166. package/dist/transports/interfaces.d.ts +36 -0
  167. package/dist/transports/interfaces.d.ts.map +1 -0
  168. package/dist/transports/interfaces.js +3 -0
  169. package/dist/transports/interfaces.js.map +1 -0
  170. package/dist/transports/registry.d.ts +25 -0
  171. package/dist/transports/registry.d.ts.map +1 -0
  172. package/dist/transports/registry.js +55 -0
  173. package/dist/transports/registry.js.map +1 -0
  174. package/dist/transports/rpc.d.ts +23 -0
  175. package/dist/transports/rpc.d.ts.map +1 -0
  176. package/dist/transports/rpc.js +54 -0
  177. package/dist/transports/rpc.js.map +1 -0
  178. package/dist/transports/utils.d.ts +21 -0
  179. package/dist/transports/utils.d.ts.map +1 -0
  180. package/dist/transports/utils.js +86 -0
  181. package/dist/transports/utils.js.map +1 -0
  182. package/dist/transports/ws.d.ts +31 -0
  183. package/dist/transports/ws.d.ts.map +1 -0
  184. package/dist/transports/ws.js +52 -0
  185. package/dist/transports/ws.js.map +1 -0
  186. package/package.json +102 -60
  187. package/dist/classes/memory.user.d.ts +0 -7
  188. package/dist/classes/memory.user.d.ts.map +0 -1
  189. package/dist/classes/memory.user.js.map +0 -1
  190. package/dist/providers/auth.memory.user.provider.d.ts +0 -15
  191. package/dist/providers/auth.memory.user.provider.d.ts.map +0 -1
  192. package/dist/providers/auth.memory.user.provider.js +0 -30
  193. package/dist/providers/auth.memory.user.provider.js.map +0 -1
  194. package/dist/user.serializer.d.ts +0 -10
  195. package/dist/user.serializer.d.ts.map +0 -1
  196. package/dist/user.serializer.js +0 -53
  197. package/dist/user.serializer.js.map +0 -1
@@ -0,0 +1,79 @@
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
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
16
+ return new (P || (P = Promise))(function (resolve, reject) {
17
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
20
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
21
+ });
22
+ };
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.SessionStrategy = void 0;
25
+ const common_1 = require("@nestjs/common");
26
+ const constants_1 = require("../constants");
27
+ const service_1 = require("../service");
28
+ const manager_1 = require("../session/manager");
29
+ /**
30
+ * The `session` strategy: loads the user stored in the session (`fromSession: true` in a guard).
31
+ *
32
+ * The session holds the payload of the user (`session.passport.user`, the format of passport), loaded back with the
33
+ * auth provider of its namespace. If the user or its provider does not exist anymore, it is removed from the session.
34
+ */
35
+ let SessionStrategy = class SessionStrategy {
36
+ constructor(authService, moduleOptions) {
37
+ this.authService = authService;
38
+ this.moduleOptions = moduleOptions;
39
+ this.name = 'session';
40
+ }
41
+ get key() {
42
+ var _a, _b;
43
+ return (_b = (_a = this.moduleOptions) === null || _a === void 0 ? void 0 : _a.sessionKey) !== null && _b !== void 0 ? _b : manager_1.DEFAULT_SESSION_KEY;
44
+ }
45
+ authenticate(_a) {
46
+ return __awaiter(this, arguments, void 0, function* ({ request }) {
47
+ if (request.session === undefined || request.session === null) {
48
+ throw new Error('Login sessions require session support. Did you forget to use `express-session` middleware?');
49
+ }
50
+ const payload = (0, manager_1.getSessionPayload)(request, this.key);
51
+ if (payload === undefined) {
52
+ return undefined;
53
+ }
54
+ let user;
55
+ try {
56
+ user = yield this.authService.loadUser(payload);
57
+ }
58
+ catch (e) {
59
+ if (!(e instanceof common_1.NotFoundException)) {
60
+ throw e;
61
+ }
62
+ }
63
+ if (user === undefined || user === null) {
64
+ (0, manager_1.removeSessionUser)(request.session, this.key);
65
+ return undefined;
66
+ }
67
+ const namespace = typeof payload.namespace === 'string' ? payload.namespace : undefined;
68
+ return { user, namespace, info: { namespace } };
69
+ });
70
+ }
71
+ };
72
+ exports.SessionStrategy = SessionStrategy;
73
+ exports.SessionStrategy = SessionStrategy = __decorate([
74
+ (0, common_1.Injectable)(),
75
+ __param(1, (0, common_1.Optional)()),
76
+ __param(1, (0, common_1.Inject)(constants_1.MODULE_OPTIONS_TOKEN)),
77
+ __metadata("design:paramtypes", [service_1.AuthService, Object])
78
+ ], SessionStrategy);
79
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/strategies/session.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAiF;AAEjF,4CAAoD;AAEpD,wCAAyC;AACzC,gDAA+F;AAG/F;;;;;GAKG;AAEI,IAAM,eAAe,GAArB,MAAM,eAAe;IAGxB,YACuB,WAAwB,EACD,aAAqD;QAD5E,gBAAW,GAAX,WAAW,CAAa;QACkB,kBAAa,GAAb,aAAa,CAAqB;QAJ1F,SAAI,GAAG,SAAS,CAAC;IAKvB,CAAC;IAEJ,IAAc,GAAG;;QACb,OAAO,MAAA,MAAA,IAAI,CAAC,aAAa,0CAAE,UAAU,mCAAI,6BAAmB,CAAC;IACjE,CAAC;IAEK,YAAY;6DAAC,EAAE,OAAO,EAAuB;YAC/C,IAAI,OAAO,CAAC,OAAO,KAAK,SAAS,IAAI,OAAO,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;gBAC5D,MAAM,IAAI,KAAK,CACX,6FAA6F,CAChG,CAAC;YACN,CAAC;YACD,MAAM,OAAO,GAAG,IAAA,2BAAiB,EAAC,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;YACrD,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;gBACxB,OAAO,SAAS,CAAC;YACrB,CAAC;YACD,IAAI,IAAsB,CAAC;YAC3B,IAAI,CAAC;gBACD,IAAI,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAO,OAAO,CAAC,CAAC;YAC1D,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACT,IAAI,CAAC,CAAC,CAAC,YAAY,0BAAiB,CAAC,EAAE,CAAC;oBACpC,MAAM,CAAC,CAAC;gBACZ,CAAC;YACL,CAAC;YACD,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;gBACtC,IAAA,2BAAiB,EAAC,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;gBAC7C,OAAO,SAAS,CAAC;YACrB,CAAC;YACD,MAAM,SAAS,GAAG,OAAO,OAAO,CAAC,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;YACxF,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,SAAS,EAAE,EAAE,CAAC;QACpD,CAAC;KAAA;CACJ,CAAA;AArCY,0CAAe;0BAAf,eAAe;IAD3B,IAAA,mBAAU,GAAE;IAMJ,WAAA,IAAA,iBAAQ,GAAE,CAAA;IAAE,WAAA,IAAA,eAAM,EAAC,gCAAoB,CAAC,CAAA;qCADT,qBAAW;GAJtC,eAAe,CAqC3B"}
@@ -0,0 +1,28 @@
1
+ import { ExecutionContext } from '@nestjs/common';
2
+ import { AuthRequest, AuthTransport } from './interfaces';
3
+ import { RequestCache } from './utils';
4
+ /**
5
+ * The GraphQL transport (queries, mutations and subscriptions), for Apollo, Mercurius, Yoga...
6
+ *
7
+ * The request is found in the GraphQL context (third resolver argument), in this order:
8
+ *
9
+ * - `context.req` (Apollo with express, the default context of `@nestjs/apollo`)
10
+ * - `context.request`, `context.reply.request` (fastify, Mercurius)
11
+ * - subscriptions (graphql-ws): a request built from `context.extra.request` (the upgrade request: headers,
12
+ * cookies, session) and `context.connectionParams`
13
+ *
14
+ * For subscriptions, `connectionParams.authorization` (or `Authorization`, `authToken`, `token`) is set as the
15
+ * `authorization` header (`Bearer <token>` if it has no scheme), and `connectionParams.headers` are added to the headers.
16
+ * Return `connectionParams` and `extra` from your context factory for subscriptions:
17
+ * `context: ({ req, res, connectionParams, extra }) => ({ req, res, connectionParams, extra })`.
18
+ */
19
+ export declare class GraphqlAuthTransport implements AuthTransport {
20
+ readonly name = "graphql";
21
+ protected readonly requests: RequestCache;
22
+ supports(context: ExecutionContext): boolean;
23
+ protected getContext(context: ExecutionContext): any;
24
+ getRequest(context: ExecutionContext): AuthRequest;
25
+ getResponse(context: ExecutionContext): any;
26
+ protected createSubscriptionRequest(ctx: any, request?: any): AuthRequest;
27
+ }
28
+ //# sourceMappingURL=graphql.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../../src/transports/graphql.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAElD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAmC,YAAY,EAAmB,MAAM,SAAS,CAAC;AAEzF;;;;;;;;;;;;;;GAcG;AACH,qBAAa,oBAAqB,YAAW,aAAa;IACtD,QAAQ,CAAC,IAAI,aAAa;IAC1B,SAAS,CAAC,QAAQ,CAAC,QAAQ,eAAsB;IAEjD,QAAQ,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO;IAI5C,SAAS,CAAC,UAAU,CAAC,OAAO,EAAE,gBAAgB,GAAG,GAAG;IAIpD,UAAU,CAAC,OAAO,EAAE,gBAAgB,GAAG,WAAW;IASlD,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,GAAG;IAK3C,SAAS,CAAC,yBAAyB,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,GAAG,GAAG,WAAW;CAY5E"}
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GraphqlAuthTransport = void 0;
4
+ const utils_1 = require("./utils");
5
+ /**
6
+ * The GraphQL transport (queries, mutations and subscriptions), for Apollo, Mercurius, Yoga...
7
+ *
8
+ * The request is found in the GraphQL context (third resolver argument), in this order:
9
+ *
10
+ * - `context.req` (Apollo with express, the default context of `@nestjs/apollo`)
11
+ * - `context.request`, `context.reply.request` (fastify, Mercurius)
12
+ * - subscriptions (graphql-ws): a request built from `context.extra.request` (the upgrade request: headers,
13
+ * cookies, session) and `context.connectionParams`
14
+ *
15
+ * For subscriptions, `connectionParams.authorization` (or `Authorization`, `authToken`, `token`) is set as the
16
+ * `authorization` header (`Bearer <token>` if it has no scheme), and `connectionParams.headers` are added to the headers.
17
+ * Return `connectionParams` and `extra` from your context factory for subscriptions:
18
+ * `context: ({ req, res, connectionParams, extra }) => ({ req, res, connectionParams, extra })`.
19
+ */
20
+ class GraphqlAuthTransport {
21
+ constructor() {
22
+ this.name = 'graphql';
23
+ this.requests = new utils_1.RequestCache();
24
+ }
25
+ supports(context) {
26
+ return context.getType() === 'graphql';
27
+ }
28
+ getContext(context) {
29
+ var _a;
30
+ return (_a = context.getArgByIndex(2)) !== null && _a !== void 0 ? _a : {};
31
+ }
32
+ getRequest(context) {
33
+ var _a, _b, _c;
34
+ const ctx = this.getContext(context);
35
+ const request = (_b = (_a = ctx.req) !== null && _a !== void 0 ? _a : ctx.request) !== null && _b !== void 0 ? _b : (_c = ctx.reply) === null || _c === void 0 ? void 0 : _c.request;
36
+ if (request !== undefined && request !== null && ctx.connectionParams === undefined) {
37
+ return request;
38
+ }
39
+ return this.requests.get(ctx, () => this.createSubscriptionRequest(ctx, request));
40
+ }
41
+ getResponse(context) {
42
+ var _a;
43
+ const ctx = this.getContext(context);
44
+ return (_a = ctx.res) !== null && _a !== void 0 ? _a : ctx.reply;
45
+ }
46
+ createSubscriptionRequest(ctx, request) {
47
+ var _a, _b, _c, _d, _e;
48
+ const upgrade = request !== null && request !== void 0 ? request : (_a = ctx.extra) === null || _a === void 0 ? void 0 : _a.request;
49
+ const params = (_b = ctx.connectionParams) !== null && _b !== void 0 ? _b : {};
50
+ const headers = Object.assign(Object.assign({}, (0, utils_1.normalizeHeaders)(upgrade === null || upgrade === void 0 ? void 0 : upgrade.headers)), (0, utils_1.normalizeHeaders)(params.headers));
51
+ const authorization = (0, utils_1.toAuthorization)((_e = (_d = (_c = params.authorization) !== null && _c !== void 0 ? _c : params.Authorization) !== null && _d !== void 0 ? _d : params.authToken) !== null && _e !== void 0 ? _e : params.token);
52
+ if (authorization !== undefined) {
53
+ headers.authorization = authorization;
54
+ }
55
+ return (0, utils_1.createRequest)(upgrade, { headers, query: {}, body: {} });
56
+ }
57
+ }
58
+ exports.GraphqlAuthTransport = GraphqlAuthTransport;
59
+ //# sourceMappingURL=graphql.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql.js","sourceRoot":"","sources":["../../src/transports/graphql.ts"],"names":[],"mappings":";;;AAGA,mCAAyF;AAEzF;;;;;;;;;;;;;;GAcG;AACH,MAAa,oBAAoB;IAAjC;QACa,SAAI,GAAG,SAAS,CAAC;QACP,aAAQ,GAAG,IAAI,oBAAY,EAAE,CAAC;IAoCrD,CAAC;IAlCG,QAAQ,CAAC,OAAyB;QAC9B,OAAO,OAAO,CAAC,OAAO,EAAU,KAAK,SAAS,CAAC;IACnD,CAAC;IAES,UAAU,CAAC,OAAyB;;QAC1C,OAAO,MAAA,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,mCAAI,EA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C,KAAK,CACnF,CAAC;QACF,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;YAC9B,OAAO,CAAC,aAAa,GAAG,aAAa,CAAC;QAC1C,CAAC;QACD,OAAO,IAAA,qBAAa,EAAC,OAAO,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;IACpE,CAAC;CACJ;AAtCD,oDAsCC"}
@@ -0,0 +1,13 @@
1
+ import { ExecutionContext } from '@nestjs/common';
2
+ import { AuthRequest, AuthTransport } from './interfaces';
3
+ /**
4
+ * The http transport: the request and the response of the http adapter (express or fastify).
5
+ */
6
+ export declare class HttpAuthTransport implements AuthTransport {
7
+ readonly name = "http";
8
+ supports(context: ExecutionContext): boolean;
9
+ getRequest(context: ExecutionContext): AuthRequest;
10
+ getResponse(context: ExecutionContext): any;
11
+ getNext(context: ExecutionContext): (err?: any) => void;
12
+ }
13
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/transports/http.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAElD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE1D;;GAEG;AACH,qBAAa,iBAAkB,YAAW,aAAa;IACnD,QAAQ,CAAC,IAAI,UAAU;IAEvB,QAAQ,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO;IAI5C,UAAU,CAAC,OAAO,EAAE,gBAAgB,GAAG,WAAW;IAIlD,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,GAAG;IAI3C,OAAO,CAAC,OAAO,EAAE,gBAAgB,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,KAAK,IAAI;CAI1D"}
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpAuthTransport = void 0;
4
+ /**
5
+ * The http transport: the request and the response of the http adapter (express or fastify).
6
+ */
7
+ class HttpAuthTransport {
8
+ constructor() {
9
+ this.name = 'http';
10
+ }
11
+ supports(context) {
12
+ return context.getType() === 'http';
13
+ }
14
+ getRequest(context) {
15
+ return context.switchToHttp().getRequest();
16
+ }
17
+ getResponse(context) {
18
+ return context.switchToHttp().getResponse();
19
+ }
20
+ getNext(context) {
21
+ const next = context.switchToHttp().getNext();
22
+ return typeof next === 'function' ? next : throwError;
23
+ }
24
+ }
25
+ exports.HttpAuthTransport = HttpAuthTransport;
26
+ function throwError(err) {
27
+ if (err !== undefined && err !== null) {
28
+ throw err;
29
+ }
30
+ }
31
+ //# sourceMappingURL=http.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/transports/http.ts"],"names":[],"mappings":";;;AAIA;;GAEG;AACH,MAAa,iBAAiB;IAA9B;QACa,SAAI,GAAG,MAAM,CAAC;IAkB3B,CAAC;IAhBG,QAAQ,CAAC,OAAyB;QAC9B,OAAO,OAAO,CAAC,OAAO,EAAE,KAAK,MAAM,CAAC;IACxC,CAAC;IAED,UAAU,CAAC,OAAyB;QAChC,OAAO,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;IAC/C,CAAC;IAED,WAAW,CAAC,OAAyB;QACjC,OAAO,OAAO,CAAC,YAAY,EAAE,CAAC,WAAW,EAAE,CAAC;IAChD,CAAC;IAED,OAAO,CAAC,OAAyB;QAC7B,MAAM,IAAI,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,OAAO,EAAE,CAAC;QAC9C,OAAO,OAAO,IAAI,KAAK,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC;IAC1D,CAAC;CACJ;AAnBD,8CAmBC;AAED,SAAS,UAAU,CAAC,GAAS;IACzB,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;QACpC,MAAM,GAAG,CAAC;IACd,CAAC;AACL,CAAC"}
@@ -0,0 +1,8 @@
1
+ export * from './graphql';
2
+ export * from './http';
3
+ export * from './interfaces';
4
+ export * from './registry';
5
+ export * from './rpc';
6
+ export * from './utils';
7
+ export * from './ws';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/transports/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AACvB,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC;AACtB,cAAc,SAAS,CAAC;AACxB,cAAc,MAAM,CAAC"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./graphql"), exports);
18
+ __exportStar(require("./http"), exports);
19
+ __exportStar(require("./interfaces"), exports);
20
+ __exportStar(require("./registry"), exports);
21
+ __exportStar(require("./rpc"), exports);
22
+ __exportStar(require("./utils"), exports);
23
+ __exportStar(require("./ws"), exports);
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/transports/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA0B;AAC1B,yCAAuB;AACvB,+CAA6B;AAC7B,6CAA2B;AAC3B,wCAAsB;AACtB,0CAAwB;AACxB,uCAAqB"}
@@ -0,0 +1,36 @@
1
+ import { ExecutionContext } from '@nestjs/common';
2
+ /**
3
+ * The request given to the passport strategies: what they read (`headers`, `query`, `body`, `session`)
4
+ * and where passport sets the user (`user`, `logIn()`, `isAuthenticated()`...).
5
+ *
6
+ * For http it is the request of the framework (express, fastify). For the other transports it is built
7
+ * from the context (GraphQL context, socket handshake, message metadata) and cached, so all the guards and
8
+ * decorators of the same request, operation or connection share it.
9
+ */
10
+ export interface AuthRequest {
11
+ headers: Record<string, string | string[] | undefined>;
12
+ query?: Record<string, unknown>;
13
+ body?: any;
14
+ session?: any;
15
+ user?: any;
16
+ [key: string]: any;
17
+ }
18
+ /**
19
+ * Reads the request of a type of execution context (http, GraphQL, websockets, microservices...).
20
+ *
21
+ * Built-in: {@link HttpAuthTransport}, {@link GraphqlAuthTransport}, {@link WsAuthTransport}, {@link RpcAuthTransport}.
22
+ * Add yours with the `transports` option of the {@link AuthModule}, or of the {@link AuthGuard}.
23
+ */
24
+ export interface AuthTransport {
25
+ /** The name of the transport, given to the exception factories (`http`, `graphql`, `ws`, `rpc`...). */
26
+ readonly name: string;
27
+ /** Returns `true` if this transport handles the context. The first transport that does is used. */
28
+ supports(context: ExecutionContext): boolean;
29
+ /** Returns the request given to passport. Must return the same object for the same request. */
30
+ getRequest(context: ExecutionContext): AuthRequest;
31
+ /** Returns the response given to passport, if the transport has one. */
32
+ getResponse?(context: ExecutionContext): any;
33
+ /** Returns the `next` function given to passport. Defaults to a function that throws the error it receives. */
34
+ getNext?(context: ExecutionContext): (err?: any) => void;
35
+ }
36
+ //# sourceMappingURL=interfaces.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../../src/transports/interfaces.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAElD;;;;;;;GAOG;AACH,MAAM,WAAW,WAAW;IACxB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;IACvD,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACtB;AAED;;;;;GAKG;AACH,MAAM,WAAW,aAAa;IAC1B,uGAAuG;IACvG,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,mGAAmG;IACnG,QAAQ,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC;IAC7C,+FAA+F;IAC/F,UAAU,CAAC,OAAO,EAAE,gBAAgB,GAAG,WAAW,CAAC;IACnD,wEAAwE;IACxE,WAAW,CAAC,CAAC,OAAO,EAAE,gBAAgB,GAAG,GAAG,CAAC;IAC7C,+GAA+G;IAC/G,OAAO,CAAC,CAAC,OAAO,EAAE,gBAAgB,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,KAAK,IAAI,CAAC;CAC5D"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=interfaces.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../src/transports/interfaces.ts"],"names":[],"mappings":""}
@@ -0,0 +1,25 @@
1
+ import { ExecutionContext } from '@nestjs/common';
2
+ import { AuthTransport } from './interfaces';
3
+ /**
4
+ * The transports used by the guards and the decorators: the built-in ones, and the ones added with
5
+ * `AuthModule.forRoot({ transports })` or {@link AuthTransports.register}.
6
+ */
7
+ export declare class AuthTransports {
8
+ private static custom;
9
+ private static readonly builtIn;
10
+ /** Adds transports. They are tried before the built-in ones, in the order they are added. */
11
+ static register(...transports: AuthTransport[]): void;
12
+ /** Removes the transports added with {@link register} (for tests). */
13
+ static reset(): void;
14
+ /** Returns the transports: the given ones first, then the registered ones, then the built-in ones. */
15
+ static all(extra?: AuthTransport[]): AuthTransport[];
16
+ /** Returns the first transport that supports the context, or `undefined`. */
17
+ static find(context: ExecutionContext, extra?: AuthTransport[]): AuthTransport | undefined;
18
+ /**
19
+ * Returns the first transport that supports the context.
20
+ *
21
+ * @throws NotImplementedException if none does
22
+ */
23
+ static get(context: ExecutionContext, extra?: AuthTransport[]): AuthTransport;
24
+ }
25
+ //# sourceMappingURL=registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/transports/registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAA2B,MAAM,gBAAgB,CAAC;AAI3E,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAI7C;;;GAGG;AACH,qBAAa,cAAc;IACvB,OAAO,CAAC,MAAM,CAAC,MAAM,CAAuB;IAC5C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAK7B;IAEF,6FAA6F;IAC7F,MAAM,CAAC,QAAQ,CAAC,GAAG,UAAU,EAAE,aAAa,EAAE,GAAG,IAAI;IAQrD,sEAAsE;IACtE,MAAM,CAAC,KAAK,IAAI,IAAI;IAIpB,sGAAsG;IACtG,MAAM,CAAC,GAAG,CAAC,KAAK,GAAE,aAAa,EAAO,GAAG,aAAa,EAAE;IAIxD,6EAA6E;IAC7E,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,gBAAgB,EAAE,KAAK,GAAE,aAAa,EAAO,GAAG,aAAa,GAAG,SAAS;IAI9F;;;;OAIG;IACH,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,gBAAgB,EAAE,KAAK,GAAE,aAAa,EAAO,GAAG,aAAa;CAOpF"}
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthTransports = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ const graphql_1 = require("./graphql");
6
+ const http_1 = require("./http");
7
+ const rpc_1 = require("./rpc");
8
+ const ws_1 = require("./ws");
9
+ /**
10
+ * The transports used by the guards and the decorators: the built-in ones, and the ones added with
11
+ * `AuthModule.forRoot({ transports })` or {@link AuthTransports.register}.
12
+ */
13
+ class AuthTransports {
14
+ /** Adds transports. They are tried before the built-in ones, in the order they are added. */
15
+ static register(...transports) {
16
+ for (const transport of transports) {
17
+ if (!AuthTransports.custom.includes(transport)) {
18
+ AuthTransports.custom.push(transport);
19
+ }
20
+ }
21
+ }
22
+ /** Removes the transports added with {@link register} (for tests). */
23
+ static reset() {
24
+ AuthTransports.custom = [];
25
+ }
26
+ /** Returns the transports: the given ones first, then the registered ones, then the built-in ones. */
27
+ static all(extra = []) {
28
+ return [...extra, ...AuthTransports.custom, ...AuthTransports.builtIn];
29
+ }
30
+ /** Returns the first transport that supports the context, or `undefined`. */
31
+ static find(context, extra = []) {
32
+ return AuthTransports.all(extra).find((t) => t.supports(context));
33
+ }
34
+ /**
35
+ * Returns the first transport that supports the context.
36
+ *
37
+ * @throws NotImplementedException if none does
38
+ */
39
+ static get(context, extra = []) {
40
+ const transport = AuthTransports.find(context, extra);
41
+ if (transport === undefined) {
42
+ throw new common_1.NotImplementedException(`Auth Context ${context.getType()} not implemented`);
43
+ }
44
+ return transport;
45
+ }
46
+ }
47
+ exports.AuthTransports = AuthTransports;
48
+ AuthTransports.custom = [];
49
+ AuthTransports.builtIn = [
50
+ new http_1.HttpAuthTransport(),
51
+ new graphql_1.GraphqlAuthTransport(),
52
+ new ws_1.WsAuthTransport(),
53
+ new rpc_1.RpcAuthTransport()
54
+ ];
55
+ //# sourceMappingURL=registry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.js","sourceRoot":"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,CAAC;IACrB,CAAC;;AA5CL,wCA6CC;AA5CkB,qBAAM,GAAoB,EAAE,CAAC;AACpB,sBAAO,GAAoB;IAC/C,IAAI,wBAAiB,EAAE;IACvB,IAAI,8BAAoB,EAAE;IAC1B,IAAI,oBAAe,EAAE;IACrB,IAAI,sBAAgB,EAAE;CACzB,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { ExecutionContext } from '@nestjs/common';
2
+ import { AuthRequest, AuthTransport } from './interfaces';
3
+ import { RequestCache } from './utils';
4
+ /**
5
+ * The microservices transport (message and event patterns).
6
+ *
7
+ * The headers are read from the message metadata of the transporter:
8
+ *
9
+ * - gRPC: the `Metadata`
10
+ * - NATS: `NatsContext.getHeaders()`
11
+ * - Kafka: the message headers; RabbitMQ: the message properties headers
12
+ * - the other transporters (TCP, Redis, MQTT): the `headers` property of the data
13
+ *
14
+ * The data is the `body`.
15
+ */
16
+ export declare class RpcAuthTransport implements AuthTransport {
17
+ readonly name = "rpc";
18
+ protected readonly requests: RequestCache;
19
+ supports(context: ExecutionContext): boolean;
20
+ getRequest(context: ExecutionContext): AuthRequest;
21
+ protected getHeaders(ctx: any, data: any): Record<string, string | string[]>;
22
+ }
23
+ //# sourceMappingURL=rpc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../../src/transports/rpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAElD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAmC,YAAY,EAAE,MAAM,SAAS,CAAC;AAExE;;;;;;;;;;;GAWG;AACH,qBAAa,gBAAiB,YAAW,aAAa;IAClD,QAAQ,CAAC,IAAI,SAAS;IACtB,SAAS,CAAC,QAAQ,CAAC,QAAQ,eAAsB;IAEjD,QAAQ,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO;IAI5C,UAAU,CAAC,OAAO,EAAE,gBAAgB,GAAG,WAAW;IASlD,SAAS,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAsB/E"}
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RpcAuthTransport = void 0;
4
+ const utils_1 = require("./utils");
5
+ /**
6
+ * The microservices transport (message and event patterns).
7
+ *
8
+ * The headers are read from the message metadata of the transporter:
9
+ *
10
+ * - gRPC: the `Metadata`
11
+ * - NATS: `NatsContext.getHeaders()`
12
+ * - Kafka: the message headers; RabbitMQ: the message properties headers
13
+ * - the other transporters (TCP, Redis, MQTT): the `headers` property of the data
14
+ *
15
+ * The data is the `body`.
16
+ */
17
+ class RpcAuthTransport {
18
+ constructor() {
19
+ this.name = 'rpc';
20
+ this.requests = new utils_1.RequestCache();
21
+ }
22
+ supports(context) {
23
+ return context.getType() === 'rpc';
24
+ }
25
+ getRequest(context) {
26
+ const rpc = context.switchToRpc();
27
+ const ctx = rpc.getContext();
28
+ const data = rpc.getData();
29
+ return this.requests.get(ctx !== null && ctx !== void 0 ? ctx : data, () => (0, utils_1.createRequest)(undefined, { headers: this.getHeaders(ctx, data), query: {}, body: data }));
30
+ }
31
+ getHeaders(ctx, data) {
32
+ var _a, _b;
33
+ // gRPC Metadata
34
+ if (typeof (ctx === null || ctx === void 0 ? void 0 : ctx.getMap) === 'function') {
35
+ return (0, utils_1.normalizeHeaders)(ctx.getMap());
36
+ }
37
+ // NATS
38
+ if (typeof (ctx === null || ctx === void 0 ? void 0 : ctx.getHeaders) === 'function') {
39
+ const headers = ctx.getHeaders();
40
+ if (headers !== undefined && typeof headers.keys === 'function' && typeof headers.get === 'function') {
41
+ return (0, utils_1.normalizeHeaders)(Object.fromEntries(Array.from(headers.keys(), (k) => [k, headers.get(k)])));
42
+ }
43
+ return (0, utils_1.normalizeHeaders)(headers);
44
+ }
45
+ // Kafka, RabbitMQ
46
+ if (typeof (ctx === null || ctx === void 0 ? void 0 : ctx.getMessage) === 'function') {
47
+ const message = ctx.getMessage();
48
+ return (0, utils_1.normalizeHeaders)((_a = message === null || message === void 0 ? void 0 : message.headers) !== null && _a !== void 0 ? _a : (_b = message === null || message === void 0 ? void 0 : message.properties) === null || _b === void 0 ? void 0 : _b.headers);
49
+ }
50
+ return (0, utils_1.normalizeHeaders)(data === null || data === void 0 ? void 0 : data.headers);
51
+ }
52
+ }
53
+ exports.RpcAuthTransport = RpcAuthTransport;
54
+ //# sourceMappingURL=rpc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rpc.js","sourceRoot":"","sources":["../../src/transports/rpc.ts"],"names":[],"mappings":";;;AAGA,mCAAwE;AAExE;;;;;;;;;;;GAWG;AACH,MAAa,gBAAgB;IAA7B;QACa,SAAI,GAAG,KAAK,CAAC;QACH,aAAQ,GAAG,IAAI,oBAAY,EAAE,CAAC;IAqCrD,CAAC;IAnCG,QAAQ,CAAC,OAAyB;QAC9B,OAAO,OAAO,CAAC,OAAO,EAAE,KAAK,KAAK,CAAC;IACvC,CAAC;IAED,UAAU,CAAC,OAAyB;QAChC,MAAM,GAAG,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;QAClC,MAAM,GAAG,GAAG,GAAG,CAAC,UAAU,EAAE,CAAC;QAC7B,MAAM,IAAI,GAAG,GAAG,CAAC,OAAO,EAAE,CAAC;QAC3B,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,aAAH,GAAG,cAAH,GAAG,GAAI,IAAI,EAAE,GAAG,EAAE,CACvC,IAAA,qBAAa,EAAC,SAAS,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAC3F,CAAC;IACN,CAAC;IAES,UAAU,C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AC;IAC3C,CAAC;CACJ;AAvCD,4CAuCC"}
@@ -0,0 +1,21 @@
1
+ import { AuthRequest } from './interfaces';
2
+ /** Lower cases the header names and converts the values (Buffer, number...) to strings. */
3
+ export declare function normalizeHeaders(headers: unknown): Record<string, string | string[]>;
4
+ /** Returns `Bearer <token>`, or the value if it already has a scheme. */
5
+ export declare function toAuthorization(token: unknown): string | undefined;
6
+ /**
7
+ * Returns a request that reads what is not set on it from `base` (e.g. the session of the upgrade request),
8
+ * with its own `headers`, `query` and `body`, so `base` is never modified.
9
+ */
10
+ export declare function createRequest(base: object | undefined, parts: {
11
+ headers: Record<string, string | string[]>;
12
+ query?: Record<string, unknown>;
13
+ body?: any;
14
+ }): AuthRequest;
15
+ /** Caches the requests built for an object (a GraphQL context, a socket, a message context). */
16
+ export declare class RequestCache {
17
+ private readonly cache;
18
+ get(key: unknown, create: () => AuthRequest): AuthRequest;
19
+ set(key: object, request: AuthRequest): void;
20
+ }
21
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/transports/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,2FAA2F;AAC3F,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAyBpF;AAeD,yEAAyE;AACzE,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAKlE;AAED;;;GAGG;AACH,wBAAgB,aAAa,CACzB,IAAI,EAAE,MAAM,GAAG,SAAS,EACxB,KAAK,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAAC,IAAI,CAAC,EAAE,GAAG,CAAA;CAAE,GACnG,WAAW,CAMb;AAED,gGAAgG;AAChG,qBAAa,YAAY;IACrB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAsC;IAE5D,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,GAAG,WAAW;IAYzD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,GAAG,IAAI;CAG/C"}
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RequestCache = void 0;
4
+ exports.normalizeHeaders = normalizeHeaders;
5
+ exports.toAuthorization = toAuthorization;
6
+ exports.createRequest = createRequest;
7
+ /** Lower cases the header names and converts the values (Buffer, number...) to strings. */
8
+ function normalizeHeaders(headers) {
9
+ const result = {};
10
+ if (headers === undefined || headers === null || typeof headers !== 'object') {
11
+ return result;
12
+ }
13
+ const entries = typeof headers.entries === 'function' && !(headers instanceof Array)
14
+ ? Array.from(headers.entries())
15
+ : Object.entries(headers);
16
+ for (const [key, value] of entries) {
17
+ if (value === undefined || value === null) {
18
+ continue;
19
+ }
20
+ const name = key.toLowerCase();
21
+ if (Array.isArray(value)) {
22
+ const values = value.map((v) => toHeaderValue(v)).filter((v) => v !== undefined);
23
+ result[name] = values.length === 1 ? values[0] : values;
24
+ }
25
+ else {
26
+ const v = toHeaderValue(value);
27
+ if (v !== undefined) {
28
+ result[name] = v;
29
+ }
30
+ }
31
+ }
32
+ return result;
33
+ }
34
+ function toHeaderValue(value) {
35
+ if (typeof value === 'string') {
36
+ return value;
37
+ }
38
+ if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
39
+ return Buffer.from(value).toString('utf8');
40
+ }
41
+ if (typeof value === 'number' || typeof value === 'boolean') {
42
+ return String(value);
43
+ }
44
+ return undefined;
45
+ }
46
+ /** Returns `Bearer <token>`, or the value if it already has a scheme. */
47
+ function toAuthorization(token) {
48
+ if (typeof token !== 'string' || token === '') {
49
+ return undefined;
50
+ }
51
+ return /^[A-Za-z][A-Za-z0-9-]*\s/.test(token) ? token : `Bearer ${token}`;
52
+ }
53
+ /**
54
+ * Returns a request that reads what is not set on it from `base` (e.g. the session of the upgrade request),
55
+ * with its own `headers`, `query` and `body`, so `base` is never modified.
56
+ */
57
+ function createRequest(base, parts) {
58
+ var _a, _b;
59
+ const request = Object.create(base !== null && base !== void 0 ? base : null);
60
+ request.headers = parts.headers;
61
+ request.query = (_a = parts.query) !== null && _a !== void 0 ? _a : {};
62
+ request.body = (_b = parts.body) !== null && _b !== void 0 ? _b : {};
63
+ return request;
64
+ }
65
+ /** Caches the requests built for an object (a GraphQL context, a socket, a message context). */
66
+ class RequestCache {
67
+ constructor() {
68
+ this.cache = new WeakMap();
69
+ }
70
+ get(key, create) {
71
+ if (key === null || (typeof key !== 'object' && typeof key !== 'function')) {
72
+ return create();
73
+ }
74
+ let request = this.cache.get(key);
75
+ if (request === undefined) {
76
+ request = create();
77
+ this.cache.set(key, request);
78
+ }
79
+ return request;
80
+ }
81
+ set(key, request) {
82
+ this.cache.set(key, request);
83
+ }
84
+ }
85
+ exports.RequestCache = RequestCache;
86
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/transports/utils.ts"],"names":[],"mappings":";;;AAGA,4CAyBC;AAgBD,0CAKC;AAMD,sCASC;AA9DD,2FAA2F;AAC3F,SAAgB,gBAAgB,CAAC,OAAgB;IAC7C,MAAM,MAAM,GAAsC,EAAE,CAAC;IACrD,IAAI,OAAO,KAAK,SAAS,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAC3E,OAAO,MAAM,CAAC;IAClB,CAAC;IACD,MAAM,OAAO,GACT,OAAQ,OAAe,CAAC,OAAO,KAAK,UAAU,IAAI,CAAC,CAAC,OAAO,YAAY,KAAK,CAAC;QACzE,CAAC,CAAC,KAAK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