@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.
- package/README.md +147 -98
- package/dist/index.d.ts +34 -11
- package/dist/index.js +18 -8
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +1671 -393
- package/dist/index.test.js.map +1 -1
- package/dist/libs/authorizationRequests.d.ts +6 -0
- package/dist/libs/authorizationRequests.js +22 -0
- package/dist/libs/authorizationRequests.js.map +1 -0
- package/dist/libs/authorizationRequests.test.js +24 -0
- package/dist/libs/authorizationRequests.test.js.map +1 -0
- package/dist/libs/clients.d.ts +2 -0
- package/dist/libs/clients.js +13 -0
- package/dist/libs/clients.js.map +1 -0
- package/dist/libs/errors.d.ts +143 -0
- package/dist/libs/errors.js +227 -0
- package/dist/libs/errors.js.map +1 -0
- package/dist/libs/grants.d.ts +2 -0
- package/dist/libs/grants.js +10 -0
- package/dist/libs/grants.js.map +1 -0
- package/dist/libs/redirectURI.d.ts +8 -0
- package/dist/libs/redirectURI.js +94 -0
- package/dist/libs/redirectURI.js.map +1 -0
- package/dist/libs/schemas.d.ts +31 -0
- package/dist/libs/schemas.js +33 -0
- package/dist/libs/schemas.js.map +1 -0
- package/dist/libs/scopes.d.ts +16 -0
- package/dist/libs/scopes.js +37 -0
- package/dist/libs/scopes.js.map +1 -1
- package/dist/libs/utils.d.ts +2 -0
- package/dist/libs/utils.js +17 -0
- package/dist/libs/utils.js.map +1 -0
- package/dist/libs/verifier.d.ts +10 -0
- package/dist/libs/verifier.js +51 -0
- package/dist/libs/verifier.js.map +1 -0
- package/dist/libs/verifier.test.d.ts +1 -0
- package/dist/libs/verifier.test.js +21 -0
- package/dist/libs/verifier.test.js.map +1 -0
- package/dist/routes/getOAuth2Authorize.d.ts +38 -59
- package/dist/routes/getOAuth2Authorize.js +129 -93
- package/dist/routes/getOAuth2Authorize.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.test.js +959 -73
- package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.d.ts +18 -11
- package/dist/routes/getOAuth2WellKnownMetadata.js +43 -11
- package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.test.js +51 -49
- package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +3 -6
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +3 -6
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
- package/dist/routes/postAuthLogin.d.ts +14 -9
- package/dist/routes/postAuthLogin.js +10 -5
- package/dist/routes/postAuthLogin.js.map +1 -1
- package/dist/routes/postAuthLogin.test.js +79 -55
- package/dist/routes/postAuthLogin.test.js.map +1 -1
- package/dist/routes/postAuthLogout.d.ts +5 -2
- package/dist/routes/postAuthLogout.js +12 -7
- package/dist/routes/postAuthLogout.js.map +1 -1
- package/dist/routes/postAuthLogout.test.js +35 -17
- package/dist/routes/postAuthLogout.test.js.map +1 -1
- package/dist/routes/postAuthRefresh.d.ts +14 -9
- package/dist/routes/postAuthRefresh.js +10 -6
- package/dist/routes/postAuthRefresh.js.map +1 -1
- package/dist/routes/postAuthRefresh.test.js +90 -59
- package/dist/routes/postAuthRefresh.test.js.map +1 -1
- package/dist/routes/postOAuth2Acknowledge.d.ts +11 -10
- package/dist/routes/postOAuth2Acknowledge.js +43 -37
- package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
- package/dist/routes/postOAuth2Acknowledge.test.js +135 -70
- package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
- package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +137 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.js +187 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.d.ts +1 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +554 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -0
- package/dist/routes/postOAuth2Revoke.d.ts +99 -0
- package/dist/routes/postOAuth2Revoke.js +102 -0
- package/dist/routes/postOAuth2Revoke.js.map +1 -0
- package/dist/routes/postOAuth2Revoke.test.d.ts +1 -0
- package/dist/routes/postOAuth2Revoke.test.js +161 -0
- package/dist/routes/postOAuth2Revoke.test.js.map +1 -0
- package/dist/routes/postOAuth2Token.d.ts +47 -108
- package/dist/routes/postOAuth2Token.js +65 -114
- package/dist/routes/postOAuth2Token.js.map +1 -1
- package/dist/routes/postOAuth2Token.test.js +493 -61
- package/dist/routes/postOAuth2Token.test.js.map +1 -1
- package/dist/services/authCookies.d.ts +9 -13
- package/dist/services/authCookies.js +9 -5
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/authCookies.test.js.map +1 -1
- package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +124 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js +204 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js +345 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -0
- package/dist/services/oAuth2AuthorizationRequests.d.ts +41 -0
- package/dist/services/oAuth2AuthorizationRequests.js +61 -0
- package/dist/services/oAuth2AuthorizationRequests.js.map +1 -0
- package/dist/services/oAuth2AuthorizationRequests.test.d.ts +1 -0
- package/dist/services/oAuth2AuthorizationRequests.test.js +249 -0
- package/dist/services/oAuth2AuthorizationRequests.test.js.map +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.d.ts +28 -12
- package/dist/services/oAuth2ClientCredentialsGranter.js +39 -21
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
- package/dist/services/oAuth2ClientCredentialsGranter.test.d.ts +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.test.js +265 -0
- package/dist/services/oAuth2ClientCredentialsGranter.test.js.map +1 -0
- package/dist/services/oAuth2Granters.d.ts +79 -86
- package/dist/services/oAuth2Granters.js +2 -67
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2ImplicitGranter.d.ts +38 -0
- package/dist/services/oAuth2ImplicitGranter.js +61 -0
- package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.js +589 -0
- package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -0
- package/dist/services/oAuth2PasswordGranter.d.ts +43 -10
- package/dist/services/oAuth2PasswordGranter.js +53 -18
- package/dist/services/oAuth2PasswordGranter.js.map +1 -1
- package/dist/services/oAuth2PasswordGranter.test.d.ts +1 -0
- package/dist/services/oAuth2PasswordGranter.test.js +455 -0
- package/dist/services/oAuth2PasswordGranter.test.js.map +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +42 -9
- package/dist/services/oAuth2RefreshTokenGranter.js +60 -21
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.test.d.ts +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.test.js +403 -0
- package/dist/services/oAuth2RefreshTokenGranter.test.js.map +1 -0
- package/package.json +14 -13
- package/src/index.test.ts +1773 -407
- package/src/index.ts +63 -53
- package/src/libs/authorizationRequests.test.ts +40 -0
- package/src/libs/authorizationRequests.ts +42 -0
- package/src/libs/clients.ts +22 -0
- package/src/libs/errors.ts +411 -0
- package/src/libs/grants.ts +15 -0
- package/src/libs/redirectURI.ts +142 -0
- package/src/libs/schemas.ts +37 -0
- package/src/libs/scopes.ts +49 -0
- package/src/libs/utils.ts +24 -0
- package/src/libs/verifier.test.ts +37 -0
- package/src/libs/verifier.ts +75 -0
- package/src/routes/getOAuth2Authorize.test.ts +1102 -102
- package/src/routes/getOAuth2Authorize.ts +243 -151
- package/src/routes/getOAuth2WellKnownMetadata.test.ts +73 -74
- package/src/routes/getOAuth2WellKnownMetadata.ts +63 -16
- package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +4 -6
- package/src/routes/postAuthLogin.test.ts +86 -57
- package/src/routes/postAuthLogin.ts +24 -14
- package/src/routes/postAuthLogout.test.ts +42 -18
- package/src/routes/postAuthLogout.ts +22 -8
- package/src/routes/postAuthRefresh.test.ts +98 -61
- package/src/routes/postAuthRefresh.ts +25 -13
- package/src/routes/postOAuth2Acknowledge.test.ts +167 -108
- package/src/routes/postOAuth2Acknowledge.ts +90 -64
- package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +681 -0
- package/src/routes/postOAuth2PushedAuthorizationRequest.ts +292 -0
- package/src/routes/postOAuth2Revoke.test.ts +179 -0
- package/src/routes/postOAuth2Revoke.ts +156 -0
- package/src/routes/postOAuth2Token.test.ts +570 -108
- package/src/routes/postOAuth2Token.ts +104 -136
- package/src/services/authCookies.test.ts +5 -5
- package/src/services/authCookies.ts +24 -22
- package/src/services/oAuth2AuthorizationCodeGranter.test.ts +407 -0
- package/src/services/oAuth2AuthorizationCodeGranter.ts +365 -0
- package/src/services/oAuth2AuthorizationRequests.test.ts +282 -0
- package/src/services/oAuth2AuthorizationRequests.ts +141 -0
- package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
- package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
- package/src/services/oAuth2Granters.ts +100 -196
- package/src/services/oAuth2ImplicitGranter.test.ts +650 -0
- package/src/services/oAuth2ImplicitGranter.ts +147 -0
- package/src/services/oAuth2PasswordGranter.test.ts +493 -0
- package/src/services/oAuth2PasswordGranter.ts +116 -40
- package/src/services/oAuth2RefreshTokenGranter.test.ts +441 -0
- package/src/services/oAuth2RefreshTokenGranter.ts +118 -51
- package/src/types.d.ts +1 -0
- package/dist/services/oAuth2CodeGranter.d.ts +0 -41
- package/dist/services/oAuth2CodeGranter.js +0 -105
- package/dist/services/oAuth2CodeGranter.js.map +0 -1
- package/dist/services/oAuth2CodeGranter.test.js +0 -123
- package/dist/services/oAuth2CodeGranter.test.js.map +0 -1
- package/dist/services/oAuth2TokenGranter.d.ts +0 -22
- package/dist/services/oAuth2TokenGranter.js +0 -64
- package/dist/services/oAuth2TokenGranter.js.map +0 -1
- package/src/__snapshots__/index.test.ts.snap +0 -401
- package/src/routes/__snapshots__/getOAuth2Authorize.test.ts.snap +0 -37
- package/src/routes/__snapshots__/postOAuth2Acknowledge.test.ts.snap +0 -61
- package/src/routes/__snapshots__/postOAuth2Token.test.ts.snap +0 -52
- package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +0 -52
- package/src/services/oAuth2CodeGranter.test.ts +0 -176
- package/src/services/oAuth2CodeGranter.ts +0 -210
- package/src/services/oAuth2TokenGranter.ts +0 -119
- /package/dist/{services/oAuth2CodeGranter.test.d.ts → libs/authorizationRequests.test.d.ts} +0 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { autoService, location } from 'knifecycle';
|
|
2
|
+
import { YError } from 'yerror';
|
|
3
|
+
import { type LogService } from 'common-services';
|
|
4
|
+
import {
|
|
5
|
+
noop,
|
|
6
|
+
refersTo,
|
|
7
|
+
type WhookAPISchemaDefinition,
|
|
8
|
+
type WhookRouteDefinition,
|
|
9
|
+
} from '@whook/whook';
|
|
10
|
+
import {
|
|
11
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
12
|
+
type WhookOAuth2GranterDefinitions,
|
|
13
|
+
type WhookOAuth2GranterService,
|
|
14
|
+
type WhookOAuth2Options,
|
|
15
|
+
} from '../services/oAuth2Granters.js';
|
|
16
|
+
import {
|
|
17
|
+
filterScopes,
|
|
18
|
+
parseOAuth2Scope,
|
|
19
|
+
stringifyScopes,
|
|
20
|
+
} from '../libs/scopes.js';
|
|
21
|
+
import {
|
|
22
|
+
AUTHORIZATION_CODE_RESPONSE_TYPE,
|
|
23
|
+
codeChallengeMethodSchema,
|
|
24
|
+
codeChallengeSchema,
|
|
25
|
+
} from '../services/oAuth2AuthorizationCodeGranter.js';
|
|
26
|
+
import { IMPLICIT_RESPONSE_TYPE } from '../services/oAuth2ImplicitGranter.js';
|
|
27
|
+
import { scopeSchema, requestURISchema } from '../libs/schemas.js';
|
|
28
|
+
import {
|
|
29
|
+
checkCodeChallengeParameters,
|
|
30
|
+
PLAIN_CODE_CHALLENGE_METHOD,
|
|
31
|
+
} from '../libs/verifier.js';
|
|
32
|
+
import {
|
|
33
|
+
DEFAULT_OAUTH2_PAR,
|
|
34
|
+
type WhookOAuth2AuthorizationRequestsConfig,
|
|
35
|
+
type WhookOAuth2AuthorizationRequestsService,
|
|
36
|
+
} from '../services/oAuth2AuthorizationRequests.js';
|
|
37
|
+
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
38
|
+
import { toUsableClientId } from '../libs/clients.js';
|
|
39
|
+
import { getUsableRedirectURI } from '../libs/redirectURI.js';
|
|
40
|
+
import { type WhookOAuth2AuthorizeRequestParameters } from './getOAuth2Authorize.js';
|
|
41
|
+
|
|
42
|
+
export {
|
|
43
|
+
scopeSchema,
|
|
44
|
+
requestURISchema,
|
|
45
|
+
codeChallengeSchema,
|
|
46
|
+
codeChallengeMethodSchema,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const pushedAuthorizationRequestBodySchema = {
|
|
50
|
+
name: 'PushedAuthorizationRequestBody',
|
|
51
|
+
schema: {
|
|
52
|
+
description:
|
|
53
|
+
'OAuth2 Pushed Authorization Request, see https://datatracker.ietf.org/doc/html/rfc9126#section-2.1.',
|
|
54
|
+
type: 'object',
|
|
55
|
+
required: ['response_type', 'client_id'],
|
|
56
|
+
properties: {
|
|
57
|
+
response_type: {
|
|
58
|
+
type: 'string',
|
|
59
|
+
enum: [AUTHORIZATION_CODE_RESPONSE_TYPE, IMPLICIT_RESPONSE_TYPE],
|
|
60
|
+
},
|
|
61
|
+
client_id: {
|
|
62
|
+
type: 'string',
|
|
63
|
+
},
|
|
64
|
+
redirect_uri: {
|
|
65
|
+
type: 'string',
|
|
66
|
+
pattern: '^https?://',
|
|
67
|
+
format: 'uri',
|
|
68
|
+
},
|
|
69
|
+
scope: refersTo(scopeSchema),
|
|
70
|
+
state: {
|
|
71
|
+
type: 'string',
|
|
72
|
+
},
|
|
73
|
+
code_challenge: refersTo(codeChallengeSchema),
|
|
74
|
+
code_challenge_method: refersTo(codeChallengeMethodSchema),
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
} as const satisfies WhookAPISchemaDefinition;
|
|
78
|
+
|
|
79
|
+
export const definition = {
|
|
80
|
+
method: 'post',
|
|
81
|
+
path: '/oauth2/par',
|
|
82
|
+
config: {
|
|
83
|
+
environments: [],
|
|
84
|
+
},
|
|
85
|
+
operation: {
|
|
86
|
+
operationId: 'postOAuth2PushedAuthorizationRequest',
|
|
87
|
+
summary: `Implements the [Pushed Authorization Request Endpoint](https://datatracker.ietf.org/doc/html/rfc9126#section-2).`,
|
|
88
|
+
tags: ['oauth2'],
|
|
89
|
+
parameters: [],
|
|
90
|
+
requestBody: {
|
|
91
|
+
required: true,
|
|
92
|
+
content: {
|
|
93
|
+
'application/x-www-form-urlencoded': {
|
|
94
|
+
schema: refersTo(pushedAuthorizationRequestBodySchema),
|
|
95
|
+
},
|
|
96
|
+
'application/json': {
|
|
97
|
+
schema: refersTo(pushedAuthorizationRequestBodySchema),
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
responses: {
|
|
102
|
+
'201': {
|
|
103
|
+
description: 'Request URI created.',
|
|
104
|
+
headers: {
|
|
105
|
+
'cache-control': {
|
|
106
|
+
schema: {
|
|
107
|
+
type: 'string',
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
pragma: {
|
|
111
|
+
schema: {
|
|
112
|
+
type: 'string',
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
content: {
|
|
117
|
+
'application/json': {
|
|
118
|
+
schema: {
|
|
119
|
+
type: 'object',
|
|
120
|
+
required: ['request_uri', 'expires_in'],
|
|
121
|
+
properties: {
|
|
122
|
+
request_uri: refersTo(requestURISchema),
|
|
123
|
+
expires_in: {
|
|
124
|
+
type: 'number',
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
'400': {
|
|
132
|
+
description:
|
|
133
|
+
'Request URI error response, see https://datatracker.ietf.org/doc/html/rfc9126#section-2.3.',
|
|
134
|
+
content: {
|
|
135
|
+
'application/json': {
|
|
136
|
+
schema: {
|
|
137
|
+
type: 'object',
|
|
138
|
+
required: ['error'],
|
|
139
|
+
properties: {
|
|
140
|
+
error: {
|
|
141
|
+
type: 'string',
|
|
142
|
+
enum: [
|
|
143
|
+
'invalid_request',
|
|
144
|
+
'invalid_scope',
|
|
145
|
+
'invalid_client',
|
|
146
|
+
'unauthorized_client',
|
|
147
|
+
'unsupported_response_type',
|
|
148
|
+
],
|
|
149
|
+
},
|
|
150
|
+
error_description: { type: 'string' },
|
|
151
|
+
error_uri: { type: 'string' },
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
} as const satisfies WhookRouteDefinition;
|
|
160
|
+
|
|
161
|
+
async function initPostOAuth2PushedAuthorizationRequest({
|
|
162
|
+
OAUTH2,
|
|
163
|
+
OAUTH2_PAR = DEFAULT_OAUTH2_PAR,
|
|
164
|
+
oAuth2Granters,
|
|
165
|
+
oAuth2AuthorizationRequests,
|
|
166
|
+
readClientGrants,
|
|
167
|
+
log = noop,
|
|
168
|
+
}: WhookOAuth2AuthorizationRequestsConfig & {
|
|
169
|
+
OAUTH2: WhookOAuth2Options;
|
|
170
|
+
oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
|
|
171
|
+
oAuth2AuthorizationRequests: Pick<
|
|
172
|
+
WhookOAuth2AuthorizationRequestsService,
|
|
173
|
+
'create'
|
|
174
|
+
>;
|
|
175
|
+
readClientGrants: WhookOAuth2ReadClientGrantsService;
|
|
176
|
+
log?: LogService;
|
|
177
|
+
}) {
|
|
178
|
+
if (OAUTH2_PAR.mode === 'disabled') {
|
|
179
|
+
log('error', `💥 - OAuth2 PAR endpoint should be disabled when PAR is.`);
|
|
180
|
+
throw new YError('E_OAUTH2_MISCONFIGURED');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return async ({
|
|
184
|
+
body: {
|
|
185
|
+
response_type: responseType,
|
|
186
|
+
client_id: givenClientId,
|
|
187
|
+
redirect_uri: demandedRedirectURI = '',
|
|
188
|
+
scope: demandedScope = '',
|
|
189
|
+
state,
|
|
190
|
+
code_challenge: codeChallenge,
|
|
191
|
+
code_challenge_method: codeChallengeMethod,
|
|
192
|
+
request_uri: requestURI,
|
|
193
|
+
},
|
|
194
|
+
authenticationData: optionalAuthenticationData,
|
|
195
|
+
}: {
|
|
196
|
+
body: WhookOAuth2AuthorizeRequestParameters & {
|
|
197
|
+
request_uri?: string;
|
|
198
|
+
} & Record<string, string>;
|
|
199
|
+
authenticationData?: WhookAuthenticationData;
|
|
200
|
+
}) => {
|
|
201
|
+
const usableClientId = toUsableClientId([
|
|
202
|
+
optionalAuthenticationData?.clientId,
|
|
203
|
+
givenClientId,
|
|
204
|
+
]);
|
|
205
|
+
|
|
206
|
+
const clientGrants = await readClientGrants(usableClientId);
|
|
207
|
+
|
|
208
|
+
if (usableClientId !== clientGrants.authenticationData.clientId) {
|
|
209
|
+
throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
|
|
210
|
+
usableClientId,
|
|
211
|
+
clientGrants.authenticationData.clientId,
|
|
212
|
+
]);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (!clientGrants.isPublicClient) {
|
|
216
|
+
if (!optionalAuthenticationData) {
|
|
217
|
+
throw new YError('E_OAUTH2_AUTHENTICATION_REQUIRED', [usableClientId]);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const usableRedirectURI = getUsableRedirectURI(
|
|
222
|
+
clientGrants.allowedRedirectURIS,
|
|
223
|
+
demandedRedirectURI,
|
|
224
|
+
);
|
|
225
|
+
const demandedScopes = filterScopes(
|
|
226
|
+
filterScopes(
|
|
227
|
+
parseOAuth2Scope(demandedScope),
|
|
228
|
+
OAUTH2.allowedScopes,
|
|
229
|
+
!!OAUTH2.strictScopesChecks,
|
|
230
|
+
),
|
|
231
|
+
clientGrants.allowedScopes,
|
|
232
|
+
!!OAUTH2.strictScopesChecks,
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
if (requestURI) {
|
|
236
|
+
throw new YError('E_OAUTH2_REQUEST_URI_NOT_ALLOWED');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
checkCodeChallengeParameters(responseType, codeChallenge, OAUTH2.forcePKCE);
|
|
240
|
+
|
|
241
|
+
const granter = oAuth2Granters.find(
|
|
242
|
+
(granter) => granter.responseType === responseType,
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
if (!granter?.authorize) {
|
|
246
|
+
throw new YError('E_OAUTH2_UNKNOWN_RESPONSE_TYPE', [responseType]);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const { scopes } = await granter.authorize({
|
|
250
|
+
clientId: usableClientId,
|
|
251
|
+
clientGrants,
|
|
252
|
+
demandedScopes,
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const paramsHash: WhookOAuth2AuthorizeRequestParameters = {
|
|
256
|
+
response_type: responseType,
|
|
257
|
+
redirect_uri: usableRedirectURI,
|
|
258
|
+
scope: stringifyScopes(scopes),
|
|
259
|
+
client_id: usableClientId,
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
if (responseType === AUTHORIZATION_CODE_RESPONSE_TYPE && codeChallenge) {
|
|
263
|
+
paramsHash.code_challenge = codeChallenge;
|
|
264
|
+
paramsHash.code_challenge_method =
|
|
265
|
+
codeChallengeMethod || PLAIN_CODE_CHALLENGE_METHOD;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (state) {
|
|
269
|
+
paramsHash.state = state;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const { requestURI: pushedRequestURI, expiresIn } =
|
|
273
|
+
await oAuth2AuthorizationRequests.create(usableClientId, paramsHash);
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
status: 201,
|
|
277
|
+
headers: {
|
|
278
|
+
'cache-control': 'no-store',
|
|
279
|
+
pragma: 'no-cache',
|
|
280
|
+
},
|
|
281
|
+
body: {
|
|
282
|
+
request_uri: pushedRequestURI,
|
|
283
|
+
expires_in: Math.floor(expiresIn / 1000),
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export default location(
|
|
290
|
+
autoService(initPostOAuth2PushedAuthorizationRequest),
|
|
291
|
+
import.meta.url,
|
|
292
|
+
);
|
|
@@ -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);
|