@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
@@ -0,0 +1,102 @@
1
+ import { autoService, location } from 'knifecycle';
2
+ import { noop, refersTo, } from '@whook/whook';
3
+ import { pickYErrorWithCode, printStackTrace } from 'yerror';
4
+ export const REVOCATION_TOKEN_TYPE_HINT = [
5
+ 'access_token',
6
+ 'refresh_token',
7
+ ];
8
+ export const tokenTypeHintSchema = {
9
+ name: 'TokenTypeHint',
10
+ schema: {
11
+ description: 'OAuth2 revocation token type hint as defined in RFC7009 section 2.1.',
12
+ type: 'string',
13
+ enum: REVOCATION_TOKEN_TYPE_HINT.concat(),
14
+ },
15
+ };
16
+ export const revokeTokenRequestBodySchema = {
17
+ name: 'RevokeTokenRequestBody',
18
+ schema: {
19
+ type: 'object',
20
+ description: 'Token revocation request as defined in RFC7009 section 2.1.',
21
+ required: ['token'],
22
+ properties: {
23
+ token: {
24
+ type: 'string',
25
+ },
26
+ token_type_hint: refersTo(tokenTypeHintSchema),
27
+ },
28
+ },
29
+ };
30
+ export const definition = {
31
+ method: 'post',
32
+ path: '/oauth2/revoke',
33
+ config: {
34
+ environments: [],
35
+ },
36
+ operation: {
37
+ operationId: 'postOAuth2Revoke',
38
+ summary: `Implements the [Token Revocation Endpoint](https://datatracker.ietf.org/doc/html/rfc7009#section-2)
39
+ as defined per RFC7009.`,
40
+ tags: ['oauth2'],
41
+ parameters: [],
42
+ requestBody: {
43
+ required: true,
44
+ content: {
45
+ 'application/x-www-form-urlencoded': {
46
+ schema: refersTo(revokeTokenRequestBodySchema),
47
+ },
48
+ 'application/json': {
49
+ schema: refersTo(revokeTokenRequestBodySchema),
50
+ },
51
+ },
52
+ },
53
+ responses: {
54
+ '200': {
55
+ description: 'Token successfully revoked or unknown token (RFC7009 section 2.2).',
56
+ },
57
+ '400': {
58
+ description: 'Token revocation error response.',
59
+ content: {
60
+ 'application/json': {
61
+ schema: {
62
+ type: 'object',
63
+ required: ['error'],
64
+ properties: {
65
+ error: {
66
+ type: 'string',
67
+ enum: ['invalid_request', 'unsupported_token_type'],
68
+ },
69
+ error_description: { type: 'string' },
70
+ error_uri: { type: 'string' },
71
+ },
72
+ },
73
+ },
74
+ },
75
+ },
76
+ },
77
+ },
78
+ };
79
+ async function initPostOAuth2Revoke({ oAuth2RevokeToken, log = noop, }) {
80
+ return async ({ body: { token, token_type_hint: tokenTypeHint }, authenticationData: optionalAuthenticationData, }) => {
81
+ for (const currentTokenTypeHint of tokenTypeHint
82
+ ? [tokenTypeHint]
83
+ : REVOCATION_TOKEN_TYPE_HINT) {
84
+ try {
85
+ await oAuth2RevokeToken.revoke(currentTokenTypeHint, token, optionalAuthenticationData);
86
+ }
87
+ catch (err) {
88
+ const castedErr = pickYErrorWithCode(err, 'E_BAD_TOKEN');
89
+ if (!castedErr) {
90
+ log('debug', '👫 - OAuth2 token revocation error', err.code);
91
+ log('debug-stack', printStackTrace(err));
92
+ throw err;
93
+ }
94
+ }
95
+ }
96
+ return {
97
+ status: 200,
98
+ };
99
+ };
100
+ }
101
+ export default location(autoService(initPostOAuth2Revoke), import.meta.url);
102
+ //# sourceMappingURL=postOAuth2Revoke.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postOAuth2Revoke.js","sourceRoot":"","sources":["../../src/routes/postOAuth2Revoke.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EACL,IAAI,EACJ,QAAQ,GAGT,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,kBAAkB,EAAU,eAAe,EAAE,MAAM,QAAQ,CAAC;AAGrE,MAAM,CAAC,MAAM,0BAA0B,GAAG;IACxC,cAAc;IACd,eAAe;CACP,CAAC;AAkBX,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,IAAI,EAAE,eAAe;IACrB,MAAM,EAAE;QACN,WAAW,EACT,sEAAsE;QACxE,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,0BAA0B,CAAC,MAAM,EAAE;KAC1C;CAC0C,CAAC;AAE9C,MAAM,CAAC,MAAM,4BAA4B,GAAG;IAC1C,IAAI,EAAE,wBAAwB;IAC9B,MAAM,EAAE;QACN,IAAI,EAAE,QAAQ;QACd,WAAW,EAAE,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GAAG,IAAI,GAIX;IACC,OAAO,KAAK,EAAE,EACZ,IAAI,EAAE,EAAE,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,EAC/C,kBAAkB,EAAE,0BAA0B,GAO/C,EAAE,EAAE;QACH,KAAK,MAAM,oBAAoB,IAAI,aAAa;YAC9C,CAAC,CAAC,CAAC,aAAa,CAAC;YACjB,CAAC,CAAC,0BAA0B,EAAE,CAAC;YAC/B,IAAI,CAAC;gBACH,MAAM,iBAAiB,CAAC,MAAM,CAC5B,oBAAoB,EACpB,KAAK,EACL,0BAA0B,CAC3B,CAAC;YACJ,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,MAAM,SAAS,GAAG,kBAAkB,CAAC,GAAY,EAAE,aAAa,CAAC,CAAC;gBAElE,IAAI,CAAC,SAAS,EAAE,CAAC;oBACf,GAAG,CACD,OAAO,EACP,oCAAoC,EACnC,GAAc,CAAC,IAAI,CACrB,CAAC;oBACF,GAAG,CAAC,aAAa,EAAE,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC;oBACzC,MAAM,GAAG,CAAC;gBACZ,CAAC;YACH,CAAC;QACH,CAAC;QAED,OAAO;YACL,MAAM,EAAE,GAAG;SACZ,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAED,eAAe,QAAQ,CAAC,WAAW,CAAC,oBAAoB,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,161 @@
1
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
2
+ import { YError } from 'yerror';
3
+ import initPostOAuth2Revoke from './postOAuth2Revoke.js';
4
+ describe('postOAuth2Revoke', () => {
5
+ const oAuth2RevokeToken = {
6
+ revoke: jest.fn(),
7
+ };
8
+ const log = jest.fn();
9
+ beforeEach(() => {
10
+ oAuth2RevokeToken.revoke.mockReset();
11
+ log.mockReset();
12
+ });
13
+ test('should revoke a token', async () => {
14
+ oAuth2RevokeToken.revoke.mockResolvedValueOnce();
15
+ const postOAuth2Revoke = await initPostOAuth2Revoke({
16
+ oAuth2RevokeToken,
17
+ log,
18
+ });
19
+ const response = await postOAuth2Revoke({
20
+ body: {
21
+ token: 'a_token',
22
+ token_type_hint: 'refresh_token',
23
+ },
24
+ authenticationData: {
25
+ clientId: 'a_client',
26
+ userId: 'a_user',
27
+ scopes: ['oauth'],
28
+ },
29
+ });
30
+ expect({
31
+ response,
32
+ oAuth2RevokeTokenRevokeCalls: oAuth2RevokeToken.revoke.mock.calls,
33
+ logCalls: log.mock.calls,
34
+ }).toMatchInlineSnapshot(`
35
+ {
36
+ "logCalls": [],
37
+ "oAuth2RevokeTokenRevokeCalls": [
38
+ [
39
+ "refresh_token",
40
+ "a_token",
41
+ {
42
+ "clientId": "a_client",
43
+ "scopes": [
44
+ "oauth",
45
+ ],
46
+ "userId": "a_user",
47
+ },
48
+ ],
49
+ ],
50
+ "response": {
51
+ "status": 200,
52
+ },
53
+ }
54
+ `);
55
+ });
56
+ test('should revoke a token with no type provided', async () => {
57
+ oAuth2RevokeToken.revoke.mockResolvedValueOnce();
58
+ const postOAuth2Revoke = await initPostOAuth2Revoke({
59
+ oAuth2RevokeToken,
60
+ log,
61
+ });
62
+ const response = await postOAuth2Revoke({
63
+ body: {
64
+ token: 'a_token',
65
+ },
66
+ authenticationData: {
67
+ clientId: 'a_client',
68
+ userId: 'a_user',
69
+ scopes: ['oauth'],
70
+ },
71
+ });
72
+ expect({
73
+ response,
74
+ oAuth2RevokeTokenRevokeCalls: oAuth2RevokeToken.revoke.mock.calls,
75
+ logCalls: log.mock.calls,
76
+ }).toMatchInlineSnapshot(`
77
+ {
78
+ "logCalls": [],
79
+ "oAuth2RevokeTokenRevokeCalls": [
80
+ [
81
+ "access_token",
82
+ "a_token",
83
+ {
84
+ "clientId": "a_client",
85
+ "scopes": [
86
+ "oauth",
87
+ ],
88
+ "userId": "a_user",
89
+ },
90
+ ],
91
+ [
92
+ "refresh_token",
93
+ "a_token",
94
+ {
95
+ "clientId": "a_client",
96
+ "scopes": [
97
+ "oauth",
98
+ ],
99
+ "userId": "a_user",
100
+ },
101
+ ],
102
+ ],
103
+ "response": {
104
+ "status": 200,
105
+ },
106
+ }
107
+ `);
108
+ });
109
+ test('should ignore already invalid tokens', async () => {
110
+ oAuth2RevokeToken.revoke.mockRejectedValueOnce(new YError('E_BAD_TOKEN'));
111
+ const postOAuth2Revoke = await initPostOAuth2Revoke({
112
+ oAuth2RevokeToken,
113
+ log,
114
+ });
115
+ const response = await postOAuth2Revoke({
116
+ body: {
117
+ token: 'already_invalid',
118
+ },
119
+ });
120
+ expect({
121
+ response,
122
+ oAuth2RevokeTokenRevokeCalls: oAuth2RevokeToken.revoke.mock.calls,
123
+ logCalls: log.mock.calls,
124
+ }).toMatchInlineSnapshot(`
125
+ {
126
+ "logCalls": [],
127
+ "oAuth2RevokeTokenRevokeCalls": [
128
+ [
129
+ "access_token",
130
+ "already_invalid",
131
+ undefined,
132
+ ],
133
+ [
134
+ "refresh_token",
135
+ "already_invalid",
136
+ undefined,
137
+ ],
138
+ ],
139
+ "response": {
140
+ "status": 200,
141
+ },
142
+ }
143
+ `);
144
+ });
145
+ test('should bubble up unexpected errors', async () => {
146
+ oAuth2RevokeToken.revoke.mockRejectedValueOnce(new YError('E_OAUTH2_UNEXPECTED_ERROR'));
147
+ const postOAuth2Revoke = await initPostOAuth2Revoke({
148
+ oAuth2RevokeToken,
149
+ log,
150
+ });
151
+ await expect(postOAuth2Revoke({
152
+ body: {
153
+ token: 'a_token',
154
+ },
155
+ })).rejects.toMatchObject({
156
+ code: 'E_OAUTH2_UNEXPECTED_ERROR',
157
+ });
158
+ expect(log.mock.calls.length).toBe(2);
159
+ });
160
+ });
161
+ //# sourceMappingURL=postOAuth2Revoke.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postOAuth2Revoke.test.js","sourceRoot":"","sources":["../../src/routes/postOAuth2Revoke.test.ts"],"names":[],"mappings":"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IAEH,IAAI,CAAC,oCAAoC,EAAE,KAAK,IAAI,EAAE;QACpD,iBAAiB,CAAC,MAAM,CAAC,qBAAqB,CAC5C,IAAI,MAAM,CAAC,2BAA2B,CAAC,CACxC,CAAC;QACF,MAAM,gBAAgB,GAAG,MAAM,oBAAoB,CAAC;YAClD,iBAAiB;YACjB,GAAG;SACJ,CAAC,CAAC;QAEH,MAAM,MAAM,CACV,gBAAgB,CAAC;YACf,IAAI,EAAE;gBACJ,KAAK,EAAE,SAAS;aACjB;SACF,CAAC,CACH,CAAC,OAAO,CAAC,aAAa,CAAC;YACtB,IAAI,EAAE,2BAA2B;SAClC,CAAC,CAAC;QACH,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -1,104 +1,13 @@
1
1
  import { type LogService, type TimeService } from 'common-services';
2
- import { type OAuth2GranterService, type OAuth2RefreshTokenService, type OAuth2AccessTokenService } from '../services/oAuth2Granters.js';
2
+ import { type WhookOAuth2GranterDefinitions, type WhookOAuth2GranterService, type WhookOAuth2Options } from '../services/oAuth2Granters.js';
3
3
  import { type WhookAuthenticationData } from '@whook/authorization';
4
- export declare const codeVerifierSchema: {
5
- readonly name: "CodeVerifier";
6
- readonly schema: {
7
- readonly type: "string";
8
- readonly minLength: 43;
9
- readonly maxLength: 128;
10
- readonly pattern: "^[A-Za-z0-9._~-]+$";
11
- };
12
- };
13
- export declare const authorizationCodeTokenRequestBodySchema: {
14
- readonly name: "AuthorizationCodeRequestBody";
15
- readonly schema: {
16
- readonly type: "object";
17
- readonly description: "Authorization code grant, see https://tools.ietf.org/html/rfc6749#section-4.1";
18
- readonly required: ["grant_type"];
19
- readonly properties: {
20
- readonly grant_type: {
21
- readonly type: "string";
22
- readonly enum: ["authorization_code"];
23
- };
24
- readonly code: {
25
- readonly type: "string";
26
- };
27
- readonly client_id: {
28
- readonly type: "string";
29
- };
30
- readonly redirect_uri: {
31
- readonly type: "string";
32
- readonly pattern: "^https?://";
33
- readonly format: "uri";
34
- };
35
- readonly code_verifier: import("ya-json-schema-types").ExpressiveJSONSchema;
36
- };
37
- };
38
- };
39
- export declare const passwordTokenRequestBodySchema: {
40
- readonly name: "PasswordRequestBody";
41
- readonly schema: {
42
- readonly type: "object";
43
- readonly description: "Resource owner password credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.3";
44
- readonly required: ["grant_type", "username", "password"];
45
- readonly properties: {
46
- readonly grant_type: {
47
- readonly type: "string";
48
- readonly enum: ["password"];
49
- };
50
- readonly username: {
51
- readonly type: "string";
52
- };
53
- readonly password: {
54
- readonly type: "string";
55
- };
56
- readonly scope: {
57
- readonly type: "string";
58
- readonly description: "See https://tools.ietf.org/html/rfc6749#section-3.3";
59
- };
60
- };
61
- };
62
- };
63
- export declare const clientCredentialsTokenRequestBodySchema: {
64
- readonly name: "ClientCredentialsRequestBody";
65
- readonly schema: {
66
- readonly type: "object";
67
- readonly description: "Client credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.4";
68
- readonly required: ["grant_type"];
69
- readonly properties: {
70
- readonly grant_type: {
71
- readonly type: "string";
72
- readonly enum: ["client_credentials"];
73
- };
74
- readonly scope: {
75
- readonly type: "string";
76
- readonly description: "See https://tools.ietf.org/html/rfc6749#section-3.3";
77
- };
78
- };
79
- };
80
- };
81
- export declare const refreshTokenRequestBodySchema: {
82
- readonly name: "RefreshTokenRequestBody";
83
- readonly schema: {
84
- readonly type: "object";
85
- readonly description: "Token refresh grant type, see https://tools.ietf.org/html/rfc6749#section-6 .";
86
- readonly required: ["grant_type", "refresh_token"];
87
- readonly properties: {
88
- readonly grant_type: {
89
- readonly type: "string";
90
- readonly enum: ["refresh_token"];
91
- };
92
- readonly refresh_token: {
93
- readonly type: "string";
94
- };
95
- readonly scope: {
96
- readonly type: "string";
97
- readonly description: "See https://tools.ietf.org/html/rfc6749#section-3.3";
98
- };
99
- };
100
- };
101
- };
4
+ import { type WhookOAuth2AccessTokenService } from '../services/oAuth2ImplicitGranter.js';
5
+ import { refreshTokenRequestBodySchema, type WhookOAuth2RefreshTokenService } from '../services/oAuth2RefreshTokenGranter.js';
6
+ import { authorizationCodeTokenRequestBodySchema, codeVerifierSchema } from '../services/oAuth2AuthorizationCodeGranter.js';
7
+ import { passwordTokenRequestBodySchema } from '../services/oAuth2PasswordGranter.js';
8
+ import { clientCredentialsTokenRequestBodySchema } from '../services/oAuth2ClientCredentialsGranter.js';
9
+ import { scopeSchema } from '../libs/schemas.js';
10
+ export { passwordTokenRequestBodySchema, refreshTokenRequestBodySchema, clientCredentialsTokenRequestBodySchema, authorizationCodeTokenRequestBodySchema, codeVerifierSchema, scopeSchema, };
102
11
  export declare const tokenBodySchema: {
103
12
  readonly name: "TokenRequestBody";
104
13
  readonly schema: {
@@ -127,6 +36,18 @@ export declare const definition: {
127
36
  readonly responses: {
128
37
  readonly '200': {
129
38
  readonly description: "Token successfully issued, see https://tools.ietf.org/html/rfc6749#section-5 .";
39
+ readonly headers: {
40
+ readonly 'cache-control': {
41
+ readonly schema: {
42
+ readonly type: "string";
43
+ };
44
+ };
45
+ readonly pragma: {
46
+ readonly schema: {
47
+ readonly type: "string";
48
+ };
49
+ };
50
+ };
130
51
  readonly content: {
131
52
  readonly 'application/json': {
132
53
  readonly schema: {
@@ -145,10 +66,23 @@ export declare const definition: {
145
66
  readonly description: "The lifetime in seconds of the access token";
146
67
  readonly type: "number";
147
68
  };
69
+ readonly expiration_date: {
70
+ readonly type: "string";
71
+ readonly format: "date-time";
72
+ };
148
73
  readonly refresh_token: {
149
74
  readonly description: "See https://tools.ietf.org/html/rfc6749#section-6";
150
75
  readonly type: "string";
151
76
  };
77
+ readonly refresh_token_expires_in: {
78
+ readonly description: "The lifetime in seconds of the refresh token";
79
+ readonly type: "number";
80
+ };
81
+ readonly refresh_token_expiration_date: {
82
+ readonly type: "string";
83
+ readonly format: "date-time";
84
+ };
85
+ readonly scope: import("ya-json-schema-types").ExpressiveJSONSchema;
152
86
  };
153
87
  };
154
88
  };
@@ -181,28 +115,33 @@ export declare const definition: {
181
115
  };
182
116
  };
183
117
  declare const _default: import("knifecycle").ServiceInitializer<{
184
- oAuth2Granters: OAuth2GranterService[];
185
- oAuth2AccessToken: OAuth2AccessTokenService;
186
- oAuth2RefreshToken: OAuth2RefreshTokenService;
118
+ OAUTH2: WhookOAuth2Options;
119
+ oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
120
+ oAuth2AccessToken: WhookOAuth2AccessTokenService;
121
+ oAuth2RefreshToken: WhookOAuth2RefreshTokenService;
187
122
  log: LogService;
188
123
  time: TimeService;
189
- }, ({ body: { grant_type: grantType, ...grantParameters }, authenticationData, }: {
124
+ }, ({ body: { grant_type: grantType, scope: demandedScope, ...grantParameters }, authenticationData: optionalAuthenticationData, }: {
190
125
  body: {
191
126
  grant_type: string;
192
127
  [name: string]: unknown;
193
128
  };
194
- authenticationData: WhookAuthenticationData;
129
+ authenticationData?: WhookAuthenticationData;
195
130
  }) => Promise<{
196
131
  status: number;
197
- headers: {};
132
+ headers: {
133
+ 'Cache-Control': string;
134
+ Pragma: string;
135
+ };
198
136
  body: {
137
+ scope: string;
138
+ refresh_token?: string | undefined;
139
+ refresh_token_expires_in?: number | undefined;
140
+ refresh_token_expiration_date?: string | undefined;
199
141
  access_token: string;
200
142
  token_type: string;
201
143
  expires_in: number;
202
144
  expiration_date: string;
203
- refresh_token: string;
204
- refresh_token_expires_in: number;
205
- refresh_token_expiration_date: string;
206
145
  };
207
146
  }>>;
208
147
  export default _default;