@whook/oauth2 25.2.1 → 26.0.2

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/README.md +147 -98
  2. package/dist/index.d.ts +34 -11
  3. package/dist/index.js +18 -8
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.test.js +1671 -393
  6. package/dist/index.test.js.map +1 -1
  7. package/dist/libs/authorizationRequests.d.ts +6 -0
  8. package/dist/libs/authorizationRequests.js +22 -0
  9. package/dist/libs/authorizationRequests.js.map +1 -0
  10. package/dist/libs/authorizationRequests.test.js +24 -0
  11. package/dist/libs/authorizationRequests.test.js.map +1 -0
  12. package/dist/libs/clients.d.ts +2 -0
  13. package/dist/libs/clients.js +13 -0
  14. package/dist/libs/clients.js.map +1 -0
  15. package/dist/libs/errors.d.ts +143 -0
  16. package/dist/libs/errors.js +227 -0
  17. package/dist/libs/errors.js.map +1 -0
  18. package/dist/libs/grants.d.ts +2 -0
  19. package/dist/libs/grants.js +10 -0
  20. package/dist/libs/grants.js.map +1 -0
  21. package/dist/libs/redirectURI.d.ts +8 -0
  22. package/dist/libs/redirectURI.js +94 -0
  23. package/dist/libs/redirectURI.js.map +1 -0
  24. package/dist/libs/schemas.d.ts +31 -0
  25. package/dist/libs/schemas.js +33 -0
  26. package/dist/libs/schemas.js.map +1 -0
  27. package/dist/libs/scopes.d.ts +16 -0
  28. package/dist/libs/scopes.js +37 -0
  29. package/dist/libs/scopes.js.map +1 -1
  30. package/dist/libs/utils.d.ts +2 -0
  31. package/dist/libs/utils.js +17 -0
  32. package/dist/libs/utils.js.map +1 -0
  33. package/dist/libs/verifier.d.ts +10 -0
  34. package/dist/libs/verifier.js +51 -0
  35. package/dist/libs/verifier.js.map +1 -0
  36. package/dist/libs/verifier.test.d.ts +1 -0
  37. package/dist/libs/verifier.test.js +21 -0
  38. package/dist/libs/verifier.test.js.map +1 -0
  39. package/dist/routes/getOAuth2Authorize.d.ts +38 -59
  40. package/dist/routes/getOAuth2Authorize.js +129 -93
  41. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  42. package/dist/routes/getOAuth2Authorize.test.js +959 -73
  43. package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
  44. package/dist/routes/getOAuth2WellKnownMetadata.d.ts +18 -11
  45. package/dist/routes/getOAuth2WellKnownMetadata.js +43 -11
  46. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  47. package/dist/routes/getOAuth2WellKnownMetadata.test.js +51 -49
  48. package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
  49. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +3 -6
  50. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +3 -6
  51. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
  52. package/dist/routes/postAuthLogin.d.ts +14 -9
  53. package/dist/routes/postAuthLogin.js +10 -5
  54. package/dist/routes/postAuthLogin.js.map +1 -1
  55. package/dist/routes/postAuthLogin.test.js +79 -55
  56. package/dist/routes/postAuthLogin.test.js.map +1 -1
  57. package/dist/routes/postAuthLogout.d.ts +5 -2
  58. package/dist/routes/postAuthLogout.js +12 -7
  59. package/dist/routes/postAuthLogout.js.map +1 -1
  60. package/dist/routes/postAuthLogout.test.js +35 -17
  61. package/dist/routes/postAuthLogout.test.js.map +1 -1
  62. package/dist/routes/postAuthRefresh.d.ts +14 -9
  63. package/dist/routes/postAuthRefresh.js +10 -6
  64. package/dist/routes/postAuthRefresh.js.map +1 -1
  65. package/dist/routes/postAuthRefresh.test.js +90 -59
  66. package/dist/routes/postAuthRefresh.test.js.map +1 -1
  67. package/dist/routes/postOAuth2Acknowledge.d.ts +11 -10
  68. package/dist/routes/postOAuth2Acknowledge.js +43 -37
  69. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  70. package/dist/routes/postOAuth2Acknowledge.test.js +135 -70
  71. package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
  72. package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +137 -0
  73. package/dist/routes/postOAuth2PushedAuthorizationRequest.js +187 -0
  74. package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -0
  75. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.d.ts +1 -0
  76. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +554 -0
  77. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -0
  78. package/dist/routes/postOAuth2Revoke.d.ts +99 -0
  79. package/dist/routes/postOAuth2Revoke.js +102 -0
  80. package/dist/routes/postOAuth2Revoke.js.map +1 -0
  81. package/dist/routes/postOAuth2Revoke.test.d.ts +1 -0
  82. package/dist/routes/postOAuth2Revoke.test.js +161 -0
  83. package/dist/routes/postOAuth2Revoke.test.js.map +1 -0
  84. package/dist/routes/postOAuth2Token.d.ts +47 -108
  85. package/dist/routes/postOAuth2Token.js +65 -114
  86. package/dist/routes/postOAuth2Token.js.map +1 -1
  87. package/dist/routes/postOAuth2Token.test.js +493 -61
  88. package/dist/routes/postOAuth2Token.test.js.map +1 -1
  89. package/dist/services/authCookies.d.ts +9 -13
  90. package/dist/services/authCookies.js +9 -5
  91. package/dist/services/authCookies.js.map +1 -1
  92. package/dist/services/authCookies.test.js.map +1 -1
  93. package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +124 -0
  94. package/dist/services/oAuth2AuthorizationCodeGranter.js +204 -0
  95. package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
  96. package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
  97. package/dist/services/oAuth2AuthorizationCodeGranter.test.js +345 -0
  98. package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -0
  99. package/dist/services/oAuth2AuthorizationRequests.d.ts +41 -0
  100. package/dist/services/oAuth2AuthorizationRequests.js +61 -0
  101. package/dist/services/oAuth2AuthorizationRequests.js.map +1 -0
  102. package/dist/services/oAuth2AuthorizationRequests.test.d.ts +1 -0
  103. package/dist/services/oAuth2AuthorizationRequests.test.js +249 -0
  104. package/dist/services/oAuth2AuthorizationRequests.test.js.map +1 -0
  105. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +28 -12
  106. package/dist/services/oAuth2ClientCredentialsGranter.js +39 -21
  107. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
  108. package/dist/services/oAuth2ClientCredentialsGranter.test.d.ts +1 -0
  109. package/dist/services/oAuth2ClientCredentialsGranter.test.js +265 -0
  110. package/dist/services/oAuth2ClientCredentialsGranter.test.js.map +1 -0
  111. package/dist/services/oAuth2Granters.d.ts +79 -86
  112. package/dist/services/oAuth2Granters.js +2 -67
  113. package/dist/services/oAuth2Granters.js.map +1 -1
  114. package/dist/services/oAuth2ImplicitGranter.d.ts +38 -0
  115. package/dist/services/oAuth2ImplicitGranter.js +61 -0
  116. package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
  117. package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
  118. package/dist/services/oAuth2ImplicitGranter.test.js +589 -0
  119. package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -0
  120. package/dist/services/oAuth2PasswordGranter.d.ts +43 -10
  121. package/dist/services/oAuth2PasswordGranter.js +53 -18
  122. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  123. package/dist/services/oAuth2PasswordGranter.test.d.ts +1 -0
  124. package/dist/services/oAuth2PasswordGranter.test.js +455 -0
  125. package/dist/services/oAuth2PasswordGranter.test.js.map +1 -0
  126. package/dist/services/oAuth2RefreshTokenGranter.d.ts +42 -9
  127. package/dist/services/oAuth2RefreshTokenGranter.js +60 -21
  128. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  129. package/dist/services/oAuth2RefreshTokenGranter.test.d.ts +1 -0
  130. package/dist/services/oAuth2RefreshTokenGranter.test.js +403 -0
  131. package/dist/services/oAuth2RefreshTokenGranter.test.js.map +1 -0
  132. package/package.json +14 -13
  133. package/src/index.test.ts +1773 -407
  134. package/src/index.ts +63 -53
  135. package/src/libs/authorizationRequests.test.ts +40 -0
  136. package/src/libs/authorizationRequests.ts +42 -0
  137. package/src/libs/clients.ts +22 -0
  138. package/src/libs/errors.ts +411 -0
  139. package/src/libs/grants.ts +15 -0
  140. package/src/libs/redirectURI.ts +142 -0
  141. package/src/libs/schemas.ts +37 -0
  142. package/src/libs/scopes.ts +49 -0
  143. package/src/libs/utils.ts +24 -0
  144. package/src/libs/verifier.test.ts +37 -0
  145. package/src/libs/verifier.ts +75 -0
  146. package/src/routes/getOAuth2Authorize.test.ts +1102 -102
  147. package/src/routes/getOAuth2Authorize.ts +243 -151
  148. package/src/routes/getOAuth2WellKnownMetadata.test.ts +73 -74
  149. package/src/routes/getOAuth2WellKnownMetadata.ts +63 -16
  150. package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +4 -6
  151. package/src/routes/postAuthLogin.test.ts +86 -57
  152. package/src/routes/postAuthLogin.ts +24 -14
  153. package/src/routes/postAuthLogout.test.ts +42 -18
  154. package/src/routes/postAuthLogout.ts +22 -8
  155. package/src/routes/postAuthRefresh.test.ts +98 -61
  156. package/src/routes/postAuthRefresh.ts +25 -13
  157. package/src/routes/postOAuth2Acknowledge.test.ts +167 -108
  158. package/src/routes/postOAuth2Acknowledge.ts +90 -64
  159. package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +681 -0
  160. package/src/routes/postOAuth2PushedAuthorizationRequest.ts +292 -0
  161. package/src/routes/postOAuth2Revoke.test.ts +179 -0
  162. package/src/routes/postOAuth2Revoke.ts +156 -0
  163. package/src/routes/postOAuth2Token.test.ts +570 -108
  164. package/src/routes/postOAuth2Token.ts +104 -136
  165. package/src/services/authCookies.test.ts +5 -5
  166. package/src/services/authCookies.ts +24 -22
  167. package/src/services/oAuth2AuthorizationCodeGranter.test.ts +407 -0
  168. package/src/services/oAuth2AuthorizationCodeGranter.ts +365 -0
  169. package/src/services/oAuth2AuthorizationRequests.test.ts +282 -0
  170. package/src/services/oAuth2AuthorizationRequests.ts +141 -0
  171. package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
  172. package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
  173. package/src/services/oAuth2Granters.ts +100 -196
  174. package/src/services/oAuth2ImplicitGranter.test.ts +650 -0
  175. package/src/services/oAuth2ImplicitGranter.ts +147 -0
  176. package/src/services/oAuth2PasswordGranter.test.ts +493 -0
  177. package/src/services/oAuth2PasswordGranter.ts +116 -40
  178. package/src/services/oAuth2RefreshTokenGranter.test.ts +441 -0
  179. package/src/services/oAuth2RefreshTokenGranter.ts +118 -51
  180. package/src/types.d.ts +1 -0
  181. package/dist/services/oAuth2CodeGranter.d.ts +0 -41
  182. package/dist/services/oAuth2CodeGranter.js +0 -105
  183. package/dist/services/oAuth2CodeGranter.js.map +0 -1
  184. package/dist/services/oAuth2CodeGranter.test.js +0 -123
  185. package/dist/services/oAuth2CodeGranter.test.js.map +0 -1
  186. package/dist/services/oAuth2TokenGranter.d.ts +0 -22
  187. package/dist/services/oAuth2TokenGranter.js +0 -64
  188. package/dist/services/oAuth2TokenGranter.js.map +0 -1
  189. package/src/__snapshots__/index.test.ts.snap +0 -401
  190. package/src/routes/__snapshots__/getOAuth2Authorize.test.ts.snap +0 -37
  191. package/src/routes/__snapshots__/postOAuth2Acknowledge.test.ts.snap +0 -61
  192. package/src/routes/__snapshots__/postOAuth2Token.test.ts.snap +0 -52
  193. package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +0 -52
  194. package/src/services/oAuth2CodeGranter.test.ts +0 -176
  195. package/src/services/oAuth2CodeGranter.ts +0 -210
  196. package/src/services/oAuth2TokenGranter.ts +0 -119
  197. /package/dist/{services/oAuth2CodeGranter.test.d.ts → libs/authorizationRequests.test.d.ts} +0 -0
@@ -1,102 +1,95 @@
1
1
  import { type Dependencies } from 'knifecycle';
2
2
  import { type WhookErrorsDescriptors } from '@whook/whook';
3
- import { type WhookAuthenticationData } from '@whook/authorization';
4
- export interface OAuth2YErrorRegistry {
5
- E_UNKNOWN_AUTHORIZER_TYPE: [responseType: string];
6
- E_UNKNOWN_ACKNOWLEDGER_TYPE: [responseType: string];
7
- E_APPLICATION_ID_MISMATCH: [
8
- clientApplicationId: string,
9
- authenticationApplicationId: string
10
- ];
11
- /**
12
- * Thrown when the refresh token is expired/invalid
13
- */
14
- E_BAD_REFRESH_TOKEN: [];
15
- /**
16
- * Thrown when application has no access allowed
17
- */
18
- E_ACCESS_DENIED: [clientId: string];
19
- /**
20
- * Thrown when the OAuth2 server had unexpected errors
21
- */
22
- E_OAUTH2: [];
23
- /**
24
- * Thrown when refresh_token cookie is absent and required
25
- */
26
- E_REFRESH_COOKIE: [cookie: string];
27
- /**
28
- * Thrown when server requires PKCE
29
- */
30
- E_PKCE_REQUIRED: [responseType: string];
31
- /**
32
- * Thrown when server does not support PKCE
33
- */
34
- E_PKCE_NOT_SUPPORTED: [responseType: string];
3
+ import { type WhookAuthenticationScope, type WhookAuthenticationData, type WhookBaseAuthenticationData } from '@whook/authorization';
4
+ export interface WhookOAuth2Types {
5
+ clientId: string;
35
6
  }
36
- export declare const OAUTH2_ERRORS_DESCRIPTORS: Record<keyof OAuth2YErrorRegistry, WhookErrorsDescriptors[string]>;
37
- export interface OAuth2PasswordService<USERNAME extends string = string, PASSWORD extends string = string> {
38
- check: (authenticationData: WhookAuthenticationData, username: USERNAME, password: PASSWORD, demandedScope: WhookAuthenticationData['scope']) => Promise<WhookAuthenticationData>;
7
+ export type WhookOAuth2ClientId = WhookOAuth2Types['clientId'];
8
+ declare module '@whook/authorization' {
9
+ interface WhookAuthenticationData extends WhookBaseAuthenticationData {
10
+ clientId: WhookOAuth2ClientId;
11
+ }
39
12
  }
40
- export interface OAuth2AccessTokenService<TOKEN extends string = string> {
41
- create: (authenticationData: WhookAuthenticationData, tokenAuthenticationData: WhookAuthenticationData, additionalParameters?: Record<string, unknown>) => Promise<{
42
- token: TOKEN;
43
- expiresAt: number;
44
- }>;
45
- check: (authenticationData: WhookAuthenticationData, token: TOKEN, scope?: WhookAuthenticationData['scope']) => Promise<WhookAuthenticationData>;
13
+ export interface WhookOAuth2ClientGrants {
14
+ /** Grant types the client can be used with */
15
+ allowedGrantTypes: string[];
16
+ /** Scopes the client can delegate */
17
+ allowedScopes: WhookAuthenticationScope[];
18
+ /** Redirect URIS the client can be used with */
19
+ allowedRedirectURIS: string[];
20
+ /** Authentication data of the client */
21
+ authenticationData: WhookAuthenticationData;
22
+ /** Whether the client is public or private */
23
+ isPublicClient: boolean;
24
+ /** Whether the client can acknowledge for a user */
25
+ canAcknowledge?: boolean;
46
26
  }
47
- export type OAuth2RefreshTokenService<TOKEN extends string = string> = OAuth2AccessTokenService<TOKEN>;
48
- export type CheckApplicationService = (context: {
49
- applicationId: string;
50
- type: string;
51
- scope: string;
52
- redirectURI?: string;
27
+ /**
28
+ * A service that returns acknowledgments for a given client
29
+ */
30
+ export type WhookOAuth2ReadClientGrantsService = (clientId: WhookOAuth2ClientId) => Promise<WhookOAuth2ClientGrants>;
31
+ /**
32
+ * A function handling the OAuth2 authorize step
33
+ */
34
+ export type WhookOAuth2GranterAuthorize = (authorizeParameters: {
35
+ clientId: WhookOAuth2ClientId;
36
+ clientGrants: WhookOAuth2ClientGrants;
37
+ demandedScopes: WhookAuthenticationScope[];
53
38
  }) => Promise<{
54
- applicationId: string;
55
- type: string;
56
- scope: string;
57
- redirectURI: string;
58
- }>;
59
- export type OAuth2GranterAuthorize<P extends object> = (context: {
60
- clientId: WhookAuthenticationData['applicationId'];
61
- redirectURI: string;
62
- scope: WhookAuthenticationData['scope'];
63
- }, authorizeParameters: P) => Promise<{
64
- applicationId: WhookAuthenticationData['applicationId'];
65
- redirectURI: string;
66
- scope: WhookAuthenticationData['scope'];
39
+ scopes: WhookAuthenticationScope[];
67
40
  }>;
68
- export type Oauth2GranterAuthenticate<P extends object> = (authenticateParameters: P, authenticationData: WhookAuthenticationData) => Promise<WhookAuthenticationData>;
69
- export type OAuth2GranterAcknowledge<A extends object, P extends object = Record<string, unknown>> = (authenticationData: WhookAuthenticationData, acknowledgeParameters: {
70
- clientId: WhookAuthenticationData['applicationId'];
71
- redirectURI: string;
72
- scope: WhookAuthenticationData['scope'];
73
- }, additionalParameters: P) => Promise<{
74
- authenticationData: WhookAuthenticationData;
75
- redirectURI: string;
76
- acknowledgedData: A;
41
+ /**
42
+ * A function handling the Whook specific acknowledge
43
+ * step allowing to build a front end over it (for users
44
+ * to actually authorize clients to act on their behalf)
45
+ */
46
+ export type WhookOAuth2GranterAcknowledge<T extends object, U extends object> = (
47
+ /** The acknowledging user authentication data */
48
+ userAuthenticationData: WhookAuthenticationData, acknowledgeParameters: {
49
+ clientId: WhookOAuth2ClientId;
50
+ demandedRedirectURI: string;
51
+ demandedScopes: WhookAuthenticationScope[];
52
+ }, acknowledgeData: T) => Promise<{
53
+ acknowledgedAuthenticationData: WhookAuthenticationData;
54
+ acknowledgedRedirectURI: string;
55
+ acknowledgedScopes: WhookAuthenticationScope[];
56
+ acknowledgedData: U;
77
57
  }>;
78
- export interface OAuth2GranterService<T extends object = Record<string, unknown>, U extends object = Record<string, unknown>, V extends object = Record<string, unknown>, W extends object = Record<string, unknown>> {
79
- type: string;
80
- authorizer?: {
81
- responseType: string;
82
- authorize: OAuth2GranterAuthorize<T>;
83
- };
84
- acknowledger?: {
85
- acknowledgmentType: string;
86
- acknowledge: OAuth2GranterAcknowledge<U, V>;
87
- };
88
- authenticator?: {
89
- grantType: string;
90
- authenticate: Oauth2GranterAuthenticate<W>;
91
- };
58
+ /**
59
+ * A function handling the authenticate step
60
+ */
61
+ export type WhookOAuth2GranterAuthenticate<T extends object> = (authenticateParameters: T, optionalAuthenticationData?: WhookAuthenticationData | undefined) => Promise<WhookAuthenticationData>;
62
+ export interface WhookOAuth2GranterDefinitions {
63
+ grantType?: string;
64
+ responseType?: string;
65
+ acknowledgeParameters: Record<string, unknown>;
66
+ acknowledgedData: Record<string, unknown>;
67
+ authenticateParameters: Record<string, unknown>;
68
+ }
69
+ export interface WhookOAuth2GranterService<T extends WhookOAuth2GranterDefinitions> {
70
+ grantType: T['grantType'];
71
+ responseType?: T['responseType'];
72
+ issuesRefreshToken: boolean;
73
+ authorize?: WhookOAuth2GranterAuthorize;
74
+ acknowledge?: WhookOAuth2GranterAcknowledge<T['acknowledgeParameters'], T['acknowledgedData']>;
75
+ authenticate?: WhookOAuth2GranterAuthenticate<T['authenticateParameters']>;
92
76
  }
93
- export interface OAuth2Options {
77
+ export interface WhookOAuth2Options {
78
+ /** Id of the root application */
79
+ rootClientId: WhookOAuth2ClientId;
80
+ /** Where client authenticate */
94
81
  authenticateURL: string;
82
+ /** The available scopes of the whole application */
83
+ allowedScopes: WhookAuthenticationScope[];
84
+ /** Whether the client scope should be used as the default scope */
95
85
  defaultToClientScope?: boolean;
86
+ /** Whether PKCE should be forced or not */
96
87
  forcePKCE?: boolean;
88
+ /** Whether scopes should not only be filtered but strictly checked */
89
+ strictScopesChecks?: boolean;
97
90
  }
98
- export interface OAuth2Config {
99
- OAUTH2: OAuth2Options;
91
+ export interface WhookOAuth2Config {
92
+ OAUTH2: WhookOAuth2Options;
100
93
  ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
101
94
  }
102
95
  declare const _default: import("knifecycle").ServiceInitializer<Dependencies, any[]>;
@@ -1,78 +1,13 @@
1
1
  import { initializer, location } from 'knifecycle';
2
- import { DEFAULT_ERROR_URI, DEFAULT_HELP_URI, } from '@whook/whook';
3
- export const OAUTH2_ERRORS_DESCRIPTORS = {
4
- E_UNKNOWN_AUTHORIZER_TYPE: {
5
- code: 'unsupported_response_type',
6
- status: 400,
7
- description: `The type "$0" is not supported.`,
8
- uri: DEFAULT_ERROR_URI,
9
- help: DEFAULT_HELP_URI,
10
- },
11
- E_APPLICATION_ID_MISMATCH: {
12
- code: 'unauthorized_client',
13
- status: 400,
14
- description: `The clients ids "$0"/"$1" are inconsistent.`,
15
- uri: DEFAULT_ERROR_URI,
16
- help: DEFAULT_HELP_URI,
17
- },
18
- E_BAD_REFRESH_TOKEN: {
19
- code: 'invalid_request',
20
- status: 400,
21
- description: 'The refresh token is expired/invalid.',
22
- uri: DEFAULT_ERROR_URI,
23
- help: DEFAULT_HELP_URI,
24
- },
25
- E_UNKNOWN_ACKNOWLEDGER_TYPE: {
26
- code: 'unsupported_response_type',
27
- status: 400,
28
- description: `Type "$0" not supported.`,
29
- uri: DEFAULT_ERROR_URI,
30
- help: DEFAULT_HELP_URI,
31
- },
32
- E_ACCESS_DENIED: {
33
- code: 'access_denied',
34
- status: 403,
35
- description: 'The user denied access to your application (id: "$0").',
36
- uri: DEFAULT_ERROR_URI,
37
- help: DEFAULT_HELP_URI,
38
- },
39
- E_OAUTH2: {
40
- code: 'server_error',
41
- status: 500,
42
- uri: DEFAULT_ERROR_URI,
43
- help: DEFAULT_HELP_URI,
44
- },
45
- E_REFRESH_COOKIE: {
46
- code: 'invalid_request',
47
- status: 400,
48
- description: `Could not find any refresh_token value in the cookie header ($0).`,
49
- uri: DEFAULT_ERROR_URI,
50
- help: DEFAULT_HELP_URI,
51
- },
52
- E_PKCE_REQUIRED: {
53
- code: 'invalid_request',
54
- status: 400,
55
- description: 'Code challenge required for this response type ($0)',
56
- uri: DEFAULT_ERROR_URI,
57
- help: DEFAULT_HELP_URI,
58
- },
59
- E_PKCE_NOT_SUPPORTED: {
60
- code: 'invalid_request',
61
- status: 400,
62
- description: 'Code challenge not supported for this response type ($0)',
63
- uri: DEFAULT_ERROR_URI,
64
- help: DEFAULT_HELP_URI,
65
- },
66
- };
67
2
  export default location(initializer({
68
3
  name: 'oAuth2Granters',
69
4
  type: 'service',
70
5
  inject: [
71
6
  'oAuth2ClientCredentialsGranter',
72
- 'oAuth2CodeGranter',
7
+ 'oAuth2AuthorizationCodeGranter',
73
8
  'oAuth2PasswordGranter',
74
9
  'oAuth2RefreshTokenGranter',
75
- 'oAuth2TokenGranter',
10
+ 'oAuth2ImplicitGranter',
76
11
  ],
77
12
  }, async (services) => Object.keys(services).map((key) => services[key])), import.meta.url);
78
13
  //# sourceMappingURL=oAuth2Granters.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"oAuth2Granters.js","sourceRoot":"","sources":["../../src/services/oAuth2Granters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtE,OAAO,EACL,iBAAiB,EACjB,gBAAgB,GAEjB,MAAM,cAAc,CAAC;AA0CtB,MAAM,CAAC,MAAM,yBAAyB,GAGlC;IACF,yBAAyB,EAAE;QACzB,IAAI,EAAE,2BAA2B;QACjC,MAAM,EAAE,GAAG;QACX,WAAW,EAAE,iCAAiC;QAC9C,GAAG,EAAE,iBAAiB;QACtB,IAAI,EAAE,gBAAgB;KACvB;IACD,yBAAyB,EAAE;QACzB,IAAI,EAAE,qBAAqB;QAC3B,MAAM,EAAE,GAAG;QACX,WAAW,EAAE,6CAA6C;QAC1D,GAAG,EAAE,iBAAiB;QACtB,IAAI,EAAE,gBAAgB;KACvB;IACD,mBAAmB,EAAE;QACnB,IAAI,EAAE,iBAAiB;QACvB,MAAM,EAAE,GAAG;QACX,WAAW,EAAE,uCAAuC;QACpD,GAAG,EAAE,iBAAiB;QACtB,IAAI,EAAE,gBAAgB;KACvB;IACD,2BAA2B,EAAE;QAC3B,IAAI,EAAE,2BAA2B;QACjC,MAAM,EAAE,GAAG;QACX,WAAW,EAAE,0BAA0B;QACvC,GAAG,EAAE,iBAAiB;QACtB,IAAI,EAAE,gBAAgB;KACvB;IACD,eAAe,EAAE;QACf,IAAI,EAAE,eAAe;QACrB,MAAM,EAAE,GAAG;QACX,WAAW,EAAE,wDAAwD;QACrE,GAAG,EAAE,iBAAiB;QACtB,IAAI,EAAE,gBAAgB;KACvB;IACD,QAAQ,EAAE;QACR,IAAI,EAAE,cAAc;QACpB,MAAM,EAAE,GAAG;QACX,GAAG,EAAE,iBAAiB;QACtB,IAAI,EAAE,gBAAgB;KACvB;IACD,gBAAgB,EAAE;QAChB,IAAI,EAAE,iBAAiB;QACvB,MAAM,EAAE,GAAG;QACX,WAAW,EAAE,mEAAmE;QAChF,GAAG,EAAE,iBAAiB;QACtB,IAAI,EAAE,gBAAgB;KACvB;IACD,eAAe,EAAE;QACf,IAAI,EAAE,iBAAiB;QACvB,MAAM,EAAE,GAAG;QACX,WAAW,EAAE,qDAAqD;QAClE,GAAG,EAAE,iBAAiB;QACtB,IAAI,EAAE,gBAAgB;KACvB;IACD,oBAAoB,EAAE;QACpB,IAAI,EAAE,iBAAiB;QACvB,MAAM,EAAE,GAAG;QACX,WAAW,EAAE,0DAA0D;QACvE,GAAG,EAAE,iBAAiB;QACtB,IAAI,EAAE,gBAAgB;KACvB;CACF,CAAC;AAgHF,eAAe,QAAQ,CACrB,WAAW,CACT;IACE,IAAI,EAAE,gBAAgB;IACtB,IAAI,EAAE,SAAS;IACf,MAAM,EAAE;QACN,gCAAgC;QAChC,mBAAmB;QACnB,uBAAuB;QACvB,2BAA2B;QAC3B,oBAAoB;KACrB;CACF,EACD,KAAK,EAAE,QAAsB,EAAE,EAAE,CAC/B,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CACpD,EACD,MAAM,CAAC,IAAI,CAAC,GAAG,CAChB,CAAC"}
1
+ {"version":3,"file":"oAuth2Granters.js","sourceRoot":"","sources":["../../src/services/oAuth2Granters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAiItE,eAAe,QAAQ,CACrB,WAAW,CACT;IACE,IAAI,EAAE,gBAAgB;IACtB,IAAI,EAAE,SAAS;IACf,MAAM,EAAE;QACN,gCAAgC;QAChC,gCAAgC;QAChC,uBAAuB;QACvB,2BAA2B;QAC3B,uBAAuB;KACxB;CACF,EACD,KAAK,EAAE,QAAsB,EAAE,EAAE,CAC/B,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CACpD,EACD,MAAM,CAAC,IAAI,CAAC,GAAG,CAChB,CAAC"}
@@ -0,0 +1,38 @@
1
+ import { type LogService, type TimeService } from 'common-services';
2
+ import { type WhookOAuth2GranterService, type WhookOAuth2ReadClientGrantsService, type WhookOAuth2Options, type WhookOAuth2GranterDefinitions } from './oAuth2Granters.js';
3
+ import { type WhookAuthenticationData } from '@whook/authorization';
4
+ export declare const IMPLICIT_GRANT_TYPE = "implicit";
5
+ export declare const IMPLICIT_RESPONSE_TYPE = "token";
6
+ /**
7
+ * A type allowing to override the access token types
8
+ */
9
+ export type WhookOAuth2AccessToken = string;
10
+ /**
11
+ * A service to create and check access tokens
12
+ */
13
+ export interface WhookOAuth2AccessTokenService<T extends string = WhookOAuth2AccessToken> {
14
+ create: (authenticationData: WhookAuthenticationData) => Promise<{
15
+ token: T;
16
+ expiresAt: number;
17
+ }>;
18
+ check: (token: T) => Promise<WhookAuthenticationData>;
19
+ }
20
+ export interface WhookOAuth2ImplicitGranterDependencies {
21
+ OAUTH2: WhookOAuth2Options;
22
+ readClientGrants: WhookOAuth2ReadClientGrantsService;
23
+ oAuth2AccessToken: Pick<WhookOAuth2AccessTokenService, 'create'>;
24
+ time?: TimeService;
25
+ log?: LogService;
26
+ }
27
+ export interface WhookOAuth2ImplicitGranterDefinitions extends WhookOAuth2GranterDefinitions {
28
+ grantType: typeof IMPLICIT_GRANT_TYPE;
29
+ responseType: typeof IMPLICIT_RESPONSE_TYPE;
30
+ acknowledgedData: {
31
+ accessToken: string;
32
+ tokenType: 'bearer';
33
+ expiresIn: number;
34
+ };
35
+ }
36
+ export type WhookOAuth2ImplicitGranterService = WhookOAuth2GranterService<WhookOAuth2ImplicitGranterDefinitions>;
37
+ declare const _default: import("knifecycle").ServiceInitializer<WhookOAuth2ImplicitGranterDependencies, WhookOAuth2ImplicitGranterService>;
38
+ export default _default;
@@ -0,0 +1,61 @@
1
+ import { autoService, location } from 'knifecycle';
2
+ import { noop } from '@whook/whook';
3
+ import { YError } from 'yerror';
4
+ import { checkGrantType } from '../libs/grants.js';
5
+ import { filterScopes } from '../libs/scopes.js';
6
+ import { checkRedirectURI } from '../libs/redirectURI.js';
7
+ export const IMPLICIT_GRANT_TYPE = 'implicit';
8
+ export const IMPLICIT_RESPONSE_TYPE = 'token';
9
+ // Implicit Grant
10
+ // https://tools.ietf.org/html/rfc6749#section-4.2
11
+ async function initOAuth2ImplicitGranter({ OAUTH2, readClientGrants, oAuth2AccessToken, time = Date.now.bind(Date), log = noop, }) {
12
+ log('warning', `⚠️ - Using the token flow is deprecated and not recommended.`);
13
+ const authorizeWithToken = async ({ clientGrants, demandedScopes }) => {
14
+ checkGrantType(clientGrants.allowedGrantTypes, IMPLICIT_GRANT_TYPE);
15
+ return {
16
+ scopes: demandedScopes,
17
+ };
18
+ };
19
+ // Access Token Response:
20
+ // https://tools.ietf.org/html/rfc6749#section-4.2.2
21
+ const acknowledgeWithToken = async (userAuthenticationData, { clientId, demandedRedirectURI, demandedScopes }) => {
22
+ const grants = await readClientGrants(clientId);
23
+ if (!grants.isPublicClient) {
24
+ throw new YError('E_OAUTH2_AUTHENTICATION_REQUIRED', [
25
+ grants.authenticationData.clientId,
26
+ ]);
27
+ }
28
+ const filteredScopes = filterScopes(filterScopes(demandedScopes, grants.allowedScopes, !!OAUTH2.strictScopesChecks), userAuthenticationData.scopes, !!OAUTH2.strictScopesChecks);
29
+ checkGrantType(grants.allowedGrantTypes, IMPLICIT_GRANT_TYPE);
30
+ checkRedirectURI(grants.allowedRedirectURIS, demandedRedirectURI);
31
+ const { token: accessToken, expiresAt: accessTokenExpiresAt } = await oAuth2AccessToken.create({
32
+ ...userAuthenticationData,
33
+ clientId,
34
+ scopes: filteredScopes,
35
+ });
36
+ return {
37
+ acknowledgedAuthenticationData: {
38
+ ...userAuthenticationData,
39
+ clientId,
40
+ scopes: filteredScopes,
41
+ },
42
+ acknowledgedRedirectURI: demandedRedirectURI,
43
+ acknowledgedScopes: filteredScopes,
44
+ acknowledgedData: {
45
+ accessToken,
46
+ tokenType: 'bearer',
47
+ expiresIn: Math.round((accessTokenExpiresAt - time()) / 1000),
48
+ },
49
+ };
50
+ };
51
+ log('debug', '👫 - OAuth2ImplicitGranter Service Initialized!');
52
+ return {
53
+ grantType: IMPLICIT_GRANT_TYPE,
54
+ responseType: IMPLICIT_RESPONSE_TYPE,
55
+ issuesRefreshToken: false,
56
+ authorize: authorizeWithToken,
57
+ acknowledge: acknowledgeWithToken,
58
+ };
59
+ }
60
+ export default location(autoService(initOAuth2ImplicitGranter), import.meta.url);
61
+ //# sourceMappingURL=oAuth2ImplicitGranter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oAuth2ImplicitGranter.js","sourceRoot":"","sources":["../../src/services/oAuth2ImplicitGranter.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1 @@
1
+ export {};