@whook/oauth2 25.2.0 → 26.0.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 (173) hide show
  1. package/README.md +147 -98
  2. package/dist/index.d.ts +30 -11
  3. package/dist/index.js +15 -8
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.test.js +1258 -399
  6. package/dist/index.test.js.map +1 -1
  7. package/dist/libs/clients.d.ts +2 -0
  8. package/dist/libs/clients.js +13 -0
  9. package/dist/libs/clients.js.map +1 -0
  10. package/dist/libs/errors.d.ts +113 -0
  11. package/dist/libs/errors.js +178 -0
  12. package/dist/libs/errors.js.map +1 -0
  13. package/dist/libs/grants.d.ts +2 -0
  14. package/dist/libs/grants.js +10 -0
  15. package/dist/libs/grants.js.map +1 -0
  16. package/dist/libs/redirectURI.d.ts +7 -0
  17. package/dist/libs/redirectURI.js +78 -0
  18. package/dist/libs/redirectURI.js.map +1 -0
  19. package/dist/libs/schemas.d.ts +23 -0
  20. package/dist/libs/schemas.js +25 -0
  21. package/dist/libs/schemas.js.map +1 -0
  22. package/dist/libs/scopes.d.ts +16 -0
  23. package/dist/libs/scopes.js +37 -0
  24. package/dist/libs/scopes.js.map +1 -1
  25. package/dist/libs/utils.d.ts +2 -0
  26. package/dist/libs/utils.js +17 -0
  27. package/dist/libs/utils.js.map +1 -0
  28. package/dist/libs/verifier.d.ts +7 -0
  29. package/dist/libs/verifier.js +37 -0
  30. package/dist/libs/verifier.js.map +1 -0
  31. package/dist/libs/verifier.test.js +21 -0
  32. package/dist/libs/verifier.test.js.map +1 -0
  33. package/dist/routes/getOAuth2Authorize.d.ts +13 -50
  34. package/dist/routes/getOAuth2Authorize.js +60 -86
  35. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  36. package/dist/routes/getOAuth2Authorize.test.js +147 -68
  37. package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
  38. package/dist/routes/getOAuth2WellKnownMetadata.d.ts +8 -8
  39. package/dist/routes/getOAuth2WellKnownMetadata.js +20 -9
  40. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  41. package/dist/routes/getOAuth2WellKnownMetadata.test.js +38 -37
  42. package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
  43. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +3 -6
  44. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +3 -6
  45. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
  46. package/dist/routes/postAuthLogin.d.ts +14 -9
  47. package/dist/routes/postAuthLogin.js +10 -5
  48. package/dist/routes/postAuthLogin.js.map +1 -1
  49. package/dist/routes/postAuthLogin.test.js +79 -55
  50. package/dist/routes/postAuthLogin.test.js.map +1 -1
  51. package/dist/routes/postAuthLogout.d.ts +5 -2
  52. package/dist/routes/postAuthLogout.js +12 -7
  53. package/dist/routes/postAuthLogout.js.map +1 -1
  54. package/dist/routes/postAuthLogout.test.js +35 -17
  55. package/dist/routes/postAuthLogout.test.js.map +1 -1
  56. package/dist/routes/postAuthRefresh.d.ts +14 -9
  57. package/dist/routes/postAuthRefresh.js +10 -6
  58. package/dist/routes/postAuthRefresh.js.map +1 -1
  59. package/dist/routes/postAuthRefresh.test.js +90 -59
  60. package/dist/routes/postAuthRefresh.test.js.map +1 -1
  61. package/dist/routes/postOAuth2Acknowledge.d.ts +20 -10
  62. package/dist/routes/postOAuth2Acknowledge.js +54 -38
  63. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  64. package/dist/routes/postOAuth2Acknowledge.test.js +137 -72
  65. package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
  66. package/dist/routes/postOAuth2Revoke.d.ts +99 -0
  67. package/dist/routes/postOAuth2Revoke.js +102 -0
  68. package/dist/routes/postOAuth2Revoke.js.map +1 -0
  69. package/dist/routes/postOAuth2Revoke.test.d.ts +1 -0
  70. package/dist/routes/postOAuth2Revoke.test.js +161 -0
  71. package/dist/routes/postOAuth2Revoke.test.js.map +1 -0
  72. package/dist/routes/postOAuth2Token.d.ts +35 -108
  73. package/dist/routes/postOAuth2Token.js +53 -114
  74. package/dist/routes/postOAuth2Token.js.map +1 -1
  75. package/dist/routes/postOAuth2Token.test.js +493 -61
  76. package/dist/routes/postOAuth2Token.test.js.map +1 -1
  77. package/dist/services/authCookies.d.ts +9 -13
  78. package/dist/services/authCookies.js +1 -1
  79. package/dist/services/authCookies.js.map +1 -1
  80. package/dist/services/authCookies.test.js.map +1 -1
  81. package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +129 -0
  82. package/dist/services/oAuth2AuthorizationCodeGranter.js +221 -0
  83. package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
  84. package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
  85. package/dist/services/oAuth2AuthorizationCodeGranter.test.js +474 -0
  86. package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -0
  87. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +28 -12
  88. package/dist/services/oAuth2ClientCredentialsGranter.js +39 -21
  89. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
  90. package/dist/services/oAuth2ClientCredentialsGranter.test.d.ts +1 -0
  91. package/dist/services/oAuth2ClientCredentialsGranter.test.js +265 -0
  92. package/dist/services/oAuth2ClientCredentialsGranter.test.js.map +1 -0
  93. package/dist/services/oAuth2Granters.d.ts +81 -86
  94. package/dist/services/oAuth2Granters.js +2 -67
  95. package/dist/services/oAuth2Granters.js.map +1 -1
  96. package/dist/services/oAuth2ImplicitGranter.d.ts +38 -0
  97. package/dist/services/oAuth2ImplicitGranter.js +72 -0
  98. package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
  99. package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
  100. package/dist/services/oAuth2ImplicitGranter.test.js +727 -0
  101. package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -0
  102. package/dist/services/oAuth2PasswordGranter.d.ts +43 -10
  103. package/dist/services/oAuth2PasswordGranter.js +53 -18
  104. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  105. package/dist/services/oAuth2PasswordGranter.test.d.ts +1 -0
  106. package/dist/services/oAuth2PasswordGranter.test.js +455 -0
  107. package/dist/services/oAuth2PasswordGranter.test.js.map +1 -0
  108. package/dist/services/oAuth2RefreshTokenGranter.d.ts +42 -9
  109. package/dist/services/oAuth2RefreshTokenGranter.js +60 -21
  110. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  111. package/dist/services/oAuth2RefreshTokenGranter.test.d.ts +1 -0
  112. package/dist/services/oAuth2RefreshTokenGranter.test.js +403 -0
  113. package/dist/services/oAuth2RefreshTokenGranter.test.js.map +1 -0
  114. package/package.json +11 -11
  115. package/src/index.test.ts +1314 -404
  116. package/src/index.ts +46 -53
  117. package/src/libs/clients.ts +22 -0
  118. package/src/libs/errors.ts +325 -0
  119. package/src/libs/grants.ts +15 -0
  120. package/src/libs/redirectURI.ts +120 -0
  121. package/src/libs/schemas.ts +27 -0
  122. package/src/libs/scopes.ts +49 -0
  123. package/src/libs/utils.ts +24 -0
  124. package/src/libs/verifier.test.ts +37 -0
  125. package/src/libs/verifier.ts +57 -0
  126. package/src/routes/getOAuth2Authorize.test.ts +179 -96
  127. package/src/routes/getOAuth2Authorize.ts +97 -124
  128. package/src/routes/getOAuth2WellKnownMetadata.test.ts +58 -60
  129. package/src/routes/getOAuth2WellKnownMetadata.ts +26 -11
  130. package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +4 -6
  131. package/src/routes/postAuthLogin.test.ts +86 -57
  132. package/src/routes/postAuthLogin.ts +24 -14
  133. package/src/routes/postAuthLogout.test.ts +42 -18
  134. package/src/routes/postAuthLogout.ts +22 -8
  135. package/src/routes/postAuthRefresh.test.ts +98 -61
  136. package/src/routes/postAuthRefresh.ts +25 -13
  137. package/src/routes/postOAuth2Acknowledge.test.ts +169 -110
  138. package/src/routes/postOAuth2Acknowledge.ts +101 -61
  139. package/src/routes/postOAuth2Revoke.test.ts +179 -0
  140. package/src/routes/postOAuth2Revoke.ts +156 -0
  141. package/src/routes/postOAuth2Token.test.ts +570 -108
  142. package/src/routes/postOAuth2Token.ts +92 -136
  143. package/src/services/authCookies.test.ts +5 -5
  144. package/src/services/authCookies.ts +16 -18
  145. package/src/services/oAuth2AuthorizationCodeGranter.test.ts +564 -0
  146. package/src/services/oAuth2AuthorizationCodeGranter.ts +388 -0
  147. package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
  148. package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
  149. package/src/services/oAuth2Granters.ts +101 -195
  150. package/src/services/oAuth2ImplicitGranter.test.ts +811 -0
  151. package/src/services/oAuth2ImplicitGranter.ts +163 -0
  152. package/src/services/oAuth2PasswordGranter.test.ts +493 -0
  153. package/src/services/oAuth2PasswordGranter.ts +116 -40
  154. package/src/services/oAuth2RefreshTokenGranter.test.ts +441 -0
  155. package/src/services/oAuth2RefreshTokenGranter.ts +118 -51
  156. package/src/types.d.ts +1 -0
  157. package/dist/services/oAuth2CodeGranter.d.ts +0 -41
  158. package/dist/services/oAuth2CodeGranter.js +0 -105
  159. package/dist/services/oAuth2CodeGranter.js.map +0 -1
  160. package/dist/services/oAuth2CodeGranter.test.js +0 -123
  161. package/dist/services/oAuth2CodeGranter.test.js.map +0 -1
  162. package/dist/services/oAuth2TokenGranter.d.ts +0 -22
  163. package/dist/services/oAuth2TokenGranter.js +0 -64
  164. package/dist/services/oAuth2TokenGranter.js.map +0 -1
  165. package/src/__snapshots__/index.test.ts.snap +0 -401
  166. package/src/routes/__snapshots__/getOAuth2Authorize.test.ts.snap +0 -37
  167. package/src/routes/__snapshots__/postOAuth2Acknowledge.test.ts.snap +0 -61
  168. package/src/routes/__snapshots__/postOAuth2Token.test.ts.snap +0 -52
  169. package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +0 -52
  170. package/src/services/oAuth2CodeGranter.test.ts +0 -176
  171. package/src/services/oAuth2CodeGranter.ts +0 -210
  172. package/src/services/oAuth2TokenGranter.ts +0 -119
  173. /package/dist/{services/oAuth2CodeGranter.test.d.ts → libs/verifier.test.d.ts} +0 -0
@@ -1,20 +1,45 @@
1
- import { describe, test, jest, expect } from '@jest/globals';
1
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
2
2
  import initPostAuthLogin from './postAuthLogin.js';
3
- import { type AuthCookiesService } from '../services/authCookies.js';
4
- import { type WhookAuthenticationData } from '@whook/authorization';
3
+ import { type WhookAuthCookiesService } from '../services/authCookies.js';
4
+ import {
5
+ type WhookOAuth2ReadClientGrantsService,
6
+ type WhookOAuth2Options,
7
+ } from '../services/oAuth2Granters.js';
5
8
 
6
9
  describe('postAuthLogin', () => {
10
+ const OAUTH2: WhookOAuth2Options = {
11
+ authenticateURL: 'https://auth.example.com/sign_in',
12
+ allowedScopes: ['user', 'admin'],
13
+ rootClientId: 'root_app_id',
14
+ };
15
+ const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
16
+ const authCookies = {
17
+ build: jest.fn<WhookAuthCookiesService['build']>(),
18
+ };
19
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
+ const postOAuth2Token = jest.fn<any>();
21
+
22
+ beforeEach(() => {
23
+ readClientGrants.mockReset();
24
+ postOAuth2Token.mockReset();
25
+ authCookies.build.mockReset();
26
+ readClientGrants.mockResolvedValue({
27
+ allowedScopes: ['user', 'admin'],
28
+ allowedRedirectURIS: ['https://www.example.com'],
29
+ allowedGrantTypes: ['code'],
30
+ authenticationData: {
31
+ clientId: 'root_app_id',
32
+ scopes: ['user', 'admin'],
33
+ userId: 'user_id',
34
+ },
35
+ isPublicClient: true,
36
+ });
37
+ });
38
+
7
39
  test('should work', async () => {
8
- const authCookies = {
9
- build: jest.fn<AuthCookiesService['build']>(),
10
- };
11
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
- const postOAuth2Token = jest.fn<any>();
13
40
  const postAuthLogin = await initPostAuthLogin({
14
- ROOT_AUTHENTICATION_DATA: {
15
- scope: 'user',
16
- applicationId: 'root_app_id',
17
- } as WhookAuthenticationData,
41
+ OAUTH2,
42
+ readClientGrants,
18
43
  authCookies,
19
44
  postOAuth2Token,
20
45
  });
@@ -44,51 +69,55 @@ describe('postAuthLogin', () => {
44
69
  authCookiesBuildCalls: authCookies.build.mock.calls,
45
70
  postOAuth2TokenCalls: postOAuth2Token.mock.calls,
46
71
  }).toMatchInlineSnapshot(`
47
- {
48
- "authCookiesBuildCalls": [
49
- [
50
- {
51
- "access_token": "an_access_token",
52
- "expiration_date": "2020-02-02T20:22:02Z",
53
- "expires_in": 3600,
54
- "token_type": "bearer",
55
- },
56
- {
57
- "session": true,
58
- },
59
- ],
60
- ],
61
- "postOAuth2TokenCalls": [
62
- [
63
- {
64
- "authenticationData": {
65
- "applicationId": "root_app_id",
66
- "scope": "user",
67
- },
68
- "body": {
69
- "grant_type": "password",
70
- "password": "a_password",
71
- "scope": "user",
72
- "username": "a_username",
73
- },
74
- },
75
- ],
76
- ],
77
- "response": {
78
- "body": {
79
- "access_token": "an_access_token",
80
- "expiration_date": "2020-02-02T20:22:02Z",
81
- "expires_in": 3600,
82
- "token_type": "bearer",
83
- },
84
- "headers": {
85
- "Set-Cookie": [
86
- "the_build_cookies",
87
- ],
88
- },
89
- "status": 200,
90
- },
91
- }
72
+ {
73
+ "authCookiesBuildCalls": [
74
+ [
75
+ {
76
+ "access_token": "an_access_token",
77
+ "expiration_date": "2020-02-02T20:22:02Z",
78
+ "expires_in": 3600,
79
+ "token_type": "bearer",
80
+ },
81
+ {
82
+ "session": true,
83
+ },
84
+ ],
85
+ ],
86
+ "postOAuth2TokenCalls": [
87
+ [
88
+ {
89
+ "authenticationData": {
90
+ "clientId": "root_app_id",
91
+ "scopes": [
92
+ "user",
93
+ "admin",
94
+ ],
95
+ "userId": "user_id",
96
+ },
97
+ "body": {
98
+ "grant_type": "password",
99
+ "password": "a_password",
100
+ "scope": "user",
101
+ "username": "a_username",
102
+ },
103
+ },
104
+ ],
105
+ ],
106
+ "response": {
107
+ "body": {
108
+ "access_token": "an_access_token",
109
+ "expiration_date": "2020-02-02T20:22:02Z",
110
+ "expires_in": 3600,
111
+ "token_type": "bearer",
112
+ },
113
+ "headers": {
114
+ "Set-Cookie": [
115
+ "the_build_cookies",
116
+ ],
117
+ },
118
+ "status": 200,
119
+ },
120
+ }
92
121
  `);
93
122
  });
94
123
  });
@@ -1,12 +1,17 @@
1
1
  import { autoService, location } from 'knifecycle';
2
+ import {
3
+ type WhookOAuth2ReadClientGrantsService,
4
+ type WhookOAuth2Options,
5
+ } from '../services/oAuth2Granters.js';
2
6
  import { YHTTPError } from 'yhttperror';
3
- import initPostOauth2Token from './postOAuth2Token.js';
7
+ import initPostOAuth2Token from './postOAuth2Token.js';
4
8
  import { AUTH_API_PREFIX } from '../services/authCookies.js';
5
- import { type WhookRouteDefinition } from '@whook/whook';
6
- import {
7
- type AuthCookiesService,
8
- type AuthHandlersConfig,
9
- } from '../services/authCookies.js';
9
+ import { refersTo, type WhookRouteDefinition } from '@whook/whook';
10
+ import { type WhookAuthCookiesService } from '../services/authCookies.js';
11
+ import { scopeSchema } from '../libs/schemas.js';
12
+ import { PASSWORD_GRANT_TYPE } from '../services/oAuth2PasswordGranter.js';
13
+
14
+ export { scopeSchema };
10
15
 
11
16
  export const definition = {
12
17
  method: 'post',
@@ -29,7 +34,7 @@ export const definition = {
29
34
  properties: {
30
35
  username: { type: 'string' },
31
36
  password: { type: 'string' },
32
- scope: { type: 'string' },
37
+ scope: refersTo(scopeSchema),
33
38
  remember: { type: 'boolean' },
34
39
  },
35
40
  },
@@ -46,7 +51,7 @@ export const definition = {
46
51
  properties: {
47
52
  access_token: { type: 'string' },
48
53
  expiration_date: { type: 'string' },
49
- expires_in: { type: 'string' },
54
+ expires_in: { type: 'number' },
50
55
  token_type: { type: 'string' },
51
56
  },
52
57
  },
@@ -58,12 +63,15 @@ export const definition = {
58
63
  } as const satisfies WhookRouteDefinition;
59
64
 
60
65
  async function initPostAuthLogin({
61
- ROOT_AUTHENTICATION_DATA,
66
+ OAUTH2,
67
+ readClientGrants,
62
68
  authCookies,
63
69
  postOAuth2Token,
64
- }: AuthHandlersConfig & {
65
- authCookies: Pick<AuthCookiesService, 'build'>;
66
- postOAuth2Token: Awaited<ReturnType<typeof initPostOauth2Token>>;
70
+ }: {
71
+ OAUTH2: WhookOAuth2Options;
72
+ readClientGrants: WhookOAuth2ReadClientGrantsService;
73
+ authCookies: Pick<WhookAuthCookiesService, 'build'>;
74
+ postOAuth2Token: Awaited<ReturnType<typeof initPostOAuth2Token>>;
67
75
  }) {
68
76
  return async ({
69
77
  body,
@@ -75,15 +83,17 @@ async function initPostAuthLogin({
75
83
  remember: boolean;
76
84
  };
77
85
  }) => {
86
+ const { authenticationData } = await readClientGrants(OAUTH2.rootClientId);
87
+
78
88
  try {
79
89
  const response = await postOAuth2Token({
80
90
  body: {
81
- grant_type: 'password',
91
+ grant_type: PASSWORD_GRANT_TYPE,
82
92
  scope: body.scope,
83
93
  username: body.username,
84
94
  password: body.password,
85
95
  },
86
- authenticationData: ROOT_AUTHENTICATION_DATA,
96
+ authenticationData,
87
97
  });
88
98
 
89
99
  return {
@@ -1,13 +1,31 @@
1
- import { describe, test, jest, expect } from '@jest/globals';
1
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
2
2
  import initPostAuthLogout from './postAuthLogout.js';
3
- import { type AuthCookiesService } from '../services/authCookies.js';
3
+ import { type WhookAuthCookiesService } from '../services/authCookies.js';
4
+ import {
5
+ type WhookOAuth2ReadClientGrantsService,
6
+ type WhookOAuth2Options,
7
+ } from '../services/oAuth2Granters.js';
4
8
 
5
9
  describe('postAuthLogout', () => {
10
+ const OAUTH2: WhookOAuth2Options = {
11
+ authenticateURL: 'https://auth.example.com/sign_in',
12
+ allowedScopes: ['user', 'admin'],
13
+ rootClientId: 'root_app_id',
14
+ };
15
+ const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
16
+ const authCookies = {
17
+ build: jest.fn<WhookAuthCookiesService['build']>(),
18
+ };
19
+
20
+ beforeEach(() => {
21
+ readClientGrants.mockReset();
22
+ authCookies.build.mockReset();
23
+ });
24
+
6
25
  test('should work', async () => {
7
- const authCookies = {
8
- build: jest.fn<AuthCookiesService['build']>(),
9
- };
10
26
  const postAuthLogout = await initPostAuthLogout({
27
+ OAUTH2,
28
+ readClientGrants,
11
29
  authCookies,
12
30
  });
13
31
 
@@ -17,21 +35,27 @@ describe('postAuthLogout', () => {
17
35
 
18
36
  expect({
19
37
  response,
38
+ readClientGrantsCalls: readClientGrants.mock.calls,
20
39
  authCookiesBuildCalls: authCookies.build.mock.calls,
21
40
  }).toMatchInlineSnapshot(`
22
- {
23
- "authCookiesBuildCalls": [
24
- [],
25
- ],
26
- "response": {
27
- "headers": {
28
- "Set-Cookie": [
29
- "the_build_cookies",
30
- ],
31
- },
32
- "status": 204,
33
- },
34
- }
41
+ {
42
+ "authCookiesBuildCalls": [
43
+ [],
44
+ ],
45
+ "readClientGrantsCalls": [
46
+ [
47
+ "root_app_id",
48
+ ],
49
+ ],
50
+ "response": {
51
+ "headers": {
52
+ "Set-Cookie": [
53
+ "the_build_cookies",
54
+ ],
55
+ },
56
+ "status": 204,
57
+ },
58
+ }
35
59
  `);
36
60
  });
37
61
  });
@@ -1,7 +1,11 @@
1
1
  import { autoService, location } from 'knifecycle';
2
2
  import { AUTH_API_PREFIX } from '../services/authCookies.js';
3
3
  import { type WhookRouteDefinition } from '@whook/whook';
4
- import { type AuthCookiesService } from '../services/authCookies.js';
4
+ import { type WhookAuthCookiesService } from '../services/authCookies.js';
5
+ import {
6
+ type WhookOAuth2ReadClientGrantsService,
7
+ type WhookOAuth2Options,
8
+ } from '../services/oAuth2Granters.js';
5
9
 
6
10
  export const definition = {
7
11
  method: 'post',
@@ -23,16 +27,26 @@ export const definition = {
23
27
  } as const satisfies WhookRouteDefinition;
24
28
 
25
29
  async function initPostAuthLogout({
30
+ OAUTH2,
31
+ readClientGrants,
26
32
  authCookies,
27
33
  }: {
28
- authCookies: Pick<AuthCookiesService, 'build'>;
34
+ OAUTH2: WhookOAuth2Options;
35
+ readClientGrants: WhookOAuth2ReadClientGrantsService;
36
+ authCookies: Pick<WhookAuthCookiesService, 'build'>;
29
37
  }) {
30
- return async () => ({
31
- status: 204,
32
- headers: {
33
- 'Set-Cookie': authCookies.build(),
34
- },
35
- });
38
+ return async () => {
39
+ // Used only for the side effect of throwing if
40
+ // the application doesn't exists
41
+ await readClientGrants(OAUTH2.rootClientId);
42
+
43
+ return {
44
+ status: 204,
45
+ headers: {
46
+ 'Set-Cookie': authCookies.build(),
47
+ },
48
+ };
49
+ };
36
50
  }
37
51
 
38
52
  export default location(autoService(initPostAuthLogout), import.meta.url);
@@ -1,25 +1,52 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { describe, test, jest, expect } from '@jest/globals';
2
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
3
3
  import initPostAuthRefresh from './postAuthRefresh.js';
4
- import { type AuthCookiesService } from '../services/authCookies.js';
5
- import { type WhookAuthenticationData } from '@whook/authorization';
4
+ import { type WhookAuthCookiesService } from '../services/authCookies.js';
5
+ import {
6
+ type WhookOAuth2ReadClientGrantsService,
7
+ type WhookOAuth2Options,
8
+ } from '../services/oAuth2Granters.js';
6
9
 
7
10
  describe('postAuthRefresh', () => {
11
+ const OAUTH2: WhookOAuth2Options = {
12
+ authenticateURL: 'https://auth.example.com/sign_in',
13
+ allowedScopes: ['user', 'admin'],
14
+ rootClientId: 'root_app_id',
15
+ };
16
+ const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
17
+ const authCookies = {
18
+ parse: jest.fn<WhookAuthCookiesService['parse']>(),
19
+ build: jest.fn<WhookAuthCookiesService['build']>(),
20
+ };
21
+ const postOAuth2Token = jest.fn<any>();
22
+
23
+ beforeEach(() => {
24
+ readClientGrants.mockReset();
25
+ authCookies.parse.mockReset();
26
+ authCookies.build.mockReset();
27
+ postOAuth2Token.mockReset();
28
+ });
29
+
8
30
  test('should work', async () => {
9
- const authCookies = {
10
- parse: jest.fn<AuthCookiesService['parse']>(),
11
- build: jest.fn<AuthCookiesService['build']>(),
12
- };
13
- const postOAuth2Token = jest.fn<any>();
14
31
  const postAuthRefresh = await initPostAuthRefresh({
15
- ROOT_AUTHENTICATION_DATA: {
16
- scope: 'user',
17
- applicationId: 'root_app_id',
18
- } as WhookAuthenticationData,
32
+ OAUTH2,
19
33
  authCookies,
34
+ readClientGrants,
20
35
  postOAuth2Token,
21
36
  });
22
37
 
38
+ readClientGrants.mockResolvedValue({
39
+ allowedScopes: ['user', 'admin'],
40
+ allowedRedirectURIS: ['https://www.example.com'],
41
+ allowedGrantTypes: ['code'],
42
+ authenticationData: {
43
+ clientId: 'root_app_id',
44
+ scopes: ['user', 'admin'],
45
+ userId: 'user_id',
46
+ },
47
+ isPublicClient: true,
48
+ });
49
+
23
50
  authCookies.parse.mockReturnValueOnce({
24
51
  access_token: 'an_access_token',
25
52
  refresh_token: 'a_refresh_token',
@@ -45,59 +72,69 @@ describe('postAuthRefresh', () => {
45
72
 
46
73
  expect({
47
74
  response,
75
+ readClientGrantsCalls: readClientGrants.mock.calls,
48
76
  authCookiesParseCalls: authCookies.parse.mock.calls,
49
77
  authCookiesBuildCalls: authCookies.build.mock.calls,
50
78
  postOAuth2TokenCalls: postOAuth2Token.mock.calls,
51
79
  }).toMatchInlineSnapshot(`
52
- {
53
- "authCookiesBuildCalls": [
54
- [
55
- {
56
- "access_token": "an_access_token",
57
- "expiration_date": "2020-02-02T20:22:02Z",
58
- "expires_in": 3600,
59
- "token_type": "bearer",
60
- },
61
- {
62
- "session": true,
63
- },
64
- ],
65
- ],
66
- "authCookiesParseCalls": [
67
- [
68
- "a_given_cookie",
69
- ],
70
- ],
71
- "postOAuth2TokenCalls": [
72
- [
73
- {
74
- "authenticationData": {
75
- "applicationId": "root_app_id",
76
- "scope": "user",
77
- },
78
- "body": {
79
- "grant_type": "refresh_token",
80
- "refresh_token": "a_refresh_token",
81
- "scope": "user",
82
- },
83
- },
84
- ],
85
- ],
86
- "response": {
87
- "body": {
88
- "access_token": "an_access_token",
89
- "expiration_date": "2020-02-02T20:22:02Z",
90
- "expires_in": 3600,
91
- "token_type": "bearer",
92
- },
93
- "headers": {
94
- "Set-Cookie": [
95
- "the_build_cookies",
96
- ],
97
- },
98
- "status": 200,
99
- },
100
- }
80
+ {
81
+ "authCookiesBuildCalls": [
82
+ [
83
+ {
84
+ "access_token": "an_access_token",
85
+ "expiration_date": "2020-02-02T20:22:02Z",
86
+ "expires_in": 3600,
87
+ "token_type": "bearer",
88
+ },
89
+ {
90
+ "session": true,
91
+ },
92
+ ],
93
+ ],
94
+ "authCookiesParseCalls": [
95
+ [
96
+ "a_given_cookie",
97
+ ],
98
+ ],
99
+ "postOAuth2TokenCalls": [
100
+ [
101
+ {
102
+ "authenticationData": {
103
+ "clientId": "root_app_id",
104
+ "scopes": [
105
+ "user",
106
+ "admin",
107
+ ],
108
+ "userId": "user_id",
109
+ },
110
+ "body": {
111
+ "grant_type": "refresh_token",
112
+ "refresh_token": "a_refresh_token",
113
+ "scope": "user",
114
+ },
115
+ },
116
+ ],
117
+ ],
118
+ "readClientGrantsCalls": [
119
+ [
120
+ "root_app_id",
121
+ ],
122
+ ],
123
+ "response": {
124
+ "body": {
125
+ "access_token": "an_access_token",
126
+ "expiration_date": "2020-02-02T20:22:02Z",
127
+ "expires_in": 3600,
128
+ "token_type": "bearer",
129
+ },
130
+ "headers": {
131
+ "Set-Cookie": [
132
+ "the_build_cookies",
133
+ ],
134
+ },
135
+ "status": 200,
136
+ },
137
+ }
101
138
  `);
102
139
  });
103
140
  });
@@ -1,16 +1,21 @@
1
1
  import { autoService, location } from 'knifecycle';
2
2
  import { YHTTPError } from 'yhttperror';
3
- import initPostOauth2Token from './postOAuth2Token.js';
3
+ import initPostOAuth2Token from './postOAuth2Token.js';
4
4
  import { AUTH_API_PREFIX } from '../services/authCookies.js';
5
5
  import {
6
6
  refersTo,
7
7
  type WhookRouteDefinition,
8
8
  type WhookAPIParameterDefinition,
9
9
  } from '@whook/whook';
10
+ import { type WhookAuthCookiesService } from '../services/authCookies.js';
10
11
  import {
11
- type AuthCookiesService,
12
- type AuthHandlersConfig,
13
- } from '../services/authCookies.js';
12
+ type WhookOAuth2ReadClientGrantsService,
13
+ type WhookOAuth2Options,
14
+ } from '../services/oAuth2Granters.js';
15
+ import { scopeSchema } from '../libs/schemas.js';
16
+ import { REFRESH_TOKEN_GRANT_TYPE } from '../services/oAuth2RefreshTokenGranter.js';
17
+
18
+ export { scopeSchema };
14
19
 
15
20
  export const authCookieHeaderParameter = {
16
21
  name: 'cookie',
@@ -44,7 +49,7 @@ export const definition = {
44
49
  type: 'object',
45
50
  required: [],
46
51
  properties: {
47
- scope: { type: 'string' },
52
+ scope: refersTo(scopeSchema),
48
53
  remember: { type: 'boolean' },
49
54
  },
50
55
  },
@@ -61,7 +66,7 @@ export const definition = {
61
66
  properties: {
62
67
  access_token: { type: 'string' },
63
68
  expiration_date: { type: 'string' },
64
- expires_in: { type: 'string' },
69
+ expires_in: { type: 'number' },
65
70
  token_type: { type: 'string' },
66
71
  },
67
72
  },
@@ -75,12 +80,15 @@ export const definition = {
75
80
  export default location(autoService(initPostAuthRefresh), import.meta.url);
76
81
 
77
82
  async function initPostAuthRefresh({
78
- ROOT_AUTHENTICATION_DATA,
83
+ OAUTH2,
79
84
  authCookies,
85
+ readClientGrants,
80
86
  postOAuth2Token,
81
- }: AuthHandlersConfig & {
82
- authCookies: AuthCookiesService;
83
- postOAuth2Token: Awaited<ReturnType<typeof initPostOauth2Token>>;
87
+ }: {
88
+ OAUTH2: WhookOAuth2Options;
89
+ authCookies: WhookAuthCookiesService;
90
+ readClientGrants: WhookOAuth2ReadClientGrantsService;
91
+ postOAuth2Token: Awaited<ReturnType<typeof initPostOAuth2Token>>;
84
92
  }) {
85
93
  return async ({
86
94
  body,
@@ -98,16 +106,20 @@ async function initPostAuthRefresh({
98
106
 
99
107
  try {
100
108
  if (!parsedCookies.refresh_token) {
101
- throw new YHTTPError(401, 'E_REFRESH_COOKIE', [cookie]);
109
+ throw new YHTTPError(401, 'E_AUTH_REFRESH_COOKIE', [cookie]);
102
110
  }
103
111
 
112
+ const { authenticationData } = await readClientGrants(
113
+ OAUTH2.rootClientId,
114
+ );
115
+
104
116
  const response = await postOAuth2Token({
105
117
  body: {
106
- grant_type: 'refresh_token',
118
+ grant_type: REFRESH_TOKEN_GRANT_TYPE,
107
119
  scope: body.scope,
108
120
  refresh_token: parsedCookies.refresh_token,
109
121
  },
110
- authenticationData: ROOT_AUTHENTICATION_DATA,
122
+ authenticationData,
111
123
  });
112
124
 
113
125
  return {