@whook/oauth2 25.2.1 → 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 +15 -8
  40. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  41. package/dist/routes/getOAuth2WellKnownMetadata.test.js +37 -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 +11 -10
  62. package/dist/routes/postOAuth2Acknowledge.js +43 -37
  63. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  64. package/dist/routes/postOAuth2Acknowledge.test.js +135 -70
  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 +57 -60
  129. package/src/routes/getOAuth2WellKnownMetadata.ts +21 -10
  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 +167 -108
  138. package/src/routes/postOAuth2Acknowledge.ts +90 -64
  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
@@ -0,0 +1,179 @@
1
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
2
+ import { YError } from 'yerror';
3
+ import initPostOAuth2Revoke from './postOAuth2Revoke.js';
4
+ import { type WhookOAuth2RevokeTokenService } from './postOAuth2Revoke.js';
5
+ import { type LogService } from 'common-services';
6
+
7
+ describe('postOAuth2Revoke', () => {
8
+ const oAuth2RevokeToken = {
9
+ revoke: jest.fn<WhookOAuth2RevokeTokenService['revoke']>(),
10
+ };
11
+ const log = jest.fn<LogService>();
12
+
13
+ beforeEach(() => {
14
+ oAuth2RevokeToken.revoke.mockReset();
15
+ log.mockReset();
16
+ });
17
+
18
+ test('should revoke a token', async () => {
19
+ oAuth2RevokeToken.revoke.mockResolvedValueOnce();
20
+
21
+ const postOAuth2Revoke = await initPostOAuth2Revoke({
22
+ oAuth2RevokeToken,
23
+ log,
24
+ });
25
+ const response = await postOAuth2Revoke({
26
+ body: {
27
+ token: 'a_token',
28
+ token_type_hint: 'refresh_token',
29
+ },
30
+ authenticationData: {
31
+ clientId: 'a_client',
32
+ userId: 'a_user',
33
+ scopes: ['oauth'],
34
+ },
35
+ });
36
+
37
+ expect({
38
+ response,
39
+ oAuth2RevokeTokenRevokeCalls: oAuth2RevokeToken.revoke.mock.calls,
40
+ logCalls: log.mock.calls,
41
+ }).toMatchInlineSnapshot(`
42
+ {
43
+ "logCalls": [],
44
+ "oAuth2RevokeTokenRevokeCalls": [
45
+ [
46
+ "refresh_token",
47
+ "a_token",
48
+ {
49
+ "clientId": "a_client",
50
+ "scopes": [
51
+ "oauth",
52
+ ],
53
+ "userId": "a_user",
54
+ },
55
+ ],
56
+ ],
57
+ "response": {
58
+ "status": 200,
59
+ },
60
+ }
61
+ `);
62
+ });
63
+
64
+ test('should revoke a token with no type provided', async () => {
65
+ oAuth2RevokeToken.revoke.mockResolvedValueOnce();
66
+
67
+ const postOAuth2Revoke = await initPostOAuth2Revoke({
68
+ oAuth2RevokeToken,
69
+ log,
70
+ });
71
+ const response = await postOAuth2Revoke({
72
+ body: {
73
+ token: 'a_token',
74
+ },
75
+ authenticationData: {
76
+ clientId: 'a_client',
77
+ userId: 'a_user',
78
+ scopes: ['oauth'],
79
+ },
80
+ });
81
+
82
+ expect({
83
+ response,
84
+ oAuth2RevokeTokenRevokeCalls: oAuth2RevokeToken.revoke.mock.calls,
85
+ logCalls: log.mock.calls,
86
+ }).toMatchInlineSnapshot(`
87
+ {
88
+ "logCalls": [],
89
+ "oAuth2RevokeTokenRevokeCalls": [
90
+ [
91
+ "access_token",
92
+ "a_token",
93
+ {
94
+ "clientId": "a_client",
95
+ "scopes": [
96
+ "oauth",
97
+ ],
98
+ "userId": "a_user",
99
+ },
100
+ ],
101
+ [
102
+ "refresh_token",
103
+ "a_token",
104
+ {
105
+ "clientId": "a_client",
106
+ "scopes": [
107
+ "oauth",
108
+ ],
109
+ "userId": "a_user",
110
+ },
111
+ ],
112
+ ],
113
+ "response": {
114
+ "status": 200,
115
+ },
116
+ }
117
+ `);
118
+ });
119
+
120
+ test('should ignore already invalid tokens', async () => {
121
+ oAuth2RevokeToken.revoke.mockRejectedValueOnce(new YError('E_BAD_TOKEN'));
122
+
123
+ const postOAuth2Revoke = await initPostOAuth2Revoke({
124
+ oAuth2RevokeToken,
125
+ log,
126
+ });
127
+ const response = await postOAuth2Revoke({
128
+ body: {
129
+ token: 'already_invalid',
130
+ },
131
+ });
132
+
133
+ expect({
134
+ response,
135
+ oAuth2RevokeTokenRevokeCalls: oAuth2RevokeToken.revoke.mock.calls,
136
+ logCalls: log.mock.calls,
137
+ }).toMatchInlineSnapshot(`
138
+ {
139
+ "logCalls": [],
140
+ "oAuth2RevokeTokenRevokeCalls": [
141
+ [
142
+ "access_token",
143
+ "already_invalid",
144
+ undefined,
145
+ ],
146
+ [
147
+ "refresh_token",
148
+ "already_invalid",
149
+ undefined,
150
+ ],
151
+ ],
152
+ "response": {
153
+ "status": 200,
154
+ },
155
+ }
156
+ `);
157
+ });
158
+
159
+ test('should bubble up unexpected errors', async () => {
160
+ oAuth2RevokeToken.revoke.mockRejectedValueOnce(
161
+ new YError('E_OAUTH2_UNEXPECTED_ERROR'),
162
+ );
163
+ const postOAuth2Revoke = await initPostOAuth2Revoke({
164
+ oAuth2RevokeToken,
165
+ log,
166
+ });
167
+
168
+ await expect(
169
+ postOAuth2Revoke({
170
+ body: {
171
+ token: 'a_token',
172
+ },
173
+ }),
174
+ ).rejects.toMatchObject({
175
+ code: 'E_OAUTH2_UNEXPECTED_ERROR',
176
+ });
177
+ expect(log.mock.calls.length).toBe(2);
178
+ });
179
+ });
@@ -0,0 +1,156 @@
1
+ import { autoService, location } from 'knifecycle';
2
+ import {
3
+ noop,
4
+ refersTo,
5
+ type WhookRouteDefinition,
6
+ type WhookAPISchemaDefinition,
7
+ } from '@whook/whook';
8
+ import { type LogService } from 'common-services';
9
+ import { pickYErrorWithCode, YError, printStackTrace } from 'yerror';
10
+ import { type WhookAuthenticationData } from '@whook/authorization';
11
+
12
+ export const REVOCATION_TOKEN_TYPE_HINT = [
13
+ 'access_token',
14
+ 'refresh_token',
15
+ ] as const;
16
+
17
+ export type WhookOAuth2RevocationTokenTypeHint =
18
+ (typeof REVOCATION_TOKEN_TYPE_HINT)[number];
19
+
20
+ /**
21
+ * A service to revoke tokens. Must check that the
22
+ * token is owned by the client when authentication
23
+ * data are provided.
24
+ */
25
+ export interface WhookOAuth2RevokeTokenService {
26
+ revoke: (
27
+ tokenTypeHint: WhookOAuth2RevocationTokenTypeHint,
28
+ token: string,
29
+ optionalAuthenticationData?: WhookAuthenticationData,
30
+ ) => Promise<void>;
31
+ }
32
+
33
+ export const tokenTypeHintSchema = {
34
+ name: 'TokenTypeHint',
35
+ schema: {
36
+ description:
37
+ 'OAuth2 revocation token type hint as defined in RFC7009 section 2.1.',
38
+ type: 'string',
39
+ enum: REVOCATION_TOKEN_TYPE_HINT.concat(),
40
+ },
41
+ } as const satisfies WhookAPISchemaDefinition;
42
+
43
+ export const revokeTokenRequestBodySchema = {
44
+ name: 'RevokeTokenRequestBody',
45
+ schema: {
46
+ type: 'object',
47
+ description: 'Token revocation request as defined in RFC7009 section 2.1.',
48
+ required: ['token'],
49
+ properties: {
50
+ token: {
51
+ type: 'string',
52
+ },
53
+ token_type_hint: refersTo(tokenTypeHintSchema),
54
+ },
55
+ },
56
+ } as const satisfies WhookAPISchemaDefinition;
57
+
58
+ export const definition = {
59
+ method: 'post',
60
+ path: '/oauth2/revoke',
61
+ config: {
62
+ environments: [],
63
+ },
64
+ operation: {
65
+ operationId: 'postOAuth2Revoke',
66
+ summary: `Implements the [Token Revocation Endpoint](https://datatracker.ietf.org/doc/html/rfc7009#section-2)
67
+ as defined per RFC7009.`,
68
+ tags: ['oauth2'],
69
+ parameters: [],
70
+ requestBody: {
71
+ required: true,
72
+ content: {
73
+ 'application/x-www-form-urlencoded': {
74
+ schema: refersTo(revokeTokenRequestBodySchema),
75
+ },
76
+ 'application/json': {
77
+ schema: refersTo(revokeTokenRequestBodySchema),
78
+ },
79
+ },
80
+ },
81
+ responses: {
82
+ '200': {
83
+ description:
84
+ 'Token successfully revoked or unknown token (RFC7009 section 2.2).',
85
+ },
86
+ '400': {
87
+ description: 'Token revocation error response.',
88
+ content: {
89
+ 'application/json': {
90
+ schema: {
91
+ type: 'object',
92
+ required: ['error'],
93
+ properties: {
94
+ error: {
95
+ type: 'string',
96
+ enum: ['invalid_request', 'unsupported_token_type'],
97
+ },
98
+ error_description: { type: 'string' },
99
+ error_uri: { type: 'string' },
100
+ },
101
+ },
102
+ },
103
+ },
104
+ },
105
+ },
106
+ },
107
+ } as const satisfies WhookRouteDefinition;
108
+
109
+ async function initPostOAuth2Revoke({
110
+ oAuth2RevokeToken,
111
+ log = noop,
112
+ }: {
113
+ oAuth2RevokeToken: WhookOAuth2RevokeTokenService;
114
+ log?: LogService;
115
+ }) {
116
+ return async ({
117
+ body: { token, token_type_hint: tokenTypeHint },
118
+ authenticationData: optionalAuthenticationData,
119
+ }: {
120
+ body: {
121
+ token: string;
122
+ token_type_hint?: WhookOAuth2RevocationTokenTypeHint;
123
+ };
124
+ authenticationData?: WhookAuthenticationData;
125
+ }) => {
126
+ for (const currentTokenTypeHint of tokenTypeHint
127
+ ? [tokenTypeHint]
128
+ : REVOCATION_TOKEN_TYPE_HINT) {
129
+ try {
130
+ await oAuth2RevokeToken.revoke(
131
+ currentTokenTypeHint,
132
+ token,
133
+ optionalAuthenticationData,
134
+ );
135
+ } catch (err) {
136
+ const castedErr = pickYErrorWithCode(err as Error, 'E_BAD_TOKEN');
137
+
138
+ if (!castedErr) {
139
+ log(
140
+ 'debug',
141
+ '👫 - OAuth2 token revocation error',
142
+ (err as YError).code,
143
+ );
144
+ log('debug-stack', printStackTrace(err));
145
+ throw err;
146
+ }
147
+ }
148
+ }
149
+
150
+ return {
151
+ status: 200,
152
+ };
153
+ };
154
+ }
155
+
156
+ export default location(autoService(initPostOAuth2Revoke), import.meta.url);