@whook/oauth2 27.0.2 → 27.1.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 (121) hide show
  1. package/README.md +44 -25
  2. package/dist/index.d.ts +11 -7
  3. package/dist/index.js +9 -6
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.test.js +131 -15
  6. package/dist/index.test.js.map +1 -1
  7. package/dist/libs/clients.d.ts +17 -1
  8. package/dist/libs/clients.js +39 -6
  9. package/dist/libs/clients.js.map +1 -1
  10. package/dist/libs/clients.test.d.ts +1 -0
  11. package/dist/libs/clients.test.js +32 -0
  12. package/dist/libs/clients.test.js.map +1 -0
  13. package/dist/libs/errors.d.ts +24 -0
  14. package/dist/libs/errors.js +42 -0
  15. package/dist/libs/errors.js.map +1 -1
  16. package/dist/libs/verifier.d.ts +3 -3
  17. package/dist/libs/verifier.js +4 -3
  18. package/dist/libs/verifier.js.map +1 -1
  19. package/dist/routes/getOAuth2Authorize.d.ts +19 -6
  20. package/dist/routes/getOAuth2Authorize.js +29 -14
  21. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  22. package/dist/routes/getOAuth2WellKnownMetadata.d.ts +3 -4
  23. package/dist/routes/getOAuth2WellKnownMetadata.js +19 -7
  24. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  25. package/dist/routes/getOAuth2WellKnownMetadata.test.js +81 -0
  26. package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
  27. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +0 -2
  28. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +1 -2
  29. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
  30. package/dist/routes/postAuthLogin.d.ts +1 -1
  31. package/dist/routes/postAuthLogin.js +1 -1
  32. package/dist/routes/postAuthLogin.js.map +1 -1
  33. package/dist/routes/postAuthRefresh.d.ts +1 -1
  34. package/dist/routes/postAuthRefresh.js +1 -1
  35. package/dist/routes/postAuthRefresh.js.map +1 -1
  36. package/dist/routes/postOAuth2Acknowledge.d.ts +0 -3
  37. package/dist/routes/postOAuth2Acknowledge.js +1 -3
  38. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  39. package/dist/routes/postOAuth2DeviceAcknowledge.d.ts +64 -0
  40. package/dist/routes/postOAuth2DeviceAcknowledge.js +84 -0
  41. package/dist/routes/postOAuth2DeviceAcknowledge.js.map +1 -0
  42. package/dist/routes/postOAuth2DeviceAcknowledge.test.d.ts +1 -0
  43. package/dist/routes/postOAuth2DeviceAcknowledge.test.js +369 -0
  44. package/dist/routes/postOAuth2DeviceAcknowledge.test.js.map +1 -0
  45. package/dist/routes/postOAuth2DeviceAuthorization.d.ts +133 -0
  46. package/dist/routes/postOAuth2DeviceAuthorization.js +177 -0
  47. package/dist/routes/postOAuth2DeviceAuthorization.js.map +1 -0
  48. package/dist/routes/postOAuth2DeviceAuthorization.test.d.ts +1 -0
  49. package/dist/routes/postOAuth2DeviceAuthorization.test.js +174 -0
  50. package/dist/routes/postOAuth2DeviceAuthorization.test.js.map +1 -0
  51. package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +1 -6
  52. package/dist/routes/postOAuth2PushedAuthorizationRequest.js +11 -14
  53. package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -1
  54. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +47 -0
  55. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -1
  56. package/dist/routes/postOAuth2Token.d.ts +2 -7
  57. package/dist/routes/postOAuth2Token.js +2 -6
  58. package/dist/routes/postOAuth2Token.js.map +1 -1
  59. package/dist/routes/postOAuth2Token.test.js +107 -0
  60. package/dist/routes/postOAuth2Token.test.js.map +1 -1
  61. package/dist/routes/schemas.d.ts +178 -0
  62. package/dist/routes/schemas.js +188 -0
  63. package/dist/routes/schemas.js.map +1 -0
  64. package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +0 -70
  65. package/dist/services/oAuth2AuthorizationCodeGranter.js +7 -84
  66. package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -1
  67. package/dist/services/oAuth2AuthorizationRequests.js +4 -5
  68. package/dist/services/oAuth2AuthorizationRequests.js.map +1 -1
  69. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +1 -16
  70. package/dist/services/oAuth2ClientCredentialsGranter.js +7 -25
  71. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
  72. package/dist/services/oAuth2DeviceAuthorizationCodeGranter.test.d.ts +1 -0
  73. package/dist/services/oAuth2DeviceAuthorizationCodeGranter.test.js +332 -0
  74. package/dist/services/oAuth2DeviceAuthorizationCodeGranter.test.js.map +1 -0
  75. package/dist/services/oAuth2DeviceAuthorizationGranter.d.ts +54 -0
  76. package/dist/services/oAuth2DeviceAuthorizationGranter.js +67 -0
  77. package/dist/services/oAuth2DeviceAuthorizationGranter.js.map +1 -0
  78. package/dist/services/oAuth2Granters.js +0 -2
  79. package/dist/services/oAuth2Granters.js.map +1 -1
  80. package/dist/services/oAuth2PasswordGranter.d.ts +1 -22
  81. package/dist/services/oAuth2PasswordGranter.js +5 -30
  82. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  83. package/dist/services/oAuth2RefreshTokenGranter.d.ts +1 -19
  84. package/dist/services/oAuth2RefreshTokenGranter.js +5 -28
  85. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  86. package/package.json +4 -4
  87. package/src/index.test.ts +169 -26
  88. package/src/index.ts +21 -26
  89. package/src/libs/clients.test.ts +47 -0
  90. package/src/libs/clients.ts +60 -10
  91. package/src/libs/errors.ts +72 -0
  92. package/src/libs/verifier.ts +11 -3
  93. package/src/routes/__snapshots__/postOAuth2DeviceAcknowledge.test.ts.snap +5 -0
  94. package/src/routes/getOAuth2Authorize.ts +41 -26
  95. package/src/routes/getOAuth2WellKnownMetadata.test.ts +96 -0
  96. package/src/routes/getOAuth2WellKnownMetadata.ts +24 -10
  97. package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +1 -3
  98. package/src/routes/postAuthLogin.ts +1 -1
  99. package/src/routes/postAuthRefresh.ts +1 -1
  100. package/src/routes/postOAuth2Acknowledge.ts +2 -4
  101. package/src/routes/postOAuth2DeviceAcknowledge.test.ts +408 -0
  102. package/src/routes/postOAuth2DeviceAcknowledge.ts +157 -0
  103. package/src/routes/postOAuth2DeviceAuthorization.test.ts +192 -0
  104. package/src/routes/postOAuth2DeviceAuthorization.ts +261 -0
  105. package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +59 -0
  106. package/src/routes/postOAuth2PushedAuthorizationRequest.ts +16 -22
  107. package/src/routes/postOAuth2Token.test.ts +119 -0
  108. package/src/routes/postOAuth2Token.ts +6 -16
  109. package/src/routes/schemas.ts +210 -0
  110. package/src/services/oAuth2AuthorizationCodeGranter.ts +11 -94
  111. package/src/services/oAuth2AuthorizationRequests.ts +5 -5
  112. package/src/services/oAuth2ClientCredentialsGranter.ts +8 -27
  113. package/src/services/oAuth2DeviceAuthorizationCodeGranter.test.ts +372 -0
  114. package/src/services/oAuth2DeviceAuthorizationGranter.ts +194 -0
  115. package/src/services/oAuth2Granters.ts +0 -2
  116. package/src/services/oAuth2PasswordGranter.ts +7 -33
  117. package/src/services/oAuth2RefreshTokenGranter.ts +6 -31
  118. package/dist/libs/schemas.d.ts +0 -31
  119. package/dist/libs/schemas.js +0 -33
  120. package/dist/libs/schemas.js.map +0 -1
  121. package/src/libs/schemas.ts +0 -37
@@ -0,0 +1,54 @@
1
+ import { type LogService } from 'common-services';
2
+ import { type WhookOAuth2GranterService, type WhookOAuth2ReadClientGrantsService, type WhookOAuth2GranterDefinitions, type WhookOAuth2ClientId, type WhookOAuth2Options } from './oAuth2Granters.js';
3
+ import { type WhookAuthenticationScope, type WhookAuthenticationData } from '@whook/authorization';
4
+ export declare const DEVICE_AUTHORIZATION_GRANT_TYPE: "urn:ietf:params:oauth:grant-type:device_code";
5
+ export declare const DEFAULT_DEVICE_AUTHORIZATION_INTERVAL = 5;
6
+ /**
7
+ * A type allowing to override the device code type
8
+ */
9
+ export type WhookOAuth2DeviceCode = string;
10
+ /**
11
+ * A type allowing to override the user code type
12
+ */
13
+ export type WhookOAuth2UserCode = string;
14
+ /**
15
+ * A service to create and check device codes
16
+ */
17
+ export interface WhookOAuth2DeviceAuthorizationCodeService {
18
+ create: (clientAuthenticationData: WhookAuthenticationData, context: {
19
+ demandedScopes: WhookAuthenticationScope[];
20
+ filteredScopes: WhookAuthenticationScope[];
21
+ }) => Promise<{
22
+ deviceCode: WhookOAuth2DeviceCode;
23
+ userCode: WhookOAuth2UserCode;
24
+ expiresAt: number;
25
+ }>;
26
+ /**
27
+ * Must reject unless expectedClientId exactly matches the stored challenge's
28
+ * client ID, before recording approval, denial, or acknowledged scopes.
29
+ */
30
+ acknowledge: (acknowledged: boolean, userAuthenticationData: WhookAuthenticationData, userCode: WhookOAuth2UserCode, expectedClientId: WhookOAuth2ClientId, acknowledgedScopes: WhookAuthenticationScope[]) => Promise<void>;
31
+ check: (clientAuthenticationData: WhookAuthenticationData, deviceCode: WhookOAuth2DeviceCode) => Promise<WhookAuthenticationData>;
32
+ }
33
+ export interface WhookOAuth2DeviceAuthorizationOptions {
34
+ /** The interval the device may poll for a token */
35
+ interval?: number;
36
+ /** The user code verification interface URI */
37
+ verificationURI: string;
38
+ }
39
+ export interface WhookOAuth2DeviceAuthorizationGranterDependencies {
40
+ OAUTH2: WhookOAuth2Options;
41
+ readClientGrants: WhookOAuth2ReadClientGrantsService;
42
+ oAuth2DeviceAuthorizationCode: Pick<WhookOAuth2DeviceAuthorizationCodeService, 'check'>;
43
+ log?: LogService;
44
+ }
45
+ export interface WhookOAuth2DeviceAuthorizationGranterDefinitions extends WhookOAuth2GranterDefinitions {
46
+ grantType: typeof DEVICE_AUTHORIZATION_GRANT_TYPE;
47
+ authenticateParameters: {
48
+ deviceCode: WhookOAuth2DeviceCode;
49
+ clientId?: WhookOAuth2ClientId;
50
+ };
51
+ }
52
+ export type WhookOAuth2DeviceAuthorizationGranterService = WhookOAuth2GranterService<WhookOAuth2DeviceAuthorizationGranterDefinitions>;
53
+ declare const _default: import("knifecycle").ServiceInitializer<WhookOAuth2DeviceAuthorizationGranterDependencies, WhookOAuth2DeviceAuthorizationGranterService>;
54
+ export default _default;
@@ -0,0 +1,67 @@
1
+ import { autoService, location } from 'knifecycle';
2
+ import { noop } from '@whook/whook';
3
+ import { pickYErrorWithCode, YError } from 'yerror';
4
+ import { checkGrantType } from '../libs/grants.js';
5
+ import { filterScopes } from '../libs/scopes.js';
6
+ import { checkClientsIds, checkGrants, toUsableClientId, } from '../libs/clients.js';
7
+ import { deviceAuthorizationTokenRequestBodySchema } from '../routes/schemas.js';
8
+ export const DEVICE_AUTHORIZATION_GRANT_TYPE = deviceAuthorizationTokenRequestBodySchema.schema.properties.grant_type.const;
9
+ export const DEFAULT_DEVICE_AUTHORIZATION_INTERVAL = 5;
10
+ // Device Code Grant
11
+ // https://datatracker.ietf.org/doc/html/rfc8628#section-3.4
12
+ async function initOAuth2DeviceAuthorizationGranter({ OAUTH2, readClientGrants, oAuth2DeviceAuthorizationCode, log = noop, }) {
13
+ const authenticateWithDeviceCode = async ({ deviceCode, clientId }, optionalAuthenticationData) => {
14
+ const usableClientId = toUsableClientId([
15
+ optionalAuthenticationData?.clientId,
16
+ clientId,
17
+ ]);
18
+ const grants = await readClientGrants(usableClientId);
19
+ checkGrants(usableClientId, grants);
20
+ if (!grants.isPublicClient) {
21
+ if (!optionalAuthenticationData) {
22
+ throw new YError('E_OAUTH2_AUTHENTICATION_REQUIRED', [
23
+ grants.authenticationData.clientId,
24
+ ]);
25
+ }
26
+ }
27
+ checkGrantType(grants.allowedGrantTypes, DEVICE_AUTHORIZATION_GRANT_TYPE);
28
+ try {
29
+ const authorizationCodeAuthenticationData = await oAuth2DeviceAuthorizationCode.check(optionalAuthenticationData || grants.authenticationData, deviceCode);
30
+ if (!authorizationCodeAuthenticationData.clientId) {
31
+ throw new YError('E_OAUTH2_UNEXPECTED_EMPTY_CLIENT_ID', []);
32
+ }
33
+ checkClientsIds(usableClientId, [
34
+ authorizationCodeAuthenticationData.clientId,
35
+ ]);
36
+ return {
37
+ ...authorizationCodeAuthenticationData,
38
+ scopes: filterScopes(filterScopes(authorizationCodeAuthenticationData.scopes, OAUTH2.allowedScopes, !!OAUTH2.strictScopesChecks), grants.allowedScopes, !!OAUTH2.strictScopesChecks),
39
+ };
40
+ }
41
+ catch (err) {
42
+ const deniedError = pickYErrorWithCode(err, 'E_OAUTH2_ACCESS_DENIED');
43
+ if (deniedError) {
44
+ throw YError.wrap(deniedError, 'E_OAUTH2_DEVICE_ACCESS_DENIED', [
45
+ usableClientId,
46
+ ]);
47
+ }
48
+ const castedErr = pickYErrorWithCode(err, 'E_BAD_TOKEN');
49
+ if (castedErr) {
50
+ throw YError.wrap(castedErr, 'E_OAUTH2_BAD_DEVICE_CODE');
51
+ }
52
+ const castedErr2 = pickYErrorWithCode(err, 'E_EXPIRED_TOKEN');
53
+ if (castedErr2) {
54
+ throw YError.wrap(castedErr2, 'E_OAUTH2_EXPIRED_DEVICE_CODE');
55
+ }
56
+ throw err;
57
+ }
58
+ };
59
+ log('debug', '👫 - OAuth2DeviceAuthorizationGranter Service Initialized!');
60
+ return {
61
+ grantType: DEVICE_AUTHORIZATION_GRANT_TYPE,
62
+ issuesRefreshToken: true,
63
+ authenticate: authenticateWithDeviceCode,
64
+ };
65
+ }
66
+ export default location(autoService(initOAuth2DeviceAuthorizationGranter), import.meta.url);
67
+ //# sourceMappingURL=oAuth2DeviceAuthorizationGranter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oAuth2DeviceAuthorizationGranter.js","sourceRoot":"","sources":["../../src/services/oAuth2DeviceAuthorizationGranter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,IAAI,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O,EAAE,4DAA4D,CAAC,CAAC;IAE3E,OAAO;QACL,SAAS,EAAE,+BAA+B;QAC1C,kBAAkB,EAAE,IAAI;QACxB,YAAY,EAAE,0BAA0B;KACzC,CAAC;AACJ,CAAC;AAED,eAAe,QAAQ,CACrB,WAAW,CAAC,oCAAoC,CAAC,EACjD,MAAM,CAAC,IAAI,CAAC,GAAG,CAChB,CAAC"}
@@ -5,9 +5,7 @@ export default location(initializer({
5
5
  inject: [
6
6
  'oAuth2ClientCredentialsGranter',
7
7
  'oAuth2AuthorizationCodeGranter',
8
- 'oAuth2PasswordGranter',
9
8
  'oAuth2RefreshTokenGranter',
10
- 'oAuth2ImplicitGranter',
11
9
  ],
12
10
  }, async (services) => Object.keys(services).map((key) => services[key])), import.meta.url);
13
11
  //# sourceMappingURL=oAuth2Granters.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"oAuth2Granters.js","sourceRoot":"","sources":["../../src/services/oAuth2Granters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AA+HtE,eAAe,QAAQ,CACrB,WAAW,CACT;IACE,IAAI,EAAE,gBAAgB;IACtB,IAAI,EAAE,SAAS;IACf,MAAM,EAAE;QACN,gCAAgC;QAChC,gCAAgC;QAChC,uBAAuB;QACvB,2BAA2B;QAC3B,uBAAuB;KACxB;CACF,EACD,KAAK,EAAE,QAAsB,EAAE,EAAE,CAC/B,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CACpD,EACD,MAAM,CAAC,IAAI,CAAC,GAAG,CAChB,CAAC"}
1
+ {"version":3,"file":"oAuth2Granters.js","sourceRoot":"","sources":["../../src/services/oAuth2Granters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AA+HtE,eAAe,QAAQ,CACrB,WAAW,CACT;IACE,IAAI,EAAE,gBAAgB;IACtB,IAAI,EAAE,SAAS;IACf,MAAM,EAAE;QACN,gCAAgC;QAChC,gCAAgC;QAChC,2BAA2B;KAC5B;CACF,EACD,KAAK,EAAE,QAAsB,EAAE,EAAE,CAC/B,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CACpD,EACD,MAAM,CAAC,IAAI,CAAC,GAAG,CAChB,CAAC"}
@@ -1,28 +1,7 @@
1
1
  import { type LogService } from 'common-services';
2
2
  import { type WhookOAuth2GranterService, type WhookOAuth2ReadClientGrantsService, type WhookOAuth2Options, type WhookOAuth2GranterDefinitions } from './oAuth2Granters.js';
3
3
  import { type WhookAuthenticationData, type WhookAuthenticationScope } from '@whook/authorization';
4
- export declare const PASSWORD_GRANT_TYPE = "password";
5
- export declare const passwordTokenRequestBodySchema: {
6
- readonly name: "PasswordRequestBody";
7
- readonly schema: {
8
- readonly type: "object";
9
- readonly description: "Resource owner password credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.3";
10
- readonly required: ["grant_type", "username", "password"];
11
- readonly properties: {
12
- readonly grant_type: {
13
- readonly type: "string";
14
- readonly enum: ["password"];
15
- };
16
- readonly username: {
17
- readonly type: "string";
18
- };
19
- readonly password: {
20
- readonly type: "string";
21
- };
22
- readonly scope: import("ya-json-schema-types").ExpressiveJSONSchema;
23
- };
24
- };
25
- };
4
+ export declare const PASSWORD_GRANT_TYPE: "password";
26
5
  /**
27
6
  * A service to check a user password
28
7
  */
@@ -1,31 +1,11 @@
1
1
  import { autoService, location } from 'knifecycle';
2
- import { noop, refersTo } from '@whook/whook';
2
+ import { noop } from '@whook/whook';
3
3
  import { YError } from 'yerror';
4
4
  import { filterScopes } from '../libs/scopes.js';
5
5
  import { checkGrantType } from '../libs/grants.js';
6
- import { scopeSchema } from '../libs/schemas.js';
7
- export const PASSWORD_GRANT_TYPE = 'password';
8
- export const passwordTokenRequestBodySchema = {
9
- name: 'PasswordRequestBody',
10
- schema: {
11
- type: 'object',
12
- description: 'Resource owner password credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.3',
13
- required: ['grant_type', 'username', 'password'],
14
- properties: {
15
- grant_type: {
16
- type: 'string',
17
- enum: [PASSWORD_GRANT_TYPE],
18
- },
19
- username: {
20
- type: 'string',
21
- },
22
- password: {
23
- type: 'string',
24
- },
25
- scope: refersTo(scopeSchema),
26
- },
27
- },
28
- };
6
+ import { checkGrants } from '../libs/clients.js';
7
+ import { passwordTokenRequestBodySchema } from '../routes/schemas.js';
8
+ export const PASSWORD_GRANT_TYPE = passwordTokenRequestBodySchema.schema.properties.grant_type.const;
29
9
  export default location(autoService(initOAuth2PasswordGranter), import.meta.url);
30
10
  // Resource Owner Password Credentials Grant
31
11
  // https://tools.ietf.org/html/rfc6749#section-4.3
@@ -34,12 +14,7 @@ async function initOAuth2PasswordGranter({ OAUTH2, oAuth2Password, readClientGra
34
14
  const authenticateWithPassword = async ({ username, password, demandedScopes }, optionalAuthenticationData) => {
35
15
  const usableClientId = optionalAuthenticationData?.clientId || OAUTH2.rootClientId;
36
16
  const grants = await readClientGrants(usableClientId);
37
- if (usableClientId !== grants.authenticationData.clientId) {
38
- throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
39
- usableClientId,
40
- grants.authenticationData.clientId,
41
- ]);
42
- }
17
+ checkGrants(usableClientId, grants);
43
18
  if (!grants.isPublicClient) {
44
19
  if (!optionalAuthenticationData) {
45
20
  throw new YError('E_OAUTH2_AUTHENTICATION_REQUIRED', [
@@ -1 +1 @@
1
- {"version":3,"file":"oAuth2PasswordGranter.js","sourceRoot":"","sources":["../../src/services/oAuth2PasswordGranter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,IAAI,EAAE,QAAQ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AM,CAAC,kBAAkB,CAAC,QAAQ;iBACnC,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAED,cAAc,CAAC,MAAM,CAAC,iBAAiB,EAAE,mBAAmB,CAAC,CAAC;QAE9D,MAAM,sBAAsB,GAAG,MAAM,cAAc,CAAC,KAAK,CACvD,0BAA0B,IAAI,MAAM,CAAC,kBAAkB,EACvD,QAAQ,EACR,QAAQ,CACT,CAAC;QAEF,MAAM,cAAc,GAAG,YAAY,CACjC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,sBAAsB,CAAC,MAAM,EACtE,MAAM,CAAC,aAAa,EACpB,CAAC,CAAC,MAAM,CAAC,kBAAkB,CAC5B,CAAC;QAEF,OAAO;YACL,GAAG,sBAAsB;YACzB,MAAM,EAAE,YAAY,CAClB,cAAc,EACd,sBAAsB,CAAC,MAAM,EAC7B,CAAC,CAAC,MAAM,CAAC,kBAAkB,CAC5B;SACF,CAAC;IACJ,CAAC,CAAC;IAEF,GAAG,CAAC,OAAO,EAAE,iDAAiD,CAAC,CAAC;IAEhE,OAAO;QACL,SAAS,EAAE,mBAAmB;QAC9B,kBAAkB,EAAE,IAAI;QACxB,YAAY,EAAE,wBAAwB;KACvC,CAAC;AACJ,CAAC"}
@@ -2,25 +2,7 @@ import { type LogService } from 'common-services';
2
2
  import { type WhookOAuth2GranterService, type WhookOAuth2ReadClientGrantsService, type WhookOAuth2Options, type WhookOAuth2GranterDefinitions } from './oAuth2Granters.js';
3
3
  import { type WhookAuthenticationScope } from '@whook/authorization';
4
4
  import { type WhookOAuth2AccessTokenService } from './oAuth2ImplicitGranter.js';
5
- export declare const REFRESH_TOKEN_GRANT_TYPE = "refresh_token";
6
- export declare const refreshTokenRequestBodySchema: {
7
- readonly name: "RefreshTokenRequestBody";
8
- readonly schema: {
9
- readonly type: "object";
10
- readonly description: "Token refresh grant type, see https://tools.ietf.org/html/rfc6749#section-6 .";
11
- readonly required: ["grant_type", "refresh_token"];
12
- readonly properties: {
13
- readonly grant_type: {
14
- readonly type: "string";
15
- readonly enum: ["refresh_token"];
16
- };
17
- readonly refresh_token: {
18
- readonly type: "string";
19
- };
20
- readonly scope: import("ya-json-schema-types").ExpressiveJSONSchema;
21
- };
22
- };
23
- };
5
+ export declare const REFRESH_TOKEN_GRANT_TYPE: "refresh_token";
24
6
  /**
25
7
  * A type allowing to override the refresh token type
26
8
  */
@@ -1,29 +1,11 @@
1
1
  import { autoService, location } from 'knifecycle';
2
- import { noop, refersTo } from '@whook/whook';
2
+ import { noop } from '@whook/whook';
3
3
  import { pickYErrorWithCode, YError } from 'yerror';
4
4
  import { checkGrantType } from '../libs/grants.js';
5
5
  import { filterScopes } from '../libs/scopes.js';
6
- import { scopeSchema } from '../libs/schemas.js';
7
- import { toUsableClientId } from '../libs/clients.js';
8
- export const REFRESH_TOKEN_GRANT_TYPE = 'refresh_token';
9
- export const refreshTokenRequestBodySchema = {
10
- name: 'RefreshTokenRequestBody',
11
- schema: {
12
- type: 'object',
13
- description: 'Token refresh grant type, see https://tools.ietf.org/html/rfc6749#section-6 .',
14
- required: ['grant_type', 'refresh_token'],
15
- properties: {
16
- grant_type: {
17
- type: 'string',
18
- enum: [REFRESH_TOKEN_GRANT_TYPE],
19
- },
20
- refresh_token: {
21
- type: 'string',
22
- },
23
- scope: refersTo(scopeSchema),
24
- },
25
- },
26
- };
6
+ import { checkGrants, toUsableClientId } from '../libs/clients.js';
7
+ import { refreshTokenRequestBodySchema } from '../routes/schemas.js';
8
+ export const REFRESH_TOKEN_GRANT_TYPE = refreshTokenRequestBodySchema.schema.properties.grant_type.const;
27
9
  // Refresh Token Grant
28
10
  // https://tools.ietf.org/html/rfc6749#page-47
29
11
  async function initOAuth2RefreshTokenGranter({ OAUTH2, readClientGrants, oAuth2RefreshToken, log = noop, }) {
@@ -52,12 +34,7 @@ async function initOAuth2RefreshTokenGranter({ OAUTH2, readClientGrants, oAuth2R
52
34
  ]);
53
35
  }
54
36
  }
55
- if (usableClientId !== grants.authenticationData.clientId) {
56
- throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
57
- usableClientId,
58
- grants.authenticationData.clientId,
59
- ]);
60
- }
37
+ checkGrants(usableClientId, grants);
61
38
  checkGrantType(grants.allowedGrantTypes, REFRESH_TOKEN_GRANT_TYPE);
62
39
  const filteredScopes = filterScopes(filterScopes(demandedScopes.length
63
40
  ? demandedScopes
@@ -1 +1 @@
1
- {"version":3,"file":"oAuth2RefreshTokenGranter.js","sourceRoot":"","sources":["../../src/services/oAuth2RefreshTokenGranter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAiC,MAAM,cAAc,CAAC;AAC7E,OAAO,EAAE,kBAAkB,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAYpD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEtD,MAAM,CAAC,MAAM,wBAAwB,GAAG,eAAe,CAAC;AAExD,MAAM,CAAC,MAAM,6BAA6B,GAAG;IAC3C,IAAI,EAAE,yBAAyB;IAC/B,MAAM,EAAE;QACN,IAAI,EAAE,QAAQ;QACd,WAAW,EACT,+EAA+E;QACjF,QAAQ,EAAE,CAAC,YAAY,EAAE,eAAe,CAAC;QACzC,UAAU,EAAE;YACV,UAAU,EAAE;gBACV,IAAI,EAAE,QAAQ;gBACd,IAAI,EAAE,CAAC,wBAAwB,CAAC;aACjC;YACD,aAAa,EAAE;gBACb,IAAI,EAAE,QAAQ;aACf;YACD,KAAK,EAAE,QAAQ,CAAC,WAAW,CAAC;SAC7B;KACF;CAC0C,CAAC;AA+B9C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8B,CAAC,MAAM,EACzC,MAAM,CAAC,aAAa,EACpB,CAAC,CAAC,MAAM,CAAC,kBAAkB,CAC5B,EACD,8BAA8B,CAAC,MAAM,EACrC,CAAC,CAAC,MAAM,CAAC,kBAAkB,CAC5B,CAAC;QAEF,OAAO;YACL,GAAG,8BAA8B;YACjC,MAAM,EAAE,cAAc;SACvB,CAAC;IACJ,CAAC,CAAC;IAEF,GAAG,CAAC,OAAO,EAAE,qDAAqD,CAAC,CAAC;IAEpE,OAAO;QACL,SAAS,EAAE,wBAAwB;QACnC,kBAAkB,EAAE,IAAI;QACxB,YAAY,EAAE,4BAA4B;KAC3C,CAAC;AACJ,CAAC;AAED,eAAe,QAAQ,CACrB,WAAW,CAAC,6BAA6B,CAAC,EAC1C,MAAM,CAAC,IAAI,CAAC,GAAG,CAChB,CAAC"}
package/package.json CHANGED
@@ -23,7 +23,7 @@
23
23
  }
24
24
  },
25
25
  "name": "@whook/oauth2",
26
- "version": "27.0.2",
26
+ "version": "27.1.0",
27
27
  "description": "OAuth2 implementation for Whook servers",
28
28
  "type": "module",
29
29
  "publishConfig": {
@@ -46,8 +46,8 @@
46
46
  },
47
47
  "homepage": "https://github.com/nfroidure/whook",
48
48
  "dependencies": {
49
- "@whook/authorization": "^27.0.2",
50
- "@whook/whook": "^27.0.2",
49
+ "@whook/authorization": "^27.0.3",
50
+ "@whook/whook": "^27.0.3",
51
51
  "camelcase": "^10.0.0",
52
52
  "common-services": "^20.1.0",
53
53
  "cookie": "^2.0.1",
@@ -136,7 +136,7 @@
136
136
  "rebuild": "swc ./src -s -d dist -C jsc.target=es2022",
137
137
  "test": "node --run jest"
138
138
  },
139
- "gitHead": "7923fdd220c69c994c7e1107ef88ee5878c68f5b",
139
+ "gitHead": "6d77eea4e34025e181990a2f322cd351561677d2",
140
140
  "funding": {
141
141
  "type": "individual",
142
142
  "url": "https://github.com/sponsors/nfroidure"
package/src/index.test.ts CHANGED
@@ -35,22 +35,20 @@ import {
35
35
  initGetOAuth2Authorize,
36
36
  getOAuth2AuthorizeDefinition,
37
37
  getOAuth2AuthorizeResponseTypeParameter,
38
+ getOAuth2AuthorizeCodeChallengeParameter,
39
+ getOAuth2AuthorizeCodeChallengeMethodParameter,
40
+ clientIdSchema,
38
41
  getOAuth2AuthorizeClientIdParameter,
39
42
  getOAuth2AuthorizeRedirectURIParameter,
40
43
  getOAuth2AuthorizeScopeParameter,
41
44
  getOAuth2AuthorizeStateParameter,
42
45
  getOAuth2AuthorizeRequestURIParameter,
43
- getOAuth2AuthorizeScopeSchema,
44
- getOAuth2AuthorizeRequestURISchema,
45
- getOAuth2AuthorizeCodeChallengeSchema,
46
- getOAuth2AuthorizeCodeChallengeParameter,
47
- getOAuth2AuthorizeCodeChallengeMethodSchema,
48
- getOAuth2AuthorizeCodeChallengeMethodParameter,
49
46
  initPostOAuth2Acknowledge,
50
47
  postOAuth2AcknowledgeDefinition,
51
48
  initPostOAuth2Token,
52
- postOAuth2TokenCodeVerifierSchema,
49
+ initPostOAuth2DeviceAuthorization,
53
50
  postOAuth2TokenDefinition,
51
+ postOAuth2DeviceAuthorizationDefinition,
54
52
  OAUTH2_ERRORS_DESCRIPTORS,
55
53
  initOAuth2AuthorizationCodeGranter,
56
54
  initOAuth2PasswordGranter,
@@ -58,21 +56,35 @@ import {
58
56
  initOAuth2Granters,
59
57
  initOAuth2ClientCredentialsGranter,
60
58
  initOAuth2ImplicitGranter,
61
- postOAuth2TokenAuthorizationCodeTokenRequestBodySchema,
62
- postOAuth2TokenPasswordTokenRequestBodySchema,
63
- postOAuth2TokenClientCredentialsTokenRequestBodySchema,
59
+ initOAuth2DeviceAuthorizationGranter,
64
60
  postOAuth2TokenTokenBodySchema,
65
- postOAuth2TokenRefreshTokenRequestBodySchema,
66
61
  postOAuth2PushedAuthorizationRequestBodySchema,
67
62
  postOAuth2PushedAuthorizationRequestDefinition,
68
- postOAuth2PushedAuthorizationRequestRequestURISchema,
69
63
  initPostOAuth2PushedAuthorizationRequest,
64
+ postOAuth2DeviceAuthorizationRequestBodySchema,
65
+ postOAuth2DeviceAuthorizationUserCodeSchema,
70
66
  type WhookOAuth2Options,
71
67
  type WhookOAuth2ReadClientGrantsService,
72
68
  type WhookOAuth2PasswordService,
73
69
  type WhookOAuth2AuthorizationCodeService,
74
70
  type WhookOAuth2RefreshTokenService,
75
71
  type WhookOAuth2AccessTokenService,
72
+ type WhookOAuth2DeviceAuthorizationCodeService,
73
+ type WhookOAuth2DeviceAuthorizationOptions,
74
+ authorizationCodeSchema,
75
+ scopeSchema,
76
+ requestURISchema,
77
+ codeChallengeSchema,
78
+ codeChallengeMethodSchema,
79
+ authorizationCodeTokenRequestBodySchema,
80
+ passwordTokenRequestBodySchema,
81
+ clientCredentialsTokenRequestBodySchema,
82
+ refreshTokenRequestBodySchema,
83
+ deviceAuthorizationTokenRequestBodySchema,
84
+ codeVerifierSchema,
85
+ deviceCodeSchema,
86
+ plainCodeChallengeMethodSchema,
87
+ s256CodeChallengeMethodSchema,
76
88
  } from './index.js';
77
89
  import { type Knifecycle } from 'knifecycle';
78
90
  import { type OpenAPI } from 'ya-open-api-types';
@@ -137,6 +149,16 @@ describe('OAuth2 server', () => {
137
149
  ],
138
150
  },
139
151
  },
152
+ [`${BASE_PATH}${postOAuth2DeviceAuthorizationDefinition.path}`]: {
153
+ [postOAuth2DeviceAuthorizationDefinition.method]: {
154
+ ...postOAuth2DeviceAuthorizationDefinition.operation,
155
+ security: [
156
+ {
157
+ basicAuth: ['oauth'],
158
+ },
159
+ ],
160
+ },
161
+ },
140
162
  },
141
163
  components: {
142
164
  securitySchemes: {
@@ -168,18 +190,26 @@ describe('OAuth2 server', () => {
168
190
  {},
169
191
  ),
170
192
  schemas: [
171
- getOAuth2AuthorizeScopeSchema,
172
- getOAuth2AuthorizeRequestURISchema,
173
- getOAuth2AuthorizeCodeChallengeSchema,
174
- getOAuth2AuthorizeCodeChallengeMethodSchema,
175
- postOAuth2TokenAuthorizationCodeTokenRequestBodySchema,
176
- postOAuth2TokenPasswordTokenRequestBodySchema,
177
- postOAuth2TokenClientCredentialsTokenRequestBodySchema,
178
- postOAuth2TokenRefreshTokenRequestBodySchema,
193
+ clientIdSchema,
194
+ scopeSchema,
195
+ requestURISchema,
196
+ codeChallengeSchema,
197
+ plainCodeChallengeMethodSchema,
198
+ s256CodeChallengeMethodSchema,
199
+ codeChallengeMethodSchema,
200
+ authorizationCodeTokenRequestBodySchema,
201
+ passwordTokenRequestBodySchema,
202
+ clientCredentialsTokenRequestBodySchema,
203
+ refreshTokenRequestBodySchema,
204
+ deviceAuthorizationTokenRequestBodySchema,
205
+ postOAuth2DeviceAuthorizationRequestBodySchema,
206
+ postOAuth2DeviceAuthorizationUserCodeSchema,
207
+ deviceCodeSchema,
179
208
  postOAuth2TokenTokenBodySchema,
180
- postOAuth2TokenCodeVerifierSchema,
209
+ codeVerifierSchema,
181
210
  postOAuth2PushedAuthorizationRequestBodySchema,
182
- postOAuth2PushedAuthorizationRequestRequestURISchema,
211
+ requestURISchema,
212
+ authorizationCodeSchema,
183
213
  ].reduce(
184
214
  (schemasHash, { name, schema }) => ({
185
215
  ...schemasHash,
@@ -218,6 +248,13 @@ describe('OAuth2 server', () => {
218
248
  allowedScopes: ['user', 'oauth'],
219
249
  rootClientId: 'the_root_client_id',
220
250
  };
251
+ const OAUTH2_PAR: WhookOAuth2AuthorizationRequestsOptions = {
252
+ mode: 'enabled',
253
+ };
254
+ const OAUTH2_DEVICE_AUTHORIZATION: WhookOAuth2DeviceAuthorizationOptions = {
255
+ verificationURI: 'https://auth.example.com/device',
256
+ interval: 8,
257
+ };
221
258
  const authentication = {
222
259
  check: jest.fn<WhookAuthenticationService<any>['check']>(),
223
260
  };
@@ -238,6 +275,10 @@ describe('OAuth2 server', () => {
238
275
  create: jest.fn<WhookOAuth2AuthorizationCodeService['create']>(),
239
276
  check: jest.fn<WhookOAuth2AuthorizationCodeService['check']>(),
240
277
  };
278
+ const oAuth2DeviceAuthorizationCode = {
279
+ create: jest.fn<WhookOAuth2DeviceAuthorizationCodeService['create']>(),
280
+ check: jest.fn<WhookOAuth2DeviceAuthorizationCodeService['check']>(),
281
+ };
241
282
  const oAuth2Password = {
242
283
  check: jest.fn<WhookOAuth2PasswordService['check']>(),
243
284
  };
@@ -283,10 +324,9 @@ describe('OAuth2 server', () => {
283
324
  // OAuth2 Specifics
284
325
  $.register(constant('OAUTH2', OAUTH2));
285
326
  $.register(
286
- constant('OAUTH2_PAR', {
287
- mode: 'enabled',
288
- } satisfies WhookOAuth2AuthorizationRequestsOptions),
327
+ constant('OAUTH2_DEVICE_AUTHORIZATION', OAUTH2_DEVICE_AUTHORIZATION),
289
328
  );
329
+ $.register(constant('OAUTH2_PAR', OAUTH2_PAR));
290
330
  $.register(
291
331
  constant('ERRORS_DESCRIPTORS', {
292
332
  ...DEFAULT_ERRORS_DESCRIPTORS,
@@ -301,6 +341,7 @@ describe('OAuth2 server', () => {
301
341
  'postOAuth2Acknowledge',
302
342
  'postOAuth2Token',
303
343
  'postOAuth2PushedAuthorizationRequest',
344
+ 'postOAuth2DeviceAuthorization',
304
345
  ],
305
346
  initRoutesHandlers,
306
347
  ),
@@ -313,18 +354,30 @@ describe('OAuth2 server', () => {
313
354
  $.register(constant('oAuth2AccessToken', oAuth2AccessToken));
314
355
  $.register(constant('oAuth2RefreshToken', oAuth2RefreshToken));
315
356
  $.register(constant('oAuth2AuthorizationCode', oAuth2AuthorizationCode));
357
+ $.register(
358
+ constant('oAuth2DeviceAuthorizationCode', oAuth2DeviceAuthorizationCode),
359
+ );
316
360
  $.register(constant('oAuth2Password', oAuth2Password));
317
361
  [
318
362
  initGetOAuth2Authorize,
319
363
  initPostOAuth2Acknowledge,
320
364
  initPostOAuth2Token,
321
- initOAuth2Granters,
365
+ initPostOAuth2DeviceAuthorization,
366
+ alsoInject(
367
+ [
368
+ 'oAuth2DeviceAuthorizationGranter',
369
+ 'oAuth2PasswordGranter',
370
+ 'oAuth2ImplicitGranter',
371
+ ],
372
+ initOAuth2Granters,
373
+ ),
322
374
  initOAuth2ClientCredentialsGranter,
323
375
  initOAuth2AuthorizationCodeGranter,
324
376
  initOAuth2PasswordGranter,
325
377
  initOAuth2RefreshTokenGranter,
326
378
  initOAuth2ImplicitGranter,
327
379
  initPostOAuth2PushedAuthorizationRequest,
380
+ initOAuth2DeviceAuthorizationGranter,
328
381
  ].forEach((handlerInitializer) => $.register(handlerInitializer as any));
329
382
 
330
383
  return $;
@@ -363,6 +416,8 @@ describe('OAuth2 server', () => {
363
416
  oAuth2RefreshToken.check,
364
417
  oAuth2AuthorizationCode.check,
365
418
  oAuth2AuthorizationCode.create,
419
+ oAuth2DeviceAuthorizationCode.create,
420
+ oAuth2DeviceAuthorizationCode.check,
366
421
  oAuth2Password.check,
367
422
  readClientGrants,
368
423
  authentication.check,
@@ -2448,4 +2503,92 @@ describe('OAuth2 server', () => {
2448
2503
  `);
2449
2504
  });
2450
2505
  });
2506
+
2507
+ describe('with the device authorization flow', () => {
2508
+ test('should return HTTP 400 access_denied for a denied device poll', async () => {
2509
+ authentication.check.mockResolvedValueOnce({
2510
+ clientId: 'the_client_id',
2511
+ scopes: ['oauth'],
2512
+ userId: 'auth_user_id',
2513
+ });
2514
+ readClientGrants.mockResolvedValueOnce({
2515
+ allowedGrantTypes: ['urn:ietf:params:oauth:grant-type:device_code'],
2516
+ allowedScopes: ['user'],
2517
+ allowedRedirectURIS: [],
2518
+ isPublicClient: false,
2519
+ authenticationData: {
2520
+ clientId: 'the_client_id',
2521
+ scopes: ['user'],
2522
+ userId: 'client_user_id',
2523
+ },
2524
+ });
2525
+ oAuth2DeviceAuthorizationCode.check.mockRejectedValueOnce(
2526
+ new YError('E_OAUTH2_ACCESS_DENIED', ['the_client_id']),
2527
+ );
2528
+
2529
+ const { status, data } = await axios({
2530
+ method: 'post',
2531
+ url: `http://${HOST}:${PORT}${BASE_PATH}${postOAuth2TokenDefinition.path}`,
2532
+ headers: {
2533
+ authorization: `basic ${Buffer.from('ali:open_sesame').toString(
2534
+ 'base64',
2535
+ )}`,
2536
+ },
2537
+ data: {
2538
+ grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
2539
+ device_code: 'a_device_code',
2540
+ },
2541
+ validateStatus: () => true,
2542
+ });
2543
+
2544
+ expect(status).toBe(400);
2545
+ expect(data).toMatchObject({ error: 'access_denied' });
2546
+ expect(oAuth2DeviceAuthorizationCode.check).toHaveBeenCalledTimes(1);
2547
+ expect(oAuth2AccessToken.create).not.toHaveBeenCalled();
2548
+ expect(oAuth2RefreshToken.create).not.toHaveBeenCalled();
2549
+ expect(OAUTH2_ERRORS_DESCRIPTORS.E_OAUTH2_ACCESS_DENIED).toMatchObject({
2550
+ code: 'access_denied',
2551
+ status: 403,
2552
+ });
2553
+ });
2554
+
2555
+ test.each([
2556
+ ['application/json', { client_id: '' }],
2557
+ ['application/x-www-form-urlencoded', 'client_id='],
2558
+ ])('should reject an empty client ID in %s', async (contentType, body) => {
2559
+ authentication.check.mockResolvedValueOnce({
2560
+ clientId: 'the_client_id',
2561
+ scopes: ['oauth'],
2562
+ userId: 'auth_user_id',
2563
+ });
2564
+
2565
+ const { status, data } = await axios({
2566
+ method: 'post',
2567
+ url: `http://${HOST}:${PORT}${BASE_PATH}${postOAuth2DeviceAuthorizationDefinition.path}`,
2568
+ headers: {
2569
+ authorization: `basic ${Buffer.from('ali:open_sesame').toString('base64')}`,
2570
+ 'content-type': contentType,
2571
+ },
2572
+ data: body,
2573
+ validateStatus: () => true,
2574
+ });
2575
+
2576
+ expect(status).toBe(400);
2577
+ expect(data).toMatchObject({
2578
+ error: 'bad_request',
2579
+ error_description: 'The request body does not match the schema',
2580
+ error_params: [
2581
+ { client_id: '' },
2582
+ expect.arrayContaining([
2583
+ expect.objectContaining({
2584
+ instancePath: '/client_id',
2585
+ keyword: 'minLength',
2586
+ }),
2587
+ ]),
2588
+ ],
2589
+ });
2590
+ expect(readClientGrants).not.toHaveBeenCalled();
2591
+ expect(oAuth2DeviceAuthorizationCode.create).not.toHaveBeenCalled();
2592
+ });
2593
+ });
2451
2594
  });