@udibo/oauth2 0.4.0 → 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 (161) hide show
  1. package/README.md +3 -3
  2. package/SECURITY.md +0 -4
  3. package/esm/adapters/hono/_common.d.ts +10 -13
  4. package/esm/adapters/hono/_common.js +11 -20
  5. package/esm/adapters/hono/authorization-server.d.ts +16 -10
  6. package/esm/adapters/hono/authorization-server.js +13 -8
  7. package/esm/adapters/hono/bff/auth-request-store.d.ts +4 -0
  8. package/esm/adapters/hono/bff/auth-request-store.js +4 -0
  9. package/esm/adapters/hono/bff/bff.d.ts +29 -18
  10. package/esm/adapters/hono/bff/bff.js +15 -11
  11. package/esm/adapters/hono/bff/proxy.js +7 -55
  12. package/esm/adapters/hono/bff/session-store.d.ts +9 -9
  13. package/esm/adapters/hono/bff/session-store.js +3 -2
  14. package/esm/adapters/hono/bff/testing.d.ts +14 -3
  15. package/esm/adapters/hono/bff/testing.js +14 -3
  16. package/esm/adapters/hono/identity.d.ts +5 -3
  17. package/esm/adapters/hono/identity.js +1 -17
  18. package/esm/adapters/hono/resource-server.d.ts +3 -2
  19. package/esm/adapters/hono/resource-server.js +3 -2
  20. package/esm/client/_http.d.ts +4 -7
  21. package/esm/client/_http.js +4 -37
  22. package/esm/client/base.d.ts +3 -6
  23. package/esm/client/bff-client.d.ts +7 -6
  24. package/esm/client/bff-client.js +7 -6
  25. package/esm/client/browser-storage.d.ts +33 -34
  26. package/esm/client/browser-storage.js +47 -32
  27. package/esm/client/check.d.ts +10 -3
  28. package/esm/client/check.js +28 -8
  29. package/esm/client/direct-client.d.ts +49 -29
  30. package/esm/client/direct-client.js +59 -59
  31. package/esm/client/discovery-cache.d.ts +12 -5
  32. package/esm/client/discovery-cache.js +11 -4
  33. package/esm/client/events.d.ts +11 -3
  34. package/esm/client/events.js +1 -1
  35. package/esm/client/mod.d.ts +6 -4
  36. package/esm/client/mod.js +5 -3
  37. package/esm/client/storage.d.ts +53 -23
  38. package/esm/client/storage.js +43 -15
  39. package/esm/errors.d.ts +15 -12
  40. package/esm/errors.js +12 -8
  41. package/esm/identity/delivery.d.ts +7 -7
  42. package/esm/identity/delivery.js +1 -2
  43. package/esm/identity/errors.d.ts +10 -4
  44. package/esm/identity/errors.js +4 -1
  45. package/esm/identity/events.d.ts +21 -5
  46. package/esm/identity/events.js +4 -2
  47. package/esm/identity/external/_shared.d.ts +5 -7
  48. package/esm/identity/external/_shared.js +5 -7
  49. package/esm/identity/external/_token-exchange.d.ts +3 -6
  50. package/esm/identity/external/_token-exchange.js +2 -5
  51. package/esm/identity/external/apple-client-secret.d.ts +13 -8
  52. package/esm/identity/external/apple-client-secret.js +9 -6
  53. package/esm/identity/external/apple.d.ts +17 -10
  54. package/esm/identity/external/apple.js +12 -9
  55. package/esm/identity/external/discord.d.ts +5 -1
  56. package/esm/identity/external/errors.d.ts +6 -2
  57. package/esm/identity/external/errors.js +6 -2
  58. package/esm/identity/external/flow.d.ts +9 -6
  59. package/esm/identity/external/flow.js +6 -5
  60. package/esm/identity/external/github.d.ts +7 -3
  61. package/esm/identity/external/github.js +2 -2
  62. package/esm/identity/external/google.d.ts +5 -1
  63. package/esm/identity/external/oauth2.d.ts +13 -2
  64. package/esm/identity/external/oauth2.js +3 -3
  65. package/esm/identity/external/oidc.d.ts +21 -10
  66. package/esm/identity/external/oidc.js +11 -6
  67. package/esm/identity/external/provider.d.ts +4 -2
  68. package/esm/identity/identifier.d.ts +6 -5
  69. package/esm/identity/identifier.js +6 -5
  70. package/esm/identity/lockout.d.ts +4 -3
  71. package/esm/identity/lockout.js +4 -3
  72. package/esm/identity/mfa/mod.d.ts +2 -1
  73. package/esm/identity/mfa/mod.js +2 -1
  74. package/esm/identity/mfa/recovery-codes.d.ts +2 -1
  75. package/esm/identity/mfa/service.d.ts +29 -11
  76. package/esm/identity/mfa/service.js +35 -11
  77. package/esm/identity/mfa/totp.d.ts +4 -2
  78. package/esm/identity/mfa/totp.js +4 -2
  79. package/esm/identity/migration.d.ts +8 -7
  80. package/esm/identity/migration.js +9 -12
  81. package/esm/identity/mod.d.ts +2 -2
  82. package/esm/identity/mod.js +2 -2
  83. package/esm/identity/otp.d.ts +23 -16
  84. package/esm/identity/otp.js +13 -12
  85. package/esm/identity/password-policy.d.ts +3 -3
  86. package/esm/identity/password-policy.js +3 -3
  87. package/esm/identity/password.d.ts +23 -20
  88. package/esm/identity/password.js +15 -16
  89. package/esm/identity/rate-limit.d.ts +1 -7
  90. package/esm/identity/rate-limit.js +1 -2
  91. package/esm/identity/service.d.ts +76 -44
  92. package/esm/identity/service.js +86 -60
  93. package/esm/identity/session.d.ts +5 -4
  94. package/esm/identity/session.js +5 -4
  95. package/esm/identity/token-flow.d.ts +8 -8
  96. package/esm/identity/token-flow.js +5 -6
  97. package/esm/models/client.d.ts +7 -4
  98. package/esm/models/token.d.ts +2 -3
  99. package/esm/react/callback.d.ts +3 -3
  100. package/esm/react/callback.js +3 -3
  101. package/esm/react/components/class-names.d.ts +3 -6
  102. package/esm/react/components/class-names.js +3 -6
  103. package/esm/react/components/internal.d.ts +7 -17
  104. package/esm/react/components/internal.js +7 -17
  105. package/esm/react/components/mod.d.ts +7 -5
  106. package/esm/react/components/mod.js +7 -5
  107. package/esm/react/components/types.d.ts +8 -6
  108. package/esm/react/context.d.ts +3 -3
  109. package/esm/react/provider.d.ts +3 -2
  110. package/esm/react/provider.js +0 -13
  111. package/esm/react/require-auth.d.ts +0 -3
  112. package/esm/react/require-auth.js +0 -3
  113. package/esm/react/testing.d.ts +3 -2
  114. package/esm/react/testing.js +3 -3
  115. package/esm/react/use-oauth2.d.ts +1 -1
  116. package/esm/server/authorization-server.d.ts +81 -54
  117. package/esm/server/authorization-server.js +76 -100
  118. package/esm/server/grants/authorization-code.d.ts +7 -3
  119. package/esm/server/grants/authorization-code.js +6 -2
  120. package/esm/server/grants/device-authorization.d.ts +1 -1
  121. package/esm/server/grants/grant.d.ts +15 -8
  122. package/esm/server/grants/grant.js +26 -18
  123. package/esm/server/grants/password.d.ts +1 -1
  124. package/esm/server/grants/refresh-token.d.ts +6 -3
  125. package/esm/server/grants/refresh-token.js +4 -3
  126. package/esm/server/introspection-token-reader.d.ts +16 -7
  127. package/esm/server/introspection-token-reader.js +9 -3
  128. package/esm/server/jwks-token-reader.d.ts +3 -3
  129. package/esm/server/jwks-token-reader.js +3 -3
  130. package/esm/server/public-suffix/rules.d.ts +4 -4
  131. package/esm/server/public-suffix/rules.js +4 -4
  132. package/esm/server/redirect-uri.d.ts +6 -9
  133. package/esm/server/redirect-uri.js +7 -34
  134. package/esm/server/resource-server.d.ts +5 -3
  135. package/esm/server/resource-server.js +5 -3
  136. package/esm/server/services/client.d.ts +5 -1
  137. package/esm/server/services/device-authorization.d.ts +6 -6
  138. package/esm/server/services/token.d.ts +15 -8
  139. package/esm/server/services/token.js +1 -1
  140. package/esm/server/signing-keys.d.ts +24 -9
  141. package/esm/server/signing-keys.js +20 -9
  142. package/esm/testing/contract/mod.d.ts +3 -3
  143. package/esm/testing/contract/mod.js +3 -3
  144. package/esm/testing/mod.d.ts +4 -2
  145. package/esm/testing/mod.js +4 -2
  146. package/esm/testing/services.d.ts +1 -1
  147. package/esm/testing/services.js +1 -1
  148. package/esm/testing/tenant.d.ts +13 -4
  149. package/esm/utils/_buffer.d.ts +3 -2
  150. package/esm/utils/_buffer.js +3 -2
  151. package/esm/utils/_timing-safe.d.ts +8 -6
  152. package/esm/utils/_timing-safe.js +8 -6
  153. package/esm/utils/basic-auth.d.ts +5 -12
  154. package/esm/utils/basic-auth.js +5 -12
  155. package/esm/utils/crypto.d.ts +2 -1
  156. package/esm/utils/crypto.js +2 -1
  157. package/esm/utils/text.d.ts +2 -9
  158. package/esm/utils/text.js +2 -9
  159. package/esm/utils/url.d.ts +2 -2
  160. package/esm/utils/url.js +0 -2
  161. package/package.json +1 -1
@@ -40,8 +40,9 @@ export class RefreshTokenGrant extends AbstractGrant {
40
40
  * authorization.
41
41
  *
42
42
  * @throws {InvalidRequestError} If `refresh_token` is missing.
43
- * @throws {InvalidGrantError} If the refresh token is unknown or expired, or
44
- * its family has outlived the absolute cap.
43
+ * @throws {InvalidGrantError} If the refresh token is unknown or expired, its
44
+ * family has outlived the absolute cap, or a concurrent exchange already
45
+ * claimed it.
45
46
  * @throws {InvalidClientError} If the token was issued to another client.
46
47
  * @throws {InvalidScopeError} If the requested scope exceeds the original grant.
47
48
  */
@@ -98,7 +99,7 @@ export class RefreshTokenGrant extends AbstractGrant {
98
99
  if (familyExpiresAt && familyExpiresAt.getTime() <= Date.now()) {
99
100
  throw new InvalidGrantError("refresh_token family expired");
100
101
  }
101
- const nextToken = await this.generateToken(client, user, nextScope, tokenService, currentToken.authenticationContext);
102
+ const nextToken = await this.generateToken(client, user, nextScope, tokenService, currentToken.authenticationContext, familyExpiresAt);
102
103
  const nextRefreshToken = "refreshToken" in nextToken
103
104
  ? nextToken
104
105
  : {
@@ -46,10 +46,13 @@ export interface IntrospectionTokenReaderOptions<Client extends ClientInterface,
46
46
  * `undefined` when the token doesn't represent a user. Omit entirely if
47
47
  * your app never reads user info off the token.
48
48
  *
49
- * A machine token (client credentials) carries no `sub`, so `data.sub` is
50
- * present only when there is a resource owner — `data.sub ? {…} : undefined`
51
- * is the whole discriminator, and a client-credentials caller resolves to no
52
- * user.
49
+ * This package's authorization server sets `sub` from its `subjectOf`
50
+ * option (the user's `id` by default — the same value its id_token and
51
+ * UserInfo carry) and omits it for a token with no user, whatever its
52
+ * `introspectionClaims` hook returns, so `data.sub` is present only when
53
+ * there is a resource owner and `data.sub ? {…} : undefined` is the whole
54
+ * discriminator. Another authorization server may set `sub` to the client
55
+ * id for a machine token; check how yours answers.
53
56
  *
54
57
  * May be async — return a promise to enrich from a DB lookup or the
55
58
  * authorization server's OIDC UserInfo endpoint, using `data.sub` as
@@ -81,6 +84,11 @@ export interface IntrospectionTokenReaderOptions<Client extends ClientInterface,
81
84
  * types via the {@linkcode IntrospectionTokenReaderOptions.getClient} and
82
85
  * {@linkcode IntrospectionTokenReaderOptions.getUser} mappers.
83
86
  *
87
+ * The reader does not check `aud` or `iss`: any token the endpoint reports
88
+ * active with a bearer `token_type` is accepted. When one authorization server
89
+ * issues tokens for several APIs, check the audience yourself (it is on
90
+ * {@linkcode Token.claims}) or have the endpoint scope its answers.
91
+ *
84
92
  * @example
85
93
  * ```ts
86
94
  * import {
@@ -137,9 +145,10 @@ export declare class IntrospectionTokenReader<Client extends ClientInterface, Us
137
145
  * returns a 5xx — a down authorization server must be distinguishable from
138
146
  * a genuinely invalid token (which is `undefined` → `invalid_token`), not
139
147
  * collapsed into it.
140
- * @throws {ServerError} If the endpoint returns a 4xx (e.g. the reader's own
141
- * introspection credentials are rejected) — a misconfiguration, not a
142
- * verdict on the caller's token.
148
+ * @throws {ServerError} If the endpoint returns any other non-2xx status
149
+ * (e.g. the reader's own introspection credentials are rejected) or a body
150
+ * that is not JSON — a misconfiguration, not a verdict on the caller's
151
+ * token.
143
152
  *
144
153
  * An active response is only accepted when its `token_type` names a bearer
145
154
  * token. RFC 7662 answers for refresh tokens too, and a refresh token's
@@ -22,6 +22,11 @@ import { encodeBasicAuth } from "../utils/basic-auth.js";
22
22
  * types via the {@linkcode IntrospectionTokenReaderOptions.getClient} and
23
23
  * {@linkcode IntrospectionTokenReaderOptions.getUser} mappers.
24
24
  *
25
+ * The reader does not check `aud` or `iss`: any token the endpoint reports
26
+ * active with a bearer `token_type` is accepted. When one authorization server
27
+ * issues tokens for several APIs, check the audience yourself (it is on
28
+ * {@linkcode Token.claims}) or have the endpoint scope its answers.
29
+ *
25
30
  * @example
26
31
  * ```ts
27
32
  * import {
@@ -87,9 +92,10 @@ export class IntrospectionTokenReader {
87
92
  * returns a 5xx — a down authorization server must be distinguishable from
88
93
  * a genuinely invalid token (which is `undefined` → `invalid_token`), not
89
94
  * collapsed into it.
90
- * @throws {ServerError} If the endpoint returns a 4xx (e.g. the reader's own
91
- * introspection credentials are rejected) — a misconfiguration, not a
92
- * verdict on the caller's token.
95
+ * @throws {ServerError} If the endpoint returns any other non-2xx status
96
+ * (e.g. the reader's own introspection credentials are rejected) or a body
97
+ * that is not JSON — a misconfiguration, not a verdict on the caller's
98
+ * token.
93
99
  *
94
100
  * An active response is only accepted when its `token_type` names a bearer
95
101
  * token. RFC 7662 answers for refresh tokens too, and a refresh token's
@@ -255,9 +255,9 @@ export declare class JwksTokenReader<Client extends ClientInterface, User, S ext
255
255
  * available — a down issuer must be distinguishable from a genuinely
256
256
  * invalid token, not collapsed into it. Once keys are cached, a failed
257
257
  * refresh is absorbed and the cached keys keep serving.
258
- * @throws {ServerError} If the JWKS endpoint returns a 4xx or a body that
259
- * is not a JWKS, or discovery reports no `jwks_uri` — a
260
- * misconfiguration, not a verdict on the caller's token.
258
+ * @throws {ServerError} If, with no keys cached, the JWKS endpoint returns
259
+ * a 4xx or a body that is not a JWKS, or discovery reports no `jwks_uri`
260
+ * — a misconfiguration, not a verdict on the caller's token.
261
261
  */
262
262
  getToken(accessToken: string): Promise<Token<Client, User, S> | undefined>;
263
263
  }
@@ -276,9 +276,9 @@ export class JwksTokenReader {
276
276
  * available — a down issuer must be distinguishable from a genuinely
277
277
  * invalid token, not collapsed into it. Once keys are cached, a failed
278
278
  * refresh is absorbed and the cached keys keep serving.
279
- * @throws {ServerError} If the JWKS endpoint returns a 4xx or a body that
280
- * is not a JWKS, or discovery reports no `jwks_uri` — a
281
- * misconfiguration, not a verdict on the caller's token.
279
+ * @throws {ServerError} If, with no keys cached, the JWKS endpoint returns
280
+ * a 4xx or a body that is not a JWKS, or discovery reports no `jwks_uri`
281
+ * — a misconfiguration, not a verdict on the caller's token.
282
282
  */
283
283
  async getToken(accessToken) {
284
284
  const parsed = parseJws(accessToken);