@whook/oauth2 8.3.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.
- package/LICENSE +20 -0
- package/README.md +384 -0
- package/dist/handlers/getOAuth2Authorize.d.ts +26 -0
- package/dist/handlers/getOAuth2Authorize.js +192 -0
- package/dist/handlers/getOAuth2Authorize.js.map +1 -0
- package/dist/handlers/getOAuth2Authorize.mjs +169 -0
- package/dist/handlers/getOAuth2Authorize.mjs.map +1 -0
- package/dist/handlers/getOAuth2Authorize.test.d.ts +1 -0
- package/dist/handlers/getOAuth2Authorize.test.js +122 -0
- package/dist/handlers/getOAuth2Authorize.test.js.map +1 -0
- package/dist/handlers/getOAuth2Authorize.test.mjs +114 -0
- package/dist/handlers/getOAuth2Authorize.test.mjs.map +1 -0
- package/dist/handlers/postAuthLogin.d.ts +45 -0
- package/dist/handlers/postAuthLogin.js +125 -0
- package/dist/handlers/postAuthLogin.js.map +1 -0
- package/dist/handlers/postAuthLogin.mjs +108 -0
- package/dist/handlers/postAuthLogin.mjs.map +1 -0
- package/dist/handlers/postAuthLogin.test.d.ts +1 -0
- package/dist/handlers/postAuthLogin.test.js +90 -0
- package/dist/handlers/postAuthLogin.test.js.map +1 -0
- package/dist/handlers/postAuthLogin.test.mjs +85 -0
- package/dist/handlers/postAuthLogin.test.mjs.map +1 -0
- package/dist/handlers/postAuthLogout.d.ts +12 -0
- package/dist/handlers/postAuthLogout.js +46 -0
- package/dist/handlers/postAuthLogout.js.map +1 -0
- package/dist/handlers/postAuthLogout.mjs +33 -0
- package/dist/handlers/postAuthLogout.mjs.map +1 -0
- package/dist/handlers/postAuthLogout.test.d.ts +1 -0
- package/dist/handlers/postAuthLogout.test.js +35 -0
- package/dist/handlers/postAuthLogout.test.js.map +1 -0
- package/dist/handlers/postAuthLogout.test.mjs +30 -0
- package/dist/handlers/postAuthLogout.test.mjs.map +1 -0
- package/dist/handlers/postAuthRefresh.d.ts +45 -0
- package/dist/handlers/postAuthRefresh.js +140 -0
- package/dist/handlers/postAuthRefresh.js.map +1 -0
- package/dist/handlers/postAuthRefresh.mjs +122 -0
- package/dist/handlers/postAuthRefresh.mjs.map +1 -0
- package/dist/handlers/postAuthRefresh.test.d.ts +1 -0
- package/dist/handlers/postAuthRefresh.test.js +99 -0
- package/dist/handlers/postAuthRefresh.test.js.map +1 -0
- package/dist/handlers/postAuthRefresh.test.mjs +94 -0
- package/dist/handlers/postAuthRefresh.test.mjs.map +1 -0
- package/dist/handlers/postOAuth2Acknowledge.d.ts +28 -0
- package/dist/handlers/postOAuth2Acknowledge.js +165 -0
- package/dist/handlers/postOAuth2Acknowledge.js.map +1 -0
- package/dist/handlers/postOAuth2Acknowledge.mjs +150 -0
- package/dist/handlers/postOAuth2Acknowledge.mjs.map +1 -0
- package/dist/handlers/postOAuth2Acknowledge.test.d.ts +1 -0
- package/dist/handlers/postOAuth2Acknowledge.test.js +148 -0
- package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -0
- package/dist/handlers/postOAuth2Acknowledge.test.mjs +140 -0
- package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +1 -0
- package/dist/handlers/postOAuth2Token.d.ts +36 -0
- package/dist/handlers/postOAuth2Token.js +263 -0
- package/dist/handlers/postOAuth2Token.js.map +1 -0
- package/dist/handlers/postOAuth2Token.mjs +242 -0
- package/dist/handlers/postOAuth2Token.mjs.map +1 -0
- package/dist/handlers/postOAuth2Token.test.d.ts +1 -0
- package/dist/handlers/postOAuth2Token.test.js +166 -0
- package/dist/handlers/postOAuth2Token.test.js.map +1 -0
- package/dist/handlers/postOAuth2Token.test.mjs +160 -0
- package/dist/handlers/postOAuth2Token.test.mjs.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +230 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +15 -0
- package/dist/index.mjs.map +1 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +759 -0
- package/dist/index.test.js.map +1 -0
- package/dist/index.test.mjs +748 -0
- package/dist/index.test.mjs.map +1 -0
- package/dist/services/authCookies.d.ts +26 -0
- package/dist/services/authCookies.js +74 -0
- package/dist/services/authCookies.js.map +1 -0
- package/dist/services/authCookies.mjs +58 -0
- package/dist/services/authCookies.mjs.map +1 -0
- package/dist/services/authCookies.test.d.ts +1 -0
- package/dist/services/authCookies.test.js +82 -0
- package/dist/services/authCookies.test.js.map +1 -0
- package/dist/services/authCookies.test.mjs +77 -0
- package/dist/services/authCookies.test.mjs.map +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.d.ts +17 -0
- package/dist/services/oAuth2ClientCredentialsGranter.js +56 -0
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.mjs +41 -0
- package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +1 -0
- package/dist/services/oAuth2CodeGranter.d.ts +17 -0
- package/dist/services/oAuth2CodeGranter.js +115 -0
- package/dist/services/oAuth2CodeGranter.js.map +1 -0
- package/dist/services/oAuth2CodeGranter.mjs +98 -0
- package/dist/services/oAuth2CodeGranter.mjs.map +1 -0
- package/dist/services/oAuth2CodeGranter.test.d.ts +1 -0
- package/dist/services/oAuth2CodeGranter.test.js +86 -0
- package/dist/services/oAuth2CodeGranter.test.js.map +1 -0
- package/dist/services/oAuth2CodeGranter.test.mjs +81 -0
- package/dist/services/oAuth2CodeGranter.test.mjs.map +1 -0
- package/dist/services/oAuth2Granters.d.ts +79 -0
- package/dist/services/oAuth2Granters.js +140 -0
- package/dist/services/oAuth2Granters.js.map +1 -0
- package/dist/services/oAuth2Granters.mjs +127 -0
- package/dist/services/oAuth2Granters.mjs.map +1 -0
- package/dist/services/oAuth2PasswordGranter.d.ts +16 -0
- package/dist/services/oAuth2PasswordGranter.js +55 -0
- package/dist/services/oAuth2PasswordGranter.js.map +1 -0
- package/dist/services/oAuth2PasswordGranter.mjs +40 -0
- package/dist/services/oAuth2PasswordGranter.mjs.map +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +15 -0
- package/dist/services/oAuth2RefreshTokenGranter.js +62 -0
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.mjs +47 -0
- package/dist/services/oAuth2RefreshTokenGranter.mjs.map +1 -0
- package/dist/services/oAuth2TokenGranter.d.ts +15 -0
- package/dist/services/oAuth2TokenGranter.js +92 -0
- package/dist/services/oAuth2TokenGranter.js.map +1 -0
- package/dist/services/oAuth2TokenGranter.mjs +78 -0
- package/dist/services/oAuth2TokenGranter.mjs.map +1 -0
- package/package.json +205 -0
- package/src/__snapshots__/index.test.ts.snap +396 -0
- package/src/handlers/__snapshots__/getOAuth2Authorize.test.ts.snap +38 -0
- package/src/handlers/__snapshots__/postOAuth2Acknowledge.test.ts.snap +61 -0
- package/src/handlers/__snapshots__/postOAuth2Token.test.ts.snap +52 -0
- package/src/handlers/getOAuth2Authorize.test.ts +142 -0
- package/src/handlers/getOAuth2Authorize.ts +222 -0
- package/src/handlers/postAuthLogin.test.ts +88 -0
- package/src/handlers/postAuthLogin.ts +123 -0
- package/src/handlers/postAuthLogout.test.ts +33 -0
- package/src/handlers/postAuthLogout.ts +38 -0
- package/src/handlers/postAuthRefresh.test.ts +97 -0
- package/src/handlers/postAuthRefresh.ts +146 -0
- package/src/handlers/postOAuth2Acknowledge.test.ts +175 -0
- package/src/handlers/postOAuth2Acknowledge.ts +209 -0
- package/src/handlers/postOAuth2Token.test.ts +153 -0
- package/src/handlers/postOAuth2Token.ts +302 -0
- package/src/index.test.ts +936 -0
- package/src/index.ts +105 -0
- package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +49 -0
- package/src/services/authCookies.test.ts +93 -0
- package/src/services/authCookies.ts +82 -0
- package/src/services/oAuth2ClientCredentialsGranter.ts +76 -0
- package/src/services/oAuth2CodeGranter.test.ts +94 -0
- package/src/services/oAuth2CodeGranter.ts +133 -0
- package/src/services/oAuth2Granters.ts +274 -0
- package/src/services/oAuth2PasswordGranter.ts +75 -0
- package/src/services/oAuth2RefreshTokenGranter.ts +77 -0
- package/src/services/oAuth2TokenGranter.ts +105 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { handler as _handler } from 'knifecycle';
|
|
2
|
+
import { camelCaseObjectProperties } from './getOAuth2Authorize';
|
|
3
|
+
import { noop } from '@whook/whook';
|
|
4
|
+
import YError from 'yerror';
|
|
5
|
+
|
|
6
|
+
/* Architecture Note #2: OAuth2 acknowledge
|
|
7
|
+
This endpoint is to be used by the authentication server page
|
|
8
|
+
to acknowlege that the user accepted the client request.
|
|
9
|
+
*/
|
|
10
|
+
export const authorizationCodeTokenRequestBodySchema = {
|
|
11
|
+
name: 'AuthorizationCodeRequestBody',
|
|
12
|
+
schema: {
|
|
13
|
+
type: 'object',
|
|
14
|
+
description: 'Authorization code grant, see https://tools.ietf.org/html/rfc6749#section-4.1',
|
|
15
|
+
required: ['grant_type'],
|
|
16
|
+
properties: {
|
|
17
|
+
grant_type: {
|
|
18
|
+
type: 'string',
|
|
19
|
+
enum: ['authorization_code']
|
|
20
|
+
},
|
|
21
|
+
code: {
|
|
22
|
+
type: 'string'
|
|
23
|
+
},
|
|
24
|
+
client_id: {
|
|
25
|
+
type: 'string'
|
|
26
|
+
},
|
|
27
|
+
redirect_uri: {
|
|
28
|
+
type: 'string',
|
|
29
|
+
pattern: '^https?://',
|
|
30
|
+
format: 'uri'
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
export const passwordTokenRequestBodySchema = {
|
|
36
|
+
name: 'PasswordRequestBody',
|
|
37
|
+
schema: {
|
|
38
|
+
type: 'object',
|
|
39
|
+
description: 'Resource owner password credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.3',
|
|
40
|
+
required: ['grant_type', 'username', 'password'],
|
|
41
|
+
properties: {
|
|
42
|
+
grant_type: {
|
|
43
|
+
type: 'string',
|
|
44
|
+
enum: ['password']
|
|
45
|
+
},
|
|
46
|
+
username: {
|
|
47
|
+
type: 'string'
|
|
48
|
+
},
|
|
49
|
+
password: {
|
|
50
|
+
type: 'string'
|
|
51
|
+
},
|
|
52
|
+
scope: {
|
|
53
|
+
type: 'string',
|
|
54
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3'
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
export const clientCredentialsTokenRequestBodySchema = {
|
|
60
|
+
name: 'ClientCredentialsRequestBody',
|
|
61
|
+
schema: {
|
|
62
|
+
type: 'object',
|
|
63
|
+
description: 'Client credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.4',
|
|
64
|
+
required: ['grant_type'],
|
|
65
|
+
properties: {
|
|
66
|
+
grant_type: {
|
|
67
|
+
type: 'string',
|
|
68
|
+
enum: ['client_credentials']
|
|
69
|
+
},
|
|
70
|
+
scope: {
|
|
71
|
+
type: 'string',
|
|
72
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3'
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
export const refreshTokenRequestBodySchema = {
|
|
78
|
+
name: 'RefreshTokenRequestBody',
|
|
79
|
+
schema: {
|
|
80
|
+
type: 'object',
|
|
81
|
+
description: 'Token refresh grant type, see https://tools.ietf.org/html/rfc6749#section-6 .',
|
|
82
|
+
required: ['grant_type', 'refresh_token'],
|
|
83
|
+
properties: {
|
|
84
|
+
grant_type: {
|
|
85
|
+
type: 'string',
|
|
86
|
+
enum: ['refresh_token']
|
|
87
|
+
},
|
|
88
|
+
refresh_token: {
|
|
89
|
+
type: 'string'
|
|
90
|
+
},
|
|
91
|
+
scope: {
|
|
92
|
+
type: 'string',
|
|
93
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3'
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
export const tokenBodySchema = {
|
|
99
|
+
name: 'TokenRequestBody',
|
|
100
|
+
schema: {
|
|
101
|
+
oneOf: [{
|
|
102
|
+
$ref: `#/components/schemas/${passwordTokenRequestBodySchema.name}`
|
|
103
|
+
}, {
|
|
104
|
+
$ref: `#/components/schemas/${authorizationCodeTokenRequestBodySchema.name}`
|
|
105
|
+
}, {
|
|
106
|
+
$ref: `#/components/schemas/${clientCredentialsTokenRequestBodySchema.name}`
|
|
107
|
+
}, {
|
|
108
|
+
$ref: `#/components/schemas/${refreshTokenRequestBodySchema.name}`
|
|
109
|
+
}]
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
export const definition = {
|
|
113
|
+
method: 'post',
|
|
114
|
+
path: '/oauth2/token',
|
|
115
|
+
operation: {
|
|
116
|
+
operationId: 'postOAuth2Token',
|
|
117
|
+
summary: `Implements the [Token Endpoint](https://tools.ietf.org/html/rfc6749#section-3.2)
|
|
118
|
+
as defined per the OAuth2 RFC.`,
|
|
119
|
+
tags: ['oauth2'],
|
|
120
|
+
parameters: [],
|
|
121
|
+
requestBody: {
|
|
122
|
+
required: true,
|
|
123
|
+
content: {
|
|
124
|
+
'application/x-www-form-urlencoded': {
|
|
125
|
+
schema: {
|
|
126
|
+
$ref: `#/components/schemas/${tokenBodySchema.name}`
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
'application/json': {
|
|
130
|
+
schema: {
|
|
131
|
+
$ref: `#/components/schemas/${tokenBodySchema.name}`
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
responses: {
|
|
137
|
+
'200': {
|
|
138
|
+
description: 'Token successfully issued, see https://tools.ietf.org/html/rfc6749#section-5 .',
|
|
139
|
+
content: {
|
|
140
|
+
'application/json': {
|
|
141
|
+
schema: {
|
|
142
|
+
type: 'object',
|
|
143
|
+
required: ['access_token', 'token_type'],
|
|
144
|
+
properties: {
|
|
145
|
+
access_token: {
|
|
146
|
+
type: 'string'
|
|
147
|
+
},
|
|
148
|
+
token_type: {
|
|
149
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-7.1',
|
|
150
|
+
type: 'string',
|
|
151
|
+
enum: ['bearer', 'mac']
|
|
152
|
+
},
|
|
153
|
+
expires_in: {
|
|
154
|
+
description: 'The lifetime in seconds of the access token',
|
|
155
|
+
type: 'number'
|
|
156
|
+
},
|
|
157
|
+
refresh_token: {
|
|
158
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-6',
|
|
159
|
+
type: 'string'
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
'400': {
|
|
167
|
+
description: 'Access token error response, see https://tools.ietf.org/html/rfc6749#section-5.2',
|
|
168
|
+
content: {
|
|
169
|
+
'application/json': {
|
|
170
|
+
schema: {
|
|
171
|
+
type: 'object',
|
|
172
|
+
required: ['error'],
|
|
173
|
+
properties: {
|
|
174
|
+
error: {
|
|
175
|
+
type: 'string',
|
|
176
|
+
enum: ['invalid_request', 'invalid_client', 'invalid_grant', 'unauthorized_client', 'unsupported_grant_type', 'invalid_scope']
|
|
177
|
+
},
|
|
178
|
+
error_description: {
|
|
179
|
+
type: 'string'
|
|
180
|
+
},
|
|
181
|
+
error_uri: {
|
|
182
|
+
type: 'string'
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
export default _handler(postOAuth2Token, "postOAuth2Token", ["oAuth2Granters", "oAuth2AccessToken", "oAuth2RefreshToken", "?time", "?log"]);
|
|
193
|
+
|
|
194
|
+
async function postOAuth2Token({
|
|
195
|
+
oAuth2Granters,
|
|
196
|
+
oAuth2AccessToken,
|
|
197
|
+
oAuth2RefreshToken,
|
|
198
|
+
time = Date.now.bind(Date),
|
|
199
|
+
log = noop
|
|
200
|
+
}, {
|
|
201
|
+
body: {
|
|
202
|
+
grant_type: grantType,
|
|
203
|
+
...grantParameters
|
|
204
|
+
},
|
|
205
|
+
authenticationData
|
|
206
|
+
}) {
|
|
207
|
+
try {
|
|
208
|
+
const granter = oAuth2Granters.find(granter => granter.authenticator && granter.authenticator.grantType === grantType);
|
|
209
|
+
|
|
210
|
+
if (!granter) {
|
|
211
|
+
throw new YError('E_UNKNOWN_AUTHENTICATOR_TYPE', grantType);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const newAuthenticationData = await granter.authenticator.authenticate(camelCaseObjectProperties(grantParameters), authenticationData);
|
|
215
|
+
const [{
|
|
216
|
+
token: accessToken,
|
|
217
|
+
expiresAt: accessTokenExpiresAt
|
|
218
|
+
}, {
|
|
219
|
+
token: refreshToken,
|
|
220
|
+
expiresAt: refreshTokenExpiresAt
|
|
221
|
+
}] = await Promise.all([oAuth2AccessToken.create(authenticationData, newAuthenticationData), oAuth2RefreshToken.create(authenticationData, newAuthenticationData)]);
|
|
222
|
+
const currentTime = time();
|
|
223
|
+
return {
|
|
224
|
+
status: 200,
|
|
225
|
+
headers: {},
|
|
226
|
+
body: {
|
|
227
|
+
access_token: accessToken,
|
|
228
|
+
token_type: 'bearer',
|
|
229
|
+
expires_in: Math.ceil((accessTokenExpiresAt - currentTime) / 1000),
|
|
230
|
+
expiration_date: new Date(accessTokenExpiresAt).toISOString(),
|
|
231
|
+
refresh_token: refreshToken,
|
|
232
|
+
refresh_token_expires_in: Math.ceil((refreshTokenExpiresAt - currentTime) / 1000),
|
|
233
|
+
refresh_token_expiration_date: new Date(refreshTokenExpiresAt).toISOString()
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
} catch (err) {
|
|
237
|
+
log('debug', '👫 - OAuth2 token issuing error', err.code);
|
|
238
|
+
log('debug-stack', err.stack);
|
|
239
|
+
throw YError.cast(err, 'E_OAUTH2');
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
//# sourceMappingURL=postOAuth2Token.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/handlers/postOAuth2Token.ts"],"names":["autoHandler","camelCaseObjectProperties","noop","YError","authorizationCodeTokenRequestBodySchema","name","schema","type","description","required","properties","grant_type","enum","code","client_id","redirect_uri","pattern","format","passwordTokenRequestBodySchema","username","password","scope","clientCredentialsTokenRequestBodySchema","refreshTokenRequestBodySchema","refresh_token","tokenBodySchema","oneOf","$ref","definition","method","path","operation","operationId","summary","tags","parameters","requestBody","content","responses","access_token","token_type","expires_in","error","error_description","error_uri","postOAuth2Token","oAuth2Granters","oAuth2AccessToken","oAuth2RefreshToken","time","Date","now","bind","log","body","grantType","grantParameters","authenticationData","granter","find","authenticator","newAuthenticationData","authenticate","token","accessToken","expiresAt","accessTokenExpiresAt","refreshToken","refreshTokenExpiresAt","Promise","all","create","currentTime","status","headers","Math","ceil","expiration_date","toISOString","refresh_token_expires_in","refresh_token_expiration_date","err","stack","cast"],"mappings":"AAAA,SAASA,OAAW,IAAXA,QAAT,QAA4B,YAA5B;AACA,SAASC,yBAAT,QAA0C,sBAA1C;AACA,SAASC,IAAT,QAAqB,cAArB;AACA,OAAOC,MAAP,MAAmB,QAAnB;;AAcA;AACA;AACA;AACA;AACA,OAAO,MAAMC,uCAAiE,GAC5E;AACEC,EAAAA,IAAI,EAAE,8BADR;AAEEC,EAAAA,MAAM,EAAE;AACNC,IAAAA,IAAI,EAAE,QADA;AAENC,IAAAA,WAAW,EACT,+EAHI;AAINC,IAAAA,QAAQ,EAAE,CAAC,YAAD,CAJJ;AAKNC,IAAAA,UAAU,EAAE;AACVC,MAAAA,UAAU,EAAE;AACVJ,QAAAA,IAAI,EAAE,QADI;AAEVK,QAAAA,IAAI,EAAE,CAAC,oBAAD;AAFI,OADF;AAKVC,MAAAA,IAAI,EAAE;AACJN,QAAAA,IAAI,EAAE;AADF,OALI;AAQVO,MAAAA,SAAS,EAAE;AACTP,QAAAA,IAAI,EAA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getOAuth2Authorize';\nimport { noop } from '@whook/whook';\nimport YError from 'yerror';\nimport type { OpenAPIV3 } from 'openapi-types';\nimport type { LogService, TimeService } from 'common-services';\nimport type {\n WhookAPISchemaDefinition,\n WhookAPIHandlerDefinition,\n} from '@whook/whook';\nimport type {\n OAuth2GranterService,\n OAuth2AccessTokenService,\n OAuth2RefreshTokenService,\n} from '../services/oAuth2Granters';\nimport type { BaseAuthenticationData } from '@whook/authorization';\n\n/* Architecture Note #2: OAuth2 acknowledge\nThis endpoint is to be used by the authentication server page\n to acknowlege that the user accepted the client request.\n*/\nexport const authorizationCodeTokenRequestBodySchema: WhookAPISchemaDefinition =\n {\n name: 'AuthorizationCodeRequestBody',\n schema: {\n type: 'object',\n description:\n 'Authorization code grant, see https://tools.ietf.org/html/rfc6749#section-4.1',\n required: ['grant_type'],\n properties: {\n grant_type: {\n type: 'string',\n enum: ['authorization_code'],\n },\n code: {\n type: 'string',\n },\n client_id: {\n type: 'string',\n },\n redirect_uri: {\n type: 'string',\n pattern: '^https?://',\n format: 'uri',\n },\n },\n },\n };\n\nexport const passwordTokenRequestBodySchema: WhookAPISchemaDefinition = {\n name: 'PasswordRequestBody',\n schema: {\n type: 'object',\n description:\n 'Resource owner password credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.3',\n required: ['grant_type', 'username', 'password'],\n properties: {\n grant_type: {\n type: 'string',\n enum: ['password'],\n },\n username: {\n type: 'string',\n },\n password: {\n type: 'string',\n },\n scope: {\n type: 'string',\n description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',\n },\n },\n },\n};\n\nexport const clientCredentialsTokenRequestBodySchema: WhookAPISchemaDefinition =\n {\n name: 'ClientCredentialsRequestBody',\n schema: {\n type: 'object',\n description:\n 'Client credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.4',\n required: ['grant_type'],\n properties: {\n grant_type: {\n type: 'string',\n enum: ['client_credentials'],\n },\n scope: {\n type: 'string',\n description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',\n },\n },\n },\n };\n\nexport const refreshTokenRequestBodySchema: WhookAPISchemaDefinition = {\n name: 'RefreshTokenRequestBody',\n schema: {\n type: 'object',\n description:\n 'Token refresh grant type, see https://tools.ietf.org/html/rfc6749#section-6 .',\n required: ['grant_type', 'refresh_token'],\n properties: {\n grant_type: {\n type: 'string',\n enum: ['refresh_token'],\n },\n refresh_token: {\n type: 'string',\n },\n scope: {\n type: 'string',\n description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',\n },\n },\n },\n};\n\nexport const tokenBodySchema: WhookAPISchemaDefinition = {\n name: 'TokenRequestBody',\n schema: {\n oneOf: [\n {\n $ref: `#/components/schemas/${passwordTokenRequestBodySchema.name}`,\n },\n {\n $ref: `#/components/schemas/${authorizationCodeTokenRequestBodySchema.name}`,\n },\n {\n $ref: `#/components/schemas/${clientCredentialsTokenRequestBodySchema.name}`,\n },\n {\n $ref: `#/components/schemas/${refreshTokenRequestBodySchema.name}`,\n },\n ],\n } as OpenAPIV3.SchemaObject,\n};\n\nexport const definition: WhookAPIHandlerDefinition = {\n method: 'post',\n path: '/oauth2/token',\n operation: {\n operationId: 'postOAuth2Token',\n summary: `Implements the [Token Endpoint](https://tools.ietf.org/html/rfc6749#section-3.2)\n as defined per the OAuth2 RFC.`,\n tags: ['oauth2'],\n parameters: [],\n requestBody: {\n required: true,\n content: {\n 'application/x-www-form-urlencoded': {\n schema: {\n $ref: `#/components/schemas/${tokenBodySchema.name}`,\n },\n },\n 'application/json': {\n schema: {\n $ref: `#/components/schemas/${tokenBodySchema.name}`,\n },\n },\n },\n },\n responses: {\n '200': {\n description:\n 'Token successfully issued, see https://tools.ietf.org/html/rfc6749#section-5 .',\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n required: ['access_token', 'token_type'],\n properties: {\n access_token: { type: 'string' },\n token_type: {\n description:\n 'See https://tools.ietf.org/html/rfc6749#section-7.1',\n type: 'string',\n enum: ['bearer', 'mac'],\n },\n expires_in: {\n description: 'The lifetime in seconds of the access token',\n type: 'number',\n },\n refresh_token: {\n description:\n 'See https://tools.ietf.org/html/rfc6749#section-6',\n type: 'string',\n },\n },\n },\n },\n },\n },\n '400': {\n description:\n 'Access token error response, see https://tools.ietf.org/html/rfc6749#section-5.2',\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n required: ['error'],\n properties: {\n error: {\n type: 'string',\n enum: [\n 'invalid_request',\n 'invalid_client',\n 'invalid_grant',\n 'unauthorized_client',\n 'unsupported_grant_type',\n 'invalid_scope',\n ],\n },\n error_description: { type: 'string' },\n error_uri: { type: 'string' },\n },\n },\n },\n },\n },\n },\n },\n};\n\nexport default autoHandler(postOAuth2Token);\n\nasync function postOAuth2Token<\n AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,\n>(\n {\n oAuth2Granters,\n oAuth2AccessToken,\n oAuth2RefreshToken,\n time = Date.now.bind(Date),\n log = noop,\n }: {\n oAuth2Granters: OAuth2GranterService[];\n oAuth2AccessToken: OAuth2AccessTokenService;\n oAuth2RefreshToken: OAuth2RefreshTokenService;\n log: LogService;\n time: TimeService;\n },\n {\n body: { grant_type: grantType, ...grantParameters },\n authenticationData,\n }: {\n body: {\n grant_type: string;\n [name: string]: unknown;\n };\n authenticationData: AUTHENTICATION_DATA;\n },\n) {\n try {\n const granter = oAuth2Granters.find(\n (granter) =>\n granter.authenticator && granter.authenticator.grantType === grantType,\n );\n\n if (!granter) {\n throw new YError('E_UNKNOWN_AUTHENTICATOR_TYPE', grantType);\n }\n\n const newAuthenticationData = await granter.authenticator.authenticate(\n camelCaseObjectProperties(grantParameters),\n authenticationData,\n );\n\n const [\n { token: accessToken, expiresAt: accessTokenExpiresAt },\n { token: refreshToken, expiresAt: refreshTokenExpiresAt },\n ] = await Promise.all([\n oAuth2AccessToken.create(authenticationData, newAuthenticationData),\n oAuth2RefreshToken.create(authenticationData, newAuthenticationData),\n ]);\n const currentTime = time();\n\n return {\n status: 200,\n headers: {},\n body: {\n access_token: accessToken,\n token_type: 'bearer',\n expires_in: Math.ceil((accessTokenExpiresAt - currentTime) / 1000),\n expiration_date: new Date(accessTokenExpiresAt).toISOString(),\n refresh_token: refreshToken,\n refresh_token_expires_in: Math.ceil(\n (refreshTokenExpiresAt - currentTime) / 1000,\n ),\n refresh_token_expiration_date: new Date(\n refreshTokenExpiresAt,\n ).toISOString(),\n },\n };\n } catch (err) {\n log('debug', '👫 - OAuth2 token issuing error', err.code);\n log('debug-stack', err.stack);\n\n throw YError.cast(err, 'E_OAUTH2');\n }\n}\n"],"file":"postOAuth2Token.mjs"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _postOAuth2Token = _interopRequireDefault(require("./postOAuth2Token"));
|
|
4
|
+
|
|
5
|
+
var _yerror = _interopRequireDefault(require("yerror"));
|
|
6
|
+
|
|
7
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
8
|
+
|
|
9
|
+
describe('postOAuth2Token', () => {
|
|
10
|
+
const log = jest.fn();
|
|
11
|
+
const time = jest.fn();
|
|
12
|
+
const oAuth2AccessToken = {
|
|
13
|
+
create: jest.fn(),
|
|
14
|
+
check: jest.fn()
|
|
15
|
+
};
|
|
16
|
+
const oAuth2RefreshToken = {
|
|
17
|
+
create: jest.fn(),
|
|
18
|
+
check: jest.fn()
|
|
19
|
+
};
|
|
20
|
+
const codeGranter = {
|
|
21
|
+
type: 'code',
|
|
22
|
+
authorizer: {
|
|
23
|
+
responseType: 'code',
|
|
24
|
+
authorize: jest.fn()
|
|
25
|
+
},
|
|
26
|
+
acknowledger: {
|
|
27
|
+
acknowledgmentType: 'code',
|
|
28
|
+
acknowledge: jest.fn()
|
|
29
|
+
},
|
|
30
|
+
authenticator: {
|
|
31
|
+
grantType: 'authorization_code',
|
|
32
|
+
authenticate: jest.fn()
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const passwordGranter = {
|
|
36
|
+
type: 'password',
|
|
37
|
+
authenticator: {
|
|
38
|
+
grantType: 'password',
|
|
39
|
+
authenticate: jest.fn()
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const clientCredentialsGranter = {
|
|
43
|
+
type: 'client_credentials',
|
|
44
|
+
authenticator: {
|
|
45
|
+
grantType: 'client_credentials',
|
|
46
|
+
authenticate: jest.fn()
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const tokenGranter = {
|
|
50
|
+
type: 'token',
|
|
51
|
+
authorizer: {
|
|
52
|
+
responseType: 'token',
|
|
53
|
+
authorize: jest.fn()
|
|
54
|
+
},
|
|
55
|
+
acknowledger: {
|
|
56
|
+
acknowledgmentType: 'token',
|
|
57
|
+
acknowledge: jest.fn()
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const refreshTokenGranter = {
|
|
61
|
+
type: 'refresh',
|
|
62
|
+
authenticator: {
|
|
63
|
+
grantType: 'refresh',
|
|
64
|
+
authenticate: jest.fn()
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const oAuth2Granters = [codeGranter, tokenGranter, passwordGranter, refreshTokenGranter, clientCredentialsGranter];
|
|
68
|
+
beforeEach(() => {
|
|
69
|
+
log.mockReset();
|
|
70
|
+
time.mockReset();
|
|
71
|
+
oAuth2AccessToken.create.mockReset();
|
|
72
|
+
oAuth2AccessToken.check.mockReset();
|
|
73
|
+
oAuth2RefreshToken.create.mockReset();
|
|
74
|
+
oAuth2RefreshToken.check.mockReset();
|
|
75
|
+
codeGranter.authorizer.authorize.mockReset();
|
|
76
|
+
codeGranter.acknowledger.acknowledge.mockReset();
|
|
77
|
+
codeGranter.authenticator.authenticate.mockReset();
|
|
78
|
+
tokenGranter.authorizer.authorize.mockReset();
|
|
79
|
+
tokenGranter.acknowledger.acknowledge.mockReset();
|
|
80
|
+
passwordGranter.authenticator.authenticate.mockReset();
|
|
81
|
+
refreshTokenGranter.authenticator.authenticate.mockReset();
|
|
82
|
+
clientCredentialsGranter.authenticator.authenticate.mockReset();
|
|
83
|
+
});
|
|
84
|
+
test('should create a token', async () => {
|
|
85
|
+
time.mockReturnValue(new Date('2010-03-06T00:00:00Z').getTime());
|
|
86
|
+
oAuth2AccessToken.create.mockResolvedValueOnce({
|
|
87
|
+
token: 'an_access_token',
|
|
88
|
+
expiresAt: Date.parse('2010-03-07T00:00:00Z')
|
|
89
|
+
});
|
|
90
|
+
oAuth2RefreshToken.create.mockResolvedValueOnce({
|
|
91
|
+
token: 'a_refresh_token',
|
|
92
|
+
expiresAt: Date.parse('2180-03-06T00:00:00Z')
|
|
93
|
+
});
|
|
94
|
+
codeGranter.authorizer.authorize.mockResolvedValueOnce({
|
|
95
|
+
applicationId: ''
|
|
96
|
+
});
|
|
97
|
+
const postOAuth2Token = await (0, _postOAuth2Token.default)({
|
|
98
|
+
oAuth2Granters,
|
|
99
|
+
oAuth2AccessToken,
|
|
100
|
+
oAuth2RefreshToken,
|
|
101
|
+
time,
|
|
102
|
+
log
|
|
103
|
+
});
|
|
104
|
+
const response = await postOAuth2Token({
|
|
105
|
+
authenticationData: {
|
|
106
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
107
|
+
scope: 'user'
|
|
108
|
+
},
|
|
109
|
+
body: {
|
|
110
|
+
grant_type: 'authorization_code',
|
|
111
|
+
code: '007',
|
|
112
|
+
client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
113
|
+
redirect_uri: 'http://lol'
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
expect({
|
|
117
|
+
response,
|
|
118
|
+
oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
|
|
119
|
+
oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
|
|
120
|
+
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack'))
|
|
121
|
+
}).toMatchSnapshot();
|
|
122
|
+
});
|
|
123
|
+
test('should fail with a bad grant type', async () => {
|
|
124
|
+
const postOAuth2Token = await (0, _postOAuth2Token.default)({
|
|
125
|
+
oAuth2Granters,
|
|
126
|
+
oAuth2AccessToken,
|
|
127
|
+
oAuth2RefreshToken,
|
|
128
|
+
time,
|
|
129
|
+
log
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
await postOAuth2Token({
|
|
134
|
+
authenticationData: {
|
|
135
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
136
|
+
scope: 'user'
|
|
137
|
+
},
|
|
138
|
+
body: {
|
|
139
|
+
grant_type: 'yolo',
|
|
140
|
+
code: '007',
|
|
141
|
+
client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
142
|
+
redirect_uri: 'http://lol'
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
throw new _yerror.default('E_UNEXPECTED_SUCCESS');
|
|
146
|
+
} catch (err) {
|
|
147
|
+
expect({
|
|
148
|
+
errorCode: err.code,
|
|
149
|
+
errorParams: err.params
|
|
150
|
+
}).toMatchInlineSnapshot(`
|
|
151
|
+
Object {
|
|
152
|
+
"errorCode": "E_UNKNOWN_AUTHENTICATOR_TYPE",
|
|
153
|
+
"errorParams": Array [
|
|
154
|
+
"yolo",
|
|
155
|
+
],
|
|
156
|
+
}
|
|
157
|
+
`);
|
|
158
|
+
expect({
|
|
159
|
+
oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
|
|
160
|
+
oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
|
|
161
|
+
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack'))
|
|
162
|
+
}).toMatchSnapshot();
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
//# sourceMappingURL=postOAuth2Token.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/handlers/postOAuth2Token.test.ts"],"names":["describe","log","jest","fn","time","oAuth2AccessToken","create","check","oAuth2RefreshToken","codeGranter","type","authorizer","responseType","authorize","acknowledger","acknowledgmentType","acknowledge","authenticator","grantType","authenticate","passwordGranter","clientCredentialsGranter","tokenGranter","refreshTokenGranter","oAuth2Granters","beforeEach","mockReset","test","mockReturnValue","Date","getTime","mockResolvedValueOnce","token","expiresAt","parse","applicationId","postOAuth2Token","response","authenticationData","scope","body","grant_type","code","client_id","redirect_uri","expect","oAuth2AccessTokenCreateCalls","mock","calls","oAuth2RefreshTokenCreateCalls","logCalls","filter","endsWith","toMatchSnapshot","YError","err","errorCode","errorParams","params","toMatchInlineSnapshot"],"mappings":";;AAAA;;AACA;;;;AAGAA,QAAQ,CAAC,iBAAD,EAAoB,MAAM;AAChC,QAAMC,GAAG,GAAGC,IAAI,CAACC,EAAL,EAAZ;AACA,QAAMC,IAAI,GAAGF,IAAI,CAACC,EAAL,EAAb;AACA,QAAME,iBAAiB,GAAG;AAAEC,IAAAA,MAAM,EAAEJ,IAAI,CAACC,EAAL,EAAV;AAAqBI,IAAAA,KAAK,EAAEL,IAAI,CAACC,EAAL;AAA5B,GAA1B;AACA,QAAMK,kBAAkB,GAAG;AAAEF,IAAAA,MAAM,EAAEJ,IAAI,CAACC,EAAL,EAAV;AAAqBI,IAAAA,KAAK,EAAEL,IAAI,CAACC,EAAL;AAA5B,GAA3B;AACA,QAAMM,WAAW,GAAG;AAClBC,IAAAA,IAAI,EAAE,MADY;AAElBC,IAAAA,UAAU,EAAE;AAAEC,MAAAA,YAAY,EAAE,MAAhB;AAAwBC,MAAAA,SAAS,EAAEX,IAAI,CAACC,EAAL;AAAnC,KAFM;AAGlBW,IAAAA,YAAY,EAAE;AAAEC,MAAAA,kBAAkB,EAAE,MAAtB;AAA8BC,MAAAA,WAAW,EAAEd,IAAI,CAACC,EAAL;AAA3C,KAHI;AAIlBc,IAAAA,aAAa,EAAE;AACbC,MAAAA,SAAS,EAAE,oBADE;AAEbC,MAAAA,YAAY,EAAEjB,I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,sCADG;AAElBI,UAAAA,KAAK,EAAE;AAFW,SADA;AAKpBC,QAAAA,IAAI,EAAE;AACJC,UAAAA,UAAU,EAAE,MADR;AAEJC,UAAAA,IAAI,EAAE,KAFF;AAGJC,UAAAA,SAAS,EAAE,sCAHP;AAIJC,UAAAA,YAAY,EAAE;AAJV;AALc,OAAD,CAArB;AAYA,YAAM,IAAIU,eAAJ,CAAW,sBAAX,CAAN;AACD,KAdD,CAcE,OAAOC,GAAP,EAAY;AACZV,MAAAA,MAAM,CAAC;AACLW,QAAAA,SAAS,EAAED,GAAG,CAACb,IADV;AAELe,QAAAA,WAAW,EAAEF,GAAG,CAACG;AAFZ,OAAD,CAAN,CAGGC,qBAHH,CAG0B;AAChC;AACA;AACA;AACA;AACA;AACA;AACA,OAVM;AAWAd,MAAAA,MAAM,CAAC;AACLC,QAAAA,4BAA4B,EAAEzC,iBAAiB,CAACC,MAAlB,CAAyByC,IAAzB,CAA8BC,KADvD;AAELC,QAAAA,6BAA6B,EAAEzC,kBAAkB,CAACF,MAAnB,CAA0ByC,IAA1B,CAA+BC,KAFzD;AAGLE,QAAAA,QAAQ,EAAEjD,GAAG,CAAC8C,IAAJ,CAASC,KAAT,CAAeG,MAAf,CAAsB,CAAC,CAACzC,IAAD,CAAD,KAAY,CAACA,IAAI,CAAC0C,QAAL,CAAc,OAAd,CAAnC;AAHL,OAAD,CAAN,CAIGC,eAJH;AAKD;AACF,GAzCG,CAAJ;AA0CD,CApJO,CAAR","sourcesContent":["import initPostOAuth2Token from './postOAuth2Token';\nimport YError from 'yerror';\nimport type { OAuth2GranterService } from '..';\n\ndescribe('postOAuth2Token', () => {\n const log = jest.fn();\n const time = jest.fn();\n const oAuth2AccessToken = { create: jest.fn(), check: jest.fn() };\n const oAuth2RefreshToken = { create: jest.fn(), check: jest.fn() };\n const codeGranter = {\n type: 'code',\n authorizer: { responseType: 'code', authorize: jest.fn() },\n acknowledger: { acknowledgmentType: 'code', acknowledge: jest.fn() },\n authenticator: {\n grantType: 'authorization_code',\n authenticate: jest.fn(),\n },\n };\n const passwordGranter = {\n type: 'password',\n authenticator: {\n grantType: 'password',\n authenticate: jest.fn(),\n },\n };\n const clientCredentialsGranter = {\n type: 'client_credentials',\n authenticator: {\n grantType: 'client_credentials',\n authenticate: jest.fn(),\n },\n };\n const tokenGranter = {\n type: 'token',\n authorizer: { responseType: 'token', authorize: jest.fn() },\n acknowledger: { acknowledgmentType: 'token', acknowledge: jest.fn() },\n };\n const refreshTokenGranter = {\n type: 'refresh',\n authenticator: {\n grantType: 'refresh',\n authenticate: jest.fn(),\n },\n };\n const oAuth2Granters: OAuth2GranterService[] = [\n codeGranter,\n tokenGranter,\n passwordGranter,\n refreshTokenGranter,\n clientCredentialsGranter,\n ];\n\n beforeEach(() => {\n log.mockReset();\n time.mockReset();\n oAuth2AccessToken.create.mockReset();\n oAuth2AccessToken.check.mockReset();\n oAuth2RefreshToken.create.mockReset();\n oAuth2RefreshToken.check.mockReset();\n codeGranter.authorizer.authorize.mockReset();\n codeGranter.acknowledger.acknowledge.mockReset();\n codeGranter.authenticator.authenticate.mockReset();\n tokenGranter.authorizer.authorize.mockReset();\n tokenGranter.acknowledger.acknowledge.mockReset();\n passwordGranter.authenticator.authenticate.mockReset();\n refreshTokenGranter.authenticator.authenticate.mockReset();\n clientCredentialsGranter.authenticator.authenticate.mockReset();\n });\n\n test('should create a token', async () => {\n time.mockReturnValue(new Date('2010-03-06T00:00:00Z').getTime());\n oAuth2AccessToken.create.mockResolvedValueOnce({\n token: 'an_access_token',\n expiresAt: Date.parse('2010-03-07T00:00:00Z'),\n });\n oAuth2RefreshToken.create.mockResolvedValueOnce({\n token: 'a_refresh_token',\n expiresAt: Date.parse('2180-03-06T00:00:00Z'),\n });\n codeGranter.authorizer.authorize.mockResolvedValueOnce({\n applicationId: '',\n });\n\n const postOAuth2Token = await initPostOAuth2Token({\n oAuth2Granters,\n oAuth2AccessToken,\n oAuth2RefreshToken,\n time,\n log,\n });\n const response = await postOAuth2Token({\n authenticationData: {\n applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',\n scope: 'user',\n },\n body: {\n grant_type: 'authorization_code',\n code: '007',\n client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',\n redirect_uri: 'http://lol',\n },\n });\n expect({\n response,\n oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,\n oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,\n logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),\n }).toMatchSnapshot();\n });\n\n test('should fail with a bad grant type', async () => {\n const postOAuth2Token = await initPostOAuth2Token({\n oAuth2Granters,\n oAuth2AccessToken,\n oAuth2RefreshToken,\n time,\n log,\n });\n\n try {\n await postOAuth2Token({\n authenticationData: {\n applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',\n scope: 'user',\n },\n body: {\n grant_type: 'yolo',\n code: '007',\n client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',\n redirect_uri: 'http://lol',\n },\n });\n throw new YError('E_UNEXPECTED_SUCCESS');\n } catch (err) {\n expect({\n errorCode: err.code,\n errorParams: err.params,\n }).toMatchInlineSnapshot(`\n Object {\n \"errorCode\": \"E_UNKNOWN_AUTHENTICATOR_TYPE\",\n \"errorParams\": Array [\n \"yolo\",\n ],\n }\n `);\n expect({\n oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,\n oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,\n logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),\n }).toMatchSnapshot();\n }\n });\n});\n"],"file":"postOAuth2Token.test.js"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import initPostOAuth2Token from './postOAuth2Token';
|
|
2
|
+
import YError from 'yerror';
|
|
3
|
+
describe('postOAuth2Token', () => {
|
|
4
|
+
const log = jest.fn();
|
|
5
|
+
const time = jest.fn();
|
|
6
|
+
const oAuth2AccessToken = {
|
|
7
|
+
create: jest.fn(),
|
|
8
|
+
check: jest.fn()
|
|
9
|
+
};
|
|
10
|
+
const oAuth2RefreshToken = {
|
|
11
|
+
create: jest.fn(),
|
|
12
|
+
check: jest.fn()
|
|
13
|
+
};
|
|
14
|
+
const codeGranter = {
|
|
15
|
+
type: 'code',
|
|
16
|
+
authorizer: {
|
|
17
|
+
responseType: 'code',
|
|
18
|
+
authorize: jest.fn()
|
|
19
|
+
},
|
|
20
|
+
acknowledger: {
|
|
21
|
+
acknowledgmentType: 'code',
|
|
22
|
+
acknowledge: jest.fn()
|
|
23
|
+
},
|
|
24
|
+
authenticator: {
|
|
25
|
+
grantType: 'authorization_code',
|
|
26
|
+
authenticate: jest.fn()
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const passwordGranter = {
|
|
30
|
+
type: 'password',
|
|
31
|
+
authenticator: {
|
|
32
|
+
grantType: 'password',
|
|
33
|
+
authenticate: jest.fn()
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const clientCredentialsGranter = {
|
|
37
|
+
type: 'client_credentials',
|
|
38
|
+
authenticator: {
|
|
39
|
+
grantType: 'client_credentials',
|
|
40
|
+
authenticate: jest.fn()
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const tokenGranter = {
|
|
44
|
+
type: 'token',
|
|
45
|
+
authorizer: {
|
|
46
|
+
responseType: 'token',
|
|
47
|
+
authorize: jest.fn()
|
|
48
|
+
},
|
|
49
|
+
acknowledger: {
|
|
50
|
+
acknowledgmentType: 'token',
|
|
51
|
+
acknowledge: jest.fn()
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const refreshTokenGranter = {
|
|
55
|
+
type: 'refresh',
|
|
56
|
+
authenticator: {
|
|
57
|
+
grantType: 'refresh',
|
|
58
|
+
authenticate: jest.fn()
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const oAuth2Granters = [codeGranter, tokenGranter, passwordGranter, refreshTokenGranter, clientCredentialsGranter];
|
|
62
|
+
beforeEach(() => {
|
|
63
|
+
log.mockReset();
|
|
64
|
+
time.mockReset();
|
|
65
|
+
oAuth2AccessToken.create.mockReset();
|
|
66
|
+
oAuth2AccessToken.check.mockReset();
|
|
67
|
+
oAuth2RefreshToken.create.mockReset();
|
|
68
|
+
oAuth2RefreshToken.check.mockReset();
|
|
69
|
+
codeGranter.authorizer.authorize.mockReset();
|
|
70
|
+
codeGranter.acknowledger.acknowledge.mockReset();
|
|
71
|
+
codeGranter.authenticator.authenticate.mockReset();
|
|
72
|
+
tokenGranter.authorizer.authorize.mockReset();
|
|
73
|
+
tokenGranter.acknowledger.acknowledge.mockReset();
|
|
74
|
+
passwordGranter.authenticator.authenticate.mockReset();
|
|
75
|
+
refreshTokenGranter.authenticator.authenticate.mockReset();
|
|
76
|
+
clientCredentialsGranter.authenticator.authenticate.mockReset();
|
|
77
|
+
});
|
|
78
|
+
test('should create a token', async () => {
|
|
79
|
+
time.mockReturnValue(new Date('2010-03-06T00:00:00Z').getTime());
|
|
80
|
+
oAuth2AccessToken.create.mockResolvedValueOnce({
|
|
81
|
+
token: 'an_access_token',
|
|
82
|
+
expiresAt: Date.parse('2010-03-07T00:00:00Z')
|
|
83
|
+
});
|
|
84
|
+
oAuth2RefreshToken.create.mockResolvedValueOnce({
|
|
85
|
+
token: 'a_refresh_token',
|
|
86
|
+
expiresAt: Date.parse('2180-03-06T00:00:00Z')
|
|
87
|
+
});
|
|
88
|
+
codeGranter.authorizer.authorize.mockResolvedValueOnce({
|
|
89
|
+
applicationId: ''
|
|
90
|
+
});
|
|
91
|
+
const postOAuth2Token = await initPostOAuth2Token({
|
|
92
|
+
oAuth2Granters,
|
|
93
|
+
oAuth2AccessToken,
|
|
94
|
+
oAuth2RefreshToken,
|
|
95
|
+
time,
|
|
96
|
+
log
|
|
97
|
+
});
|
|
98
|
+
const response = await postOAuth2Token({
|
|
99
|
+
authenticationData: {
|
|
100
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
101
|
+
scope: 'user'
|
|
102
|
+
},
|
|
103
|
+
body: {
|
|
104
|
+
grant_type: 'authorization_code',
|
|
105
|
+
code: '007',
|
|
106
|
+
client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
107
|
+
redirect_uri: 'http://lol'
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
expect({
|
|
111
|
+
response,
|
|
112
|
+
oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
|
|
113
|
+
oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
|
|
114
|
+
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack'))
|
|
115
|
+
}).toMatchSnapshot();
|
|
116
|
+
});
|
|
117
|
+
test('should fail with a bad grant type', async () => {
|
|
118
|
+
const postOAuth2Token = await initPostOAuth2Token({
|
|
119
|
+
oAuth2Granters,
|
|
120
|
+
oAuth2AccessToken,
|
|
121
|
+
oAuth2RefreshToken,
|
|
122
|
+
time,
|
|
123
|
+
log
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
await postOAuth2Token({
|
|
128
|
+
authenticationData: {
|
|
129
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
130
|
+
scope: 'user'
|
|
131
|
+
},
|
|
132
|
+
body: {
|
|
133
|
+
grant_type: 'yolo',
|
|
134
|
+
code: '007',
|
|
135
|
+
client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
136
|
+
redirect_uri: 'http://lol'
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
140
|
+
} catch (err) {
|
|
141
|
+
expect({
|
|
142
|
+
errorCode: err.code,
|
|
143
|
+
errorParams: err.params
|
|
144
|
+
}).toMatchInlineSnapshot(`
|
|
145
|
+
Object {
|
|
146
|
+
"errorCode": "E_UNKNOWN_AUTHENTICATOR_TYPE",
|
|
147
|
+
"errorParams": Array [
|
|
148
|
+
"yolo",
|
|
149
|
+
],
|
|
150
|
+
}
|
|
151
|
+
`);
|
|
152
|
+
expect({
|
|
153
|
+
oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
|
|
154
|
+
oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
|
|
155
|
+
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack'))
|
|
156
|
+
}).toMatchSnapshot();
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
//# sourceMappingURL=postOAuth2Token.test.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/handlers/postOAuth2Token.test.ts"],"names":["initPostOAuth2Token","YError","describe","log","jest","fn","time","oAuth2AccessToken","create","check","oAuth2RefreshToken","codeGranter","type","authorizer","responseType","authorize","acknowledger","acknowledgmentType","acknowledge","authenticator","grantType","authenticate","passwordGranter","clientCredentialsGranter","tokenGranter","refreshTokenGranter","oAuth2Granters","beforeEach","mockReset","test","mockReturnValue","Date","getTime","mockResolvedValueOnce","token","expiresAt","parse","applicationId","postOAuth2Token","response","authenticationData","scope","body","grant_type","code","client_id","redirect_uri","expect","oAuth2AccessTokenCreateCalls","mock","calls","oAuth2RefreshTokenCreateCalls","logCalls","filter","endsWith","toMatchSnapshot","err","errorCode","errorParams","params","toMatchInlineSnapshot"],"mappings":"AAAA,OAAOA,mBAAP,MAAgC,mBAAhC;AACA,OAAOC,MAAP,MAAmB,QAAnB;AAGAC,QAAQ,CAAC,iBAAD,EAAoB,MAAM;AAChC,QAAMC,GAAG,GAAGC,IAAI,CAACC,EAAL,EAAZ;AACA,QAAMC,IAAI,GAAGF,IAAI,CAACC,EAAL,EAAb;AACA,QAAME,iBAAiB,GAAG;AAAEC,IAAAA,MAAM,EAAEJ,IAAI,CAACC,EAAL,EAAV;AAAqBI,IAAAA,KAAK,EAAEL,IAAI,CAACC,EAAL;AAA5B,GAA1B;AACA,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QAAAA,QAAQ,EAAEjD,GAAG,CAAC8C,IAAJ,CAASC,KAAT,CAAeG,MAAf,CAAsB,CAAC,CAACzC,IAAD,CAAD,KAAY,CAACA,IAAI,CAAC0C,QAAL,CAAc,OAAd,CAAnC;AAHL,OAAD,CAAN,CAIGC,eAJH;AAKD;AACF,GAzCG,CAAJ;AA0CD,CApJO,CAAR","sourcesContent":["import initPostOAuth2Token from './postOAuth2Token';\nimport YError from 'yerror';\nimport type { OAuth2GranterService } from '..';\n\ndescribe('postOAuth2Token', () => {\n const log = jest.fn();\n const time = jest.fn();\n const oAuth2AccessToken = { create: jest.fn(), check: jest.fn() };\n const oAuth2RefreshToken = { create: jest.fn(), check: jest.fn() };\n const codeGranter = {\n type: 'code',\n authorizer: { responseType: 'code', authorize: jest.fn() },\n acknowledger: { acknowledgmentType: 'code', acknowledge: jest.fn() },\n authenticator: {\n grantType: 'authorization_code',\n authenticate: jest.fn(),\n },\n };\n const passwordGranter = {\n type: 'password',\n authenticator: {\n grantType: 'password',\n authenticate: jest.fn(),\n },\n };\n const clientCredentialsGranter = {\n type: 'client_credentials',\n authenticator: {\n grantType: 'client_credentials',\n authenticate: jest.fn(),\n },\n };\n const tokenGranter = {\n type: 'token',\n authorizer: { responseType: 'token', authorize: jest.fn() },\n acknowledger: { acknowledgmentType: 'token', acknowledge: jest.fn() },\n };\n const refreshTokenGranter = {\n type: 'refresh',\n authenticator: {\n grantType: 'refresh',\n authenticate: jest.fn(),\n },\n };\n const oAuth2Granters: OAuth2GranterService[] = [\n codeGranter,\n tokenGranter,\n passwordGranter,\n refreshTokenGranter,\n clientCredentialsGranter,\n ];\n\n beforeEach(() => {\n log.mockReset();\n time.mockReset();\n oAuth2AccessToken.create.mockReset();\n oAuth2AccessToken.check.mockReset();\n oAuth2RefreshToken.create.mockReset();\n oAuth2RefreshToken.check.mockReset();\n codeGranter.authorizer.authorize.mockReset();\n codeGranter.acknowledger.acknowledge.mockReset();\n codeGranter.authenticator.authenticate.mockReset();\n tokenGranter.authorizer.authorize.mockReset();\n tokenGranter.acknowledger.acknowledge.mockReset();\n passwordGranter.authenticator.authenticate.mockReset();\n refreshTokenGranter.authenticator.authenticate.mockReset();\n clientCredentialsGranter.authenticator.authenticate.mockReset();\n });\n\n test('should create a token', async () => {\n time.mockReturnValue(new Date('2010-03-06T00:00:00Z').getTime());\n oAuth2AccessToken.create.mockResolvedValueOnce({\n token: 'an_access_token',\n expiresAt: Date.parse('2010-03-07T00:00:00Z'),\n });\n oAuth2RefreshToken.create.mockResolvedValueOnce({\n token: 'a_refresh_token',\n expiresAt: Date.parse('2180-03-06T00:00:00Z'),\n });\n codeGranter.authorizer.authorize.mockResolvedValueOnce({\n applicationId: '',\n });\n\n const postOAuth2Token = await initPostOAuth2Token({\n oAuth2Granters,\n oAuth2AccessToken,\n oAuth2RefreshToken,\n time,\n log,\n });\n const response = await postOAuth2Token({\n authenticationData: {\n applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',\n scope: 'user',\n },\n body: {\n grant_type: 'authorization_code',\n code: '007',\n client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',\n redirect_uri: 'http://lol',\n },\n });\n expect({\n response,\n oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,\n oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,\n logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),\n }).toMatchSnapshot();\n });\n\n test('should fail with a bad grant type', async () => {\n const postOAuth2Token = await initPostOAuth2Token({\n oAuth2Granters,\n oAuth2AccessToken,\n oAuth2RefreshToken,\n time,\n log,\n });\n\n try {\n await postOAuth2Token({\n authenticationData: {\n applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',\n scope: 'user',\n },\n body: {\n grant_type: 'yolo',\n code: '007',\n client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',\n redirect_uri: 'http://lol',\n },\n });\n throw new YError('E_UNEXPECTED_SUCCESS');\n } catch (err) {\n expect({\n errorCode: err.code,\n errorParams: err.params,\n }).toMatchInlineSnapshot(`\n Object {\n \"errorCode\": \"E_UNKNOWN_AUTHENTICATOR_TYPE\",\n \"errorParams\": Array [\n \"yolo\",\n ],\n }\n `);\n expect({\n oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,\n oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,\n logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),\n }).toMatchSnapshot();\n }\n });\n});\n"],"file":"postOAuth2Token.test.mjs"}
|