@whook/oauth2 25.2.1 → 26.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +147 -98
- package/dist/index.d.ts +30 -11
- package/dist/index.js +15 -8
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +1258 -399
- package/dist/index.test.js.map +1 -1
- 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 +113 -0
- package/dist/libs/errors.js +178 -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 +7 -0
- package/dist/libs/redirectURI.js +78 -0
- package/dist/libs/redirectURI.js.map +1 -0
- package/dist/libs/schemas.d.ts +23 -0
- package/dist/libs/schemas.js +25 -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 +7 -0
- package/dist/libs/verifier.js +37 -0
- package/dist/libs/verifier.js.map +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 +13 -50
- package/dist/routes/getOAuth2Authorize.js +60 -86
- package/dist/routes/getOAuth2Authorize.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.test.js +147 -68
- package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.d.ts +8 -8
- package/dist/routes/getOAuth2WellKnownMetadata.js +15 -8
- package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.test.js +37 -37
- 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/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 +35 -108
- package/dist/routes/postOAuth2Token.js +53 -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 +1 -1
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/authCookies.test.js.map +1 -1
- package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +129 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js +221 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js +474 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.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 +81 -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 +72 -0
- package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.js +727 -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 +11 -11
- package/src/index.test.ts +1314 -404
- package/src/index.ts +46 -53
- package/src/libs/clients.ts +22 -0
- package/src/libs/errors.ts +325 -0
- package/src/libs/grants.ts +15 -0
- package/src/libs/redirectURI.ts +120 -0
- package/src/libs/schemas.ts +27 -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 +57 -0
- package/src/routes/getOAuth2Authorize.test.ts +179 -96
- package/src/routes/getOAuth2Authorize.ts +97 -124
- package/src/routes/getOAuth2WellKnownMetadata.test.ts +57 -60
- package/src/routes/getOAuth2WellKnownMetadata.ts +21 -10
- 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/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 +92 -136
- package/src/services/authCookies.test.ts +5 -5
- package/src/services/authCookies.ts +16 -18
- package/src/services/oAuth2AuthorizationCodeGranter.test.ts +564 -0
- package/src/services/oAuth2AuthorizationCodeGranter.ts +388 -0
- package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
- package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
- package/src/services/oAuth2Granters.ts +101 -195
- package/src/services/oAuth2ImplicitGranter.test.ts +811 -0
- package/src/services/oAuth2ImplicitGranter.ts +163 -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/verifier.test.d.ts} +0 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
|
|
2
|
+
import initOAuth2ClientCredentialsGranter from './oAuth2ClientCredentialsGranter.js';
|
|
3
|
+
import { YError } from 'yerror';
|
|
4
|
+
describe('OAuth2ClientCredentialsGranter', () => {
|
|
5
|
+
const OAUTH2 = {
|
|
6
|
+
rootClientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
7
|
+
authenticateURL: 'https://auth.example.com/sign_in',
|
|
8
|
+
allowedScopes: ['user', 'admin', 'root'],
|
|
9
|
+
};
|
|
10
|
+
const readClientGrants = jest.fn();
|
|
11
|
+
const log = jest.fn();
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
readClientGrants.mockReset();
|
|
14
|
+
log.mockReset();
|
|
15
|
+
});
|
|
16
|
+
test('should work with a complete valid flow', async () => {
|
|
17
|
+
const oAuth2ClientCredentialsGranter = await initOAuth2ClientCredentialsGranter({
|
|
18
|
+
OAUTH2,
|
|
19
|
+
readClientGrants,
|
|
20
|
+
log,
|
|
21
|
+
});
|
|
22
|
+
readClientGrants.mockResolvedValue({
|
|
23
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
24
|
+
allowedGrantTypes: ['client_credentials', 'authorization_code'],
|
|
25
|
+
allowedScopes: ['user', 'admin'],
|
|
26
|
+
authenticationData: {
|
|
27
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
28
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
29
|
+
scopes: [],
|
|
30
|
+
},
|
|
31
|
+
isPublicClient: false,
|
|
32
|
+
});
|
|
33
|
+
const authenticatorResult = await oAuth2ClientCredentialsGranter.authenticate?.({
|
|
34
|
+
demandedScopes: ['user', 'root'],
|
|
35
|
+
}, {
|
|
36
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
37
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
38
|
+
scopes: ['user'],
|
|
39
|
+
});
|
|
40
|
+
expect({
|
|
41
|
+
authenticatorResult,
|
|
42
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
43
|
+
logCalls: log.mock.calls,
|
|
44
|
+
}).toMatchInlineSnapshot(`
|
|
45
|
+
{
|
|
46
|
+
"authenticatorResult": {
|
|
47
|
+
"clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
48
|
+
"scopes": [
|
|
49
|
+
"user",
|
|
50
|
+
],
|
|
51
|
+
"userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
|
|
52
|
+
},
|
|
53
|
+
"logCalls": [
|
|
54
|
+
[
|
|
55
|
+
"debug",
|
|
56
|
+
"👫 - OAuth2ClientCredentialsGranter Service Initialized!",
|
|
57
|
+
],
|
|
58
|
+
],
|
|
59
|
+
"readClientGrantsCalls": [
|
|
60
|
+
[
|
|
61
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
62
|
+
],
|
|
63
|
+
],
|
|
64
|
+
}
|
|
65
|
+
`);
|
|
66
|
+
});
|
|
67
|
+
test('should work with a complete valid flow and filter scopes', async () => {
|
|
68
|
+
const oAuth2ClientCredentialsGranter = await initOAuth2ClientCredentialsGranter({
|
|
69
|
+
OAUTH2,
|
|
70
|
+
readClientGrants,
|
|
71
|
+
log,
|
|
72
|
+
});
|
|
73
|
+
readClientGrants.mockResolvedValue({
|
|
74
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
75
|
+
allowedGrantTypes: ['client_credentials', 'authorization_code'],
|
|
76
|
+
allowedScopes: ['user', 'admin'],
|
|
77
|
+
authenticationData: {
|
|
78
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
79
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
80
|
+
scopes: [],
|
|
81
|
+
},
|
|
82
|
+
isPublicClient: false,
|
|
83
|
+
});
|
|
84
|
+
const authenticatorResult = await oAuth2ClientCredentialsGranter.authenticate?.({
|
|
85
|
+
demandedScopes: ['user', 'admin', 'oauth2', 'a_scope'],
|
|
86
|
+
}, {
|
|
87
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
88
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
89
|
+
scopes: ['user'],
|
|
90
|
+
});
|
|
91
|
+
expect({
|
|
92
|
+
authenticatorResult,
|
|
93
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
94
|
+
logCalls: log.mock.calls,
|
|
95
|
+
}).toMatchInlineSnapshot(`
|
|
96
|
+
{
|
|
97
|
+
"authenticatorResult": {
|
|
98
|
+
"clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
99
|
+
"scopes": [
|
|
100
|
+
"user",
|
|
101
|
+
"admin",
|
|
102
|
+
],
|
|
103
|
+
"userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
|
|
104
|
+
},
|
|
105
|
+
"logCalls": [
|
|
106
|
+
[
|
|
107
|
+
"debug",
|
|
108
|
+
"👫 - OAuth2ClientCredentialsGranter Service Initialized!",
|
|
109
|
+
],
|
|
110
|
+
],
|
|
111
|
+
"readClientGrantsCalls": [
|
|
112
|
+
[
|
|
113
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
114
|
+
],
|
|
115
|
+
],
|
|
116
|
+
}
|
|
117
|
+
`);
|
|
118
|
+
});
|
|
119
|
+
test('should fail with no authentication', async () => {
|
|
120
|
+
const oAuth2ClientCredentialsGranter = await initOAuth2ClientCredentialsGranter({
|
|
121
|
+
OAUTH2,
|
|
122
|
+
readClientGrants,
|
|
123
|
+
log,
|
|
124
|
+
});
|
|
125
|
+
readClientGrants.mockResolvedValue({
|
|
126
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
127
|
+
allowedGrantTypes: ['client_credentials', 'authorization_code'],
|
|
128
|
+
allowedScopes: ['user', 'admin'],
|
|
129
|
+
authenticationData: {
|
|
130
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
131
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
132
|
+
scopes: [],
|
|
133
|
+
},
|
|
134
|
+
isPublicClient: false,
|
|
135
|
+
});
|
|
136
|
+
try {
|
|
137
|
+
await oAuth2ClientCredentialsGranter.authenticate?.({
|
|
138
|
+
demandedScopes: ['user'],
|
|
139
|
+
});
|
|
140
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
expect({
|
|
144
|
+
err,
|
|
145
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
146
|
+
logCalls: log.mock.calls,
|
|
147
|
+
}).toMatchInlineSnapshot(`
|
|
148
|
+
{
|
|
149
|
+
"err": [YError: E_UNAUTHORIZED ([]): E_UNAUTHORIZED],
|
|
150
|
+
"logCalls": [
|
|
151
|
+
[
|
|
152
|
+
"debug",
|
|
153
|
+
"👫 - OAuth2ClientCredentialsGranter Service Initialized!",
|
|
154
|
+
],
|
|
155
|
+
],
|
|
156
|
+
"readClientGrantsCalls": [],
|
|
157
|
+
}
|
|
158
|
+
`);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
test('should fail with a client with not client_credentials grant', async () => {
|
|
162
|
+
const oAuth2ClientCredentialsGranter = await initOAuth2ClientCredentialsGranter({
|
|
163
|
+
OAUTH2,
|
|
164
|
+
readClientGrants,
|
|
165
|
+
log,
|
|
166
|
+
});
|
|
167
|
+
readClientGrants.mockResolvedValue({
|
|
168
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
169
|
+
allowedGrantTypes: ['authorization_code'],
|
|
170
|
+
allowedScopes: ['user', 'admin'],
|
|
171
|
+
authenticationData: {
|
|
172
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
173
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
174
|
+
scopes: [],
|
|
175
|
+
},
|
|
176
|
+
isPublicClient: false,
|
|
177
|
+
});
|
|
178
|
+
try {
|
|
179
|
+
await oAuth2ClientCredentialsGranter.authenticate?.({
|
|
180
|
+
demandedScopes: ['user'],
|
|
181
|
+
}, {
|
|
182
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
183
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
184
|
+
scopes: ['user'],
|
|
185
|
+
});
|
|
186
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
expect({
|
|
190
|
+
err,
|
|
191
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
192
|
+
logCalls: log.mock.calls,
|
|
193
|
+
}).toMatchInlineSnapshot(`
|
|
194
|
+
{
|
|
195
|
+
"err": [YError: E_OAUTH2_GRANT_TYPE_NOT_ALLOWED (["client_credentials",["authorization_code"]]): E_OAUTH2_GRANT_TYPE_NOT_ALLOWED],
|
|
196
|
+
"logCalls": [
|
|
197
|
+
[
|
|
198
|
+
"debug",
|
|
199
|
+
"👫 - OAuth2ClientCredentialsGranter Service Initialized!",
|
|
200
|
+
],
|
|
201
|
+
],
|
|
202
|
+
"readClientGrantsCalls": [
|
|
203
|
+
[
|
|
204
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
205
|
+
],
|
|
206
|
+
],
|
|
207
|
+
}
|
|
208
|
+
`);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
test('should fail with a client with a bad scope for the client', async () => {
|
|
212
|
+
const oAuth2ClientCredentialsGranter = await initOAuth2ClientCredentialsGranter({
|
|
213
|
+
OAUTH2: {
|
|
214
|
+
...OAUTH2,
|
|
215
|
+
strictScopesChecks: true,
|
|
216
|
+
},
|
|
217
|
+
readClientGrants,
|
|
218
|
+
log,
|
|
219
|
+
});
|
|
220
|
+
readClientGrants.mockResolvedValue({
|
|
221
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
222
|
+
allowedGrantTypes: ['client_credentials'],
|
|
223
|
+
allowedScopes: ['user'],
|
|
224
|
+
authenticationData: {
|
|
225
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
226
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
227
|
+
scopes: [],
|
|
228
|
+
},
|
|
229
|
+
isPublicClient: false,
|
|
230
|
+
});
|
|
231
|
+
try {
|
|
232
|
+
await oAuth2ClientCredentialsGranter.authenticate?.({
|
|
233
|
+
demandedScopes: ['user', 'admin'],
|
|
234
|
+
}, {
|
|
235
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
236
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
237
|
+
scopes: ['user'],
|
|
238
|
+
});
|
|
239
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
240
|
+
}
|
|
241
|
+
catch (err) {
|
|
242
|
+
expect({
|
|
243
|
+
err,
|
|
244
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
245
|
+
logCalls: log.mock.calls,
|
|
246
|
+
}).toMatchInlineSnapshot(`
|
|
247
|
+
{
|
|
248
|
+
"err": [YError: E_OAUTH2_BAD_SCOPE (["admin"]): E_OAUTH2_BAD_SCOPE],
|
|
249
|
+
"logCalls": [
|
|
250
|
+
[
|
|
251
|
+
"debug",
|
|
252
|
+
"👫 - OAuth2ClientCredentialsGranter Service Initialized!",
|
|
253
|
+
],
|
|
254
|
+
],
|
|
255
|
+
"readClientGrantsCalls": [
|
|
256
|
+
[
|
|
257
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
258
|
+
],
|
|
259
|
+
],
|
|
260
|
+
}
|
|
261
|
+
`);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
//# sourceMappingURL=oAuth2ClientCredentialsGranter.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oAuth2ClientCredentialsGranter.test.js","sourceRoot":"","sources":["../../src/services/oAuth2ClientCredentialsGranter.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAKzE,OAAO,kCAAkC,MAAM,qCAAqC,CAAC;AACrF,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,QAAQ,CAAC,gCAAgC,EAA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D,MAAM,EAAE,sCAAsC;gBAC9C,MAAM,EAAE,CAAC,MAAM,CAAC;aACjB,CACF,CAAC;YACF,MAAM,IAAI,MAAM,CAAC,sBAAsB,CAAC,CAAC;QAC3C,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,CAAC;gBACL,GAAG;gBACH,qBAAqB,EAAE,gBAAgB,CAAC,IAAI,CAAC,KAAK;gBAClD,QAAQ,EAAE,GAAG,CAAC,IAAI,CAAC,KAAK;aACzB,CAAC,CAAC,qBAAqB,CAAC;;;;;;;;;;;;;;;OAexB,CAAC,CAAC;QACL,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,102 +1,97 @@
|
|
|
1
1
|
import { type Dependencies } from 'knifecycle';
|
|
2
2
|
import { type WhookErrorsDescriptors } from '@whook/whook';
|
|
3
|
-
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
4
|
-
export interface
|
|
5
|
-
|
|
6
|
-
E_UNKNOWN_ACKNOWLEDGER_TYPE: [responseType: string];
|
|
7
|
-
E_APPLICATION_ID_MISMATCH: [
|
|
8
|
-
clientApplicationId: string,
|
|
9
|
-
authenticationApplicationId: string
|
|
10
|
-
];
|
|
11
|
-
/**
|
|
12
|
-
* Thrown when the refresh token is expired/invalid
|
|
13
|
-
*/
|
|
14
|
-
E_BAD_REFRESH_TOKEN: [];
|
|
15
|
-
/**
|
|
16
|
-
* Thrown when application has no access allowed
|
|
17
|
-
*/
|
|
18
|
-
E_ACCESS_DENIED: [clientId: string];
|
|
19
|
-
/**
|
|
20
|
-
* Thrown when the OAuth2 server had unexpected errors
|
|
21
|
-
*/
|
|
22
|
-
E_OAUTH2: [];
|
|
23
|
-
/**
|
|
24
|
-
* Thrown when refresh_token cookie is absent and required
|
|
25
|
-
*/
|
|
26
|
-
E_REFRESH_COOKIE: [cookie: string];
|
|
27
|
-
/**
|
|
28
|
-
* Thrown when server requires PKCE
|
|
29
|
-
*/
|
|
30
|
-
E_PKCE_REQUIRED: [responseType: string];
|
|
31
|
-
/**
|
|
32
|
-
* Thrown when server does not support PKCE
|
|
33
|
-
*/
|
|
34
|
-
E_PKCE_NOT_SUPPORTED: [responseType: string];
|
|
3
|
+
import { type WhookAuthenticationScope, type WhookAuthenticationData, type WhookBaseAuthenticationData } from '@whook/authorization';
|
|
4
|
+
export interface WhookOAuth2Types {
|
|
5
|
+
clientId: string;
|
|
35
6
|
}
|
|
36
|
-
export
|
|
37
|
-
|
|
38
|
-
|
|
7
|
+
export type WhookOAuth2ClientId = WhookOAuth2Types['clientId'];
|
|
8
|
+
declare module '@whook/authorization' {
|
|
9
|
+
interface WhookAuthenticationData extends WhookBaseAuthenticationData {
|
|
10
|
+
clientId: WhookOAuth2ClientId;
|
|
11
|
+
}
|
|
39
12
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
scope: string;
|
|
57
|
-
redirectURI: string;
|
|
13
|
+
/**
|
|
14
|
+
* A service that returns acknowledgments for a given client
|
|
15
|
+
*/
|
|
16
|
+
export type WhookOAuth2ReadClientGrantsService = (clientId: WhookOAuth2ClientId) => Promise<{
|
|
17
|
+
/** Grant types the client can be used with */
|
|
18
|
+
allowedGrantTypes: string[];
|
|
19
|
+
/** Scopes the client can delegate */
|
|
20
|
+
allowedScopes: WhookAuthenticationScope[];
|
|
21
|
+
/** Redirect URIS the client can be used with */
|
|
22
|
+
allowedRedirectURIS: string[];
|
|
23
|
+
/** Authentication data of the client */
|
|
24
|
+
authenticationData: WhookAuthenticationData;
|
|
25
|
+
/** Whether the client is public or private */
|
|
26
|
+
isPublicClient: boolean;
|
|
27
|
+
/** Whether the client can acknowledge for a user */
|
|
28
|
+
canAcknowledge?: boolean;
|
|
58
29
|
}>;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
30
|
+
/**
|
|
31
|
+
* A function handling the OAuth2 authorize step
|
|
32
|
+
*/
|
|
33
|
+
export type WhookOAuth2GranterAuthorize<T extends object> = (authorizeParameters: {
|
|
34
|
+
clientId: WhookOAuth2ClientId;
|
|
35
|
+
demandedRedirectURI?: string;
|
|
36
|
+
demandedScopes: WhookAuthenticationScope[];
|
|
37
|
+
}, additionalParameters: T) => Promise<{
|
|
38
|
+
clientId: WhookOAuth2ClientId;
|
|
65
39
|
redirectURI: string;
|
|
66
|
-
|
|
40
|
+
scopes: WhookAuthenticationScope[];
|
|
67
41
|
}>;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
42
|
+
/**
|
|
43
|
+
* A function handling the Whook specific acknowledge
|
|
44
|
+
* step allowing to build a front end over it (for users
|
|
45
|
+
* to actually authorize clients to act on their behalf)
|
|
46
|
+
*/
|
|
47
|
+
export type WhookOAuth2GranterAcknowledge<T extends object, U extends object> = (
|
|
48
|
+
/** The acknowledging user authentication data */
|
|
49
|
+
userAuthenticationData: WhookAuthenticationData, acknowledgeParameters: {
|
|
50
|
+
clientId: WhookOAuth2ClientId;
|
|
51
|
+
demandedRedirectURI: string;
|
|
52
|
+
demandedScopes: WhookAuthenticationScope[];
|
|
53
|
+
}, acknowledgeData: T) => Promise<{
|
|
54
|
+
acknowledgedAuthenticationData: WhookAuthenticationData;
|
|
55
|
+
acknowledgedRedirectURI: string;
|
|
56
|
+
acknowledgedScopes: WhookAuthenticationScope[];
|
|
57
|
+
acknowledgedData: U;
|
|
77
58
|
}>;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
59
|
+
/**
|
|
60
|
+
* A function handling the authenticate step
|
|
61
|
+
*/
|
|
62
|
+
export type WhookOAuth2GranterAuthenticate<T extends object> = (authenticateParameters: T, optionalAuthenticationData?: WhookAuthenticationData | undefined) => Promise<WhookAuthenticationData>;
|
|
63
|
+
export interface WhookOAuth2GranterDefinitions {
|
|
64
|
+
grantType?: string;
|
|
65
|
+
responseType?: string;
|
|
66
|
+
authorizeParameters: Record<string, string>;
|
|
67
|
+
acknowledgeParameters: Record<string, unknown>;
|
|
68
|
+
acknowledgedData: Record<string, unknown>;
|
|
69
|
+
authenticateParameters: Record<string, unknown>;
|
|
70
|
+
}
|
|
71
|
+
export interface WhookOAuth2GranterService<T extends WhookOAuth2GranterDefinitions> {
|
|
72
|
+
grantType: T['grantType'];
|
|
73
|
+
responseType?: T['responseType'];
|
|
74
|
+
issuesRefreshToken: boolean;
|
|
75
|
+
authorize?: WhookOAuth2GranterAuthorize<T['authorizeParameters']>;
|
|
76
|
+
acknowledge?: WhookOAuth2GranterAcknowledge<T['acknowledgeParameters'], T['acknowledgedData']>;
|
|
77
|
+
authenticate?: WhookOAuth2GranterAuthenticate<T['authenticateParameters']>;
|
|
92
78
|
}
|
|
93
|
-
export interface
|
|
79
|
+
export interface WhookOAuth2Options {
|
|
80
|
+
/** Id of the root application */
|
|
81
|
+
rootClientId: WhookOAuth2ClientId;
|
|
82
|
+
/** Where client authenticate */
|
|
94
83
|
authenticateURL: string;
|
|
84
|
+
/** The available scopes of the whole application */
|
|
85
|
+
allowedScopes: WhookAuthenticationScope[];
|
|
86
|
+
/** Whether the client scope should be used as the default scope */
|
|
95
87
|
defaultToClientScope?: boolean;
|
|
88
|
+
/** Whether PKCE should be forced or not */
|
|
96
89
|
forcePKCE?: boolean;
|
|
90
|
+
/** Whether scopes should not only be filtered but strictly checked */
|
|
91
|
+
strictScopesChecks?: boolean;
|
|
97
92
|
}
|
|
98
|
-
export interface
|
|
99
|
-
OAUTH2:
|
|
93
|
+
export interface WhookOAuth2Config {
|
|
94
|
+
OAUTH2: WhookOAuth2Options;
|
|
100
95
|
ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
|
|
101
96
|
}
|
|
102
97
|
declare const _default: import("knifecycle").ServiceInitializer<Dependencies, any[]>;
|
|
@@ -1,78 +1,13 @@
|
|
|
1
1
|
import { initializer, location } from 'knifecycle';
|
|
2
|
-
import { DEFAULT_ERROR_URI, DEFAULT_HELP_URI, } from '@whook/whook';
|
|
3
|
-
export const OAUTH2_ERRORS_DESCRIPTORS = {
|
|
4
|
-
E_UNKNOWN_AUTHORIZER_TYPE: {
|
|
5
|
-
code: 'unsupported_response_type',
|
|
6
|
-
status: 400,
|
|
7
|
-
description: `The type "$0" is not supported.`,
|
|
8
|
-
uri: DEFAULT_ERROR_URI,
|
|
9
|
-
help: DEFAULT_HELP_URI,
|
|
10
|
-
},
|
|
11
|
-
E_APPLICATION_ID_MISMATCH: {
|
|
12
|
-
code: 'unauthorized_client',
|
|
13
|
-
status: 400,
|
|
14
|
-
description: `The clients ids "$0"/"$1" are inconsistent.`,
|
|
15
|
-
uri: DEFAULT_ERROR_URI,
|
|
16
|
-
help: DEFAULT_HELP_URI,
|
|
17
|
-
},
|
|
18
|
-
E_BAD_REFRESH_TOKEN: {
|
|
19
|
-
code: 'invalid_request',
|
|
20
|
-
status: 400,
|
|
21
|
-
description: 'The refresh token is expired/invalid.',
|
|
22
|
-
uri: DEFAULT_ERROR_URI,
|
|
23
|
-
help: DEFAULT_HELP_URI,
|
|
24
|
-
},
|
|
25
|
-
E_UNKNOWN_ACKNOWLEDGER_TYPE: {
|
|
26
|
-
code: 'unsupported_response_type',
|
|
27
|
-
status: 400,
|
|
28
|
-
description: `Type "$0" not supported.`,
|
|
29
|
-
uri: DEFAULT_ERROR_URI,
|
|
30
|
-
help: DEFAULT_HELP_URI,
|
|
31
|
-
},
|
|
32
|
-
E_ACCESS_DENIED: {
|
|
33
|
-
code: 'access_denied',
|
|
34
|
-
status: 403,
|
|
35
|
-
description: 'The user denied access to your application (id: "$0").',
|
|
36
|
-
uri: DEFAULT_ERROR_URI,
|
|
37
|
-
help: DEFAULT_HELP_URI,
|
|
38
|
-
},
|
|
39
|
-
E_OAUTH2: {
|
|
40
|
-
code: 'server_error',
|
|
41
|
-
status: 500,
|
|
42
|
-
uri: DEFAULT_ERROR_URI,
|
|
43
|
-
help: DEFAULT_HELP_URI,
|
|
44
|
-
},
|
|
45
|
-
E_REFRESH_COOKIE: {
|
|
46
|
-
code: 'invalid_request',
|
|
47
|
-
status: 400,
|
|
48
|
-
description: `Could not find any refresh_token value in the cookie header ($0).`,
|
|
49
|
-
uri: DEFAULT_ERROR_URI,
|
|
50
|
-
help: DEFAULT_HELP_URI,
|
|
51
|
-
},
|
|
52
|
-
E_PKCE_REQUIRED: {
|
|
53
|
-
code: 'invalid_request',
|
|
54
|
-
status: 400,
|
|
55
|
-
description: 'Code challenge required for this response type ($0)',
|
|
56
|
-
uri: DEFAULT_ERROR_URI,
|
|
57
|
-
help: DEFAULT_HELP_URI,
|
|
58
|
-
},
|
|
59
|
-
E_PKCE_NOT_SUPPORTED: {
|
|
60
|
-
code: 'invalid_request',
|
|
61
|
-
status: 400,
|
|
62
|
-
description: 'Code challenge not supported for this response type ($0)',
|
|
63
|
-
uri: DEFAULT_ERROR_URI,
|
|
64
|
-
help: DEFAULT_HELP_URI,
|
|
65
|
-
},
|
|
66
|
-
};
|
|
67
2
|
export default location(initializer({
|
|
68
3
|
name: 'oAuth2Granters',
|
|
69
4
|
type: 'service',
|
|
70
5
|
inject: [
|
|
71
6
|
'oAuth2ClientCredentialsGranter',
|
|
72
|
-
'
|
|
7
|
+
'oAuth2AuthorizationCodeGranter',
|
|
73
8
|
'oAuth2PasswordGranter',
|
|
74
9
|
'oAuth2RefreshTokenGranter',
|
|
75
|
-
'
|
|
10
|
+
'oAuth2ImplicitGranter',
|
|
76
11
|
],
|
|
77
12
|
}, async (services) => Object.keys(services).map((key) => services[key])), import.meta.url);
|
|
78
13
|
//# 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;
|
|
1
|
+
{"version":3,"file":"oAuth2Granters.js","sourceRoot":"","sources":["../../src/services/oAuth2Granters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAmItE,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"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type LogService, type TimeService } from 'common-services';
|
|
2
|
+
import { type WhookOAuth2GranterService, type WhookOAuth2ReadClientGrantsService, type WhookOAuth2Options, type WhookOAuth2GranterDefinitions } from './oAuth2Granters.js';
|
|
3
|
+
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
4
|
+
export declare const IMPLICIT_GRANT_TYPE = "implicit";
|
|
5
|
+
export declare const IMPLICIT_RESPONSE_TYPE = "token";
|
|
6
|
+
/**
|
|
7
|
+
* A type allowing to override the access token types
|
|
8
|
+
*/
|
|
9
|
+
export type WhookOAuth2AccessToken = string;
|
|
10
|
+
/**
|
|
11
|
+
* A service to create and check access tokens
|
|
12
|
+
*/
|
|
13
|
+
export interface WhookOAuth2AccessTokenService<T extends string = WhookOAuth2AccessToken> {
|
|
14
|
+
create: (authenticationData: WhookAuthenticationData) => Promise<{
|
|
15
|
+
token: T;
|
|
16
|
+
expiresAt: number;
|
|
17
|
+
}>;
|
|
18
|
+
check: (token: T) => Promise<WhookAuthenticationData>;
|
|
19
|
+
}
|
|
20
|
+
export interface WhookOAuth2ImplicitGranterDependencies {
|
|
21
|
+
OAUTH2: WhookOAuth2Options;
|
|
22
|
+
readClientGrants: WhookOAuth2ReadClientGrantsService;
|
|
23
|
+
oAuth2AccessToken: Pick<WhookOAuth2AccessTokenService, 'create'>;
|
|
24
|
+
time?: TimeService;
|
|
25
|
+
log?: LogService;
|
|
26
|
+
}
|
|
27
|
+
export interface WhookOAuth2ImplicitGranterDefinitions extends WhookOAuth2GranterDefinitions {
|
|
28
|
+
grantType: typeof IMPLICIT_GRANT_TYPE;
|
|
29
|
+
responseType: typeof IMPLICIT_RESPONSE_TYPE;
|
|
30
|
+
acknowledgedData: {
|
|
31
|
+
accessToken: string;
|
|
32
|
+
tokenType: 'bearer';
|
|
33
|
+
expiresIn: number;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export type WhookOAuth2ImplicitGranterService = WhookOAuth2GranterService<WhookOAuth2ImplicitGranterDefinitions>;
|
|
37
|
+
declare const _default: import("knifecycle").ServiceInitializer<WhookOAuth2ImplicitGranterDependencies, WhookOAuth2ImplicitGranterService>;
|
|
38
|
+
export default _default;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { autoService, location } from 'knifecycle';
|
|
2
|
+
import { noop } from '@whook/whook';
|
|
3
|
+
import { YError } from 'yerror';
|
|
4
|
+
import { checkGrantType } from '../libs/grants.js';
|
|
5
|
+
import { filterScopes } from '../libs/scopes.js';
|
|
6
|
+
import { checkRedirectURI } from '../libs/redirectURI.js';
|
|
7
|
+
export const IMPLICIT_GRANT_TYPE = 'implicit';
|
|
8
|
+
export const IMPLICIT_RESPONSE_TYPE = 'token';
|
|
9
|
+
// Implicit Grant
|
|
10
|
+
// https://tools.ietf.org/html/rfc6749#section-4.2
|
|
11
|
+
async function initOAuth2ImplicitGranter({ OAUTH2, readClientGrants, oAuth2AccessToken, time = Date.now.bind(Date), log = noop, }) {
|
|
12
|
+
log('warning', `⚠️ - Using the token flow is deprecated and not recommended.`);
|
|
13
|
+
const authorizeWithToken = async ({ clientId, demandedRedirectURI, demandedScopes }) => {
|
|
14
|
+
const grants = await readClientGrants(clientId);
|
|
15
|
+
checkGrantType(grants.allowedGrantTypes, IMPLICIT_GRANT_TYPE);
|
|
16
|
+
if (demandedRedirectURI) {
|
|
17
|
+
checkRedirectURI(grants.allowedRedirectURIS, demandedRedirectURI);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
demandedRedirectURI = grants.allowedRedirectURIS[0];
|
|
21
|
+
}
|
|
22
|
+
const filteredScopes = filterScopes(demandedScopes, grants.allowedScopes, !!OAUTH2.strictScopesChecks);
|
|
23
|
+
return {
|
|
24
|
+
...grants.authenticationData,
|
|
25
|
+
clientId,
|
|
26
|
+
redirectURI: demandedRedirectURI,
|
|
27
|
+
scopes: filteredScopes,
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
// Access Token Response:
|
|
31
|
+
// https://tools.ietf.org/html/rfc6749#section-4.2.2
|
|
32
|
+
const acknowledgeWithToken = async (userAuthenticationData, { clientId, demandedRedirectURI, demandedScopes }) => {
|
|
33
|
+
const grants = await readClientGrants(clientId);
|
|
34
|
+
if (!grants.isPublicClient) {
|
|
35
|
+
throw new YError('E_OAUTH2_AUTHENTICATION_REQUIRED', [
|
|
36
|
+
grants.authenticationData.clientId,
|
|
37
|
+
]);
|
|
38
|
+
}
|
|
39
|
+
const filteredScopes = filterScopes(filterScopes(demandedScopes, grants.allowedScopes, !!OAUTH2.strictScopesChecks), userAuthenticationData.scopes, !!OAUTH2.strictScopesChecks);
|
|
40
|
+
checkGrantType(grants.allowedGrantTypes, IMPLICIT_GRANT_TYPE);
|
|
41
|
+
checkRedirectURI(grants.allowedRedirectURIS, demandedRedirectURI);
|
|
42
|
+
const { token: accessToken, expiresAt: accessTokenExpiresAt } = await oAuth2AccessToken.create({
|
|
43
|
+
...userAuthenticationData,
|
|
44
|
+
clientId,
|
|
45
|
+
scopes: filteredScopes,
|
|
46
|
+
});
|
|
47
|
+
return {
|
|
48
|
+
acknowledgedAuthenticationData: {
|
|
49
|
+
...userAuthenticationData,
|
|
50
|
+
clientId,
|
|
51
|
+
scopes: filteredScopes,
|
|
52
|
+
},
|
|
53
|
+
acknowledgedRedirectURI: demandedRedirectURI,
|
|
54
|
+
acknowledgedScopes: filteredScopes,
|
|
55
|
+
acknowledgedData: {
|
|
56
|
+
accessToken,
|
|
57
|
+
tokenType: 'bearer',
|
|
58
|
+
expiresIn: Math.round((accessTokenExpiresAt - time()) / 1000),
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
log('debug', '👫 - OAuth2ImplicitGranter Service Initialized!');
|
|
63
|
+
return {
|
|
64
|
+
grantType: IMPLICIT_GRANT_TYPE,
|
|
65
|
+
responseType: IMPLICIT_RESPONSE_TYPE,
|
|
66
|
+
issuesRefreshToken: false,
|
|
67
|
+
authorize: authorizeWithToken,
|
|
68
|
+
acknowledge: acknowledgeWithToken,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
export default location(autoService(initOAuth2ImplicitGranter), import.meta.url);
|
|
72
|
+
//# sourceMappingURL=oAuth2ImplicitGranter.js.map
|