@edirect/auth-fastify 0.0.0-stage → 11.2.5

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 (65) hide show
  1. package/README.md +87 -2
  2. package/package.json +34 -3
  3. package/src/auth.service.d.ts +34 -0
  4. package/src/auth.service.d.ts.map +1 -0
  5. package/src/auth.service.js +117 -0
  6. package/src/config.d.ts +33 -0
  7. package/src/config.d.ts.map +1 -0
  8. package/src/config.js +74 -0
  9. package/src/constants.d.ts +57 -0
  10. package/src/constants.d.ts.map +1 -0
  11. package/src/constants.js +74 -0
  12. package/src/decorators.d.ts +17 -0
  13. package/src/decorators.d.ts.map +1 -0
  14. package/src/decorators.js +16 -0
  15. package/src/hooks/auth-service.d.ts +20 -0
  16. package/src/hooks/auth-service.d.ts.map +1 -0
  17. package/src/hooks/auth-service.js +135 -0
  18. package/src/hooks/auto.d.ts +16 -0
  19. package/src/hooks/auto.d.ts.map +1 -0
  20. package/src/hooks/auto.js +104 -0
  21. package/src/hooks/keycloak.d.ts +21 -0
  22. package/src/hooks/keycloak.d.ts.map +1 -0
  23. package/src/hooks/keycloak.js +139 -0
  24. package/src/hooks/token-exchange.d.ts +11 -0
  25. package/src/hooks/token-exchange.d.ts.map +1 -0
  26. package/src/hooks/token-exchange.js +58 -0
  27. package/src/index.d.ts +19 -0
  28. package/src/index.d.ts.map +1 -0
  29. package/src/index.js +39 -0
  30. package/src/interfaces/entity-user.interface.d.ts +14 -0
  31. package/src/interfaces/entity-user.interface.d.ts.map +1 -0
  32. package/src/interfaces/entity-user.interface.js +19 -0
  33. package/src/interfaces/entity.interface.d.ts +25 -0
  34. package/src/interfaces/entity.interface.d.ts.map +1 -0
  35. package/src/interfaces/entity.interface.js +2 -0
  36. package/src/interfaces/index.d.ts +26 -0
  37. package/src/interfaces/index.d.ts.map +1 -0
  38. package/src/interfaces/index.js +8 -0
  39. package/src/interfaces/keycloak-token.interface.d.ts +84 -0
  40. package/src/interfaces/keycloak-token.interface.d.ts.map +1 -0
  41. package/src/interfaces/keycloak-token.interface.js +2 -0
  42. package/src/interfaces/user.interface.d.ts +40 -0
  43. package/src/interfaces/user.interface.d.ts.map +1 -0
  44. package/src/interfaces/user.interface.js +91 -0
  45. package/src/plugin.d.ts +44 -0
  46. package/src/plugin.d.ts.map +1 -0
  47. package/src/plugin.js +103 -0
  48. package/src/providers/keycloak-token-exchange.service.d.ts +15 -0
  49. package/src/providers/keycloak-token-exchange.service.d.ts.map +1 -0
  50. package/src/providers/keycloak-token-exchange.service.js +62 -0
  51. package/src/providers/memory-cache.d.ts +16 -0
  52. package/src/providers/memory-cache.d.ts.map +1 -0
  53. package/src/providers/memory-cache.js +24 -0
  54. package/src/providers/well-known.service.d.ts +18 -0
  55. package/src/providers/well-known.service.d.ts.map +1 -0
  56. package/src/providers/well-known.service.js +41 -0
  57. package/src/realm-env-key.d.ts +3 -0
  58. package/src/realm-env-key.d.ts.map +1 -0
  59. package/src/realm-env-key.js +27 -0
  60. package/src/types.d.ts +27 -0
  61. package/src/types.d.ts.map +1 -0
  62. package/src/types.js +2 -0
  63. package/src/util.d.ts +18 -0
  64. package/src/util.d.ts.map +1 -0
  65. package/src/util.js +41 -0
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRealmEnvKey = getRealmEnvKey;
4
+ exports.getProcessEnvByRealmKey = getProcessEnvByRealmKey;
5
+ const tslib_1 = require("tslib");
6
+ const debug_1 = tslib_1.__importDefault(require("debug"));
7
+ const get_1 = tslib_1.__importDefault(require("lodash/get"));
8
+ const debug = (0, debug_1.default)('auth-module:realm-env-key-functions');
9
+ function getRealmEnvKey(key, realm) {
10
+ if (key.includes('KEYCLOAK_')) {
11
+ const keySplited = key.split('_');
12
+ const firstElement = keySplited.shift();
13
+ return [firstElement, realm, ...keySplited]
14
+ .join('_')
15
+ .replace(/[-.]/g, '_')
16
+ .toUpperCase();
17
+ }
18
+ return key;
19
+ }
20
+ function getProcessEnvByRealmKey(key, realm) {
21
+ const realmKey = getRealmEnvKey(key, realm);
22
+ const result = (0, get_1.default)(process.env, realmKey);
23
+ if (result)
24
+ return result;
25
+ debug('[WARN] Config %s not found', realmKey);
26
+ return (0, get_1.default)(process.env, key);
27
+ }
package/src/types.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ import type { UserInterface } from './interfaces/user.interface';
2
+ import type { AuthService } from './auth.service';
3
+ declare module 'fastify' {
4
+ interface FastifyRequest {
5
+ /** Authenticated user. Set by the authentication hook. */
6
+ user?: UserInterface;
7
+ /** Original user when the token was exchanged. */
8
+ externalUser?: UserInterface;
9
+ token?: string;
10
+ externalToken?: string;
11
+ /** `keycloak` or `auth-service`. Set by the authentication hook. */
12
+ tokenProvider?: string;
13
+ /** Request-scoped helper, equivalent to the `AuthService` of `@edirect/auth`. */
14
+ readonly auth: AuthService;
15
+ }
16
+ interface FastifyContextConfig {
17
+ /** Route needs one of these roles. See `Roles()`. */
18
+ roles?: string[];
19
+ /** Route needs one of these permissions. See `Permissions()`. */
20
+ permissions?: string[];
21
+ /** Route needs access to this resource. See `Resources()`. */
22
+ resources?: string[];
23
+ /** Set to `false` to skip authentication and authorization on a route. */
24
+ auth?: boolean;
25
+ }
26
+ }
27
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AACjE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAElD,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAU,cAAc;QACtB,0DAA0D;QAC1D,IAAI,CAAC,EAAE,aAAa,CAAC;QACrB,kDAAkD;QAClD,YAAY,CAAC,EAAE,aAAa,CAAC;QAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,oEAAoE;QACpE,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,iFAAiF;QACjF,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;KAC5B;IAED,UAAU,oBAAoB;QAC5B,qDAAqD;QACrD,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;QACjB,iEAAiE;QACjE,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;QACvB,8DAA8D;QAC9D,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;QACrB,0EAA0E;QAC1E,IAAI,CAAC,EAAE,OAAO,CAAC;KAChB;CACF"}
package/src/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/src/util.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import type { FastifyRequest } from 'fastify';
2
+ /**
3
+ * Returns true when authentication/authorization should be bypassed.
4
+ *
5
+ * On the "idp" platform, auth is disabled by default. Setting the
6
+ * ENABLE_IDP_AUTH environment variable to "true" re-enables auth for idp.
7
+ * On any other platform, auth is always enabled.
8
+ */
9
+ export declare const isAuthDisabled: () => boolean;
10
+ /** Bearer token from the Authorization header, or `token` from a JSON body. */
11
+ export declare const getToken: (req: FastifyRequest) => string;
12
+ export declare const setToken: (req: FastifyRequest, token: string) => void;
13
+ export declare const checkPropertiesObject: (obj: object, properties: string[]) => boolean;
14
+ export declare const errorFormatter: (error: any) => {
15
+ status: boolean;
16
+ message: any;
17
+ };
18
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAS9C;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,QAAO,OAE0C,CAAC;AAE7E,+EAA+E;AAC/E,eAAO,MAAM,QAAQ,GAAI,KAAK,cAAc,KAAG,MAI3C,CAAC;AAEL,eAAO,MAAM,QAAQ,GAAI,KAAK,cAAc,EAAE,OAAO,MAAM,KAAG,IAE7D,CAAC;AAEF,eAAO,MAAM,qBAAqB,GAAI,KAAK,MAAM,EAAE,YAAY,MAAM,EAAE,YAClB,CAAC;AAEtD,eAAO,MAAM,cAAc,GAAI,OAAO,GAAG;;;CAcxC,CAAC"}
package/src/util.js ADDED
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.errorFormatter = exports.checkPropertiesObject = exports.setToken = exports.getToken = exports.isAuthDisabled = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const axios_1 = require("axios");
6
+ const set_1 = tslib_1.__importDefault(require("lodash/set"));
7
+ const constants_1 = require("./constants");
8
+ /**
9
+ * Returns true when authentication/authorization should be bypassed.
10
+ *
11
+ * On the "idp" platform, auth is disabled by default. Setting the
12
+ * ENABLE_IDP_AUTH environment variable to "true" re-enables auth for idp.
13
+ * On any other platform, auth is always enabled.
14
+ */
15
+ const isAuthDisabled = () => process.env[constants_1.BOLTTECH_PLATFORM_ENV_KEY] === constants_1.BOLTTECH_PLATFORM_IDP &&
16
+ process.env[constants_1.BOLTTECH_ENABLE_IDP_AUTH_ENV_KEY] !== constants_1.BOLTTECH_ENABLE_IDP_AUTH;
17
+ exports.isAuthDisabled = isAuthDisabled;
18
+ /** Bearer token from the Authorization header, or `token` from a JSON body. */
19
+ const getToken = (req) => (req.headers.authorization &&
20
+ req.headers.authorization.replace('Bearer ', '')) ||
21
+ req.body?.token ||
22
+ '';
23
+ exports.getToken = getToken;
24
+ const setToken = (req, token) => {
25
+ req.headers.authorization = `Bearer ${token}`;
26
+ };
27
+ exports.setToken = setToken;
28
+ const checkPropertiesObject = (obj, properties) => properties.every(i => Object.keys(obj).includes(i));
29
+ exports.checkPropertiesObject = checkPropertiesObject;
30
+ const errorFormatter = (error) => {
31
+ const payload = {
32
+ status: false,
33
+ message: error.message || error,
34
+ };
35
+ if (error instanceof axios_1.AxiosError) {
36
+ (0, set_1.default)(payload, 'details.status', error?.response?.status || 500);
37
+ (0, set_1.default)(payload, 'details.data', error?.response?.data || { message: 'unknown error' });
38
+ }
39
+ return payload;
40
+ };
41
+ exports.errorFormatter = errorFormatter;