@whook/oauth2 8.5.0 → 9.0.1
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 +87 -56
- package/dist/handlers/getOAuth2Authorize.d.ts +5 -3
- package/dist/handlers/getOAuth2Authorize.js +1 -1
- package/dist/handlers/getOAuth2Authorize.js.map +1 -1
- package/dist/handlers/getOAuth2Authorize.mjs +1 -1
- package/dist/handlers/getOAuth2Authorize.mjs.map +1 -1
- package/dist/handlers/getOAuth2Authorize.test.js.map +1 -1
- package/dist/handlers/getOAuth2Authorize.test.mjs.map +1 -1
- package/dist/handlers/postAuthLogin.d.ts +2 -2
- package/dist/handlers/postAuthLogin.js +1 -0
- package/dist/handlers/postAuthLogin.js.map +1 -1
- package/dist/handlers/postAuthLogin.mjs +1 -0
- package/dist/handlers/postAuthLogin.mjs.map +1 -1
- package/dist/handlers/postAuthLogin.test.js.map +1 -1
- package/dist/handlers/postAuthLogin.test.mjs.map +1 -1
- package/dist/handlers/postAuthLogout.js.map +1 -1
- package/dist/handlers/postAuthLogout.mjs.map +1 -1
- package/dist/handlers/postAuthLogout.test.js.map +1 -1
- package/dist/handlers/postAuthLogout.test.mjs.map +1 -1
- package/dist/handlers/postAuthRefresh.d.ts +3 -3
- package/dist/handlers/postAuthRefresh.js +1 -0
- package/dist/handlers/postAuthRefresh.js.map +1 -1
- package/dist/handlers/postAuthRefresh.mjs +1 -0
- package/dist/handlers/postAuthRefresh.mjs.map +1 -1
- package/dist/handlers/postAuthRefresh.test.js.map +1 -1
- package/dist/handlers/postAuthRefresh.test.mjs.map +1 -1
- package/dist/handlers/postOAuth2Acknowledge.d.ts +10 -7
- package/dist/handlers/postOAuth2Acknowledge.js +2 -2
- package/dist/handlers/postOAuth2Acknowledge.js.map +1 -1
- package/dist/handlers/postOAuth2Acknowledge.mjs +2 -2
- package/dist/handlers/postOAuth2Acknowledge.mjs.map +1 -1
- package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -1
- package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +1 -1
- package/dist/handlers/postOAuth2Token.d.ts +3 -3
- package/dist/handlers/postOAuth2Token.js +2 -2
- package/dist/handlers/postOAuth2Token.js.map +1 -1
- package/dist/handlers/postOAuth2Token.mjs +2 -2
- package/dist/handlers/postOAuth2Token.mjs.map +1 -1
- package/dist/handlers/postOAuth2Token.test.js.map +1 -1
- package/dist/handlers/postOAuth2Token.test.mjs.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/index.test.js +6 -7
- package/dist/index.test.js.map +1 -1
- package/dist/index.test.mjs +6 -7
- package/dist/index.test.mjs.map +1 -1
- package/dist/services/authCookies.d.ts +7 -4
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/authCookies.mjs.map +1 -1
- package/dist/services/authCookies.test.js.map +1 -1
- package/dist/services/authCookies.test.mjs.map +1 -1
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
- package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +1 -1
- package/dist/services/oAuth2CodeGranter.js.map +1 -1
- package/dist/services/oAuth2CodeGranter.mjs.map +1 -1
- package/dist/services/oAuth2CodeGranter.test.js +8 -6
- package/dist/services/oAuth2CodeGranter.test.js.map +1 -1
- package/dist/services/oAuth2CodeGranter.test.mjs +8 -6
- package/dist/services/oAuth2CodeGranter.test.mjs.map +1 -1
- package/dist/services/oAuth2Granters.d.ts +22 -19
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2Granters.mjs.map +1 -1
- package/dist/services/oAuth2PasswordGranter.js.map +1 -1
- package/dist/services/oAuth2PasswordGranter.mjs.map +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.js +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.mjs +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.mjs.map +1 -1
- package/dist/services/oAuth2TokenGranter.js.map +1 -1
- package/dist/services/oAuth2TokenGranter.mjs.map +1 -1
- package/package.json +34 -34
- package/src/handlers/getOAuth2Authorize.ts +15 -14
- package/src/handlers/postAuthLogin.ts +4 -3
- package/src/handlers/postAuthRefresh.ts +4 -3
- package/src/handlers/postOAuth2Acknowledge.ts +34 -26
- package/src/handlers/postOAuth2Token.test.ts +2 -2
- package/src/handlers/postOAuth2Token.ts +4 -4
- package/src/index.test.ts +8 -9
- package/src/index.ts +6 -4
- package/src/services/authCookies.ts +8 -4
- package/src/services/oAuth2ClientCredentialsGranter.ts +25 -21
- package/src/services/oAuth2CodeGranter.test.ts +15 -14
- package/src/services/oAuth2CodeGranter.ts +66 -60
- package/src/services/oAuth2Granters.ts +59 -29
- package/src/services/oAuth2PasswordGranter.ts +23 -22
- package/src/services/oAuth2RefreshTokenGranter.ts +28 -24
- package/src/services/oAuth2TokenGranter.ts +44 -42
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@whook/oauth2",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.1",
|
|
4
4
|
"description": "OAuth2 implementation for Whook servers",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -44,43 +44,43 @@
|
|
|
44
44
|
},
|
|
45
45
|
"homepage": "https://github.com/nfroidure/whook",
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@types/
|
|
48
|
-
"@whook/authorization": "^
|
|
49
|
-
"@whook/whook": "^
|
|
50
|
-
"camelcase": "^6.
|
|
51
|
-
"common-services": "^
|
|
52
|
-
"cookie": "^0.
|
|
53
|
-
"knifecycle": "^
|
|
54
|
-
"openapi-types": "^
|
|
55
|
-
"type-fest": "^2.
|
|
56
|
-
"yerror": "^6.0.
|
|
57
|
-
"yhttperror": "^6.0.
|
|
47
|
+
"@types/cookie": "^0.5.1",
|
|
48
|
+
"@whook/authorization": "^9.0.1",
|
|
49
|
+
"@whook/whook": "^9.0.1",
|
|
50
|
+
"camelcase": "^6.3.0",
|
|
51
|
+
"common-services": "^10.0.2",
|
|
52
|
+
"cookie": "^0.5.0",
|
|
53
|
+
"knifecycle": "^12.0.4",
|
|
54
|
+
"openapi-types": "^11.0.1",
|
|
55
|
+
"type-fest": "^2.13.0",
|
|
56
|
+
"yerror": "^6.0.2",
|
|
57
|
+
"yhttperror": "^6.0.3"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@babel/cli": "^7.
|
|
61
|
-
"@babel/core": "^7.
|
|
62
|
-
"@babel/eslint-parser": "^7.
|
|
63
|
-
"@babel/plugin-proposal-class-properties": "^7.
|
|
64
|
-
"@babel/plugin-proposal-object-rest-spread": "^7.
|
|
60
|
+
"@babel/cli": "^7.17.10",
|
|
61
|
+
"@babel/core": "^7.18.2",
|
|
62
|
+
"@babel/eslint-parser": "^7.18.2",
|
|
63
|
+
"@babel/plugin-proposal-class-properties": "^7.17.12",
|
|
64
|
+
"@babel/plugin-proposal-object-rest-spread": "^7.18.0",
|
|
65
65
|
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
|
66
66
|
"@babel/plugin-syntax-import-meta": "^7.10.4",
|
|
67
|
-
"@babel/preset-env": "^7.
|
|
68
|
-
"@babel/preset-typescript": "^7.
|
|
69
|
-
"@babel/register": "^7.
|
|
70
|
-
"@types/jest": "^
|
|
71
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
72
|
-
"@typescript-eslint/parser": "^
|
|
73
|
-
"babel-plugin-knifecycle": "^5.0.
|
|
74
|
-
"eslint": "^
|
|
75
|
-
"eslint-plugin-prettier": "^
|
|
76
|
-
"http-auth-utils": "^3.0.
|
|
77
|
-
"jest": "^
|
|
78
|
-
"jsdoc-to-markdown": "^7.
|
|
79
|
-
"metapak": "^4.0.
|
|
80
|
-
"metapak-nfroidure": "11.
|
|
81
|
-
"prettier": "^2.2
|
|
67
|
+
"@babel/preset-env": "^7.18.2",
|
|
68
|
+
"@babel/preset-typescript": "^7.17.12",
|
|
69
|
+
"@babel/register": "^7.17.7",
|
|
70
|
+
"@types/jest": "^27.0.2",
|
|
71
|
+
"@typescript-eslint/eslint-plugin": "^5.26.0",
|
|
72
|
+
"@typescript-eslint/parser": "^5.26.0",
|
|
73
|
+
"babel-plugin-knifecycle": "^5.0.3",
|
|
74
|
+
"eslint": "^8.16.0",
|
|
75
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
76
|
+
"http-auth-utils": "^3.0.3",
|
|
77
|
+
"jest": "^28.1.0",
|
|
78
|
+
"jsdoc-to-markdown": "^7.1.1",
|
|
79
|
+
"metapak": "^4.0.4",
|
|
80
|
+
"metapak-nfroidure": "11.2.1",
|
|
81
|
+
"prettier": "^2.6.2",
|
|
82
82
|
"rimraf": "^3.0.2",
|
|
83
|
-
"typescript": "^4.2
|
|
83
|
+
"typescript": "^4.7.2"
|
|
84
84
|
},
|
|
85
85
|
"contributors": [],
|
|
86
86
|
"engines": {
|
|
@@ -202,5 +202,5 @@
|
|
|
202
202
|
"test": "npm run jest",
|
|
203
203
|
"types": "rimraf -f 'dist/**/*.d.ts' && tsc --project . --declaration --emitDeclarationOnly --outDir dist"
|
|
204
204
|
},
|
|
205
|
-
"gitHead": "
|
|
205
|
+
"gitHead": "a5620b3eacf3b500a5db59db9997bf7ed60668d5"
|
|
206
206
|
}
|
|
@@ -132,7 +132,7 @@ async function getOAuth2Authorize(
|
|
|
132
132
|
scope?: string;
|
|
133
133
|
state: string;
|
|
134
134
|
} & Record<string, unknown>,
|
|
135
|
-
): Promise<WhookResponse
|
|
135
|
+
): Promise<WhookResponse<302, { location: string }>> {
|
|
136
136
|
const url = new URL(OAUTH2.authenticateURL);
|
|
137
137
|
|
|
138
138
|
try {
|
|
@@ -145,29 +145,30 @@ async function getOAuth2Authorize(
|
|
|
145
145
|
throw new YError('E_UNKNOWN_AUTHORIZER_TYPE', responseType);
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
const { applicationId, redirectURI, scope } =
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
)
|
|
148
|
+
const { applicationId, redirectURI, scope } = await (
|
|
149
|
+
granter.authorizer as NonNullable<OAuth2GranterService['authorizer']>
|
|
150
|
+
).authorize(
|
|
151
|
+
{
|
|
152
|
+
clientId,
|
|
153
|
+
redirectURI: demandedRedirectURI,
|
|
154
|
+
scope: demandedScope,
|
|
155
|
+
},
|
|
156
|
+
camelCaseObjectProperties(authorizeParameters),
|
|
157
|
+
);
|
|
157
158
|
|
|
158
159
|
url.searchParams.set('type', responseType);
|
|
159
160
|
url.searchParams.set('redirect_uri', redirectURI);
|
|
160
161
|
url.searchParams.set('scope', scope);
|
|
161
162
|
url.searchParams.set('client_id', applicationId);
|
|
162
163
|
} catch (err) {
|
|
163
|
-
log('debug', '👫 - OAuth2 initialization error', err.code);
|
|
164
|
-
log('debug-stack', err.stack);
|
|
164
|
+
log('debug', '👫 - OAuth2 initialization error', (err as YError).code);
|
|
165
|
+
log('debug-stack', (err as Error).stack || 'no_stack_trace');
|
|
165
166
|
|
|
166
167
|
url.searchParams.set('redirect_uri', demandedRedirectURI);
|
|
167
168
|
setURLError(
|
|
168
169
|
url,
|
|
169
|
-
err,
|
|
170
|
-
ERRORS_DESCRIPTORS[err.code] || ERRORS_DESCRIPTORS.E_OAUTH2,
|
|
170
|
+
err as YError,
|
|
171
|
+
ERRORS_DESCRIPTORS[(err as YError).code] || ERRORS_DESCRIPTORS.E_OAUTH2,
|
|
171
172
|
);
|
|
172
173
|
}
|
|
173
174
|
|
|
@@ -111,11 +111,12 @@ async function postAuthLogin<
|
|
|
111
111
|
},
|
|
112
112
|
};
|
|
113
113
|
} catch (err) {
|
|
114
|
-
const newErr = YHTTPError.wrap(err);
|
|
114
|
+
const newErr = YHTTPError.wrap(err as Error);
|
|
115
115
|
|
|
116
116
|
newErr.headers = {
|
|
117
|
-
...(err.headers || {}),
|
|
118
|
-
|
|
117
|
+
...((err as YHTTPError).headers || {}),
|
|
118
|
+
// TODO: Allow string[] in YHTTPError
|
|
119
|
+
'Set-Cookie': authCookies.build() as unknown as string,
|
|
119
120
|
};
|
|
120
121
|
|
|
121
122
|
throw newErr;
|
|
@@ -131,11 +131,12 @@ async function postAuthRefresh<
|
|
|
131
131
|
},
|
|
132
132
|
};
|
|
133
133
|
} catch (err) {
|
|
134
|
-
const newErr = YHTTPError.wrap(err);
|
|
134
|
+
const newErr = YHTTPError.wrap(err as Error);
|
|
135
135
|
|
|
136
136
|
newErr.headers = {
|
|
137
|
-
...(err.headers || {}),
|
|
138
|
-
|
|
137
|
+
...((err as YHTTPError).headers || {}),
|
|
138
|
+
// TODO: Allow string[] in YHTTPError
|
|
139
|
+
'Set-Cookie': authCookies.build() as unknown as string,
|
|
139
140
|
};
|
|
140
141
|
|
|
141
142
|
throw newErr;
|
|
@@ -4,7 +4,6 @@ import { setURLError } from './getOAuth2Authorize';
|
|
|
4
4
|
import type {
|
|
5
5
|
WhookAPIHandlerDefinition,
|
|
6
6
|
WhookErrorsDescriptors,
|
|
7
|
-
WhookAPIOperationConfig,
|
|
8
7
|
} from '@whook/whook';
|
|
9
8
|
import type {
|
|
10
9
|
CheckApplicationService,
|
|
@@ -28,7 +27,7 @@ export const definition: WhookAPIHandlerDefinition = {
|
|
|
28
27
|
tags: ['oauth2'],
|
|
29
28
|
'x-whook': {
|
|
30
29
|
private: true,
|
|
31
|
-
}
|
|
30
|
+
},
|
|
32
31
|
requestBody: {
|
|
33
32
|
required: true,
|
|
34
33
|
content: {
|
|
@@ -96,7 +95,32 @@ export const definition: WhookAPIHandlerDefinition = {
|
|
|
96
95
|
},
|
|
97
96
|
};
|
|
98
97
|
|
|
99
|
-
export
|
|
98
|
+
export type HandlerDependencies = {
|
|
99
|
+
ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
|
|
100
|
+
oAuth2Granters: OAuth2GranterService[];
|
|
101
|
+
checkApplication: CheckApplicationService;
|
|
102
|
+
log: LogService;
|
|
103
|
+
};
|
|
104
|
+
type HandlerParameters<AUTHENTICATION_DATA> = {
|
|
105
|
+
authenticationData: AUTHENTICATION_DATA;
|
|
106
|
+
body: {
|
|
107
|
+
responseType: string;
|
|
108
|
+
clientId: string;
|
|
109
|
+
redirectURI: string;
|
|
110
|
+
scope: string;
|
|
111
|
+
state: string;
|
|
112
|
+
acknowledged: boolean;
|
|
113
|
+
[name: string]: unknown;
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export default autoHandler(postOAuth2Acknowledge) as unknown as <
|
|
118
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
119
|
+
>(
|
|
120
|
+
dependencies: HandlerDependencies,
|
|
121
|
+
) => (
|
|
122
|
+
parameters: HandlerParameters<AUTHENTICATION_DATA>,
|
|
123
|
+
) => ReturnType<typeof postOAuth2Acknowledge>;
|
|
100
124
|
|
|
101
125
|
async function postOAuth2Acknowledge<
|
|
102
126
|
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
@@ -106,12 +130,7 @@ async function postOAuth2Acknowledge<
|
|
|
106
130
|
oAuth2Granters,
|
|
107
131
|
checkApplication,
|
|
108
132
|
log,
|
|
109
|
-
}:
|
|
110
|
-
ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
|
|
111
|
-
oAuth2Granters: OAuth2GranterService[];
|
|
112
|
-
checkApplication: CheckApplicationService;
|
|
113
|
-
log: LogService;
|
|
114
|
-
},
|
|
133
|
+
}: HandlerDependencies,
|
|
115
134
|
{
|
|
116
135
|
authenticationData,
|
|
117
136
|
body: {
|
|
@@ -123,18 +142,7 @@ async function postOAuth2Acknowledge<
|
|
|
123
142
|
acknowledged = false,
|
|
124
143
|
...additionalParameters
|
|
125
144
|
},
|
|
126
|
-
}:
|
|
127
|
-
authenticationData: AUTHENTICATION_DATA;
|
|
128
|
-
body: {
|
|
129
|
-
responseType: string;
|
|
130
|
-
clientId: string;
|
|
131
|
-
redirectURI: string;
|
|
132
|
-
scope: string;
|
|
133
|
-
state: string;
|
|
134
|
-
acknowledged: boolean;
|
|
135
|
-
[name: string]: unknown;
|
|
136
|
-
};
|
|
137
|
-
},
|
|
145
|
+
}: HandlerParameters<AUTHENTICATION_DATA>,
|
|
138
146
|
) {
|
|
139
147
|
if (!authenticationData) {
|
|
140
148
|
throw new YError('E_UNAUTHORIZED');
|
|
@@ -161,7 +169,7 @@ async function postOAuth2Acknowledge<
|
|
|
161
169
|
granter.acknowledger.acknowledgmentType === responseType,
|
|
162
170
|
);
|
|
163
171
|
|
|
164
|
-
if (!granter) {
|
|
172
|
+
if (!granter || !granter.acknowledger) {
|
|
165
173
|
throw new YError('E_UNKNOWN_ACKNOWLEDGOR_TYPE', responseType);
|
|
166
174
|
}
|
|
167
175
|
|
|
@@ -183,13 +191,13 @@ async function postOAuth2Acknowledge<
|
|
|
183
191
|
url.searchParams.set(snakeCase(key), additionalProperties[key] as string),
|
|
184
192
|
);
|
|
185
193
|
} catch (err) {
|
|
186
|
-
log('debug', '👫 - OAuth2 acknowledge error', err.code);
|
|
187
|
-
log('debug-stack', err.stack);
|
|
194
|
+
log('debug', '👫 - OAuth2 acknowledge error', (err as YError).code);
|
|
195
|
+
log('debug-stack', (err as Error).stack || 'no_stack_trace');
|
|
188
196
|
|
|
189
197
|
setURLError(
|
|
190
198
|
url,
|
|
191
|
-
err,
|
|
192
|
-
ERRORS_DESCRIPTORS[err.code] || ERRORS_DESCRIPTORS.E_OAUTH2,
|
|
199
|
+
err as YError,
|
|
200
|
+
ERRORS_DESCRIPTORS[(err as YError).code] || ERRORS_DESCRIPTORS.E_OAUTH2,
|
|
193
201
|
);
|
|
194
202
|
}
|
|
195
203
|
|
|
@@ -133,8 +133,8 @@ describe('postOAuth2Token', () => {
|
|
|
133
133
|
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
134
134
|
} catch (err) {
|
|
135
135
|
expect({
|
|
136
|
-
errorCode: err.code,
|
|
137
|
-
errorParams: err.params,
|
|
136
|
+
errorCode: (err as YError).code,
|
|
137
|
+
errorParams: (err as YError).params,
|
|
138
138
|
}).toMatchInlineSnapshot(`
|
|
139
139
|
Object {
|
|
140
140
|
"errorCode": "E_UNKNOWN_AUTHENTICATOR_TYPE",
|
|
@@ -247,7 +247,7 @@ async function postOAuth2Token<
|
|
|
247
247
|
granter.authenticator && granter.authenticator.grantType === grantType,
|
|
248
248
|
);
|
|
249
249
|
|
|
250
|
-
if (!granter) {
|
|
250
|
+
if (!granter || !granter.authenticator) {
|
|
251
251
|
throw new YError('E_UNKNOWN_AUTHENTICATOR_TYPE', grantType);
|
|
252
252
|
}
|
|
253
253
|
|
|
@@ -283,9 +283,9 @@ async function postOAuth2Token<
|
|
|
283
283
|
},
|
|
284
284
|
};
|
|
285
285
|
} catch (err) {
|
|
286
|
-
log('debug', '👫 - OAuth2 token issuing error', err.code);
|
|
287
|
-
log('debug-stack', err.stack);
|
|
286
|
+
log('debug', '👫 - OAuth2 token issuing error', (err as YError).code);
|
|
287
|
+
log('debug-stack', (err as Error).stack || 'no_stack_trace');
|
|
288
288
|
|
|
289
|
-
throw YError.cast(err, 'E_OAUTH2');
|
|
289
|
+
throw YError.cast(err as Error, 'E_OAUTH2');
|
|
290
290
|
}
|
|
291
291
|
}
|
package/src/index.test.ts
CHANGED
|
@@ -41,7 +41,7 @@ import {
|
|
|
41
41
|
postOAuth2TokenRefreshTokenRequestBodySchema,
|
|
42
42
|
} from '.';
|
|
43
43
|
import type { OAuth2Options } from '.';
|
|
44
|
-
import type { Knifecycle
|
|
44
|
+
import type { Knifecycle } from 'knifecycle';
|
|
45
45
|
import type { OpenAPIV3 } from 'openapi-types';
|
|
46
46
|
|
|
47
47
|
describe('OAuth2 server', () => {
|
|
@@ -49,10 +49,10 @@ describe('OAuth2 server', () => {
|
|
|
49
49
|
const PORT = 4444;
|
|
50
50
|
const HOST = 'localhost';
|
|
51
51
|
const logger = {
|
|
52
|
-
|
|
52
|
+
output: jest.fn(),
|
|
53
53
|
error: jest.fn(),
|
|
54
|
+
debug: jest.fn(),
|
|
54
55
|
};
|
|
55
|
-
const debug = jest.fn();
|
|
56
56
|
const time = jest.fn();
|
|
57
57
|
const $autoload = jest.fn();
|
|
58
58
|
|
|
@@ -167,7 +167,6 @@ describe('OAuth2 server', () => {
|
|
|
167
167
|
$.register(constant('MECHANISMS', [BEARER_MECHANISM, BASIC_MECHANISM]));
|
|
168
168
|
$.register(constant('logger', logger));
|
|
169
169
|
$.register(constant('time', time));
|
|
170
|
-
$.register(constant('debug', debug));
|
|
171
170
|
|
|
172
171
|
// OAuth2 Specifics
|
|
173
172
|
$.register(constant('OAUTH2', OAUTH2));
|
|
@@ -202,8 +201,8 @@ describe('OAuth2 server', () => {
|
|
|
202
201
|
$.register(constant('oAuth2Password', oAuth2Password));
|
|
203
202
|
[
|
|
204
203
|
initGetOAuth2Authorize,
|
|
205
|
-
wrapHandlerWithAuthorization(initPostOAuth2Acknowledge),
|
|
206
|
-
wrapHandlerWithAuthorization(initPostOAuth2Token),
|
|
204
|
+
wrapHandlerWithAuthorization(initPostOAuth2Acknowledge as any),
|
|
205
|
+
wrapHandlerWithAuthorization(initPostOAuth2Token as any),
|
|
207
206
|
initOAuth2Granters,
|
|
208
207
|
initOAuth2ClientCredentialsGranter,
|
|
209
208
|
initOAuth2CodeGranter,
|
|
@@ -222,7 +221,7 @@ describe('OAuth2 server', () => {
|
|
|
222
221
|
|
|
223
222
|
beforeAll(async () => {
|
|
224
223
|
const { $instance: _instance } = await runServer<{
|
|
225
|
-
$instance: Knifecycle
|
|
224
|
+
$instance: Knifecycle;
|
|
226
225
|
}>(prepareEnvironment, prepareServer, [
|
|
227
226
|
'$instance',
|
|
228
227
|
'httpServer',
|
|
@@ -236,9 +235,9 @@ describe('OAuth2 server', () => {
|
|
|
236
235
|
});
|
|
237
236
|
|
|
238
237
|
beforeEach(() => {
|
|
239
|
-
logger.
|
|
238
|
+
logger.output.mockReset();
|
|
240
239
|
logger.error.mockReset();
|
|
241
|
-
debug.mockReset();
|
|
240
|
+
logger.debug.mockReset();
|
|
242
241
|
time.mockReset();
|
|
243
242
|
$autoload.mockClear();
|
|
244
243
|
[
|
package/src/index.ts
CHANGED
|
@@ -49,10 +49,11 @@ import initAuthCookiesService, {
|
|
|
49
49
|
AUTH_API_PREFIX,
|
|
50
50
|
} from './services/authCookies';
|
|
51
51
|
import type {
|
|
52
|
-
|
|
53
|
-
AuthCookiesService,
|
|
52
|
+
AuthCookiesEnv,
|
|
54
53
|
AuthCookiesConfig,
|
|
54
|
+
AuthCookiesService,
|
|
55
55
|
AuthCookiesData,
|
|
56
|
+
AuthHandlersConfig,
|
|
56
57
|
} from './services/authCookies';
|
|
57
58
|
|
|
58
59
|
export type {
|
|
@@ -64,10 +65,11 @@ export type {
|
|
|
64
65
|
OAuth2GranterService,
|
|
65
66
|
OAuth2Options,
|
|
66
67
|
OAuth2Config,
|
|
67
|
-
|
|
68
|
-
AuthCookiesService,
|
|
68
|
+
AuthCookiesEnv,
|
|
69
69
|
AuthCookiesConfig,
|
|
70
|
+
AuthCookiesService,
|
|
70
71
|
AuthCookiesData,
|
|
72
|
+
AuthHandlersConfig,
|
|
71
73
|
};
|
|
72
74
|
export {
|
|
73
75
|
initGetOAuth2Authorize,
|
|
@@ -11,16 +11,20 @@ export type AuthHandlersConfig<
|
|
|
11
11
|
ROOT_AUTHENTICATION_DATA: AUTHENTICATION_DATA;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
+
export type AuthCookiesEnv = {
|
|
15
|
+
DEV_MODE?: string;
|
|
16
|
+
};
|
|
14
17
|
export type AuthCookiesConfig = {
|
|
15
|
-
ENV?: {
|
|
16
|
-
DEV_MODE?: string;
|
|
17
|
-
};
|
|
18
18
|
COOKIES: {
|
|
19
19
|
domain: string;
|
|
20
20
|
};
|
|
21
21
|
BASE_PATH?: string;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
+
export type AuthCookiesDependencies = AuthCookiesConfig & {
|
|
25
|
+
ENV?: AuthCookiesEnv;
|
|
26
|
+
};
|
|
27
|
+
|
|
24
28
|
export type AuthCookiesData = {
|
|
25
29
|
refresh_token: string;
|
|
26
30
|
access_token: string;
|
|
@@ -40,7 +44,7 @@ async function initAuthCookies({
|
|
|
40
44
|
ENV = {},
|
|
41
45
|
COOKIES,
|
|
42
46
|
BASE_PATH = '',
|
|
43
|
-
}:
|
|
47
|
+
}: AuthCookiesDependencies): Promise<AuthCookiesService> {
|
|
44
48
|
function build(data: Partial<AuthCookiesData> = {}, { session = true } = {}) {
|
|
45
49
|
return [
|
|
46
50
|
cookie.serialize('access_token', data.access_token || '', {
|
|
@@ -39,30 +39,34 @@ async function initOAuth2ClientCredentialsGranter({
|
|
|
39
39
|
checkApplication,
|
|
40
40
|
log = noop,
|
|
41
41
|
}: OAuth2ClientCredentialsGranterDependencies): Promise<OAuth2ClientCredentialsGranterService> {
|
|
42
|
-
const authenticateWithClientCredentials:
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
const authenticateWithClientCredentials: NonNullable<
|
|
43
|
+
OAuth2ClientCredentialsGranterService['authenticator']
|
|
44
|
+
>['authenticate'] = async (
|
|
45
|
+
{ scope: demandedScope = '' },
|
|
46
|
+
authenticationData,
|
|
47
|
+
) => {
|
|
48
|
+
// The client must be authenticated
|
|
49
|
+
if (!authenticationData) {
|
|
50
|
+
throw new YError('E_UNAUTHORIZED');
|
|
51
|
+
}
|
|
48
52
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
// Checking the scope and availability of the demanded
|
|
54
|
+
// grant type
|
|
55
|
+
await checkApplication({
|
|
56
|
+
applicationId: authenticationData.applicationId,
|
|
57
|
+
type: 'client_credentials',
|
|
58
|
+
scope: demandedScope,
|
|
59
|
+
});
|
|
56
60
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
};
|
|
61
|
+
return {
|
|
62
|
+
...authenticationData,
|
|
63
|
+
scope: demandedScope
|
|
64
|
+
? demandedScope
|
|
65
|
+
: OAUTH2.defaultToClientScope
|
|
66
|
+
? authenticationData.scope
|
|
67
|
+
: '',
|
|
65
68
|
};
|
|
69
|
+
};
|
|
66
70
|
|
|
67
71
|
log('debug', '👫 - OAuth2ClientCredentialsGranter Service Initialized!');
|
|
68
72
|
|
|
@@ -31,25 +31,26 @@ describe('OAuth2CodeGranter', () => {
|
|
|
31
31
|
scope: 'user',
|
|
32
32
|
});
|
|
33
33
|
|
|
34
|
-
const authorizerResult = await oAuth2CodeGranter.authorizer
|
|
34
|
+
const authorizerResult = await oAuth2CodeGranter.authorizer?.authorize({
|
|
35
35
|
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
36
36
|
redirectURI: 'https://www.example.com/oauth2/code',
|
|
37
37
|
scope: 'user',
|
|
38
38
|
});
|
|
39
|
-
const acknowledgerResult =
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
39
|
+
const acknowledgerResult =
|
|
40
|
+
await oAuth2CodeGranter.acknowledger?.acknowledge(
|
|
41
|
+
{
|
|
42
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
43
|
+
scope: 'user, admin',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
47
|
+
redirectURI: 'https://www.example.com/oauth2/code',
|
|
48
|
+
scope: 'user',
|
|
49
|
+
},
|
|
50
|
+
{},
|
|
51
|
+
);
|
|
51
52
|
const authenticatorResult =
|
|
52
|
-
await oAuth2CodeGranter.authenticator
|
|
53
|
+
await oAuth2CodeGranter.authenticator?.authenticate(
|
|
53
54
|
{
|
|
54
55
|
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
55
56
|
redirectURI: 'https://www.example.com/oauth2/code',
|