@whook/oauth2 8.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +384 -0
- package/dist/handlers/getOAuth2Authorize.d.ts +26 -0
- package/dist/handlers/getOAuth2Authorize.js +192 -0
- package/dist/handlers/getOAuth2Authorize.js.map +1 -0
- package/dist/handlers/getOAuth2Authorize.mjs +169 -0
- package/dist/handlers/getOAuth2Authorize.mjs.map +1 -0
- package/dist/handlers/getOAuth2Authorize.test.d.ts +1 -0
- package/dist/handlers/getOAuth2Authorize.test.js +122 -0
- package/dist/handlers/getOAuth2Authorize.test.js.map +1 -0
- package/dist/handlers/getOAuth2Authorize.test.mjs +114 -0
- package/dist/handlers/getOAuth2Authorize.test.mjs.map +1 -0
- package/dist/handlers/postAuthLogin.d.ts +45 -0
- package/dist/handlers/postAuthLogin.js +125 -0
- package/dist/handlers/postAuthLogin.js.map +1 -0
- package/dist/handlers/postAuthLogin.mjs +108 -0
- package/dist/handlers/postAuthLogin.mjs.map +1 -0
- package/dist/handlers/postAuthLogin.test.d.ts +1 -0
- package/dist/handlers/postAuthLogin.test.js +90 -0
- package/dist/handlers/postAuthLogin.test.js.map +1 -0
- package/dist/handlers/postAuthLogin.test.mjs +85 -0
- package/dist/handlers/postAuthLogin.test.mjs.map +1 -0
- package/dist/handlers/postAuthLogout.d.ts +12 -0
- package/dist/handlers/postAuthLogout.js +46 -0
- package/dist/handlers/postAuthLogout.js.map +1 -0
- package/dist/handlers/postAuthLogout.mjs +33 -0
- package/dist/handlers/postAuthLogout.mjs.map +1 -0
- package/dist/handlers/postAuthLogout.test.d.ts +1 -0
- package/dist/handlers/postAuthLogout.test.js +35 -0
- package/dist/handlers/postAuthLogout.test.js.map +1 -0
- package/dist/handlers/postAuthLogout.test.mjs +30 -0
- package/dist/handlers/postAuthLogout.test.mjs.map +1 -0
- package/dist/handlers/postAuthRefresh.d.ts +45 -0
- package/dist/handlers/postAuthRefresh.js +140 -0
- package/dist/handlers/postAuthRefresh.js.map +1 -0
- package/dist/handlers/postAuthRefresh.mjs +122 -0
- package/dist/handlers/postAuthRefresh.mjs.map +1 -0
- package/dist/handlers/postAuthRefresh.test.d.ts +1 -0
- package/dist/handlers/postAuthRefresh.test.js +99 -0
- package/dist/handlers/postAuthRefresh.test.js.map +1 -0
- package/dist/handlers/postAuthRefresh.test.mjs +94 -0
- package/dist/handlers/postAuthRefresh.test.mjs.map +1 -0
- package/dist/handlers/postOAuth2Acknowledge.d.ts +28 -0
- package/dist/handlers/postOAuth2Acknowledge.js +165 -0
- package/dist/handlers/postOAuth2Acknowledge.js.map +1 -0
- package/dist/handlers/postOAuth2Acknowledge.mjs +150 -0
- package/dist/handlers/postOAuth2Acknowledge.mjs.map +1 -0
- package/dist/handlers/postOAuth2Acknowledge.test.d.ts +1 -0
- package/dist/handlers/postOAuth2Acknowledge.test.js +148 -0
- package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -0
- package/dist/handlers/postOAuth2Acknowledge.test.mjs +140 -0
- package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +1 -0
- package/dist/handlers/postOAuth2Token.d.ts +36 -0
- package/dist/handlers/postOAuth2Token.js +263 -0
- package/dist/handlers/postOAuth2Token.js.map +1 -0
- package/dist/handlers/postOAuth2Token.mjs +242 -0
- package/dist/handlers/postOAuth2Token.mjs.map +1 -0
- package/dist/handlers/postOAuth2Token.test.d.ts +1 -0
- package/dist/handlers/postOAuth2Token.test.js +166 -0
- package/dist/handlers/postOAuth2Token.test.js.map +1 -0
- package/dist/handlers/postOAuth2Token.test.mjs +160 -0
- package/dist/handlers/postOAuth2Token.test.mjs.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +230 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +15 -0
- package/dist/index.mjs.map +1 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +759 -0
- package/dist/index.test.js.map +1 -0
- package/dist/index.test.mjs +748 -0
- package/dist/index.test.mjs.map +1 -0
- package/dist/services/authCookies.d.ts +26 -0
- package/dist/services/authCookies.js +74 -0
- package/dist/services/authCookies.js.map +1 -0
- package/dist/services/authCookies.mjs +58 -0
- package/dist/services/authCookies.mjs.map +1 -0
- package/dist/services/authCookies.test.d.ts +1 -0
- package/dist/services/authCookies.test.js +82 -0
- package/dist/services/authCookies.test.js.map +1 -0
- package/dist/services/authCookies.test.mjs +77 -0
- package/dist/services/authCookies.test.mjs.map +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.d.ts +17 -0
- package/dist/services/oAuth2ClientCredentialsGranter.js +56 -0
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.mjs +41 -0
- package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +1 -0
- package/dist/services/oAuth2CodeGranter.d.ts +17 -0
- package/dist/services/oAuth2CodeGranter.js +115 -0
- package/dist/services/oAuth2CodeGranter.js.map +1 -0
- package/dist/services/oAuth2CodeGranter.mjs +98 -0
- package/dist/services/oAuth2CodeGranter.mjs.map +1 -0
- package/dist/services/oAuth2CodeGranter.test.d.ts +1 -0
- package/dist/services/oAuth2CodeGranter.test.js +86 -0
- package/dist/services/oAuth2CodeGranter.test.js.map +1 -0
- package/dist/services/oAuth2CodeGranter.test.mjs +81 -0
- package/dist/services/oAuth2CodeGranter.test.mjs.map +1 -0
- package/dist/services/oAuth2Granters.d.ts +79 -0
- package/dist/services/oAuth2Granters.js +140 -0
- package/dist/services/oAuth2Granters.js.map +1 -0
- package/dist/services/oAuth2Granters.mjs +127 -0
- package/dist/services/oAuth2Granters.mjs.map +1 -0
- package/dist/services/oAuth2PasswordGranter.d.ts +16 -0
- package/dist/services/oAuth2PasswordGranter.js +55 -0
- package/dist/services/oAuth2PasswordGranter.js.map +1 -0
- package/dist/services/oAuth2PasswordGranter.mjs +40 -0
- package/dist/services/oAuth2PasswordGranter.mjs.map +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +15 -0
- package/dist/services/oAuth2RefreshTokenGranter.js +62 -0
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.mjs +47 -0
- package/dist/services/oAuth2RefreshTokenGranter.mjs.map +1 -0
- package/dist/services/oAuth2TokenGranter.d.ts +15 -0
- package/dist/services/oAuth2TokenGranter.js +92 -0
- package/dist/services/oAuth2TokenGranter.js.map +1 -0
- package/dist/services/oAuth2TokenGranter.mjs +78 -0
- package/dist/services/oAuth2TokenGranter.mjs.map +1 -0
- package/package.json +205 -0
- package/src/__snapshots__/index.test.ts.snap +396 -0
- package/src/handlers/__snapshots__/getOAuth2Authorize.test.ts.snap +38 -0
- package/src/handlers/__snapshots__/postOAuth2Acknowledge.test.ts.snap +61 -0
- package/src/handlers/__snapshots__/postOAuth2Token.test.ts.snap +52 -0
- package/src/handlers/getOAuth2Authorize.test.ts +142 -0
- package/src/handlers/getOAuth2Authorize.ts +222 -0
- package/src/handlers/postAuthLogin.test.ts +88 -0
- package/src/handlers/postAuthLogin.ts +123 -0
- package/src/handlers/postAuthLogout.test.ts +33 -0
- package/src/handlers/postAuthLogout.ts +38 -0
- package/src/handlers/postAuthRefresh.test.ts +97 -0
- package/src/handlers/postAuthRefresh.ts +146 -0
- package/src/handlers/postOAuth2Acknowledge.test.ts +175 -0
- package/src/handlers/postOAuth2Acknowledge.ts +209 -0
- package/src/handlers/postOAuth2Token.test.ts +153 -0
- package/src/handlers/postOAuth2Token.ts +302 -0
- package/src/index.test.ts +936 -0
- package/src/index.ts +105 -0
- package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +49 -0
- package/src/services/authCookies.test.ts +93 -0
- package/src/services/authCookies.ts +82 -0
- package/src/services/oAuth2ClientCredentialsGranter.ts +76 -0
- package/src/services/oAuth2CodeGranter.test.ts +94 -0
- package/src/services/oAuth2CodeGranter.ts +133 -0
- package/src/services/oAuth2Granters.ts +274 -0
- package/src/services/oAuth2PasswordGranter.ts +75 -0
- package/src/services/oAuth2RefreshTokenGranter.ts +77 -0
- package/src/services/oAuth2TokenGranter.ts +105 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import initPostOAuth2Acknowledge from './postOAuth2Acknowledge';
|
|
2
|
+
import YError from 'yerror';
|
|
3
|
+
import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
|
|
4
|
+
import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters';
|
|
5
|
+
describe('postOAuth2Acknowledge', () => {
|
|
6
|
+
const ERRORS_DESCRIPTORS = { ...DEFAULT_ERRORS_DESCRIPTORS,
|
|
7
|
+
...OAUTH2_ERRORS_DESCRIPTORS
|
|
8
|
+
};
|
|
9
|
+
const checkApplication = jest.fn();
|
|
10
|
+
const log = jest.fn();
|
|
11
|
+
const codeGranter = {
|
|
12
|
+
type: 'code',
|
|
13
|
+
authorizer: {
|
|
14
|
+
responseType: 'code',
|
|
15
|
+
authorize: jest.fn()
|
|
16
|
+
},
|
|
17
|
+
acknowledger: {
|
|
18
|
+
acknowledgmentType: 'code',
|
|
19
|
+
acknowledge: jest.fn()
|
|
20
|
+
},
|
|
21
|
+
authenticator: {
|
|
22
|
+
grantType: 'authorization_code',
|
|
23
|
+
authenticate: jest.fn()
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
const tokenGranter = {
|
|
27
|
+
type: 'token',
|
|
28
|
+
authorizer: {
|
|
29
|
+
responseType: 'token',
|
|
30
|
+
authorize: jest.fn()
|
|
31
|
+
},
|
|
32
|
+
acknowledger: {
|
|
33
|
+
acknowledgmentType: 'token',
|
|
34
|
+
acknowledge: jest.fn()
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const oAuth2Granters = [codeGranter, tokenGranter];
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
checkApplication.mockReset();
|
|
40
|
+
log.mockReset();
|
|
41
|
+
[codeGranter.authorizer.authorize, codeGranter.acknowledger.acknowledge, codeGranter.authenticator.authenticate, tokenGranter.authorizer.authorize, tokenGranter.acknowledger.acknowledge].forEach(mock => mock.mockReset());
|
|
42
|
+
});
|
|
43
|
+
test('should redirect', async () => {
|
|
44
|
+
[codeGranter.authorizer.authorize, codeGranter.authenticator.authenticate, tokenGranter.authorizer.authorize, tokenGranter.acknowledger.acknowledge].forEach(mock => mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')));
|
|
45
|
+
codeGranter.acknowledger.acknowledge.mockResolvedValueOnce({
|
|
46
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
47
|
+
redirectURI: 'http://lol',
|
|
48
|
+
scope: 'user',
|
|
49
|
+
userId: 1
|
|
50
|
+
});
|
|
51
|
+
const postOAuth2Acknowledge = await initPostOAuth2Acknowledge({
|
|
52
|
+
ERRORS_DESCRIPTORS,
|
|
53
|
+
oAuth2Granters,
|
|
54
|
+
checkApplication,
|
|
55
|
+
log
|
|
56
|
+
});
|
|
57
|
+
const response = await postOAuth2Acknowledge({
|
|
58
|
+
authenticationData: {
|
|
59
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
60
|
+
scope: 'auth',
|
|
61
|
+
userId: 1
|
|
62
|
+
},
|
|
63
|
+
body: {
|
|
64
|
+
responseType: 'code',
|
|
65
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
66
|
+
redirectURI: 'https://www.example.com',
|
|
67
|
+
scope: 'user',
|
|
68
|
+
state: 'bancal',
|
|
69
|
+
acknowledged: true
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
expect({
|
|
73
|
+
response
|
|
74
|
+
}).toMatchInlineSnapshot(`
|
|
75
|
+
Object {
|
|
76
|
+
"response": Object {
|
|
77
|
+
"headers": Object {
|
|
78
|
+
"location": "https://www.example.com/?client_id=abbacaca-abba-caca-abba-cacaabbacaca&scope=user&state=bancal&redirect_uri=http%3A%2F%2Flol&user_id=1",
|
|
79
|
+
},
|
|
80
|
+
"status": 302,
|
|
81
|
+
},
|
|
82
|
+
}
|
|
83
|
+
`);
|
|
84
|
+
expect({
|
|
85
|
+
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
|
|
86
|
+
checkApplicationCalls: checkApplication.mock.calls,
|
|
87
|
+
codeGranterAuthorizerAuthorizeCalls: codeGranter.authorizer.authorize.mock.calls,
|
|
88
|
+
codeGranterAcknowledgerAcknowledgeCalls: codeGranter.acknowledger.acknowledge.mock.calls,
|
|
89
|
+
codeGranterAuthenticatorAuthenticateCalls: codeGranter.authenticator.authenticate.mock.calls,
|
|
90
|
+
tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorizer.authorize.mock.calls,
|
|
91
|
+
tokenGranterAcknowledgerAcknowledgeCalls: tokenGranter.acknowledger.acknowledge.mock.calls
|
|
92
|
+
}).toMatchSnapshot();
|
|
93
|
+
});
|
|
94
|
+
test('should redirect errors too', async () => {
|
|
95
|
+
[codeGranter.authorizer.authorize, codeGranter.authenticator.authenticate, tokenGranter.authorizer.authorize, tokenGranter.acknowledger.acknowledge, codeGranter.acknowledger.acknowledge].forEach(mock => mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')));
|
|
96
|
+
const postOAuth2Acknowledge = await initPostOAuth2Acknowledge({
|
|
97
|
+
ERRORS_DESCRIPTORS,
|
|
98
|
+
oAuth2Granters,
|
|
99
|
+
checkApplication,
|
|
100
|
+
log
|
|
101
|
+
});
|
|
102
|
+
const response = await postOAuth2Acknowledge({
|
|
103
|
+
authenticationData: {
|
|
104
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
105
|
+
scope: 'auth',
|
|
106
|
+
userId: 1
|
|
107
|
+
},
|
|
108
|
+
body: {
|
|
109
|
+
responseType: 'yolo',
|
|
110
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
111
|
+
redirectURI: 'https://www.example.com',
|
|
112
|
+
scope: 'user',
|
|
113
|
+
state: 'bancal',
|
|
114
|
+
acknowledged: true
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
expect({
|
|
118
|
+
response
|
|
119
|
+
}).toMatchInlineSnapshot(`
|
|
120
|
+
Object {
|
|
121
|
+
"response": Object {
|
|
122
|
+
"headers": Object {
|
|
123
|
+
"location": "https://www.example.com/?error=unsupported_response_type&error_decription=Type+%22yolo%22+not+supported.",
|
|
124
|
+
},
|
|
125
|
+
"status": 302,
|
|
126
|
+
},
|
|
127
|
+
}
|
|
128
|
+
`);
|
|
129
|
+
expect({
|
|
130
|
+
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
|
|
131
|
+
checkApplicationCalls: checkApplication.mock.calls,
|
|
132
|
+
codeGranterAuthorizerAuthorizeCalls: codeGranter.authorizer.authorize.mock.calls,
|
|
133
|
+
codeGranterAcknowledgerAcknowledgeCalls: codeGranter.acknowledger.acknowledge.mock.calls,
|
|
134
|
+
codeGranterAuthenticatorAuthenticateCalls: codeGranter.authenticator.authenticate.mock.calls,
|
|
135
|
+
tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorizer.authorize.mock.calls,
|
|
136
|
+
tokenGranterAcknowledgerAcknowledgeCalls: tokenGranter.acknowledger.acknowledge.mock.calls
|
|
137
|
+
}).toMatchSnapshot();
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
//# sourceMappingURL=postOAuth2Acknowledge.test.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/handlers/postOAuth2Acknowledge.test.ts"],"names":["initPostOAuth2Acknowledge","YError","DEFAULT_ERRORS_DESCRIPTORS","OAUTH2_ERRORS_DESCRIPTORS","describe","ERRORS_DESCRIPTORS","checkApplication","jest","fn","log","codeGranter","type","authorizer","responseType","authorize","acknowledger","acknowledgmentType","acknowledge","authenticator","grantType","authenticate","tokenGranter","oAuth2Granters","beforeEach","mockReset","forEach","mock","test","mockRejectedValueOnce","mockResolvedValueOnce","applicationId","redirectURI","scope","userId","postOAuth2Acknowledge","response","authenticationData","body","clientId","state","acknowledged","expect","toMatchInlineSnapshot","logCalls","calls","filter","endsWith","checkApplicationCalls","codeGranterAuthorizerAuthorizeCalls","codeGranterAcknowledgerAcknowledgeCalls","codeGranterAuthenticatorAuthenticateCalls","tokenGranterAuthorizerAuthorizeCalls","tokenGranterAcknowledgerAcknowledgeCalls","toMatchSnapshot"],"mappings":"AAAA,OAAOA,yBAAP,MAAsC,yBAAtC;AACA,OAAOC,MAAP,MAAmB,QAAnB;AACA,SAASC,0BAAT,QAA2C,cAA3C;AACA,SAASC,yBAAT,QAA0C,4BAA1C;AAIAC,QAAQ,CAAC,uBAAD,EAA0B,MAAM;AACtC,QAAMC,kBAAkB,GAAG,EACzB,GAAGH,0BADsB;AAEzB,OAAGC;AAFsB,GAA3B;AAIA,QAAMG,gBAAgB,GAAGC,IAAI,CAACC,EAAL,EAAzB;AACA,QAAMC,GAAG,GAAGF,IAAI,CAACC,EAAL,EAAZ;AACA,QAAME,WAAW,GAAG;AAClBC,IAAAA,IAAI,EAAE,MADY;AAElBC,IAAAA,UAAU,EAAE;AAAEC,MAAAA,YAAY,EAAE,MAAhB;AAAwBC,MAAAA,SAAS,EAAEP,IAAI,CAACC,EAAL;AAAnC,KAFM;AAGlBO,IAAAA,YAAY,EAAE;AAAEC,MAAAA,kBAAkB,EAAE,MAAtB;AAA8BC,MAAAA,WAAW,EAAEV,IAAI,CAACC,EAAL;AAA3C,KAHI;AAIlBU,IAAAA,aAAa,EAAE;AACbC,MAAAA,SAAS,EAAE,oBADE;AAEbC,MAAAA,YAAY,EAAEb,IAAI,CAACC,EAAL;AAFD;AAJG,GAApB;AASA,QAAMa,YAAY,GAAG;AACnBV,IAAAA,IAAI,EAAE,OADa;AAEnBC,IAAAA,UAAU,EAAE;AAAEC,MAAAA,YAAY,EAAE,OAAhB;AAAyBC,MAAAA,SAAS,EAAEP,IAAI,CAACC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AACA;AACA;AACA;AACA,KAXI;AAYAD,IAAAA,MAAM,CAAC;AACLE,MAAAA,QAAQ,EAAElC,GAAG,CAACiB,IAAJ,CAASkB,KAAT,CAAeC,MAAf,CAAsB,CAAC,CAAClC,IAAD,CAAD,KAAY,CAACA,IAAI,CAACmC,QAAL,CAAc,OAAd,CAAnC,CADL;AAELC,MAAAA,qBAAqB,EAAEzC,gBAAgB,CAACoB,IAAjB,CAAsBkB,KAFxC;AAGLI,MAAAA,mCAAmC,EACjCtC,WAAW,CAACE,UAAZ,CAAuBE,SAAvB,CAAiCY,IAAjC,CAAsCkB,KAJnC;AAKLK,MAAAA,uCAAuC,EACrCvC,WAAW,CAACK,YAAZ,CAAyBE,WAAzB,CAAqCS,IAArC,CAA0CkB,KANvC;AAOLM,MAAAA,yCAAyC,EACvCxC,WAAW,CAACQ,aAAZ,CAA0BE,YAA1B,CAAuCM,IAAvC,CAA4CkB,KARzC;AASLO,MAAAA,oCAAoC,EAClC9B,YAAY,CAACT,UAAb,CAAwBE,SAAxB,CAAkCY,IAAlC,CAAuCkB,KAVpC;AAWLQ,MAAAA,wCAAwC,EACtC/B,YAAY,CAACN,YAAb,CAA0BE,WAA1B,CAAsCS,IAAtC,CAA2CkB;AAZxC,KAAD,CAAN,CAaGS,eAbH;AAcD,GA9DG,CAAJ;AA+DD,CAvKO,CAAR","sourcesContent":["import initPostOAuth2Acknowledge from './postOAuth2Acknowledge';\nimport YError from 'yerror';\nimport { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';\nimport { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters';\nimport type { OAuth2GranterService } from '../services/oAuth2Granters';\nimport type { BaseAuthenticationData } from '@whook/authorization';\n\ndescribe('postOAuth2Acknowledge', () => {\n const ERRORS_DESCRIPTORS = {\n ...DEFAULT_ERRORS_DESCRIPTORS,\n ...OAUTH2_ERRORS_DESCRIPTORS,\n };\n const checkApplication = jest.fn();\n const log = jest.fn();\n const codeGranter = {\n type: 'code',\n authorizer: { responseType: 'code', authorize: jest.fn() },\n acknowledger: { acknowledgmentType: 'code', acknowledge: jest.fn() },\n authenticator: {\n grantType: 'authorization_code',\n authenticate: jest.fn(),\n },\n };\n const tokenGranter = {\n type: 'token',\n authorizer: { responseType: 'token', authorize: jest.fn() },\n acknowledger: { acknowledgmentType: 'token', acknowledge: jest.fn() },\n };\n const oAuth2Granters: OAuth2GranterService[] = [codeGranter, tokenGranter];\n\n beforeEach(() => {\n checkApplication.mockReset();\n log.mockReset();\n [\n codeGranter.authorizer.authorize,\n codeGranter.acknowledger.acknowledge,\n codeGranter.authenticator.authenticate,\n tokenGranter.authorizer.authorize,\n tokenGranter.acknowledger.acknowledge,\n ].forEach((mock) => mock.mockReset());\n });\n\n test('should redirect', async () => {\n [\n codeGranter.authorizer.authorize,\n codeGranter.authenticator.authenticate,\n tokenGranter.authorizer.authorize,\n tokenGranter.acknowledger.acknowledge,\n ].forEach((mock) =>\n mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),\n );\n codeGranter.acknowledger.acknowledge.mockResolvedValueOnce({\n applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',\n redirectURI: 'http://lol',\n scope: 'user',\n userId: 1,\n });\n\n const postOAuth2Acknowledge = await initPostOAuth2Acknowledge<\n BaseAuthenticationData & {\n userId: number;\n }\n >({\n ERRORS_DESCRIPTORS,\n oAuth2Granters,\n checkApplication,\n log,\n });\n const response = await postOAuth2Acknowledge({\n authenticationData: {\n applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',\n scope: 'auth',\n userId: 1,\n },\n body: {\n responseType: 'code',\n clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',\n redirectURI: 'https://www.example.com',\n scope: 'user',\n state: 'bancal',\n acknowledged: true,\n },\n });\n expect({\n response,\n }).toMatchInlineSnapshot(`\n Object {\n \"response\": Object {\n \"headers\": Object {\n \"location\": \"https://www.example.com/?client_id=abbacaca-abba-caca-abba-cacaabbacaca&scope=user&state=bancal&redirect_uri=http%3A%2F%2Flol&user_id=1\",\n },\n \"status\": 302,\n },\n }\n `);\n expect({\n logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),\n checkApplicationCalls: checkApplication.mock.calls,\n codeGranterAuthorizerAuthorizeCalls:\n codeGranter.authorizer.authorize.mock.calls,\n codeGranterAcknowledgerAcknowledgeCalls:\n codeGranter.acknowledger.acknowledge.mock.calls,\n codeGranterAuthenticatorAuthenticateCalls:\n codeGranter.authenticator.authenticate.mock.calls,\n tokenGranterAuthorizerAuthorizeCalls:\n tokenGranter.authorizer.authorize.mock.calls,\n tokenGranterAcknowledgerAcknowledgeCalls:\n tokenGranter.acknowledger.acknowledge.mock.calls,\n }).toMatchSnapshot();\n });\n\n test('should redirect errors too', async () => {\n [\n codeGranter.authorizer.authorize,\n codeGranter.authenticator.authenticate,\n tokenGranter.authorizer.authorize,\n tokenGranter.acknowledger.acknowledge,\n codeGranter.acknowledger.acknowledge,\n ].forEach((mock) =>\n mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),\n );\n\n const postOAuth2Acknowledge = await initPostOAuth2Acknowledge<\n BaseAuthenticationData & {\n userId: number;\n }\n >({\n ERRORS_DESCRIPTORS,\n oAuth2Granters,\n checkApplication,\n log,\n });\n const response = await postOAuth2Acknowledge({\n authenticationData: {\n applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',\n scope: 'auth',\n userId: 1,\n },\n body: {\n responseType: 'yolo',\n clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',\n redirectURI: 'https://www.example.com',\n scope: 'user',\n state: 'bancal',\n acknowledged: true,\n },\n });\n expect({\n response,\n }).toMatchInlineSnapshot(`\n Object {\n \"response\": Object {\n \"headers\": Object {\n \"location\": \"https://www.example.com/?error=unsupported_response_type&error_decription=Type+%22yolo%22+not+supported.\",\n },\n \"status\": 302,\n },\n }\n `);\n expect({\n logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),\n checkApplicationCalls: checkApplication.mock.calls,\n codeGranterAuthorizerAuthorizeCalls:\n codeGranter.authorizer.authorize.mock.calls,\n codeGranterAcknowledgerAcknowledgeCalls:\n codeGranter.acknowledger.acknowledge.mock.calls,\n codeGranterAuthenticatorAuthenticateCalls:\n codeGranter.authenticator.authenticate.mock.calls,\n tokenGranterAuthorizerAuthorizeCalls:\n tokenGranter.authorizer.authorize.mock.calls,\n tokenGranterAcknowledgerAcknowledgeCalls:\n tokenGranter.acknowledger.acknowledge.mock.calls,\n }).toMatchSnapshot();\n });\n});\n"],"file":"postOAuth2Acknowledge.test.mjs"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { LogService, TimeService } from 'common-services';
|
|
2
|
+
import type { WhookAPISchemaDefinition, WhookAPIHandlerDefinition } from '@whook/whook';
|
|
3
|
+
import type { OAuth2GranterService, OAuth2AccessTokenService, OAuth2RefreshTokenService } from '../services/oAuth2Granters';
|
|
4
|
+
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
5
|
+
export declare const authorizationCodeTokenRequestBodySchema: WhookAPISchemaDefinition;
|
|
6
|
+
export declare const passwordTokenRequestBodySchema: WhookAPISchemaDefinition;
|
|
7
|
+
export declare const clientCredentialsTokenRequestBodySchema: WhookAPISchemaDefinition;
|
|
8
|
+
export declare const refreshTokenRequestBodySchema: WhookAPISchemaDefinition;
|
|
9
|
+
export declare const tokenBodySchema: WhookAPISchemaDefinition;
|
|
10
|
+
export declare const definition: WhookAPIHandlerDefinition;
|
|
11
|
+
declare const _default: <AUTHENTICATION_DATA extends BaseAuthenticationData<string, string> = BaseAuthenticationData<string, string>>(dependencies?: {
|
|
12
|
+
oAuth2Granters: OAuth2GranterService<Record<string, unknown>, Record<string, unknown>, Record<string, unknown>, BaseAuthenticationData<string, string>>[];
|
|
13
|
+
oAuth2AccessToken: OAuth2AccessTokenService<BaseAuthenticationData<string, string>, string>;
|
|
14
|
+
oAuth2RefreshToken: OAuth2RefreshTokenService<BaseAuthenticationData<string, string>, string>;
|
|
15
|
+
log: LogService;
|
|
16
|
+
time: TimeService;
|
|
17
|
+
}) => Promise<(parameters: {
|
|
18
|
+
body: {
|
|
19
|
+
[name: string]: unknown;
|
|
20
|
+
grant_type: string;
|
|
21
|
+
};
|
|
22
|
+
authenticationData: AUTHENTICATION_DATA;
|
|
23
|
+
}) => Promise<{
|
|
24
|
+
status: number;
|
|
25
|
+
headers: {};
|
|
26
|
+
body: {
|
|
27
|
+
access_token: string;
|
|
28
|
+
token_type: string;
|
|
29
|
+
expires_in: number;
|
|
30
|
+
expiration_date: string;
|
|
31
|
+
refresh_token: string;
|
|
32
|
+
refresh_token_expires_in: number;
|
|
33
|
+
refresh_token_expiration_date: string;
|
|
34
|
+
};
|
|
35
|
+
}>>;
|
|
36
|
+
export default _default;
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exports.definition = exports.tokenBodySchema = exports.refreshTokenRequestBodySchema = exports.clientCredentialsTokenRequestBodySchema = exports.passwordTokenRequestBodySchema = exports.authorizationCodeTokenRequestBodySchema = void 0;
|
|
7
|
+
|
|
8
|
+
var _knifecycle = require("knifecycle");
|
|
9
|
+
|
|
10
|
+
var _getOAuth2Authorize = require("./getOAuth2Authorize");
|
|
11
|
+
|
|
12
|
+
var _whook = require("@whook/whook");
|
|
13
|
+
|
|
14
|
+
var _yerror = _interopRequireDefault(require("yerror"));
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
|
|
18
|
+
/* Architecture Note #2: OAuth2 acknowledge
|
|
19
|
+
This endpoint is to be used by the authentication server page
|
|
20
|
+
to acknowlege that the user accepted the client request.
|
|
21
|
+
*/
|
|
22
|
+
const authorizationCodeTokenRequestBodySchema = {
|
|
23
|
+
name: 'AuthorizationCodeRequestBody',
|
|
24
|
+
schema: {
|
|
25
|
+
type: 'object',
|
|
26
|
+
description: 'Authorization code grant, see https://tools.ietf.org/html/rfc6749#section-4.1',
|
|
27
|
+
required: ['grant_type'],
|
|
28
|
+
properties: {
|
|
29
|
+
grant_type: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
enum: ['authorization_code']
|
|
32
|
+
},
|
|
33
|
+
code: {
|
|
34
|
+
type: 'string'
|
|
35
|
+
},
|
|
36
|
+
client_id: {
|
|
37
|
+
type: 'string'
|
|
38
|
+
},
|
|
39
|
+
redirect_uri: {
|
|
40
|
+
type: 'string',
|
|
41
|
+
pattern: '^https?://',
|
|
42
|
+
format: 'uri'
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
exports.authorizationCodeTokenRequestBodySchema = authorizationCodeTokenRequestBodySchema;
|
|
48
|
+
const passwordTokenRequestBodySchema = {
|
|
49
|
+
name: 'PasswordRequestBody',
|
|
50
|
+
schema: {
|
|
51
|
+
type: 'object',
|
|
52
|
+
description: 'Resource owner password credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.3',
|
|
53
|
+
required: ['grant_type', 'username', 'password'],
|
|
54
|
+
properties: {
|
|
55
|
+
grant_type: {
|
|
56
|
+
type: 'string',
|
|
57
|
+
enum: ['password']
|
|
58
|
+
},
|
|
59
|
+
username: {
|
|
60
|
+
type: 'string'
|
|
61
|
+
},
|
|
62
|
+
password: {
|
|
63
|
+
type: 'string'
|
|
64
|
+
},
|
|
65
|
+
scope: {
|
|
66
|
+
type: 'string',
|
|
67
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3'
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
exports.passwordTokenRequestBodySchema = passwordTokenRequestBodySchema;
|
|
73
|
+
const clientCredentialsTokenRequestBodySchema = {
|
|
74
|
+
name: 'ClientCredentialsRequestBody',
|
|
75
|
+
schema: {
|
|
76
|
+
type: 'object',
|
|
77
|
+
description: 'Client credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.4',
|
|
78
|
+
required: ['grant_type'],
|
|
79
|
+
properties: {
|
|
80
|
+
grant_type: {
|
|
81
|
+
type: 'string',
|
|
82
|
+
enum: ['client_credentials']
|
|
83
|
+
},
|
|
84
|
+
scope: {
|
|
85
|
+
type: 'string',
|
|
86
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3'
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
exports.clientCredentialsTokenRequestBodySchema = clientCredentialsTokenRequestBodySchema;
|
|
92
|
+
const refreshTokenRequestBodySchema = {
|
|
93
|
+
name: 'RefreshTokenRequestBody',
|
|
94
|
+
schema: {
|
|
95
|
+
type: 'object',
|
|
96
|
+
description: 'Token refresh grant type, see https://tools.ietf.org/html/rfc6749#section-6 .',
|
|
97
|
+
required: ['grant_type', 'refresh_token'],
|
|
98
|
+
properties: {
|
|
99
|
+
grant_type: {
|
|
100
|
+
type: 'string',
|
|
101
|
+
enum: ['refresh_token']
|
|
102
|
+
},
|
|
103
|
+
refresh_token: {
|
|
104
|
+
type: 'string'
|
|
105
|
+
},
|
|
106
|
+
scope: {
|
|
107
|
+
type: 'string',
|
|
108
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3'
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
exports.refreshTokenRequestBodySchema = refreshTokenRequestBodySchema;
|
|
114
|
+
const tokenBodySchema = {
|
|
115
|
+
name: 'TokenRequestBody',
|
|
116
|
+
schema: {
|
|
117
|
+
oneOf: [{
|
|
118
|
+
$ref: `#/components/schemas/${passwordTokenRequestBodySchema.name}`
|
|
119
|
+
}, {
|
|
120
|
+
$ref: `#/components/schemas/${authorizationCodeTokenRequestBodySchema.name}`
|
|
121
|
+
}, {
|
|
122
|
+
$ref: `#/components/schemas/${clientCredentialsTokenRequestBodySchema.name}`
|
|
123
|
+
}, {
|
|
124
|
+
$ref: `#/components/schemas/${refreshTokenRequestBodySchema.name}`
|
|
125
|
+
}]
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
exports.tokenBodySchema = tokenBodySchema;
|
|
129
|
+
const definition = {
|
|
130
|
+
method: 'post',
|
|
131
|
+
path: '/oauth2/token',
|
|
132
|
+
operation: {
|
|
133
|
+
operationId: 'postOAuth2Token',
|
|
134
|
+
summary: `Implements the [Token Endpoint](https://tools.ietf.org/html/rfc6749#section-3.2)
|
|
135
|
+
as defined per the OAuth2 RFC.`,
|
|
136
|
+
tags: ['oauth2'],
|
|
137
|
+
parameters: [],
|
|
138
|
+
requestBody: {
|
|
139
|
+
required: true,
|
|
140
|
+
content: {
|
|
141
|
+
'application/x-www-form-urlencoded': {
|
|
142
|
+
schema: {
|
|
143
|
+
$ref: `#/components/schemas/${tokenBodySchema.name}`
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
'application/json': {
|
|
147
|
+
schema: {
|
|
148
|
+
$ref: `#/components/schemas/${tokenBodySchema.name}`
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
responses: {
|
|
154
|
+
'200': {
|
|
155
|
+
description: 'Token successfully issued, see https://tools.ietf.org/html/rfc6749#section-5 .',
|
|
156
|
+
content: {
|
|
157
|
+
'application/json': {
|
|
158
|
+
schema: {
|
|
159
|
+
type: 'object',
|
|
160
|
+
required: ['access_token', 'token_type'],
|
|
161
|
+
properties: {
|
|
162
|
+
access_token: {
|
|
163
|
+
type: 'string'
|
|
164
|
+
},
|
|
165
|
+
token_type: {
|
|
166
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-7.1',
|
|
167
|
+
type: 'string',
|
|
168
|
+
enum: ['bearer', 'mac']
|
|
169
|
+
},
|
|
170
|
+
expires_in: {
|
|
171
|
+
description: 'The lifetime in seconds of the access token',
|
|
172
|
+
type: 'number'
|
|
173
|
+
},
|
|
174
|
+
refresh_token: {
|
|
175
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-6',
|
|
176
|
+
type: 'string'
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
'400': {
|
|
184
|
+
description: 'Access token error response, see https://tools.ietf.org/html/rfc6749#section-5.2',
|
|
185
|
+
content: {
|
|
186
|
+
'application/json': {
|
|
187
|
+
schema: {
|
|
188
|
+
type: 'object',
|
|
189
|
+
required: ['error'],
|
|
190
|
+
properties: {
|
|
191
|
+
error: {
|
|
192
|
+
type: 'string',
|
|
193
|
+
enum: ['invalid_request', 'invalid_client', 'invalid_grant', 'unauthorized_client', 'unsupported_grant_type', 'invalid_scope']
|
|
194
|
+
},
|
|
195
|
+
error_description: {
|
|
196
|
+
type: 'string'
|
|
197
|
+
},
|
|
198
|
+
error_uri: {
|
|
199
|
+
type: 'string'
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
exports.definition = definition;
|
|
210
|
+
|
|
211
|
+
var _default = (0, _knifecycle.handler)(postOAuth2Token, "postOAuth2Token", ["oAuth2Granters", "oAuth2AccessToken", "oAuth2RefreshToken", "?time", "?log"]);
|
|
212
|
+
|
|
213
|
+
exports.default = _default;
|
|
214
|
+
|
|
215
|
+
async function postOAuth2Token({
|
|
216
|
+
oAuth2Granters,
|
|
217
|
+
oAuth2AccessToken,
|
|
218
|
+
oAuth2RefreshToken,
|
|
219
|
+
time = Date.now.bind(Date),
|
|
220
|
+
log = _whook.noop
|
|
221
|
+
}, {
|
|
222
|
+
body: {
|
|
223
|
+
grant_type: grantType,
|
|
224
|
+
...grantParameters
|
|
225
|
+
},
|
|
226
|
+
authenticationData
|
|
227
|
+
}) {
|
|
228
|
+
try {
|
|
229
|
+
const granter = oAuth2Granters.find(granter => granter.authenticator && granter.authenticator.grantType === grantType);
|
|
230
|
+
|
|
231
|
+
if (!granter) {
|
|
232
|
+
throw new _yerror.default('E_UNKNOWN_AUTHENTICATOR_TYPE', grantType);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const newAuthenticationData = await granter.authenticator.authenticate((0, _getOAuth2Authorize.camelCaseObjectProperties)(grantParameters), authenticationData);
|
|
236
|
+
const [{
|
|
237
|
+
token: accessToken,
|
|
238
|
+
expiresAt: accessTokenExpiresAt
|
|
239
|
+
}, {
|
|
240
|
+
token: refreshToken,
|
|
241
|
+
expiresAt: refreshTokenExpiresAt
|
|
242
|
+
}] = await Promise.all([oAuth2AccessToken.create(authenticationData, newAuthenticationData), oAuth2RefreshToken.create(authenticationData, newAuthenticationData)]);
|
|
243
|
+
const currentTime = time();
|
|
244
|
+
return {
|
|
245
|
+
status: 200,
|
|
246
|
+
headers: {},
|
|
247
|
+
body: {
|
|
248
|
+
access_token: accessToken,
|
|
249
|
+
token_type: 'bearer',
|
|
250
|
+
expires_in: Math.ceil((accessTokenExpiresAt - currentTime) / 1000),
|
|
251
|
+
expiration_date: new Date(accessTokenExpiresAt).toISOString(),
|
|
252
|
+
refresh_token: refreshToken,
|
|
253
|
+
refresh_token_expires_in: Math.ceil((refreshTokenExpiresAt - currentTime) / 1000),
|
|
254
|
+
refresh_token_expiration_date: new Date(refreshTokenExpiresAt).toISOString()
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
} catch (err) {
|
|
258
|
+
log('debug', '👫 - OAuth2 token issuing error', err.code);
|
|
259
|
+
log('debug-stack', err.stack);
|
|
260
|
+
throw _yerror.default.cast(err, 'E_OAUTH2');
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
//# sourceMappingURL=postOAuth2Token.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/handlers/postOAuth2Token.ts"],"names":["authorizationCodeTokenRequestBodySchema","name","schema","type","description","required","properties","grant_type","enum","code","client_id","redirect_uri","pattern","format","passwordTokenRequestBodySchema","username","password","scope","clientCredentialsTokenRequestBodySchema","refreshTokenRequestBodySchema","refresh_token","tokenBodySchema","oneOf","$ref","definition","method","path","operation","operationId","summary","tags","parameters","requestBody","content","responses","access_token","token_type","expires_in","error","error_description","error_uri","postOAuth2Token","oAuth2Granters","oAuth2AccessToken","oAuth2RefreshToken","time","Date","now","bind","log","noop","body","grantType","grantParameters","authenticationData","granter","find","authenticator","YError","newAuthenticationData","authenticate","token","accessToken","expiresAt","accessTokenExpiresAt","refreshToken","refreshTokenExpiresAt","Promise","all","create","currentTime","status","headers","Math","ceil","expiration_date","toISOString","refresh_token_expires_in","refresh_token_expiration_date","err","stack","cast"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;;;AAcA;AACA;AACA;AACA;AACO,MAAMA,uCAAiE,GAC5E;AACEC,EAAAA,IAAI,EAAE,8BADR;AAEEC,EAAAA,MAAM,EAAE;AACNC,IAAAA,IAAI,EAAE,QADA;AAENC,IAAAA,WAAW,EACT,+EAHI;AAINC,IAAAA,QAAQ,EAAE,CAAC,YAAD,CAJJ;AAKNC,IAAAA,UAAU,EAAE;AACVC,MAAAA,UAAU,EAAE;AACVJ,QAAAA,IAAI,EAAE,QADI;AAEVK,QAAAA,IAAI,EAAE,CAAC,oBAAD;AAFI,OADF;AAKVC,MAAAA,IAAI,EAAE;AACJN,QAAAA,IAAI,EAAE;AADF,OALI;AAQVO,MAAAA,SAAS,EAAE;AACTP,QAAAA,IAAI,EAAE;AADG,OARD;AAWVQ,MAAAA,YAAY,EAAE;AACZR,QAAAA,IAAI,EAAE,QADM;AAEZS,QAAAA,OAAO,EAAE,YAFG;AAGZC,QAAAA,MAAM,EAAE;AAHI;AAXJ;AALN;AAFV,CADK;;AA4BA,MAAMC,8BAAwD,GAAG;AACtEb,EAAAA,IAAI,EAAE,qB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9C,CAFoB,CAAZ,CAHV;AAOA,UAAMW,WAAW,GAAGzB,IAAI,EAAxB;AAEA,WAAO;AACL0B,MAAAA,MAAM,EAAE,GADH;AAELC,MAAAA,OAAO,EAAE,EAFJ;AAGLrB,MAAAA,IAAI,EAAE;AACJhB,QAAAA,YAAY,EAAE2B,WADV;AAEJ1B,QAAAA,UAAU,EAAE,QAFR;AAGJC,QAAAA,UAAU,EAAEoC,IAAI,CAACC,IAAL,CAAU,CAACV,oBAAoB,GAAGM,WAAxB,IAAuC,IAAjD,CAHR;AAIJK,QAAAA,eAAe,EAAE,IAAI7B,IAAJ,CAASkB,oBAAT,EAA+BY,WAA/B,EAJb;AAKJxD,QAAAA,aAAa,EAAE6C,YALX;AAMJY,QAAAA,wBAAwB,EAAEJ,IAAI,CAACC,IAAL,CACxB,CAACR,qBAAqB,GAAGI,WAAzB,IAAwC,IADhB,CANtB;AASJQ,QAAAA,6BAA6B,EAAE,IAAIhC,IAAJ,CAC7BoB,qBAD6B,EAE7BU,WAF6B;AAT3B;AAHD,KAAP;AAiBD,GAzCD,CAyCE,OAAOG,GAAP,EAAY;AACZ9B,IAAAA,GAAG,CAAC,OAAD,EAAU,iCAAV,EAA6C8B,GAAG,CAACtE,IAAjD,CAAH;AACAwC,IAAAA,GAAG,CAAC,aAAD,EAAgB8B,GAAG,CAACC,KAApB,CAAH;AAEA,UAAMtB,gBAAOuB,IAAP,CAAYF,GAAZ,EAAiB,UAAjB,CAAN;AACD;AACF","sourcesContent":["import { autoHandler } from 'knifecycle';\nimport { camelCaseObjectProperties } from './getOAuth2Authorize';\nimport { noop } from '@whook/whook';\nimport YError from 'yerror';\nimport type { OpenAPIV3 } from 'openapi-types';\nimport type { LogService, TimeService } from 'common-services';\nimport type {\n WhookAPISchemaDefinition,\n WhookAPIHandlerDefinition,\n} from '@whook/whook';\nimport type {\n OAuth2GranterService,\n OAuth2AccessTokenService,\n OAuth2RefreshTokenService,\n} from '../services/oAuth2Granters';\nimport type { BaseAuthenticationData } from '@whook/authorization';\n\n/* Architecture Note #2: OAuth2 acknowledge\nThis endpoint is to be used by the authentication server page\n to acknowlege that the user accepted the client request.\n*/\nexport const authorizationCodeTokenRequestBodySchema: WhookAPISchemaDefinition =\n {\n name: 'AuthorizationCodeRequestBody',\n schema: {\n type: 'object',\n description:\n 'Authorization code grant, see https://tools.ietf.org/html/rfc6749#section-4.1',\n required: ['grant_type'],\n properties: {\n grant_type: {\n type: 'string',\n enum: ['authorization_code'],\n },\n code: {\n type: 'string',\n },\n client_id: {\n type: 'string',\n },\n redirect_uri: {\n type: 'string',\n pattern: '^https?://',\n format: 'uri',\n },\n },\n },\n };\n\nexport const passwordTokenRequestBodySchema: WhookAPISchemaDefinition = {\n name: 'PasswordRequestBody',\n schema: {\n type: 'object',\n description:\n 'Resource owner password credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.3',\n required: ['grant_type', 'username', 'password'],\n properties: {\n grant_type: {\n type: 'string',\n enum: ['password'],\n },\n username: {\n type: 'string',\n },\n password: {\n type: 'string',\n },\n scope: {\n type: 'string',\n description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',\n },\n },\n },\n};\n\nexport const clientCredentialsTokenRequestBodySchema: WhookAPISchemaDefinition =\n {\n name: 'ClientCredentialsRequestBody',\n schema: {\n type: 'object',\n description:\n 'Client credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.4',\n required: ['grant_type'],\n properties: {\n grant_type: {\n type: 'string',\n enum: ['client_credentials'],\n },\n scope: {\n type: 'string',\n description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',\n },\n },\n },\n };\n\nexport const refreshTokenRequestBodySchema: WhookAPISchemaDefinition = {\n name: 'RefreshTokenRequestBody',\n schema: {\n type: 'object',\n description:\n 'Token refresh grant type, see https://tools.ietf.org/html/rfc6749#section-6 .',\n required: ['grant_type', 'refresh_token'],\n properties: {\n grant_type: {\n type: 'string',\n enum: ['refresh_token'],\n },\n refresh_token: {\n type: 'string',\n },\n scope: {\n type: 'string',\n description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',\n },\n },\n },\n};\n\nexport const tokenBodySchema: WhookAPISchemaDefinition = {\n name: 'TokenRequestBody',\n schema: {\n oneOf: [\n {\n $ref: `#/components/schemas/${passwordTokenRequestBodySchema.name}`,\n },\n {\n $ref: `#/components/schemas/${authorizationCodeTokenRequestBodySchema.name}`,\n },\n {\n $ref: `#/components/schemas/${clientCredentialsTokenRequestBodySchema.name}`,\n },\n {\n $ref: `#/components/schemas/${refreshTokenRequestBodySchema.name}`,\n },\n ],\n } as OpenAPIV3.SchemaObject,\n};\n\nexport const definition: WhookAPIHandlerDefinition = {\n method: 'post',\n path: '/oauth2/token',\n operation: {\n operationId: 'postOAuth2Token',\n summary: `Implements the [Token Endpoint](https://tools.ietf.org/html/rfc6749#section-3.2)\n as defined per the OAuth2 RFC.`,\n tags: ['oauth2'],\n parameters: [],\n requestBody: {\n required: true,\n content: {\n 'application/x-www-form-urlencoded': {\n schema: {\n $ref: `#/components/schemas/${tokenBodySchema.name}`,\n },\n },\n 'application/json': {\n schema: {\n $ref: `#/components/schemas/${tokenBodySchema.name}`,\n },\n },\n },\n },\n responses: {\n '200': {\n description:\n 'Token successfully issued, see https://tools.ietf.org/html/rfc6749#section-5 .',\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n required: ['access_token', 'token_type'],\n properties: {\n access_token: { type: 'string' },\n token_type: {\n description:\n 'See https://tools.ietf.org/html/rfc6749#section-7.1',\n type: 'string',\n enum: ['bearer', 'mac'],\n },\n expires_in: {\n description: 'The lifetime in seconds of the access token',\n type: 'number',\n },\n refresh_token: {\n description:\n 'See https://tools.ietf.org/html/rfc6749#section-6',\n type: 'string',\n },\n },\n },\n },\n },\n },\n '400': {\n description:\n 'Access token error response, see https://tools.ietf.org/html/rfc6749#section-5.2',\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n required: ['error'],\n properties: {\n error: {\n type: 'string',\n enum: [\n 'invalid_request',\n 'invalid_client',\n 'invalid_grant',\n 'unauthorized_client',\n 'unsupported_grant_type',\n 'invalid_scope',\n ],\n },\n error_description: { type: 'string' },\n error_uri: { type: 'string' },\n },\n },\n },\n },\n },\n },\n },\n};\n\nexport default autoHandler(postOAuth2Token);\n\nasync function postOAuth2Token<\n AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,\n>(\n {\n oAuth2Granters,\n oAuth2AccessToken,\n oAuth2RefreshToken,\n time = Date.now.bind(Date),\n log = noop,\n }: {\n oAuth2Granters: OAuth2GranterService[];\n oAuth2AccessToken: OAuth2AccessTokenService;\n oAuth2RefreshToken: OAuth2RefreshTokenService;\n log: LogService;\n time: TimeService;\n },\n {\n body: { grant_type: grantType, ...grantParameters },\n authenticationData,\n }: {\n body: {\n grant_type: string;\n [name: string]: unknown;\n };\n authenticationData: AUTHENTICATION_DATA;\n },\n) {\n try {\n const granter = oAuth2Granters.find(\n (granter) =>\n granter.authenticator && granter.authenticator.grantType === grantType,\n );\n\n if (!granter) {\n throw new YError('E_UNKNOWN_AUTHENTICATOR_TYPE', grantType);\n }\n\n const newAuthenticationData = await granter.authenticator.authenticate(\n camelCaseObjectProperties(grantParameters),\n authenticationData,\n );\n\n const [\n { token: accessToken, expiresAt: accessTokenExpiresAt },\n { token: refreshToken, expiresAt: refreshTokenExpiresAt },\n ] = await Promise.all([\n oAuth2AccessToken.create(authenticationData, newAuthenticationData),\n oAuth2RefreshToken.create(authenticationData, newAuthenticationData),\n ]);\n const currentTime = time();\n\n return {\n status: 200,\n headers: {},\n body: {\n access_token: accessToken,\n token_type: 'bearer',\n expires_in: Math.ceil((accessTokenExpiresAt - currentTime) / 1000),\n expiration_date: new Date(accessTokenExpiresAt).toISOString(),\n refresh_token: refreshToken,\n refresh_token_expires_in: Math.ceil(\n (refreshTokenExpiresAt - currentTime) / 1000,\n ),\n refresh_token_expiration_date: new Date(\n refreshTokenExpiresAt,\n ).toISOString(),\n },\n };\n } catch (err) {\n log('debug', '👫 - OAuth2 token issuing error', err.code);\n log('debug-stack', err.stack);\n\n throw YError.cast(err, 'E_OAUTH2');\n }\n}\n"],"file":"postOAuth2Token.js"}
|