@whook/oauth2 9.0.1 → 10.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -15
- package/dist/handlers/getOAuth2Authorize.d.ts +4 -4
- package/dist/handlers/getOAuth2Authorize.js +122 -165
- package/dist/handlers/getOAuth2Authorize.js.map +1 -1
- package/dist/handlers/getOAuth2Authorize.test.js +117 -103
- package/dist/handlers/getOAuth2Authorize.test.js.map +1 -1
- package/dist/handlers/postAuthLogin.d.ts +1 -1
- package/dist/handlers/postAuthLogin.js +83 -121
- package/dist/handlers/postAuthLogin.js.map +1 -1
- package/dist/handlers/postAuthLogin.test.js +42 -44
- package/dist/handlers/postAuthLogin.test.js.map +1 -1
- package/dist/handlers/postAuthLogout.d.ts +1 -1
- package/dist/handlers/postAuthLogout.js +26 -42
- package/dist/handlers/postAuthLogout.js.map +1 -1
- package/dist/handlers/postAuthLogout.test.js +19 -21
- package/dist/handlers/postAuthLogout.test.js.map +1 -1
- package/dist/handlers/postAuthRefresh.d.ts +1 -1
- package/dist/handlers/postAuthRefresh.js +93 -132
- package/dist/handlers/postAuthRefresh.js.map +1 -1
- package/dist/handlers/postAuthRefresh.test.js +47 -49
- package/dist/handlers/postAuthRefresh.test.js.map +1 -1
- package/dist/handlers/postOAuth2Acknowledge.d.ts +5 -5
- package/dist/handlers/postOAuth2Acknowledge.js +119 -151
- package/dist/handlers/postOAuth2Acknowledge.js.map +1 -1
- package/dist/handlers/postOAuth2Acknowledge.test.js +139 -125
- package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -1
- package/dist/handlers/postOAuth2Token.d.ts +1 -1
- package/dist/handlers/postOAuth2Token.js +200 -232
- package/dist/handlers/postOAuth2Token.js.map +1 -1
- package/dist/handlers/postOAuth2Token.test.js +156 -153
- package/dist/handlers/postOAuth2Token.test.js.map +1 -1
- package/dist/index.d.ts +15 -15
- package/dist/index.js +14 -229
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +697 -619
- package/dist/index.test.js.map +1 -1
- package/dist/services/authCookies.d.ts +1 -0
- package/dist/services/authCookies.js +40 -71
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/authCookies.test.js +57 -62
- package/dist/services/authCookies.test.js.map +1 -1
- package/dist/services/oAuth2ClientCredentialsGranter.d.ts +2 -2
- package/dist/services/oAuth2ClientCredentialsGranter.js +34 -52
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
- package/dist/services/oAuth2CodeGranter.d.ts +1 -1
- package/dist/services/oAuth2CodeGranter.js +66 -109
- package/dist/services/oAuth2CodeGranter.js.map +1 -1
- package/dist/services/oAuth2CodeGranter.test.js +65 -66
- package/dist/services/oAuth2CodeGranter.test.js.map +1 -1
- package/dist/services/oAuth2Granters.js +131 -138
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2PasswordGranter.d.ts +1 -1
- package/dist/services/oAuth2PasswordGranter.js +27 -52
- package/dist/services/oAuth2PasswordGranter.js.map +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.js +34 -58
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
- package/dist/services/oAuth2TokenGranter.d.ts +1 -1
- package/dist/services/oAuth2TokenGranter.js +50 -86
- package/dist/services/oAuth2TokenGranter.js.map +1 -1
- package/package.json +46 -88
- package/src/__snapshots__/index.test.ts.snap +23 -22
- package/src/handlers/__snapshots__/postOAuth2Acknowledge.test.ts.snap +1 -1
- package/src/handlers/getOAuth2Authorize.test.ts +49 -13
- package/src/handlers/getOAuth2Authorize.ts +2 -2
- package/src/handlers/postAuthLogin.test.ts +9 -5
- package/src/handlers/postAuthLogin.ts +4 -4
- package/src/handlers/postAuthLogout.test.ts +8 -4
- package/src/handlers/postAuthLogout.ts +2 -2
- package/src/handlers/postAuthRefresh.test.ts +10 -6
- package/src/handlers/postAuthRefresh.ts +4 -4
- package/src/handlers/postOAuth2Acknowledge.test.ts +83 -35
- package/src/handlers/postOAuth2Acknowledge.ts +18 -8
- package/src/handlers/postOAuth2Token.test.ts +77 -17
- package/src/handlers/postOAuth2Token.ts +3 -3
- package/src/index.test.ts +110 -65
- package/src/index.ts +15 -15
- package/src/services/authCookies.test.ts +2 -2
- package/src/services/authCookies.ts +2 -1
- package/src/services/oAuth2ClientCredentialsGranter.ts +3 -3
- package/src/services/oAuth2CodeGranter.test.ts +27 -4
- package/src/services/oAuth2CodeGranter.ts +2 -2
- package/src/services/oAuth2PasswordGranter.ts +2 -2
- package/src/services/oAuth2RefreshTokenGranter.ts +2 -2
- package/src/services/oAuth2TokenGranter.ts +1 -1
- package/dist/handlers/getOAuth2Authorize.mjs +0 -160
- package/dist/handlers/getOAuth2Authorize.mjs.map +0 -1
- package/dist/handlers/getOAuth2Authorize.test.mjs +0 -114
- package/dist/handlers/getOAuth2Authorize.test.mjs.map +0 -1
- package/dist/handlers/postAuthLogin.mjs +0 -109
- package/dist/handlers/postAuthLogin.mjs.map +0 -1
- package/dist/handlers/postAuthLogin.test.mjs +0 -85
- package/dist/handlers/postAuthLogin.test.mjs.map +0 -1
- package/dist/handlers/postAuthLogout.mjs +0 -33
- package/dist/handlers/postAuthLogout.mjs.map +0 -1
- package/dist/handlers/postAuthLogout.test.mjs +0 -30
- package/dist/handlers/postAuthLogout.test.mjs.map +0 -1
- package/dist/handlers/postAuthRefresh.mjs +0 -122
- package/dist/handlers/postAuthRefresh.mjs.map +0 -1
- package/dist/handlers/postAuthRefresh.test.mjs +0 -94
- package/dist/handlers/postAuthRefresh.test.mjs.map +0 -1
- package/dist/handlers/postOAuth2Acknowledge.mjs +0 -150
- package/dist/handlers/postOAuth2Acknowledge.mjs.map +0 -1
- package/dist/handlers/postOAuth2Acknowledge.test.mjs +0 -140
- package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +0 -1
- package/dist/handlers/postOAuth2Token.mjs +0 -231
- package/dist/handlers/postOAuth2Token.mjs.map +0 -1
- package/dist/handlers/postOAuth2Token.test.mjs +0 -160
- package/dist/handlers/postOAuth2Token.test.mjs.map +0 -1
- package/dist/index.mjs +0 -15
- package/dist/index.mjs.map +0 -1
- package/dist/index.test.mjs +0 -747
- package/dist/index.test.mjs.map +0 -1
- package/dist/services/authCookies.mjs +0 -58
- package/dist/services/authCookies.mjs.map +0 -1
- package/dist/services/authCookies.test.mjs +0 -77
- package/dist/services/authCookies.test.mjs.map +0 -1
- package/dist/services/oAuth2ClientCredentialsGranter.mjs +0 -41
- package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +0 -1
- package/dist/services/oAuth2CodeGranter.mjs +0 -98
- package/dist/services/oAuth2CodeGranter.mjs.map +0 -1
- package/dist/services/oAuth2CodeGranter.test.mjs +0 -83
- package/dist/services/oAuth2CodeGranter.test.mjs.map +0 -1
- package/dist/services/oAuth2Granters.mjs +0 -127
- package/dist/services/oAuth2Granters.mjs.map +0 -1
- package/dist/services/oAuth2PasswordGranter.mjs +0 -40
- package/dist/services/oAuth2PasswordGranter.mjs.map +0 -1
- package/dist/services/oAuth2RefreshTokenGranter.mjs +0 -47
- package/dist/services/oAuth2RefreshTokenGranter.mjs.map +0 -1
- package/dist/services/oAuth2TokenGranter.mjs +0 -78
- package/dist/services/oAuth2TokenGranter.mjs.map +0 -1
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { jest } from '@jest/globals';
|
|
2
|
+
import initGetOAuth2Authorize from './getOAuth2Authorize.js';
|
|
2
3
|
import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
|
|
3
|
-
import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters';
|
|
4
|
-
import YError from 'yerror';
|
|
5
|
-
import type {
|
|
4
|
+
import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters.js';
|
|
5
|
+
import { YError } from 'yerror';
|
|
6
|
+
import type { OAuth2CodeGranterService } from '../services/oAuth2CodeGranter.js';
|
|
7
|
+
import type { OAuth2TokenGranterService } from '../services/oAuth2TokenGranter.js';
|
|
8
|
+
import type { LogService } from 'common-services';
|
|
9
|
+
import type { OAuth2Options, OAuth2GranterService } from '../index.js';
|
|
6
10
|
|
|
7
11
|
describe('getOAuth2Authorize', () => {
|
|
8
12
|
const OAUTH2: OAuth2Options = {
|
|
@@ -12,22 +16,54 @@ describe('getOAuth2Authorize', () => {
|
|
|
12
16
|
...DEFAULT_ERRORS_DESCRIPTORS,
|
|
13
17
|
...OAUTH2_ERRORS_DESCRIPTORS,
|
|
14
18
|
};
|
|
15
|
-
const log = jest.fn();
|
|
19
|
+
const log = jest.fn<LogService>();
|
|
16
20
|
const codeGranter = {
|
|
17
21
|
type: 'code',
|
|
18
|
-
authorizer: {
|
|
19
|
-
|
|
22
|
+
authorizer: {
|
|
23
|
+
responseType: 'code',
|
|
24
|
+
authorize:
|
|
25
|
+
jest.fn<
|
|
26
|
+
NonNullable<OAuth2CodeGranterService['authorizer']>['authorize']
|
|
27
|
+
>(),
|
|
28
|
+
},
|
|
29
|
+
acknowledger: {
|
|
30
|
+
acknowledgmentType: 'code',
|
|
31
|
+
acknowledge:
|
|
32
|
+
jest.fn<
|
|
33
|
+
NonNullable<OAuth2CodeGranterService['acknowledger']>['acknowledge']
|
|
34
|
+
>(),
|
|
35
|
+
},
|
|
20
36
|
authenticator: {
|
|
21
37
|
grantType: 'authorization_code',
|
|
22
|
-
authenticate:
|
|
38
|
+
authenticate:
|
|
39
|
+
jest.fn<
|
|
40
|
+
NonNullable<OAuth2CodeGranterService['authenticator']>['authenticate']
|
|
41
|
+
>(),
|
|
23
42
|
},
|
|
24
43
|
};
|
|
25
44
|
const tokenGranter = {
|
|
26
45
|
type: 'token',
|
|
27
|
-
authorizer: {
|
|
28
|
-
|
|
46
|
+
authorizer: {
|
|
47
|
+
responseType: 'token',
|
|
48
|
+
authorize:
|
|
49
|
+
jest.fn<
|
|
50
|
+
NonNullable<
|
|
51
|
+
NonNullable<OAuth2TokenGranterService['authorizer']>['authorize']
|
|
52
|
+
>
|
|
53
|
+
>(),
|
|
54
|
+
},
|
|
55
|
+
acknowledger: {
|
|
56
|
+
acknowledgmentType: 'token',
|
|
57
|
+
acknowledge:
|
|
58
|
+
jest.fn<
|
|
59
|
+
NonNullable<OAuth2TokenGranterService['acknowledger']>['acknowledge']
|
|
60
|
+
>(),
|
|
61
|
+
},
|
|
29
62
|
};
|
|
30
|
-
const oAuth2Granters
|
|
63
|
+
const oAuth2Granters = [
|
|
64
|
+
codeGranter,
|
|
65
|
+
tokenGranter,
|
|
66
|
+
] as unknown as OAuth2GranterService[];
|
|
31
67
|
|
|
32
68
|
beforeEach(() => {
|
|
33
69
|
log.mockReset();
|
|
@@ -46,7 +82,7 @@ describe('getOAuth2Authorize', () => {
|
|
|
46
82
|
codeGranter.authenticator.authenticate,
|
|
47
83
|
tokenGranter.authorizer.authorize,
|
|
48
84
|
tokenGranter.acknowledger.acknowledge,
|
|
49
|
-
].forEach((mock) =>
|
|
85
|
+
].forEach((mock: any) =>
|
|
50
86
|
mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
|
|
51
87
|
);
|
|
52
88
|
codeGranter.authorizer.authorize.mockResolvedValueOnce({
|
|
@@ -99,7 +135,7 @@ describe('getOAuth2Authorize', () => {
|
|
|
99
135
|
codeGranter.authenticator.authenticate,
|
|
100
136
|
tokenGranter.authorizer.authorize,
|
|
101
137
|
tokenGranter.acknowledger.acknowledge,
|
|
102
|
-
].forEach((mock) =>
|
|
138
|
+
].forEach((mock: any) =>
|
|
103
139
|
mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
|
|
104
140
|
);
|
|
105
141
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { autoHandler } from 'knifecycle';
|
|
2
2
|
import camelCase from 'camelcase';
|
|
3
|
-
import YError from 'yerror';
|
|
3
|
+
import { YError } from 'yerror';
|
|
4
4
|
import { refersTo } from '@whook/whook';
|
|
5
5
|
import type {
|
|
6
6
|
WhookAPIHandlerDefinition,
|
|
@@ -12,7 +12,7 @@ import type {
|
|
|
12
12
|
import type {
|
|
13
13
|
OAuth2Options,
|
|
14
14
|
OAuth2GranterService,
|
|
15
|
-
} from '../services/oAuth2Granters';
|
|
15
|
+
} from '../services/oAuth2Granters.js';
|
|
16
16
|
import type { LogService } from 'common-services';
|
|
17
17
|
|
|
18
18
|
/* Architecture Note #1: OAuth2 authorize
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { jest } from '@jest/globals';
|
|
2
|
+
import initPostAuthLogin from './postAuthLogin.js';
|
|
3
|
+
import type { AuthCookiesService } from '../services/authCookies.js';
|
|
2
4
|
|
|
3
5
|
describe('postAuthLogin', () => {
|
|
4
6
|
test('should work', async () => {
|
|
5
7
|
const authCookies = {
|
|
6
|
-
build: jest.fn(),
|
|
8
|
+
build: jest.fn<AuthCookiesService['build']>(),
|
|
7
9
|
};
|
|
8
|
-
const postOAuth2Token = jest.fn();
|
|
10
|
+
const postOAuth2Token = jest.fn<any>();
|
|
9
11
|
const postAuthLogin = await initPostAuthLogin({
|
|
10
12
|
ROOT_AUTHENTICATION_DATA: {
|
|
11
13
|
scope: 'user',
|
|
@@ -15,7 +17,7 @@ describe('postAuthLogin', () => {
|
|
|
15
17
|
postOAuth2Token,
|
|
16
18
|
});
|
|
17
19
|
|
|
18
|
-
authCookies.build.mockReturnValueOnce('the_build_cookies');
|
|
20
|
+
authCookies.build.mockReturnValueOnce(['the_build_cookies']);
|
|
19
21
|
postOAuth2Token.mockResolvedValueOnce({
|
|
20
22
|
status: 200,
|
|
21
23
|
body: {
|
|
@@ -78,7 +80,9 @@ describe('postAuthLogin', () => {
|
|
|
78
80
|
"token_type": "bearer",
|
|
79
81
|
},
|
|
80
82
|
"headers": Object {
|
|
81
|
-
"Set-Cookie":
|
|
83
|
+
"Set-Cookie": Array [
|
|
84
|
+
"the_build_cookies",
|
|
85
|
+
],
|
|
82
86
|
},
|
|
83
87
|
"status": 200,
|
|
84
88
|
},
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { autoHandler } from 'knifecycle';
|
|
2
|
-
import YHTTPError from 'yhttperror';
|
|
3
|
-
import initPostOauth2Token from './postOAuth2Token';
|
|
4
|
-
import { AUTH_API_PREFIX } from '../services/authCookies';
|
|
2
|
+
import { YHTTPError } from 'yhttperror';
|
|
3
|
+
import initPostOauth2Token from './postOAuth2Token.js';
|
|
4
|
+
import { AUTH_API_PREFIX } from '../services/authCookies.js';
|
|
5
5
|
import type { WhookAPIHandlerDefinition } from '@whook/whook';
|
|
6
6
|
import type {
|
|
7
7
|
AuthCookiesService,
|
|
8
8
|
AuthHandlersConfig,
|
|
9
|
-
} from '../services/authCookies';
|
|
9
|
+
} from '../services/authCookies.js';
|
|
10
10
|
import type { AsyncReturnType } from 'type-fest';
|
|
11
11
|
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
12
12
|
|
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { jest } from '@jest/globals';
|
|
2
|
+
import initPostAuthLogout from './postAuthLogout.js';
|
|
3
|
+
import type { AuthCookiesService } from '../services/authCookies.js';
|
|
2
4
|
|
|
3
5
|
describe('postAuthLogout', () => {
|
|
4
6
|
test('should work', async () => {
|
|
5
7
|
const authCookies = {
|
|
6
|
-
build: jest.fn(),
|
|
8
|
+
build: jest.fn<AuthCookiesService['build']>(),
|
|
7
9
|
};
|
|
8
10
|
const postAuthLogout = await initPostAuthLogout({
|
|
9
11
|
authCookies,
|
|
10
12
|
});
|
|
11
13
|
|
|
12
|
-
authCookies.build.mockReturnValueOnce('the_build_cookies');
|
|
14
|
+
authCookies.build.mockReturnValueOnce(['the_build_cookies']);
|
|
13
15
|
|
|
14
16
|
const response = await postAuthLogout({});
|
|
15
17
|
|
|
@@ -23,7 +25,9 @@ describe('postAuthLogout', () => {
|
|
|
23
25
|
],
|
|
24
26
|
"response": Object {
|
|
25
27
|
"headers": Object {
|
|
26
|
-
"Set-Cookie":
|
|
28
|
+
"Set-Cookie": Array [
|
|
29
|
+
"the_build_cookies",
|
|
30
|
+
],
|
|
27
31
|
},
|
|
28
32
|
"status": 204,
|
|
29
33
|
},
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { autoHandler } from 'knifecycle';
|
|
2
|
-
import { AUTH_API_PREFIX } from '../services/authCookies';
|
|
2
|
+
import { AUTH_API_PREFIX } from '../services/authCookies.js';
|
|
3
3
|
import type { WhookAPIHandlerDefinition } from '@whook/whook';
|
|
4
|
-
import type { AuthCookiesService } from '../services/authCookies';
|
|
4
|
+
import type { AuthCookiesService } from '../services/authCookies.js';
|
|
5
5
|
|
|
6
6
|
export const definition: WhookAPIHandlerDefinition = {
|
|
7
7
|
method: 'post',
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { jest } from '@jest/globals';
|
|
2
|
+
import initPostAuthRefresh from './postAuthRefresh.js';
|
|
3
|
+
import type { AuthCookiesService } from '../services/authCookies.js';
|
|
2
4
|
|
|
3
5
|
describe('postAuthRefresh', () => {
|
|
4
6
|
test('should work', async () => {
|
|
5
7
|
const authCookies = {
|
|
6
|
-
parse: jest.fn(),
|
|
7
|
-
build: jest.fn(),
|
|
8
|
+
parse: jest.fn<AuthCookiesService['parse']>(),
|
|
9
|
+
build: jest.fn<AuthCookiesService['build']>(),
|
|
8
10
|
};
|
|
9
|
-
const postOAuth2Token = jest.fn();
|
|
11
|
+
const postOAuth2Token = jest.fn<any>();
|
|
10
12
|
const postAuthRefresh = await initPostAuthRefresh({
|
|
11
13
|
ROOT_AUTHENTICATION_DATA: {
|
|
12
14
|
scope: 'user',
|
|
@@ -20,7 +22,7 @@ describe('postAuthRefresh', () => {
|
|
|
20
22
|
access_token: 'an_access_token',
|
|
21
23
|
refresh_token: 'a_refresh_token',
|
|
22
24
|
});
|
|
23
|
-
authCookies.build.mockReturnValueOnce('the_build_cookies');
|
|
25
|
+
authCookies.build.mockReturnValueOnce(['the_build_cookies']);
|
|
24
26
|
postOAuth2Token.mockResolvedValueOnce({
|
|
25
27
|
status: 200,
|
|
26
28
|
body: {
|
|
@@ -87,7 +89,9 @@ describe('postAuthRefresh', () => {
|
|
|
87
89
|
"token_type": "bearer",
|
|
88
90
|
},
|
|
89
91
|
"headers": Object {
|
|
90
|
-
"Set-Cookie":
|
|
92
|
+
"Set-Cookie": Array [
|
|
93
|
+
"the_build_cookies",
|
|
94
|
+
],
|
|
91
95
|
},
|
|
92
96
|
"status": 200,
|
|
93
97
|
},
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { autoHandler } from 'knifecycle';
|
|
2
|
-
import YHTTPError from 'yhttperror';
|
|
2
|
+
import { YHTTPError } from 'yhttperror';
|
|
3
3
|
import { refersTo } from '@whook/whook';
|
|
4
|
-
import initPostOauth2Token from './postOAuth2Token';
|
|
5
|
-
import { AUTH_API_PREFIX } from '../services/authCookies';
|
|
4
|
+
import initPostOauth2Token from './postOAuth2Token.js';
|
|
5
|
+
import { AUTH_API_PREFIX } from '../services/authCookies.js';
|
|
6
6
|
import type {
|
|
7
7
|
WhookAPIHandlerDefinition,
|
|
8
8
|
WhookAPIParameterDefinition,
|
|
@@ -10,7 +10,7 @@ import type {
|
|
|
10
10
|
import type {
|
|
11
11
|
AuthCookiesService,
|
|
12
12
|
AuthHandlersConfig,
|
|
13
|
-
} from '../services/authCookies';
|
|
13
|
+
} from '../services/authCookies.js';
|
|
14
14
|
import type { AsyncReturnType } from 'type-fest';
|
|
15
15
|
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
16
16
|
|
|
@@ -1,32 +1,86 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { jest } from '@jest/globals';
|
|
2
|
+
import initPostOAuth2Acknowledge from './postOAuth2Acknowledge.js';
|
|
3
|
+
import { YError } from 'yerror';
|
|
3
4
|
import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
|
|
4
|
-
import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters';
|
|
5
|
-
import type {
|
|
5
|
+
import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters.js';
|
|
6
|
+
import type { CheckApplicationService } from '../services/oAuth2Granters.js';
|
|
7
|
+
import type { LogService } from 'common-services';
|
|
8
|
+
import type { OAuth2GranterService } from '../services/oAuth2Granters.js';
|
|
6
9
|
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
10
|
+
import type { OAuth2CodeGranterService } from '../services/oAuth2CodeGranter.js';
|
|
11
|
+
import type { OAuth2TokenGranterService } from '../services/oAuth2TokenGranter.js';
|
|
12
|
+
|
|
13
|
+
type CustomAuthenticationData = BaseAuthenticationData & {
|
|
14
|
+
userId: string;
|
|
15
|
+
};
|
|
7
16
|
|
|
8
17
|
describe('postOAuth2Acknowledge', () => {
|
|
9
18
|
const ERRORS_DESCRIPTORS = {
|
|
10
19
|
...DEFAULT_ERRORS_DESCRIPTORS,
|
|
11
20
|
...OAUTH2_ERRORS_DESCRIPTORS,
|
|
12
21
|
};
|
|
13
|
-
const checkApplication = jest.fn();
|
|
14
|
-
const log = jest.fn();
|
|
22
|
+
const checkApplication = jest.fn<CheckApplicationService>();
|
|
23
|
+
const log = jest.fn<LogService>();
|
|
15
24
|
const codeGranter = {
|
|
16
25
|
type: 'code',
|
|
17
|
-
authorizer: {
|
|
18
|
-
|
|
26
|
+
authorizer: {
|
|
27
|
+
responseType: 'code',
|
|
28
|
+
authorize:
|
|
29
|
+
jest.fn<
|
|
30
|
+
NonNullable<
|
|
31
|
+
OAuth2CodeGranterService<CustomAuthenticationData>['authorizer']
|
|
32
|
+
>['authorize']
|
|
33
|
+
>(),
|
|
34
|
+
},
|
|
35
|
+
acknowledger: {
|
|
36
|
+
acknowledgmentType: 'code',
|
|
37
|
+
acknowledge:
|
|
38
|
+
jest.fn<
|
|
39
|
+
NonNullable<
|
|
40
|
+
OAuth2CodeGranterService<CustomAuthenticationData>['acknowledger']
|
|
41
|
+
>['acknowledge']
|
|
42
|
+
>(),
|
|
43
|
+
},
|
|
19
44
|
authenticator: {
|
|
20
45
|
grantType: 'authorization_code',
|
|
21
|
-
authenticate:
|
|
46
|
+
authenticate:
|
|
47
|
+
jest.fn<
|
|
48
|
+
NonNullable<
|
|
49
|
+
OAuth2CodeGranterService<CustomAuthenticationData>['authenticator']
|
|
50
|
+
>['authenticate']
|
|
51
|
+
>(),
|
|
22
52
|
},
|
|
23
53
|
};
|
|
24
54
|
const tokenGranter = {
|
|
25
55
|
type: 'token',
|
|
26
|
-
authorizer: {
|
|
27
|
-
|
|
56
|
+
authorizer: {
|
|
57
|
+
responseType: 'token',
|
|
58
|
+
authorize:
|
|
59
|
+
jest.fn<
|
|
60
|
+
NonNullable<
|
|
61
|
+
OAuth2TokenGranterService<CustomAuthenticationData>['authenticator']
|
|
62
|
+
>['authenticate']
|
|
63
|
+
>(),
|
|
64
|
+
},
|
|
65
|
+
acknowledger: {
|
|
66
|
+
acknowledgmentType: 'token',
|
|
67
|
+
acknowledge:
|
|
68
|
+
jest.fn<
|
|
69
|
+
NonNullable<
|
|
70
|
+
OAuth2TokenGranterService<CustomAuthenticationData>['acknowledger']
|
|
71
|
+
>['acknowledge']
|
|
72
|
+
>(),
|
|
73
|
+
},
|
|
28
74
|
};
|
|
29
|
-
const oAuth2Granters
|
|
75
|
+
const oAuth2Granters = [
|
|
76
|
+
codeGranter,
|
|
77
|
+
tokenGranter,
|
|
78
|
+
] as unknown as OAuth2GranterService<
|
|
79
|
+
Record<string, unknown>,
|
|
80
|
+
Record<string, unknown>,
|
|
81
|
+
Record<string, unknown>,
|
|
82
|
+
CustomAuthenticationData
|
|
83
|
+
>[];
|
|
30
84
|
|
|
31
85
|
beforeEach(() => {
|
|
32
86
|
checkApplication.mockReset();
|
|
@@ -53,24 +107,21 @@ describe('postOAuth2Acknowledge', () => {
|
|
|
53
107
|
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
54
108
|
redirectURI: 'http://lol',
|
|
55
109
|
scope: 'user',
|
|
56
|
-
userId: 1,
|
|
110
|
+
userId: '1',
|
|
57
111
|
});
|
|
58
112
|
|
|
59
|
-
const postOAuth2Acknowledge =
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
checkApplication,
|
|
67
|
-
log,
|
|
68
|
-
});
|
|
113
|
+
const postOAuth2Acknowledge =
|
|
114
|
+
await initPostOAuth2Acknowledge<CustomAuthenticationData>({
|
|
115
|
+
ERRORS_DESCRIPTORS,
|
|
116
|
+
oAuth2Granters,
|
|
117
|
+
checkApplication,
|
|
118
|
+
log,
|
|
119
|
+
});
|
|
69
120
|
const response = await postOAuth2Acknowledge({
|
|
70
121
|
authenticationData: {
|
|
71
122
|
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
72
123
|
scope: 'auth',
|
|
73
|
-
userId: 1,
|
|
124
|
+
userId: '1',
|
|
74
125
|
},
|
|
75
126
|
body: {
|
|
76
127
|
responseType: 'code',
|
|
@@ -120,21 +171,18 @@ describe('postOAuth2Acknowledge', () => {
|
|
|
120
171
|
mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
|
|
121
172
|
);
|
|
122
173
|
|
|
123
|
-
const postOAuth2Acknowledge =
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
checkApplication,
|
|
131
|
-
log,
|
|
132
|
-
});
|
|
174
|
+
const postOAuth2Acknowledge =
|
|
175
|
+
await initPostOAuth2Acknowledge<CustomAuthenticationData>({
|
|
176
|
+
ERRORS_DESCRIPTORS,
|
|
177
|
+
oAuth2Granters,
|
|
178
|
+
checkApplication,
|
|
179
|
+
log,
|
|
180
|
+
});
|
|
133
181
|
const response = await postOAuth2Acknowledge({
|
|
134
182
|
authenticationData: {
|
|
135
183
|
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
136
184
|
scope: 'auth',
|
|
137
|
-
userId: 1,
|
|
185
|
+
userId: '1',
|
|
138
186
|
},
|
|
139
187
|
body: {
|
|
140
188
|
responseType: 'yolo',
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { autoHandler } from 'knifecycle';
|
|
2
|
-
import YError from 'yerror';
|
|
3
|
-
import { setURLError } from './getOAuth2Authorize';
|
|
2
|
+
import { YError } from 'yerror';
|
|
3
|
+
import { setURLError } from './getOAuth2Authorize.js';
|
|
4
4
|
import type {
|
|
5
5
|
WhookAPIHandlerDefinition,
|
|
6
6
|
WhookErrorsDescriptors,
|
|
@@ -8,7 +8,7 @@ import type {
|
|
|
8
8
|
import type {
|
|
9
9
|
CheckApplicationService,
|
|
10
10
|
OAuth2GranterService,
|
|
11
|
-
} from '../services/oAuth2Granters';
|
|
11
|
+
} from '../services/oAuth2Granters.js';
|
|
12
12
|
import type { LogService } from 'common-services';
|
|
13
13
|
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
14
14
|
|
|
@@ -95,9 +95,16 @@ export const definition: WhookAPIHandlerDefinition = {
|
|
|
95
95
|
},
|
|
96
96
|
};
|
|
97
97
|
|
|
98
|
-
export type HandlerDependencies
|
|
98
|
+
export type HandlerDependencies<
|
|
99
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData<string, string>,
|
|
100
|
+
> = {
|
|
99
101
|
ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
|
|
100
|
-
oAuth2Granters: OAuth2GranterService
|
|
102
|
+
oAuth2Granters: OAuth2GranterService<
|
|
103
|
+
Record<string, unknown>,
|
|
104
|
+
Record<string, unknown>,
|
|
105
|
+
Record<string, unknown>,
|
|
106
|
+
AUTHENTICATION_DATA
|
|
107
|
+
>[];
|
|
101
108
|
checkApplication: CheckApplicationService;
|
|
102
109
|
log: LogService;
|
|
103
110
|
};
|
|
@@ -117,7 +124,7 @@ type HandlerParameters<AUTHENTICATION_DATA> = {
|
|
|
117
124
|
export default autoHandler(postOAuth2Acknowledge) as unknown as <
|
|
118
125
|
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
119
126
|
>(
|
|
120
|
-
dependencies: HandlerDependencies
|
|
127
|
+
dependencies: HandlerDependencies<AUTHENTICATION_DATA>,
|
|
121
128
|
) => (
|
|
122
129
|
parameters: HandlerParameters<AUTHENTICATION_DATA>,
|
|
123
130
|
) => ReturnType<typeof postOAuth2Acknowledge>;
|
|
@@ -130,7 +137,7 @@ async function postOAuth2Acknowledge<
|
|
|
130
137
|
oAuth2Granters,
|
|
131
138
|
checkApplication,
|
|
132
139
|
log,
|
|
133
|
-
}: HandlerDependencies
|
|
140
|
+
}: HandlerDependencies<AUTHENTICATION_DATA>,
|
|
134
141
|
{
|
|
135
142
|
authenticationData,
|
|
136
143
|
body: {
|
|
@@ -188,7 +195,10 @@ async function postOAuth2Acknowledge<
|
|
|
188
195
|
url.searchParams.set('scope', scope);
|
|
189
196
|
url.searchParams.set('state', state);
|
|
190
197
|
Object.keys(additionalProperties).forEach((key) =>
|
|
191
|
-
url.searchParams.set(
|
|
198
|
+
url.searchParams.set(
|
|
199
|
+
snakeCase(key),
|
|
200
|
+
additionalProperties[key] as unknown as string,
|
|
201
|
+
),
|
|
192
202
|
);
|
|
193
203
|
} catch (err) {
|
|
194
204
|
log('debug', '👫 - OAuth2 acknowledge error', (err as YError).code);
|
|
@@ -1,54 +1,112 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { jest } from '@jest/globals';
|
|
2
|
+
import initPostOAuth2Token from './postOAuth2Token.js';
|
|
3
|
+
import { YError } from 'yerror';
|
|
4
|
+
import type { LogService, TimeService } from 'common-services';
|
|
5
|
+
import type {
|
|
6
|
+
OAuth2GranterService,
|
|
7
|
+
OAuth2AccessTokenService,
|
|
8
|
+
OAuth2RefreshTokenService,
|
|
9
|
+
} from '../index.js';
|
|
10
|
+
import type { OAuth2CodeGranterService } from '../services/oAuth2CodeGranter.js';
|
|
11
|
+
import type { OAuth2TokenGranterService } from '../services/oAuth2TokenGranter.js';
|
|
12
|
+
import type { OAuth2PasswordGranterService } from '../services/oAuth2PasswordGranter.js';
|
|
13
|
+
import type { OAuth2RefreshTokenGranterService } from '../services/oAuth2RefreshTokenGranter.js';
|
|
4
14
|
|
|
5
15
|
describe('postOAuth2Token', () => {
|
|
6
|
-
const log = jest.fn();
|
|
7
|
-
const time = jest.fn();
|
|
8
|
-
const oAuth2AccessToken = {
|
|
9
|
-
|
|
16
|
+
const log = jest.fn<LogService>();
|
|
17
|
+
const time = jest.fn<TimeService>();
|
|
18
|
+
const oAuth2AccessToken = {
|
|
19
|
+
create: jest.fn<OAuth2AccessTokenService['create']>(),
|
|
20
|
+
check: jest.fn<OAuth2AccessTokenService['check']>(),
|
|
21
|
+
};
|
|
22
|
+
const oAuth2RefreshToken = {
|
|
23
|
+
create: jest.fn<OAuth2RefreshTokenService['create']>(),
|
|
24
|
+
check: jest.fn<OAuth2RefreshTokenService['check']>(),
|
|
25
|
+
};
|
|
10
26
|
const codeGranter = {
|
|
11
27
|
type: 'code',
|
|
12
|
-
authorizer: {
|
|
13
|
-
|
|
28
|
+
authorizer: {
|
|
29
|
+
responseType: 'code',
|
|
30
|
+
authorize:
|
|
31
|
+
jest.fn<
|
|
32
|
+
NonNullable<OAuth2CodeGranterService['authorizer']>['authorize']
|
|
33
|
+
>(),
|
|
34
|
+
},
|
|
35
|
+
acknowledger: {
|
|
36
|
+
acknowledgmentType: 'code',
|
|
37
|
+
acknowledge:
|
|
38
|
+
jest.fn<
|
|
39
|
+
NonNullable<OAuth2CodeGranterService['acknowledger']>['acknowledge']
|
|
40
|
+
>(),
|
|
41
|
+
},
|
|
14
42
|
authenticator: {
|
|
15
43
|
grantType: 'authorization_code',
|
|
16
|
-
authenticate:
|
|
44
|
+
authenticate:
|
|
45
|
+
jest.fn<
|
|
46
|
+
NonNullable<OAuth2CodeGranterService['authenticator']>['authenticate']
|
|
47
|
+
>(),
|
|
17
48
|
},
|
|
18
49
|
};
|
|
19
50
|
const passwordGranter = {
|
|
20
51
|
type: 'password',
|
|
21
52
|
authenticator: {
|
|
22
53
|
grantType: 'password',
|
|
23
|
-
authenticate:
|
|
54
|
+
authenticate:
|
|
55
|
+
jest.fn<
|
|
56
|
+
NonNullable<
|
|
57
|
+
OAuth2PasswordGranterService['authenticator']
|
|
58
|
+
>['authenticate']
|
|
59
|
+
>(),
|
|
24
60
|
},
|
|
25
61
|
};
|
|
26
62
|
const clientCredentialsGranter = {
|
|
27
63
|
type: 'client_credentials',
|
|
28
64
|
authenticator: {
|
|
29
65
|
grantType: 'client_credentials',
|
|
30
|
-
authenticate:
|
|
66
|
+
authenticate:
|
|
67
|
+
jest.fn<
|
|
68
|
+
NonNullable<OAuth2CodeGranterService['authorizer']>['authorize']
|
|
69
|
+
>(),
|
|
31
70
|
},
|
|
32
71
|
};
|
|
33
72
|
const tokenGranter = {
|
|
34
73
|
type: 'token',
|
|
35
|
-
authorizer: {
|
|
36
|
-
|
|
74
|
+
authorizer: {
|
|
75
|
+
responseType: 'token',
|
|
76
|
+
authorize:
|
|
77
|
+
jest.fn<
|
|
78
|
+
NonNullable<
|
|
79
|
+
OAuth2TokenGranterService['authenticator']
|
|
80
|
+
>['authenticate']
|
|
81
|
+
>(),
|
|
82
|
+
},
|
|
83
|
+
acknowledger: {
|
|
84
|
+
acknowledgmentType: 'token',
|
|
85
|
+
acknowledge:
|
|
86
|
+
jest.fn<
|
|
87
|
+
NonNullable<OAuth2TokenGranterService['acknowledger']>['acknowledge']
|
|
88
|
+
>(),
|
|
89
|
+
},
|
|
37
90
|
};
|
|
38
91
|
const refreshTokenGranter = {
|
|
39
92
|
type: 'refresh',
|
|
40
93
|
authenticator: {
|
|
41
94
|
grantType: 'refresh',
|
|
42
|
-
authenticate:
|
|
95
|
+
authenticate:
|
|
96
|
+
jest.fn<
|
|
97
|
+
NonNullable<
|
|
98
|
+
OAuth2RefreshTokenGranterService['authenticator']
|
|
99
|
+
>['authenticate']
|
|
100
|
+
>(),
|
|
43
101
|
},
|
|
44
102
|
};
|
|
45
|
-
const oAuth2Granters
|
|
103
|
+
const oAuth2Granters = [
|
|
46
104
|
codeGranter,
|
|
47
105
|
tokenGranter,
|
|
48
106
|
passwordGranter,
|
|
49
107
|
refreshTokenGranter,
|
|
50
108
|
clientCredentialsGranter,
|
|
51
|
-
];
|
|
109
|
+
] as unknown as OAuth2GranterService[];
|
|
52
110
|
|
|
53
111
|
beforeEach(() => {
|
|
54
112
|
log.mockReset();
|
|
@@ -79,6 +137,8 @@ describe('postOAuth2Token', () => {
|
|
|
79
137
|
});
|
|
80
138
|
codeGranter.authorizer.authorize.mockResolvedValueOnce({
|
|
81
139
|
applicationId: '',
|
|
140
|
+
redirectURI: 'http://lol',
|
|
141
|
+
scope: 'user,admin',
|
|
82
142
|
});
|
|
83
143
|
|
|
84
144
|
const postOAuth2Token = await initPostOAuth2Token({
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { autoHandler } from 'knifecycle';
|
|
2
|
-
import { camelCaseObjectProperties } from './getOAuth2Authorize';
|
|
2
|
+
import { camelCaseObjectProperties } from './getOAuth2Authorize.js';
|
|
3
3
|
import { noop } from '@whook/whook';
|
|
4
|
-
import YError from 'yerror';
|
|
4
|
+
import { YError } from 'yerror';
|
|
5
5
|
import { refersTo } from '@whook/whook';
|
|
6
6
|
import type { OpenAPIV3 } from 'openapi-types';
|
|
7
7
|
import type { LogService, TimeService } from 'common-services';
|
|
@@ -13,7 +13,7 @@ import type {
|
|
|
13
13
|
OAuth2GranterService,
|
|
14
14
|
OAuth2AccessTokenService,
|
|
15
15
|
OAuth2RefreshTokenService,
|
|
16
|
-
} from '../services/oAuth2Granters';
|
|
16
|
+
} from '../services/oAuth2Granters.js';
|
|
17
17
|
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
18
18
|
|
|
19
19
|
/* Architecture Note #2: OAuth2 acknowledge
|