@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,176 +0,0 @@
1
- import { describe, test, beforeEach, jest, expect } from '@jest/globals';
2
- import { type WhookAuthenticationData } from '@whook/authorization';
3
- import { type CheckApplicationService } from './oAuth2Granters.js';
4
- import initOAuth2CodeGranter, {
5
- base64UrlEncode,
6
- hashCodeVerifier,
7
- type OAuth2CodeService,
8
- } from './oAuth2CodeGranter.js';
9
-
10
- describe('OAuth2CodeGranter', () => {
11
- const oAuth2Code = {
12
- create: jest.fn<OAuth2CodeService['create']>(),
13
- check: jest.fn<OAuth2CodeService['check']>(),
14
- };
15
- const checkApplication = jest.fn<CheckApplicationService>();
16
- const log = jest.fn();
17
-
18
- beforeEach(() => {
19
- oAuth2Code.create.mockReset();
20
- oAuth2Code.check.mockReset();
21
- checkApplication.mockReset();
22
- log.mockReset();
23
- });
24
-
25
- test('should work with a complete valid flow', async () => {
26
- const oAuth2CodeGranter = await initOAuth2CodeGranter({
27
- checkApplication,
28
- oAuth2Code,
29
- log,
30
- });
31
-
32
- checkApplication.mockResolvedValue({
33
- redirectURI: 'https://www.example.com',
34
- type: 'a_type',
35
- scope: 'a_scope',
36
- applicationId: 'abbacaca-abba-caca-abba-cacaabbac0c0',
37
- });
38
- oAuth2Code.create.mockResolvedValueOnce('yolo');
39
- oAuth2Code.check.mockResolvedValueOnce({
40
- redirectURI: 'https://www.example2.com',
41
- applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
42
- scope: 'user',
43
- } as WhookAuthenticationData & {
44
- redirectURI: string;
45
- [name: string]: unknown;
46
- });
47
-
48
- const authorizerResult = await oAuth2CodeGranter.authorizer?.authorize(
49
- {
50
- clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
51
- redirectURI: 'https://www.example.com/oauth2/code',
52
- scope: 'user',
53
- },
54
- {
55
- codeChallenge: '',
56
- codeChallengeMethod: 'plain',
57
- },
58
- );
59
- const acknowledgerResult =
60
- await oAuth2CodeGranter.acknowledger?.acknowledge(
61
- {
62
- applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
63
- scope: 'user, admin',
64
- } as WhookAuthenticationData,
65
- {
66
- clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
67
- redirectURI: 'https://www.example.com/oauth2/code',
68
- scope: 'user',
69
- },
70
- {
71
- codeChallenge: '',
72
- codeChallengeMethod: 'plain',
73
- },
74
- );
75
- const authenticatorResult =
76
- await oAuth2CodeGranter.authenticator?.authenticate(
77
- {
78
- clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
79
- redirectURI: 'https://www.example.com/oauth2/code',
80
- code: 'yolo',
81
- codeVerifier: '',
82
- },
83
- {
84
- applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
85
- scope: 'user',
86
- } as WhookAuthenticationData,
87
- );
88
-
89
- expect({
90
- authorizerResult,
91
- acknowledgerResult,
92
- authenticatorResult,
93
- }).toMatchInlineSnapshot(`
94
- {
95
- "acknowledgerResult": {
96
- "acknowledgedData": {
97
- "code": "yolo",
98
- },
99
- "additionalParameters": {},
100
- "authenticationData": {
101
- "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
102
- "scope": "user",
103
- },
104
- "redirectURI": "https://www.example.com/oauth2/code",
105
- },
106
- "authenticatorResult": {
107
- "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
108
- "redirectURI": "https://www.example2.com",
109
- "scope": "user",
110
- },
111
- "authorizerResult": {
112
- "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
113
- "redirectURI": "https://www.example.com",
114
- "scope": "user",
115
- },
116
- }
117
- `);
118
- expect({
119
- oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
120
- oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
121
- checkApplicationCalls: checkApplication.mock.calls,
122
- logCalls: log.mock.calls,
123
- }).toMatchSnapshot();
124
- });
125
- });
126
-
127
- describe('base64UrlEncode()', () => {
128
- test('should work like here https://tools.ietf.org/html/rfc7636#appendix-A', () => {
129
- expect(
130
- base64UrlEncode(
131
- Buffer.from([
132
- 116, 24, 223, 180, 151, 153, 224, 37, 79, 250, 96, 125, 216, 173, 187,
133
- 186, 22, 212, 37, 77, 105, 214, 191, 240, 91, 88, 5, 88, 83, 132, 141,
134
- 121,
135
- ]),
136
- ),
137
- ).toEqual('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk');
138
- });
139
- });
140
-
141
- describe('base64UrlEncode()', () => {
142
- test('should work with plain method', () => {
143
- expect(
144
- hashCodeVerifier(
145
- Buffer.from('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'),
146
- 'plain',
147
- ),
148
- ).toEqual(Buffer.from('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'));
149
- });
150
-
151
- test('should work with S256 like here https://tools.ietf.org/html/rfc7636#appendix-A', () => {
152
- expect(
153
- hashCodeVerifier(
154
- Buffer.from('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'),
155
- 'S256',
156
- ),
157
- ).toEqual(
158
- Buffer.from([
159
- 19, 211, 30, 150, 26, 26, 216, 236, 47, 22, 177, 12, 76, 152, 46, 8,
160
- 118, 168, 120, 173, 109, 241, 68, 86, 110, 225, 137, 74, 203, 112, 249,
161
- 195,
162
- ]),
163
- );
164
- });
165
-
166
- test('should work base64 url encode like here https://tools.ietf.org/html/rfc7636#appendix-A', () => {
167
- expect(
168
- base64UrlEncode(
169
- hashCodeVerifier(
170
- Buffer.from('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'),
171
- 'S256',
172
- ),
173
- ),
174
- ).toEqual('E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM');
175
- });
176
- });
@@ -1,210 +0,0 @@
1
- import { autoService, location } from 'knifecycle';
2
- import { noop } from '@whook/whook';
3
- import { YError } from 'yerror';
4
- import { type LogService } from 'common-services';
5
- import {
6
- type OAuth2GranterService,
7
- type CheckApplicationService,
8
- } from './oAuth2Granters.js';
9
- import { createHash } from 'crypto';
10
- import { type WhookAuthenticationData } from '@whook/authorization';
11
-
12
- export const CODE_GRANTER_TYPE = 'code';
13
- export const PLAIN_CODE_CHALLENGE_METHOD = 'plain';
14
- export const S256_CODE_CHALLENGE_METHOD = 'S256';
15
- export const CODE_CHALLENGE_METHODS = [
16
- PLAIN_CODE_CHALLENGE_METHOD,
17
- S256_CODE_CHALLENGE_METHOD,
18
- ] as const;
19
-
20
- export type CodeChallengeMethod = (typeof CODE_CHALLENGE_METHODS)[number];
21
-
22
- export interface OAuth2CodeService<
23
- P extends object = Record<string, unknown>,
24
- C extends string = string,
25
- > {
26
- create: (
27
- authenticationData: WhookAuthenticationData,
28
- redirectURI: string,
29
- codeChallenge: string | undefined,
30
- codeChallengeMethod: CodeChallengeMethod | undefined,
31
- additionalParameters: P,
32
- ) => Promise<C>;
33
- check: (
34
- authenticationData: WhookAuthenticationData,
35
- code: C,
36
- redirectURI: string,
37
- codeVerifier: string | undefined,
38
- ) => Promise<
39
- WhookAuthenticationData & {
40
- redirectURI: string;
41
- } & P
42
- >;
43
- }
44
-
45
- export interface OAuth2CodeGranterDependencies {
46
- oAuth2Code: OAuth2CodeService;
47
- checkApplication: CheckApplicationService;
48
- log?: LogService;
49
- }
50
- export interface OAuth2CodeGranterAuthorizeParameters {
51
- codeChallenge: string;
52
- codeChallengeMethod: CodeChallengeMethod;
53
- }
54
- export interface OAuth2CodeGranterAcknowledgedData {
55
- code: string;
56
- }
57
- export interface OAuth2CodeGranterAcknowledgeParameters {
58
- codeChallenge: string;
59
- codeChallengeMethod: CodeChallengeMethod;
60
- }
61
- export interface OAuth2CodeGranterGrantParameters {
62
- code: string;
63
- redirectURI: string;
64
- clientId: string;
65
- codeVerifier: string;
66
- }
67
- export type OAuth2CodeGranterService = OAuth2GranterService<
68
- OAuth2CodeGranterAuthorizeParameters,
69
- OAuth2CodeGranterAcknowledgedData,
70
- OAuth2CodeGranterAcknowledgeParameters,
71
- OAuth2CodeGranterGrantParameters
72
- >;
73
-
74
- export default location(autoService(initOAuth2CodeGranter), import.meta.url);
75
-
76
- // Authorization Code Grant
77
- // https://tools.ietf.org/html/rfc6749#section-4.1
78
- async function initOAuth2CodeGranter({
79
- oAuth2Code,
80
- checkApplication,
81
- log = noop,
82
- }: OAuth2CodeGranterDependencies): Promise<OAuth2CodeGranterService> {
83
- const authorizeWithCode: NonNullable<
84
- OAuth2CodeGranterService['authorizer']
85
- >['authorize'] = async ({ clientId, redirectURI, scope = '' }) => {
86
- const { redirectURI: finalRedirectURI } = await checkApplication({
87
- applicationId: clientId,
88
- type: CODE_GRANTER_TYPE,
89
- redirectURI,
90
- scope,
91
- });
92
-
93
- return {
94
- applicationId: clientId,
95
- redirectURI: finalRedirectURI,
96
- scope,
97
- };
98
- };
99
-
100
- // Authorization Code Response:
101
- // https://tools.ietf.org/html/rfc6749#section-4.1.2
102
- const acknowledgeWithCode: NonNullable<
103
- OAuth2CodeGranterService['acknowledger']
104
- >['acknowledge'] = async (
105
- authenticationData,
106
- { clientId, redirectURI, scope },
107
- { codeChallenge, codeChallengeMethod, ...additionalParameters },
108
- ) => {
109
- const code = await oAuth2Code.create(
110
- { ...authenticationData, applicationId: clientId, scope },
111
- redirectURI,
112
- codeChallenge,
113
- codeChallengeMethod,
114
- additionalParameters,
115
- );
116
-
117
- return {
118
- authenticationData: {
119
- ...authenticationData,
120
- applicationId: clientId,
121
- scope,
122
- },
123
- redirectURI,
124
- acknowledgedData: {
125
- code,
126
- },
127
- additionalParameters,
128
- };
129
- };
130
-
131
- const authenticateWithCode: NonNullable<
132
- OAuth2CodeGranterService['authenticator']
133
- >['authenticate'] = async (
134
- { code, clientId, redirectURI, codeVerifier },
135
- authenticationData,
136
- ) => {
137
- // The client must be authenticated (for now, see below)
138
- if (!authenticationData) {
139
- throw new YError('E_UNAUTHORIZED');
140
- }
141
-
142
- // This check is not really necessary atm but it acts
143
- // as a reminder that this grant type could be used
144
- // without authenticating the client. In this
145
- // scenario, the authenticationData should be deducted
146
- // from the clientId and the code given in parameters
147
- // See https://tools.ietf.org/html/rfc6749#section-4.1.3
148
- if (clientId && clientId !== authenticationData.applicationId) {
149
- throw new YError('E_APPLICATION_ID_MISMATCH', [
150
- clientId,
151
- authenticationData.applicationId,
152
- ]);
153
- }
154
-
155
- await checkApplication({
156
- applicationId: authenticationData.applicationId,
157
- type: CODE_GRANTER_TYPE,
158
- scope: '',
159
- });
160
-
161
- const newAuthenticationData = await oAuth2Code.check(
162
- authenticationData,
163
- code,
164
- redirectURI,
165
- codeVerifier,
166
- );
167
-
168
- return newAuthenticationData;
169
- };
170
-
171
- log('debug', '👫 - OAuth2CodeGranter Service Initialized!');
172
-
173
- return {
174
- type: CODE_GRANTER_TYPE,
175
- authorizer: {
176
- responseType: 'code',
177
- authorize: authorizeWithCode,
178
- },
179
- acknowledger: {
180
- acknowledgmentType: 'code',
181
- acknowledge: acknowledgeWithCode,
182
- },
183
- authenticator: {
184
- grantType: 'authorization_code',
185
- authenticate: authenticateWithCode,
186
- },
187
- };
188
- }
189
-
190
- // See https://tools.ietf.org/html/rfc7636#appendix-A
191
- export function base64UrlEncode(buf: Buffer): string {
192
- let s = buf.toString('base64');
193
- s = s.split('=')[0];
194
- s = s.replaceAll('+', '-');
195
- s = s.replaceAll('/', '_');
196
- return s;
197
- }
198
-
199
- export function hashCodeVerifier(
200
- codeVerifier: Buffer,
201
- method: CodeChallengeMethod,
202
- ): Buffer {
203
- if (PLAIN_CODE_CHALLENGE_METHOD === method) {
204
- return codeVerifier;
205
- }
206
- if (S256_CODE_CHALLENGE_METHOD === method) {
207
- return createHash('sha256').update(codeVerifier).digest();
208
- }
209
- throw new YError('E_UNSUPPORTED_CODE_CHALLENGE_METHOD', [method]);
210
- }
@@ -1,119 +0,0 @@
1
- import { autoService, location } from 'knifecycle';
2
- import { noop } from '@whook/whook';
3
- import { type LogService, type TimeService } from 'common-services';
4
- import {
5
- type OAuth2GranterService,
6
- type OAuth2AccessTokenService,
7
- type CheckApplicationService,
8
- } from './oAuth2Granters.js';
9
- import { type OAuth2CodeService } from './oAuth2CodeGranter.js';
10
-
11
- export const IMPLICIT_GRANTER_TYPE = 'token';
12
-
13
- export interface OAuth2TokenGranterDependencies {
14
- oAuth2Code: OAuth2CodeService;
15
- checkApplication: CheckApplicationService;
16
- oAuth2AccessToken: OAuth2AccessTokenService;
17
- time?: TimeService;
18
- log?: LogService;
19
- }
20
- export type OAuth2TokenGranterAuthorizeParameters = Record<string, unknown>;
21
- export interface OAuth2TokenGranterAcknowledgedData {
22
- accessToken: string;
23
- tokenType: 'bearer';
24
- expiresIn: number;
25
- }
26
- export type OAuth2TokenGranterAcknowledgeParameters = Record<string, unknown>;
27
- export type OAuth2TokenGranterGrantParameters = Record<string, unknown>;
28
- export type OAuth2TokenGranterService = OAuth2GranterService<
29
- OAuth2TokenGranterAuthorizeParameters,
30
- OAuth2TokenGranterAcknowledgedData,
31
- OAuth2TokenGranterAcknowledgeParameters,
32
- OAuth2TokenGranterGrantParameters
33
- >;
34
-
35
- export default location(autoService(initOAuth2TokenGranter), import.meta.url);
36
-
37
- // Implicit Grant
38
- // https://tools.ietf.org/html/rfc6749#section-4.2
39
- async function initOAuth2TokenGranter({
40
- checkApplication,
41
- oAuth2AccessToken,
42
- time = Date.now.bind(Date),
43
- log = noop,
44
- }: OAuth2TokenGranterDependencies): Promise<OAuth2TokenGranterService> {
45
- const authorizeWithToken: NonNullable<
46
- OAuth2TokenGranterService['authorizer']
47
- >['authorize'] = async ({ clientId, redirectURI, scope }) => {
48
- const { redirectURI: finalRedirectURI } = await checkApplication({
49
- applicationId: clientId,
50
- type: IMPLICIT_GRANTER_TYPE,
51
- redirectURI,
52
- scope,
53
- });
54
-
55
- return {
56
- applicationId: clientId,
57
- redirectURI: finalRedirectURI,
58
- scope,
59
- };
60
- };
61
-
62
- // Access Token Response:
63
- // https://tools.ietf.org/html/rfc6749#section-4.2.2
64
- const acknowledgeWithToken: NonNullable<
65
- OAuth2TokenGranterService['acknowledger']
66
- >['acknowledge'] = async (
67
- authenticationData,
68
- { clientId, redirectURI, scope: providedScope },
69
- additionalParameters,
70
- ) => {
71
- const { redirectURI: finalRedirectURI } = await checkApplication({
72
- applicationId: clientId,
73
- redirectURI,
74
- scope: providedScope,
75
- type: IMPLICIT_GRANTER_TYPE,
76
- });
77
-
78
- const { token: accessToken, expiresAt: accessTokenExpiresAt } =
79
- await oAuth2AccessToken.create(
80
- authenticationData,
81
- {
82
- // TODO: check a way to avoid this by adding params
83
- ...authenticationData,
84
- applicationId: clientId,
85
- scope: providedScope,
86
- },
87
- additionalParameters,
88
- );
89
-
90
- return {
91
- authenticationData: {
92
- ...authenticationData,
93
- applicationId: clientId,
94
- scope: providedScope,
95
- },
96
- redirectURI: finalRedirectURI,
97
- acknowledgedData: {
98
- accessToken,
99
- tokenType: 'bearer',
100
- expiresIn: Math.round((accessTokenExpiresAt - time()) / 1000),
101
- },
102
- additionalParameters,
103
- };
104
- };
105
-
106
- log('debug', '👫 - OAuth2TokenGranter Service Initialized!');
107
-
108
- return {
109
- type: IMPLICIT_GRANTER_TYPE,
110
- authorizer: {
111
- responseType: 'token',
112
- authorize: authorizeWithToken,
113
- },
114
- acknowledger: {
115
- acknowledgmentType: 'token',
116
- acknowledge: acknowledgeWithToken,
117
- },
118
- };
119
- }