@tstdl/base 0.92.145 → 0.92.147

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 (251) hide show
  1. package/ai/ai-file.service.d.ts +29 -1
  2. package/ai/ai-file.service.js +66 -23
  3. package/ai/ai-session.d.ts +28 -1
  4. package/ai/ai-session.js +27 -0
  5. package/ai/ai.service.d.ts +89 -5
  6. package/ai/ai.service.js +130 -27
  7. package/ai/functions.d.ts +7 -1
  8. package/ai/functions.js +7 -1
  9. package/ai/module.d.ts +8 -0
  10. package/ai/module.js +4 -0
  11. package/ai/types.d.ts +115 -2
  12. package/ai/types.js +16 -0
  13. package/api/client/client.d.ts +1 -1
  14. package/api/client/client.js +1 -1
  15. package/api/default-error-handlers.d.ts +1 -1
  16. package/api/index.d.ts +1 -9
  17. package/api/index.js +1 -9
  18. package/api/response.d.ts +1 -1
  19. package/api/server/api-controller.d.ts +1 -1
  20. package/api/server/error-handler.d.ts +1 -1
  21. package/api/server/gateway.d.ts +1 -5
  22. package/api/server/gateway.js +0 -4
  23. package/api/server/middlewares/catch-error.middleware.d.ts +1 -1
  24. package/api/server/module.d.ts +1 -1
  25. package/api/types.d.ts +1 -1
  26. package/application/application.d.ts +1 -1
  27. package/authentication/authentication.api.d.ts +36 -1
  28. package/authentication/authentication.api.js +28 -0
  29. package/authentication/client/api.client.d.ts +14 -1
  30. package/authentication/client/api.client.js +13 -0
  31. package/authentication/client/authentication.service.d.ts +104 -1
  32. package/authentication/client/authentication.service.js +103 -0
  33. package/authentication/client/http-client.middleware.d.ts +5 -0
  34. package/authentication/client/http-client.middleware.js +6 -2
  35. package/authentication/client/module.d.ts +20 -1
  36. package/authentication/client/module.js +6 -1
  37. package/authentication/client/tokens.d.ts +6 -0
  38. package/authentication/client/tokens.js +6 -0
  39. package/authentication/models/authentication-credentials.model.d.ts +6 -0
  40. package/authentication/models/authentication-credentials.model.js +6 -0
  41. package/authentication/models/authentication-session.model.d.ts +6 -0
  42. package/authentication/models/authentication-session.model.js +6 -0
  43. package/authentication/models/init-secret-reset-data.model.d.ts +10 -1
  44. package/authentication/models/init-secret-reset-data.model.js +10 -1
  45. package/authentication/models/token-payload-base.model.d.ts +24 -4
  46. package/authentication/models/token-payload-base.model.js +24 -4
  47. package/authentication/models/token.model.d.ts +33 -2
  48. package/authentication/server/authentication-ancillary.service.d.ts +27 -4
  49. package/authentication/server/authentication-ancillary.service.js +7 -0
  50. package/authentication/server/authentication-api-request-token.provider.d.ts +3 -0
  51. package/authentication/server/authentication-api-request-token.provider.js +3 -0
  52. package/authentication/server/authentication-secret-requirements.validator.d.ts +37 -0
  53. package/authentication/server/authentication-secret-requirements.validator.js +22 -0
  54. package/authentication/server/authentication.api-controller.d.ts +66 -1
  55. package/authentication/server/authentication.api-controller.js +65 -0
  56. package/authentication/server/authentication.service.d.ts +191 -11
  57. package/authentication/server/authentication.service.js +157 -8
  58. package/authentication/server/helper.d.ts +44 -5
  59. package/authentication/server/helper.js +43 -4
  60. package/authentication/server/module.d.ts +23 -1
  61. package/authentication/server/module.js +23 -1
  62. package/browser/browser-context-controller.d.ts +1 -1
  63. package/browser/browser-controller.d.ts +1 -1
  64. package/browser/browser-controller.js +1 -1
  65. package/browser/element-controller.d.ts +1 -1
  66. package/browser/locator-controller.d.ts +1 -1
  67. package/context/context.d.ts +1 -1
  68. package/data-structures/context-data-map.d.ts +1 -1
  69. package/database/mongo/mongo-base.repository.d.ts +1 -1
  70. package/database/mongo/types.d.ts +1 -1
  71. package/database/query.d.ts +1 -1
  72. package/document-management/models/document-management-table.d.ts +1 -1
  73. package/document-management/models/document-validation-definition.model.d.ts +1 -1
  74. package/document-management/server/schemas.d.ts +1 -1
  75. package/document-management/server/services/document-collection.service.d.ts +1 -1
  76. package/document-management/server/services/document-management-observation.service.d.ts +1 -1
  77. package/document-management/server/services/document-management.service.d.ts +1 -1
  78. package/document-management/server/services/document-property.service.d.ts +3 -3
  79. package/document-management/server/services/document-request.service.d.ts +1 -1
  80. package/document-management/server/services/document-validation.service.d.ts +1 -1
  81. package/document-management/server/services/document-workflow.service.d.ts +1 -1
  82. package/document-management/server/services/document.service.d.ts +1 -1
  83. package/document-management/server/services/singleton.d.ts +1 -1
  84. package/document-management/service-models/document-management.view-model.d.ts +1 -1
  85. package/document-management/service-models/enriched/enriched-document-assignment.view.d.ts +1 -1
  86. package/document-management/service-models/enriched/enriched-document-category.view.d.ts +1 -1
  87. package/document-management/service-models/enriched/enriched-document-collection.view.d.ts +1 -1
  88. package/document-management/service-models/enriched/enriched-document-request.view.d.ts +1 -1
  89. package/document-management/service-models/enriched/enriched-document-type.view.d.ts +1 -1
  90. package/document-management/service-models/enriched/enriched-document.view.d.ts +1 -1
  91. package/document-management/service-models/enriched/enriched-requests-template-data.model.d.ts +1 -1
  92. package/dom/file-select-dialog.d.ts +1 -1
  93. package/enumeration/enumeration.d.ts +1 -1
  94. package/errors/custom.error.d.ts +3 -0
  95. package/errors/custom.error.js +0 -1
  96. package/errors/errors.localization.d.ts +1 -1
  97. package/errors/not-supported.error.d.ts +1 -1
  98. package/{formats.js → formats/formats.js} +3 -3
  99. package/formats/index.d.ts +1 -0
  100. package/formats/index.js +1 -0
  101. package/http/client/http-client-request.d.ts +1 -1
  102. package/http/client/http-client-response.d.ts +1 -1
  103. package/http/client/http-client.d.ts +1 -1
  104. package/http/client/module.d.ts +1 -1
  105. package/http/http-body.d.ts +1 -1
  106. package/http/http-value-map.d.ts +1 -1
  107. package/http/http.error.d.ts +1 -1
  108. package/http/server/http-server-request.d.ts +1 -1
  109. package/http/server/http-server-response.d.ts +1 -1
  110. package/http/types.d.ts +1 -1
  111. package/http/utils.d.ts +1 -1
  112. package/injector/decorators.d.ts +1 -1
  113. package/injector/index.d.ts +1 -1
  114. package/injector/index.js +1 -1
  115. package/injector/injector.d.ts +10 -1
  116. package/injector/injector.js +6 -0
  117. package/injector/interfaces.d.ts +1 -1
  118. package/injector/provider.d.ts +1 -1
  119. package/injector/resolution.d.ts +10 -5
  120. package/injector/resolve-chain.d.ts +2 -2
  121. package/injector/resolve-chain.js +1 -1
  122. package/injector/resolve.error.js +1 -1
  123. package/injector/token.d.ts +8 -1
  124. package/injector/token.js +7 -0
  125. package/injector/types.d.ts +1 -1
  126. package/key-value-store/key-value-store.provider.d.ts +2 -2
  127. package/key-value-store/key-value.store.d.ts +2 -2
  128. package/key-value-store/mongo/mongo-key-value-store.provider.d.ts +1 -1
  129. package/key-value-store/mongo/mongo-key-value.store.d.ts +1 -1
  130. package/key-value-store/postgres/key-value-store.service.d.ts +1 -1
  131. package/mail/mail.service.d.ts +1 -1
  132. package/mail/models/mail-data.model.d.ts +1 -1
  133. package/mail/models/mail-template.model.d.ts +1 -1
  134. package/mail/module.d.ts +1 -1
  135. package/module/index.d.ts +0 -1
  136. package/module/index.js +0 -1
  137. package/module/module-base.d.ts +1 -1
  138. package/module/module-metric-reporter.js +1 -1
  139. package/module/module.d.ts +1 -1
  140. package/module/modules/function.module.js +1 -1
  141. package/object-storage/object.d.ts +1 -1
  142. package/openid-connect/mongo-oidc-state.repository.d.ts +1 -1
  143. package/openid-connect/oidc.service.d.ts +1 -1
  144. package/orm/decorators.d.ts +2 -2
  145. package/orm/entity.d.ts +1 -1
  146. package/orm/index.d.ts +3 -3
  147. package/orm/index.js +3 -3
  148. package/orm/query.d.ts +1 -1
  149. package/orm/repository.types.d.ts +1 -1
  150. package/orm/schemas/json.d.ts +1 -1
  151. package/orm/server/database-schema.d.ts +1 -1
  152. package/orm/server/drizzle/schema-converter.d.ts +1 -1
  153. package/orm/server/repository.d.ts +1 -1
  154. package/orm/server/transaction.d.ts +1 -1
  155. package/orm/server/transactional.d.ts +3 -3
  156. package/orm/server/types.d.ts +1 -1
  157. package/orm/types.d.ts +1 -1
  158. package/package.json +23 -20
  159. package/queue/mongo/job.d.ts +1 -1
  160. package/queue/mongo/queue.js +31 -31
  161. package/queue/postgres/job.model.d.ts +1 -1
  162. package/queue/postgres/queue.d.ts +1 -1
  163. package/queue/postgres/queue.provider.d.ts +1 -1
  164. package/queue/provider.d.ts +1 -1
  165. package/reflection/decorators.d.ts +1 -1
  166. package/reflection/registry.d.ts +1 -1
  167. package/reflection/types.d.ts +1 -1
  168. package/reflection/utils.d.ts +1 -1
  169. package/rpc/model.d.ts +1 -1
  170. package/rxjs-utils/retry-backoff.js +2 -2
  171. package/schema/converters/openapi-converter.d.ts +1 -1
  172. package/schema/decorators/schema.d.ts +1 -1
  173. package/schema/decorators/utils.d.ts +1 -1
  174. package/schema/schema.d.ts +1 -1
  175. package/schema/schema.error.d.ts +1 -1
  176. package/schema/schemas/array.d.ts +1 -1
  177. package/schema/schemas/enumeration.d.ts +1 -1
  178. package/schema/schemas/function.d.ts +1 -1
  179. package/schema/schemas/instance.d.ts +1 -1
  180. package/schema/schemas/nullable.d.ts +1 -1
  181. package/schema/schemas/number.d.ts +1 -1
  182. package/schema/schemas/object.d.ts +1 -1
  183. package/schema/schemas/one-or-many.d.ts +1 -1
  184. package/schema/schemas/optional.d.ts +1 -1
  185. package/schema/schemas/simple.d.ts +1 -1
  186. package/search-index/elastic/model/index-mapping.d.ts +1 -1
  187. package/search-index/elastic/search-index.js +3 -4
  188. package/search-index/memory/memory-search-index.d.ts +1 -1
  189. package/search-index/memory/memory-search-index.js +1 -1
  190. package/serializer/handlers/binary.d.ts +1 -1
  191. package/serializer/serializable.d.ts +1 -1
  192. package/serializer/types.d.ts +1 -1
  193. package/templates/module.d.ts +1 -1
  194. package/templates/renderers/handlebars.template-renderer.d.ts +1 -1
  195. package/templates/renderers/jsx.template-renderer.d.ts +1 -1
  196. package/templates/renderers/mjml.template-renderer.d.ts +1 -1
  197. package/templates/renderers/string.template-renderer.d.ts +1 -1
  198. package/templates/resolvers/file.template-resolver.d.ts +1 -1
  199. package/templates/resolvers/jsx.template-resolver.d.ts +1 -1
  200. package/templates/resolvers/string.template-resolver.d.ts +1 -1
  201. package/templates/template.model.d.ts +1 -1
  202. package/templates/template.renderer.d.ts +1 -1
  203. package/templates/template.service.d.ts +1 -1
  204. package/text/dynamic-text.model.d.ts +1 -1
  205. package/text/localization.service.d.ts +1 -1
  206. package/types/geo-json.d.ts +1 -1
  207. package/types/index.d.ts +2 -0
  208. package/types/tagged.d.ts +1 -1
  209. package/{types.d.ts → types/types.d.ts} +2 -1
  210. package/utils/async-hook/async-hook.d.ts +109 -0
  211. package/utils/async-hook/async-hook.js +77 -3
  212. package/utils/backoff.d.ts +125 -43
  213. package/utils/backoff.js +140 -65
  214. package/utils/base64.d.ts +1 -1
  215. package/utils/base64.js +1 -2
  216. package/utils/binary.d.ts +1 -1
  217. package/utils/comparison.d.ts +5 -5
  218. package/utils/comparison.js +5 -3
  219. package/utils/cryptography.d.ts +1 -1
  220. package/utils/encoding.d.ts +1 -1
  221. package/utils/enum.d.ts +1 -1
  222. package/utils/equals.d.ts +1 -1
  223. package/utils/format-error.d.ts +1 -1
  224. package/utils/function/class.d.ts +1 -1
  225. package/utils/function/memoize.d.ts +1 -1
  226. package/utils/helpers.d.ts +1 -1
  227. package/utils/helpers.js +2 -2
  228. package/utils/jwt.d.ts +3 -3
  229. package/utils/merge.d.ts +1 -1
  230. package/utils/middleware.js +3 -3
  231. package/utils/object/decycle.d.ts +1 -1
  232. package/utils/object/forward-ref.d.ts +1 -1
  233. package/utils/object/lazy-property.d.ts +1 -1
  234. package/utils/object/object.d.ts +1 -1
  235. package/utils/object/property-name.d.ts +1 -1
  236. package/utils/patch-worker.d.ts +1 -1
  237. package/utils/reactive-value-to-signal.d.ts +1 -1
  238. package/utils/reflection.d.ts +1 -1
  239. package/utils/repl.d.ts +1 -1
  240. package/utils/singleton.d.ts +1 -1
  241. package/utils/stream/size-limited-stream.d.ts +1 -1
  242. package/utils/type/extends.d.ts +1 -1
  243. package/utils/type-guards.d.ts +1 -1
  244. package/utils/url-builder.d.ts +1 -1
  245. package/utils/z-base32.d.ts +1 -1
  246. package/module/utils.d.ts +0 -4
  247. package/module/utils.js +0 -21
  248. /package/{formats.d.ts → formats/formats.d.ts} +0 -0
  249. /package/{types.js → types/types.js} +0 -0
  250. /package/{web-types.d.ts → types/web-types.d.ts} +0 -0
  251. /package/{web-types.js → types/web-types.js} +0 -0
@@ -17,11 +17,24 @@ import { AuthenticationService } from './authentication.service.js';
17
17
  import { tryGetAuthorizationTokenStringFromRequest } from './helper.js';
18
18
  const cookieBaseOptions = { path: '/', httpOnly: true, secure: true, sameSite: 'strict' };
19
19
  const deleteCookie = { value: '', ...cookieBaseOptions, maxAge: -1 };
20
+ /**
21
+ * API controller for authentication.
22
+ *
23
+ * @template AdditionalTokenPayload Type of additional token payload
24
+ * @template AuthenticationData Type of additional authentication data
25
+ * @template AdditionalInitSecretResetData Type of additional secret reset data
26
+ */
20
27
  let AuthenticationApiController = class AuthenticationApiController {
21
28
  authenticationService;
22
29
  constructor(authenticationService) {
23
30
  this.authenticationService = authenticationService;
24
31
  }
32
+ /**
33
+ * Get a token for a subject and secret.
34
+ * @param parameters The parameters for the request.
35
+ * @returns The token result.
36
+ * @throws {InvalidCredentialsError} If the credentials are invalid.
37
+ */
25
38
  async getToken({ parameters }) {
26
39
  const authenticationResult = await this.authenticationService.authenticate(parameters.subject, parameters.secret);
27
40
  if (!authenticationResult.success) {
@@ -30,22 +43,45 @@ let AuthenticationApiController = class AuthenticationApiController {
30
43
  const result = await this.authenticationService.getToken(authenticationResult.subject, parameters.data);
31
44
  return this.getTokenResponse(result);
32
45
  }
46
+ /**
47
+ * Refresh a token.
48
+ * @param request The request context.
49
+ * @param parameters The parameters for the request.
50
+ * @returns The token result.
51
+ */
33
52
  async refresh({ request, parameters }) {
34
53
  const refreshTokenString = tryGetAuthorizationTokenStringFromRequest(request, 'refreshToken') ?? '';
35
54
  const result = await this.authenticationService.refresh(refreshTokenString, parameters.data);
36
55
  return this.getTokenResponse(result);
37
56
  }
57
+ /**
58
+ * Impersonate a subject.
59
+ * @param request The request context.
60
+ * @param parameters The parameters for the request.
61
+ * @returns The token result.
62
+ */
38
63
  async impersonate({ request, parameters }) {
39
64
  const tokenString = tryGetAuthorizationTokenStringFromRequest(request) ?? '';
40
65
  const refreshTokenString = tryGetAuthorizationTokenStringFromRequest(request, 'refreshToken') ?? '';
41
66
  const impersonatorResult = await this.authenticationService.impersonate(tokenString, refreshTokenString, parameters.subject, parameters.data);
42
67
  return this.getTokenResponse(impersonatorResult);
43
68
  }
69
+ /**
70
+ * Unimpersonate a subject.
71
+ * @param request The request context.
72
+ * @param parameters The parameters for the request.
73
+ * @returns The token result.
74
+ */
44
75
  async unimpersonate({ request, parameters }) {
45
76
  const impersonatorRefreshTokenString = tryGetAuthorizationTokenStringFromRequest(request, 'impersonatorRefreshToken') ?? '';
46
77
  const result = await this.authenticationService.refresh(impersonatorRefreshTokenString, parameters.data, { omitImpersonator: true });
47
78
  return this.getTokenResponse(result);
48
79
  }
80
+ /**
81
+ * End a session.
82
+ * @param request The request context.
83
+ * @returns 'ok' if the session was ended.
84
+ */
49
85
  async endSession({ request }) {
50
86
  let sessionId;
51
87
  try {
@@ -76,17 +112,36 @@ let AuthenticationApiController = class AuthenticationApiController {
76
112
  },
77
113
  });
78
114
  }
115
+ /**
116
+ * Initialize a secret reset.
117
+ * @param parameters The parameters for the request.
118
+ * @returns 'ok' if the secret reset was initialized.
119
+ */
79
120
  async initSecretReset({ parameters }) {
80
121
  await this.authenticationService.initSecretReset(parameters.subject, parameters.data);
81
122
  return 'ok';
82
123
  }
124
+ /**
125
+ * Reset a secret.
126
+ * @param parameters The parameters for the request.
127
+ * @returns 'ok' if the secret was reset.
128
+ */
83
129
  async resetSecret({ parameters }) {
84
130
  await this.authenticationService.resetSecret(parameters.token, parameters.newSecret);
85
131
  return 'ok';
86
132
  }
133
+ /**
134
+ * Check a secret.
135
+ * @param parameters The parameters for the request.
136
+ * @returns The result of the secret check.
137
+ */
87
138
  async checkSecret({ parameters }) {
88
139
  return await this.authenticationService.checkSecret(parameters.secret);
89
140
  }
141
+ /**
142
+ * Get the current server timestamp.
143
+ * @returns The current server timestamp.
144
+ */
90
145
  timestamp() {
91
146
  return currentTimestamp();
92
147
  }
@@ -127,6 +182,16 @@ AuthenticationApiController = __decorate([
127
182
  __metadata("design:paramtypes", [AuthenticationService])
128
183
  ], AuthenticationApiController);
129
184
  export { AuthenticationApiController };
185
+ /**
186
+ * Get an authentication API controller.
187
+ * @param additionalTokenPayloadSchema Schema for additional token payload.
188
+ * @param authenticationDataSchema Schema for additional authentication data.
189
+ * @param additionalInitSecretResetData Schema for additional secret reset data.
190
+ * @returns An authentication API controller.
191
+ * @template AdditionalTokenPayload Type of additional token payload.
192
+ * @template AuthenticationData Type of additional authentication data.
193
+ * @template AdditionalInitSecretResetData Type of additional secret reset data.
194
+ */
130
195
  export function getAuthenticationApiController(// eslint-disable-line @typescript-eslint/explicit-function-return-type
131
196
  additionalTokenPayloadSchema, authenticationDataSchema, additionalInitSecretResetData) {
132
197
  const apiDefinition = getAuthenticationApiDefinition(additionalTokenPayloadSchema, authenticationDataSchema, additionalInitSecretResetData);
@@ -1,38 +1,72 @@
1
1
  import { type AfterResolve, afterResolve } from '../../injector/index.js';
2
- import type { BinaryData, Record } from '../../types.js';
2
+ import type { BinaryData, Record } from '../../types/index.js';
3
3
  import { type RefreshToken, type SecretCheckResult, type SecretResetToken, type Token } from '../models/index.js';
4
4
  import { type SecretTestResult } from './authentication-secret-requirements.validator.js';
5
+ /**
6
+ * Data for creating a token.
7
+ *
8
+ * @param AdditionalTokenPayload Type of additional token payload
9
+ */
5
10
  export type CreateTokenData<AdditionalTokenPayload extends Record> = {
11
+ /** Token version, forces refresh on mismatch (useful if payload changes) */
6
12
  tokenVersion?: number;
13
+ /** Custom token id */
7
14
  jwtId?: string;
15
+ /** Custom issued at timestamp */
8
16
  issuedAt?: number;
17
+ /** Custom expiration timestamp */
9
18
  expiration?: number;
19
+ /** Additional token payload */
10
20
  additionalTokenPayload: AdditionalTokenPayload;
21
+ /** Subject of the token */
11
22
  subject: string;
23
+ /** Session id */
12
24
  sessionId: string;
25
+ /** Impersonator subject */
13
26
  impersonator: string | undefined;
27
+ /** Refresh token expiration timestamp */
14
28
  refreshTokenExpiration: number;
29
+ /** Timestamp for issued at and expiration calculation */
15
30
  timestamp?: number;
16
31
  };
17
32
  export declare class AuthenticationServiceOptions {
18
33
  /**
19
- * Secrets used for signing tokens and refreshTokens
20
- * If single secret is provided, multiple secrets are derived internally
34
+ * Secrets used for signing tokens and refreshTokens.
35
+ * If single secret is provided, multiple secrets are derived internally.
21
36
  */
22
37
  secret: string | BinaryData | {
23
38
  tokenSigningSecret: Uint8Array;
24
39
  refreshTokenSigningSecret: Uint8Array;
25
40
  secretResetTokenSigningSecret: Uint8Array;
26
41
  };
27
- /** Token version, forces refresh on mismatch (useful if payload changes) */
42
+ /**
43
+ * Token version, forces refresh on mismatch (useful if payload changes).
44
+ *
45
+ * @default 1
46
+ */
28
47
  version?: number;
29
- /** How long a token is valid */
48
+ /**
49
+ * How long a token is valid in milliseconds.
50
+ *
51
+ * @default 5 minutes
52
+ */
30
53
  tokenTimeToLive?: number;
31
- /** How long a refresh token is valid. Implies session time to live. */
54
+ /**
55
+ * How long a refresh token is valid in milliseconds. Implies session time to live.
56
+ *
57
+ * @default 5 days
58
+ */
32
59
  refreshTokenTimeToLive?: number;
33
- /** How long a secret reset token is valid. */
60
+ /**
61
+ * How long a secret reset token is valid in milliseconds.
62
+ *
63
+ * @default 10 minutes
64
+ */
34
65
  secretResetTokenTimeToLive?: number;
35
66
  }
67
+ /**
68
+ * Result of an authentication attempt.
69
+ */
36
70
  export type AuthenticationResult = {
37
71
  success: true;
38
72
  subject: string;
@@ -40,6 +74,11 @@ export type AuthenticationResult = {
40
74
  success: false;
41
75
  subject?: undefined;
42
76
  };
77
+ /**
78
+ * Result of a token creation.
79
+ *
80
+ * @param AdditionalTokenPayload Type of additional token payload
81
+ */
43
82
  export type TokenResult<AdditionalTokenPayload extends Record> = {
44
83
  token: string;
45
84
  jsonToken: Token<AdditionalTokenPayload>;
@@ -49,9 +88,17 @@ export type TokenResult<AdditionalTokenPayload extends Record> = {
49
88
  impersonatorRefreshTokenExpiration?: number;
50
89
  };
51
90
  export type SetCredentialsOptions = {
52
- /** skip validation for password strength */
91
+ /**
92
+ * Skip validation for password strength.
93
+ *
94
+ * @default false
95
+ */
53
96
  skipValidation?: boolean;
54
- /** skip session invalidation */
97
+ /**
98
+ * Skip session invalidation.
99
+ *
100
+ * @default false
101
+ */
55
102
  skipSessionInvalidation?: boolean;
56
103
  };
57
104
  type CreateTokenResult<AdditionalTokenPayload extends Record> = {
@@ -64,6 +111,23 @@ type CreateRefreshTokenResult = {
64
111
  salt: Uint8Array;
65
112
  hash: Uint8Array;
66
113
  };
114
+ /**
115
+ * Handles authentication on server side.
116
+ *
117
+ * Can be used to:
118
+ * - Set credentials
119
+ * - Authenticate
120
+ * - Get token
121
+ * - End session
122
+ * - Refresh token
123
+ * - Impersonate/unimpersonate
124
+ * - Reset secret
125
+ * - Check secret
126
+ *
127
+ * @template AdditionalTokenPayload Type of additional token payload
128
+ * @template AuthenticationData Type of additional authentication data
129
+ * @template AdditionalInitSecretResetData Type of additional secret reset data
130
+ */
67
131
  export declare class AuthenticationService<AdditionalTokenPayload extends Record = Record<never>, AuthenticationData = void, AdditionalInitSecretResetData = void> implements AfterResolve {
68
132
  #private;
69
133
  private readonly tokenVersion;
@@ -73,27 +137,132 @@ export declare class AuthenticationService<AdditionalTokenPayload extends Record
73
137
  private derivedTokenSigningSecret;
74
138
  private derivedRefreshTokenSigningSecret;
75
139
  private derivedSecretResetTokenSigningSecret;
140
+ /** @internal */
76
141
  [afterResolve](): Promise<void>;
142
+ /**
143
+ * Initializes the service.
144
+ * Derives signing secrets if necessary.
145
+ *
146
+ * @internal
147
+ */
77
148
  initialize(): Promise<void>;
149
+ /**
150
+ * Sets the credentials for a subject.
151
+ * This method should not be exposed to the public API without a secret reset token check.
152
+ * @param subject The subject to set the credentials for.
153
+ * @param secret The secret to set.
154
+ * @param options Options for setting the credentials.
155
+ */
78
156
  setCredentials(subject: string, secret: string, options?: SetCredentialsOptions): Promise<void>;
157
+ /**
158
+ * Authenticates a subject with a secret.
159
+ * @param subject The subject to authenticate.
160
+ * @param secret The secret to authenticate with.
161
+ * @returns The result of the authentication.
162
+ */
79
163
  authenticate(subject: string, secret: string): Promise<AuthenticationResult>;
164
+ /**
165
+ * Gets a token for a subject.
166
+ * @param subject The subject to get the token for.
167
+ * @param authenticationData Additional authentication data.
168
+ * @param options Options for getting the token.
169
+ * @returns The token result.
170
+ */
80
171
  getToken(subject: string, authenticationData: AuthenticationData, { impersonator }?: {
81
172
  impersonator?: string;
82
173
  }): Promise<TokenResult<AdditionalTokenPayload>>;
174
+ /**
175
+ * Ends a session.
176
+ * @param sessionId The id of the session to end.
177
+ */
83
178
  endSession(sessionId: string): Promise<void>;
179
+ /**
180
+ * Refreshes a token.
181
+ * @param refreshToken The refresh token to use.
182
+ * @param authenticationData Additional authentication data.
183
+ * @param options Options for refreshing the token.
184
+ * @returns The token result.
185
+ * @throws {InvalidTokenError} If the refresh token is invalid.
186
+ */
84
187
  refresh(refreshToken: string, authenticationData: AuthenticationData, { omitImpersonator }?: {
85
188
  omitImpersonator?: boolean;
86
189
  }): Promise<TokenResult<AdditionalTokenPayload>>;
190
+ /**
191
+ * Impersonates a subject.
192
+ * @param impersonatorRoken The token of the impersonator.
193
+ * @param impersonatorRefreshToken The refresh token of the impersonator.
194
+ * @param subject The subject to impersonate.
195
+ * @param authenticationData Additional authentication data.
196
+ * @returns The token result.
197
+ * @throws {ForbiddenError} If impersonation is not allowed.
198
+ */
87
199
  impersonate(impersonatorRoken: string, impersonatorRefreshToken: string, subject: string, authenticationData: AuthenticationData): Promise<TokenResult<AdditionalTokenPayload>>;
200
+ /**
201
+ * Unimpersonates a subject.
202
+ * @param impersonatorRefreshToken The refresh token of the impersonator.
203
+ * @param authenticationData Additional authentication data.
204
+ * @returns The token result.
205
+ */
88
206
  unimpersonate(impersonatorRefreshToken: string, authenticationData: AuthenticationData): Promise<TokenResult<AdditionalTokenPayload>>;
207
+ /**
208
+ * Initializes a secret reset.
209
+ * @param subject The subject to reset the secret for.
210
+ * @param data Additional data for the secret reset.
211
+ * @throws {NotImplementedError} If no ancillary service is registered.
212
+ */
89
213
  initSecretReset(subject: string, data: AdditionalInitSecretResetData): Promise<void>;
214
+ /**
215
+ * Resets a secret.
216
+ * @param tokenString The secret reset token.
217
+ * @param newSecret The new secret.
218
+ * @throws {InvalidTokenError} If the token is invalid.
219
+ */
90
220
  resetSecret(tokenString: string, newSecret: string): Promise<void>;
221
+ /**
222
+ * Checks a secret against the requirements.
223
+ * @param secret The secret to check.
224
+ * @returns The result of the check.
225
+ */
91
226
  checkSecret(secret: string): Promise<SecretCheckResult>;
227
+ /**
228
+ * Tests a secret against the requirements.
229
+ * @param secret The secret to test.
230
+ * @returns The result of the test.
231
+ */
92
232
  testSecret(secret: string): Promise<SecretTestResult>;
233
+ /**
234
+ * Validates a secret against the requirements. Throws an error if the requirements are not met.
235
+ * @param secret The secret to validate.
236
+ * @throws {SecretRequirementsError} If the secret does not meet the requirements.
237
+ */
93
238
  validateSecret(secret: string): Promise<void>;
239
+ /**
240
+ * Validates a token.
241
+ * @param token The token to validate.
242
+ * @returns The validated token.
243
+ * @throws {InvalidTokenError} If the token is invalid.
244
+ */
94
245
  validateToken(token: string): Promise<Token<AdditionalTokenPayload>>;
246
+ /**
247
+ * Validates a refresh token.
248
+ * @param token The refresh token to validate.
249
+ * @returns The validated refresh token.
250
+ * @throws {InvalidTokenError} If the refresh token is invalid.
251
+ */
95
252
  validateRefreshToken(token: string): Promise<RefreshToken>;
253
+ /**
254
+ * Validates a secret reset token.
255
+ * @param token The secret reset token to validate.
256
+ * @returns The validated secret reset token.
257
+ * @throws {InvalidTokenError} If the secret reset token is invalid.
258
+ */
96
259
  validateSecretResetToken(token: string): Promise<SecretResetToken>;
260
+ /**
261
+ * Tries to resolve a subject.
262
+ * This method is safe to use in public facing APIs as it does not leak information about the existence of a subject.
263
+ * @param subject The subject to resolve.
264
+ * @returns The resolved subject or undefined if the subject could not be resolved.
265
+ */
97
266
  tryResolveSubject(subject: string): Promise<string | undefined>;
98
267
  /**
99
268
  * Resolves the subject to the actual subject used for authentication.
@@ -103,9 +272,20 @@ export declare class AuthenticationService<AdditionalTokenPayload extends Record
103
272
  * @returns The resolved subject or the original subject if not found.
104
273
  */
105
274
  resolveSubject(subject: string): Promise<string>;
106
- /** Creates a token without session or refresh token and is not saved in database */
275
+ /**
276
+ * Creates a token without session or refresh token and is not saved in database.
277
+ * @param data Data for creating the token.
278
+ * @returns The created token.
279
+ */
107
280
  createToken({ tokenVersion, jwtId, issuedAt, expiration, additionalTokenPayload, subject, sessionId, refreshTokenExpiration, impersonator: impersonatedBy, timestamp }: CreateTokenData<AdditionalTokenPayload>): Promise<CreateTokenResult<AdditionalTokenPayload>>;
108
- /** Creates a refresh token without session or something else. */
281
+ /**
282
+ * Creates a refresh token without session and is not saved in database.
283
+ * @param subject The subject of the refresh token.
284
+ * @param sessionId The session id of the refresh token.
285
+ * @param expirationTimestamp The expiration timestamp of the refresh token.
286
+ * @param options Options for creating the refresh token.
287
+ * @returns The created refresh token.
288
+ */
109
289
  createRefreshToken(subject: string, sessionId: string, expirationTimestamp: number, options?: {
110
290
  impersonator?: string;
111
291
  }): Promise<CreateRefreshTokenResult>;
@@ -29,20 +29,53 @@ import { getRefreshTokenFromString, getSecretResetTokenFromString, getTokenFromS
29
29
  import { AuthenticationModuleConfig } from './module.js';
30
30
  export class AuthenticationServiceOptions {
31
31
  /**
32
- * Secrets used for signing tokens and refreshTokens
33
- * If single secret is provided, multiple secrets are derived internally
32
+ * Secrets used for signing tokens and refreshTokens.
33
+ * If single secret is provided, multiple secrets are derived internally.
34
34
  */
35
35
  secret;
36
- /** Token version, forces refresh on mismatch (useful if payload changes) */
36
+ /**
37
+ * Token version, forces refresh on mismatch (useful if payload changes).
38
+ *
39
+ * @default 1
40
+ */
37
41
  version;
38
- /** How long a token is valid */
42
+ /**
43
+ * How long a token is valid in milliseconds.
44
+ *
45
+ * @default 5 minutes
46
+ */
39
47
  tokenTimeToLive;
40
- /** How long a refresh token is valid. Implies session time to live. */
48
+ /**
49
+ * How long a refresh token is valid in milliseconds. Implies session time to live.
50
+ *
51
+ * @default 5 days
52
+ */
41
53
  refreshTokenTimeToLive;
42
- /** How long a secret reset token is valid. */
54
+ /**
55
+ * How long a secret reset token is valid in milliseconds.
56
+ *
57
+ * @default 10 minutes
58
+ */
43
59
  secretResetTokenTimeToLive;
44
60
  }
45
61
  const SIGNING_SECRETS_LENGTH = 64;
62
+ /**
63
+ * Handles authentication on server side.
64
+ *
65
+ * Can be used to:
66
+ * - Set credentials
67
+ * - Authenticate
68
+ * - Get token
69
+ * - End session
70
+ * - Refresh token
71
+ * - Impersonate/unimpersonate
72
+ * - Reset secret
73
+ * - Check secret
74
+ *
75
+ * @template AdditionalTokenPayload Type of additional token payload
76
+ * @template AuthenticationData Type of additional authentication data
77
+ * @template AdditionalInitSecretResetData Type of additional secret reset data
78
+ */
46
79
  let AuthenticationService = class AuthenticationService {
47
80
  #credentialsRepository = injectRepository(AuthenticationCredentials);
48
81
  #sessionRepository = injectRepository(AuthenticationSession);
@@ -58,9 +91,16 @@ let AuthenticationService = class AuthenticationService {
58
91
  derivedTokenSigningSecret;
59
92
  derivedRefreshTokenSigningSecret;
60
93
  derivedSecretResetTokenSigningSecret;
94
+ /** @internal */
61
95
  async [afterResolve]() {
62
96
  await this.initialize();
63
97
  }
98
+ /**
99
+ * Initializes the service.
100
+ * Derives signing secrets if necessary.
101
+ *
102
+ * @internal
103
+ */
64
104
  async initialize() {
65
105
  if (isString(this.#options.secret) || isBinaryData(this.#options.secret)) {
66
106
  await this.deriveSigningSecrets(this.#options.secret);
@@ -71,6 +111,13 @@ let AuthenticationService = class AuthenticationService {
71
111
  this.derivedSecretResetTokenSigningSecret = this.#options.secret.secretResetTokenSigningSecret;
72
112
  }
73
113
  }
114
+ /**
115
+ * Sets the credentials for a subject.
116
+ * This method should not be exposed to the public API without a secret reset token check.
117
+ * @param subject The subject to set the credentials for.
118
+ * @param secret The secret to set.
119
+ * @param options Options for setting the credentials.
120
+ */
74
121
  async setCredentials(subject, secret, options) {
75
122
  // We do not need to avoid information leakage here, as this is a non-public method that is only called by a public api if the secret reset token is valid.
76
123
  const actualSubject = await this.resolveSubject(subject);
@@ -91,6 +138,12 @@ let AuthenticationService = class AuthenticationService {
91
138
  }
92
139
  });
93
140
  }
141
+ /**
142
+ * Authenticates a subject with a secret.
143
+ * @param subject The subject to authenticate.
144
+ * @param secret The secret to authenticate with.
145
+ * @returns The result of the authentication.
146
+ */
94
147
  async authenticate(subject, secret) {
95
148
  const actualSubject = await this.tryResolveSubject(subject) ?? subject;
96
149
  // Always try to load credentials, even if the subject is not resolved, to avoid information leakage.
@@ -105,6 +158,13 @@ let AuthenticationService = class AuthenticationService {
105
158
  }
106
159
  return { success: false };
107
160
  }
161
+ /**
162
+ * Gets a token for a subject.
163
+ * @param subject The subject to get the token for.
164
+ * @param authenticationData Additional authentication data.
165
+ * @param options Options for getting the token.
166
+ * @returns The token result.
167
+ */
108
168
  async getToken(subject, authenticationData, { impersonator } = {}) {
109
169
  const actualSubject = await this.resolveSubject(subject);
110
170
  const now = currentTimestamp();
@@ -130,10 +190,22 @@ let AuthenticationService = class AuthenticationService {
130
190
  return { token, jsonToken, refreshToken: refreshToken.token };
131
191
  });
132
192
  }
193
+ /**
194
+ * Ends a session.
195
+ * @param sessionId The id of the session to end.
196
+ */
133
197
  async endSession(sessionId) {
134
198
  const now = currentTimestamp();
135
199
  await this.#sessionRepository.update(sessionId, { end: now });
136
200
  }
201
+ /**
202
+ * Refreshes a token.
203
+ * @param refreshToken The refresh token to use.
204
+ * @param authenticationData Additional authentication data.
205
+ * @param options Options for refreshing the token.
206
+ * @returns The token result.
207
+ * @throws {InvalidTokenError} If the refresh token is invalid.
208
+ */
137
209
  async refresh(refreshToken, authenticationData, { omitImpersonator = false } = {}) {
138
210
  const validatedRefreshToken = await this.validateRefreshToken(refreshToken);
139
211
  const sessionId = validatedRefreshToken.payload.sessionId;
@@ -159,6 +231,15 @@ let AuthenticationService = class AuthenticationService {
159
231
  });
160
232
  return { token, jsonToken, refreshToken: newRefreshToken.token, omitImpersonatorRefreshToken: omitImpersonator };
161
233
  }
234
+ /**
235
+ * Impersonates a subject.
236
+ * @param impersonatorRoken The token of the impersonator.
237
+ * @param impersonatorRefreshToken The refresh token of the impersonator.
238
+ * @param subject The subject to impersonate.
239
+ * @param authenticationData Additional authentication data.
240
+ * @returns The token result.
241
+ * @throws {ForbiddenError} If impersonation is not allowed.
242
+ */
162
243
  async impersonate(impersonatorRoken, impersonatorRefreshToken, subject, authenticationData) {
163
244
  const validatedImpersonatorRoken = await this.validateToken(impersonatorRoken);
164
245
  const validatedImpersonatorRefreshToken = await this.validateRefreshToken(impersonatorRefreshToken);
@@ -173,9 +254,21 @@ let AuthenticationService = class AuthenticationService {
173
254
  impersonatorRefreshTokenExpiration: validatedImpersonatorRefreshToken.payload.exp,
174
255
  };
175
256
  }
257
+ /**
258
+ * Unimpersonates a subject.
259
+ * @param impersonatorRefreshToken The refresh token of the impersonator.
260
+ * @param authenticationData Additional authentication data.
261
+ * @returns The token result.
262
+ */
176
263
  async unimpersonate(impersonatorRefreshToken, authenticationData) {
177
264
  return await this.refresh(impersonatorRefreshToken, authenticationData, { omitImpersonator: true });
178
265
  }
266
+ /**
267
+ * Initializes a secret reset.
268
+ * @param subject The subject to reset the secret for.
269
+ * @param data Additional data for the secret reset.
270
+ * @throws {NotImplementedError} If no ancillary service is registered.
271
+ */
179
272
  async initSecretReset(subject, data) {
180
273
  if (isUndefined(this.#authenticationAncillaryService)) {
181
274
  throw new NotImplementedError();
@@ -198,28 +291,73 @@ let AuthenticationService = class AuthenticationService {
198
291
  };
199
292
  await this.#authenticationAncillaryService.handleInitSecretReset(initSecretResetData);
200
293
  }
294
+ /**
295
+ * Resets a secret.
296
+ * @param tokenString The secret reset token.
297
+ * @param newSecret The new secret.
298
+ * @throws {InvalidTokenError} If the token is invalid.
299
+ */
201
300
  async resetSecret(tokenString, newSecret) {
202
301
  const token = await this.validateSecretResetToken(tokenString);
203
302
  await this.setCredentials(token.payload.subject, newSecret);
204
303
  }
304
+ /**
305
+ * Checks a secret against the requirements.
306
+ * @param secret The secret to check.
307
+ * @returns The result of the check.
308
+ */
205
309
  async checkSecret(secret) {
206
310
  return await this.#authenticationSecretRequirementsValidator.checkSecretRequirements(secret);
207
311
  }
312
+ /**
313
+ * Tests a secret against the requirements.
314
+ * @param secret The secret to test.
315
+ * @returns The result of the test.
316
+ */
208
317
  async testSecret(secret) {
209
318
  return await this.#authenticationSecretRequirementsValidator.testSecretRequirements(secret);
210
319
  }
320
+ /**
321
+ * Validates a secret against the requirements. Throws an error if the requirements are not met.
322
+ * @param secret The secret to validate.
323
+ * @throws {SecretRequirementsError} If the secret does not meet the requirements.
324
+ */
211
325
  async validateSecret(secret) {
212
326
  await this.#authenticationSecretRequirementsValidator.validateSecretRequirements(secret);
213
327
  }
328
+ /**
329
+ * Validates a token.
330
+ * @param token The token to validate.
331
+ * @returns The validated token.
332
+ * @throws {InvalidTokenError} If the token is invalid.
333
+ */
214
334
  async validateToken(token) {
215
335
  return await getTokenFromString(token, this.tokenVersion, this.derivedTokenSigningSecret);
216
336
  }
337
+ /**
338
+ * Validates a refresh token.
339
+ * @param token The refresh token to validate.
340
+ * @returns The validated refresh token.
341
+ * @throws {InvalidTokenError} If the refresh token is invalid.
342
+ */
217
343
  async validateRefreshToken(token) {
218
344
  return await getRefreshTokenFromString(token, this.derivedRefreshTokenSigningSecret);
219
345
  }
346
+ /**
347
+ * Validates a secret reset token.
348
+ * @param token The secret reset token to validate.
349
+ * @returns The validated secret reset token.
350
+ * @throws {InvalidTokenError} If the secret reset token is invalid.
351
+ */
220
352
  async validateSecretResetToken(token) {
221
353
  return await getSecretResetTokenFromString(token, this.derivedSecretResetTokenSigningSecret);
222
354
  }
355
+ /**
356
+ * Tries to resolve a subject.
357
+ * This method is safe to use in public facing APIs as it does not leak information about the existence of a subject.
358
+ * @param subject The subject to resolve.
359
+ * @returns The resolved subject or undefined if the subject could not be resolved.
360
+ */
223
361
  async tryResolveSubject(subject) {
224
362
  if (isUndefined(this.#authenticationAncillaryService)) {
225
363
  return subject;
@@ -247,7 +385,11 @@ let AuthenticationService = class AuthenticationService {
247
385
  }
248
386
  throw new NotFoundError(`Subject not found.`);
249
387
  }
250
- /** Creates a token without session or refresh token and is not saved in database */
388
+ /**
389
+ * Creates a token without session or refresh token and is not saved in database.
390
+ * @param data Data for creating the token.
391
+ * @returns The created token.
392
+ */
251
393
  async createToken({ tokenVersion, jwtId, issuedAt, expiration, additionalTokenPayload, subject, sessionId, refreshTokenExpiration, impersonator: impersonatedBy, timestamp = currentTimestamp() }) {
252
394
  const header = {
253
395
  v: tokenVersion ?? this.tokenVersion,
@@ -271,7 +413,14 @@ let AuthenticationService = class AuthenticationService {
271
413
  const token = await createJwtTokenString(jsonToken, this.derivedTokenSigningSecret);
272
414
  return { token, jsonToken };
273
415
  }
274
- /** Creates a refresh token without session or something else. */
416
+ /**
417
+ * Creates a refresh token without session and is not saved in database.
418
+ * @param subject The subject of the refresh token.
419
+ * @param sessionId The session id of the refresh token.
420
+ * @param expirationTimestamp The expiration timestamp of the refresh token.
421
+ * @param options Options for creating the refresh token.
422
+ * @returns The created refresh token.
423
+ */
275
424
  async createRefreshToken(subject, sessionId, expirationTimestamp, options) {
276
425
  const secret = getRandomString(64, Alphabet.LowerUpperCaseNumbers);
277
426
  const salt = getRandomBytes(32);