@whook/oauth2 8.5.1 → 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -70
- package/dist/handlers/getOAuth2Authorize.d.ts +8 -6
- 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 +3 -3
- package/dist/handlers/postAuthLogin.js +83 -120
- 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 +4 -4
- package/dist/handlers/postAuthRefresh.js +93 -131
- 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 +11 -8
- 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 +4 -4
- 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 +16 -16
- package/dist/index.js +14 -229
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +697 -620
- package/dist/index.test.js.map +1 -1
- package/dist/services/authCookies.d.ts +8 -4
- 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 -64
- package/dist/services/oAuth2CodeGranter.test.js.map +1 -1
- package/dist/services/oAuth2Granters.d.ts +22 -19
- 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 +54 -96
- 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 +17 -16
- package/src/handlers/postAuthLogin.test.ts +9 -5
- package/src/handlers/postAuthLogin.ts +8 -7
- 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 +8 -7
- package/src/handlers/postOAuth2Acknowledge.test.ts +83 -35
- package/src/handlers/postOAuth2Acknowledge.ts +48 -30
- package/src/handlers/postOAuth2Token.test.ts +79 -19
- package/src/handlers/postOAuth2Token.ts +7 -7
- package/src/index.test.ts +116 -72
- package/src/index.ts +21 -19
- package/src/services/authCookies.test.ts +2 -2
- package/src/services/authCookies.ts +10 -5
- package/src/services/oAuth2ClientCredentialsGranter.ts +28 -24
- package/src/services/oAuth2CodeGranter.test.ts +42 -18
- package/src/services/oAuth2CodeGranter.ts +68 -62
- package/src/services/oAuth2Granters.ts +59 -29
- package/src/services/oAuth2PasswordGranter.ts +25 -24
- package/src/services/oAuth2RefreshTokenGranter.ts +30 -26
- package/src/services/oAuth2TokenGranter.ts +45 -43
- 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 -108
- 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 -121
- 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 -748
- 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 -81
- 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
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
OAuth2AccessTokenService,
|
|
7
7
|
CheckApplicationService,
|
|
8
8
|
OAuth2CodeService,
|
|
9
|
-
} from './oAuth2Granters';
|
|
9
|
+
} from './oAuth2Granters.js';
|
|
10
10
|
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
11
11
|
|
|
12
12
|
export const IMPLICIT_GRANTER_TYPE = 'token';
|
|
@@ -38,56 +38,58 @@ async function initOAuth2TokenGranter({
|
|
|
38
38
|
time = Date.now.bind(Date),
|
|
39
39
|
log = noop,
|
|
40
40
|
}: OAuth2TokenGranterDependencies): Promise<OAuth2TokenGranterService> {
|
|
41
|
-
const authorizeWithToken:
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
41
|
+
const authorizeWithToken: NonNullable<
|
|
42
|
+
OAuth2TokenGranterService['authorizer']
|
|
43
|
+
>['authorize'] = async ({ clientId, redirectURI, scope }) => {
|
|
44
|
+
const { redirectURI: finalRedirectURI } = await checkApplication({
|
|
45
|
+
applicationId: clientId,
|
|
46
|
+
type: IMPLICIT_GRANTER_TYPE,
|
|
47
|
+
redirectURI,
|
|
48
|
+
scope,
|
|
49
|
+
});
|
|
49
50
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
};
|
|
51
|
+
return {
|
|
52
|
+
applicationId: clientId,
|
|
53
|
+
redirectURI: finalRedirectURI,
|
|
54
|
+
scope,
|
|
55
55
|
};
|
|
56
|
+
};
|
|
56
57
|
|
|
57
58
|
// Access Token Response:
|
|
58
59
|
// https://tools.ietf.org/html/rfc6749#section-4.2.2
|
|
59
|
-
const acknowledgeWithToken:
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
60
|
+
const acknowledgeWithToken: NonNullable<
|
|
61
|
+
OAuth2GranterService<OAuth2TokenGranterParameters>['acknowledger']
|
|
62
|
+
>['acknowledge'] = async (
|
|
63
|
+
authenticationData,
|
|
64
|
+
{ clientId, redirectURI, scope: providedScope },
|
|
65
|
+
additionalParameters,
|
|
66
|
+
) => {
|
|
67
|
+
const { redirectURI: finalRedirectURI } = await checkApplication({
|
|
68
|
+
applicationId: clientId,
|
|
69
|
+
redirectURI,
|
|
70
|
+
scope: providedScope,
|
|
71
|
+
type: IMPLICIT_GRANTER_TYPE,
|
|
72
|
+
});
|
|
71
73
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
74
|
+
const { token: accessToken, expiresAt: accessTokenExpiresAt } =
|
|
75
|
+
await oAuth2AccessToken.create(
|
|
76
|
+
authenticationData,
|
|
77
|
+
{
|
|
78
|
+
applicationId: clientId,
|
|
79
|
+
scope: providedScope,
|
|
80
|
+
},
|
|
81
|
+
additionalParameters,
|
|
82
|
+
);
|
|
81
83
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
};
|
|
84
|
+
return {
|
|
85
|
+
applicationId: clientId,
|
|
86
|
+
redirectURI: finalRedirectURI,
|
|
87
|
+
scope: providedScope,
|
|
88
|
+
accessToken,
|
|
89
|
+
tokenType: 'bearer',
|
|
90
|
+
expiresIn: Math.round((accessTokenExpiresAt - time()) / 1000),
|
|
90
91
|
};
|
|
92
|
+
};
|
|
91
93
|
|
|
92
94
|
log('debug', '👫 - OAuth2TokenGranter Service Initialized!');
|
|
93
95
|
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import { handler as _handler } from 'knifecycle';
|
|
2
|
-
import camelCase from 'camelcase';
|
|
3
|
-
import YError from 'yerror';
|
|
4
|
-
import { refersTo } from '@whook/whook';
|
|
5
|
-
|
|
6
|
-
/* Architecture Note #1: OAuth2 authorize
|
|
7
|
-
This endpoint simply redirect the user to the authentication
|
|
8
|
-
server page by first checking the application details are
|
|
9
|
-
fine.
|
|
10
|
-
*/
|
|
11
|
-
export const responseTypeParameter = {
|
|
12
|
-
name: 'responseType',
|
|
13
|
-
parameter: {
|
|
14
|
-
in: 'query',
|
|
15
|
-
name: 'response_type',
|
|
16
|
-
required: true,
|
|
17
|
-
schema: {
|
|
18
|
-
type: 'string',
|
|
19
|
-
enum: ['code', 'token']
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
export const clientIdParameter = {
|
|
24
|
-
name: 'clientId',
|
|
25
|
-
parameter: {
|
|
26
|
-
in: 'query',
|
|
27
|
-
name: 'client_id',
|
|
28
|
-
required: true,
|
|
29
|
-
schema: {
|
|
30
|
-
type: 'string'
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
export const redirectURIParameter = {
|
|
35
|
-
name: 'redirectURI',
|
|
36
|
-
parameter: {
|
|
37
|
-
in: 'query',
|
|
38
|
-
name: 'redirect_uri',
|
|
39
|
-
required: false,
|
|
40
|
-
schema: {
|
|
41
|
-
type: 'string',
|
|
42
|
-
pattern: '^https?://',
|
|
43
|
-
format: 'uri'
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
export const scopeParameter = {
|
|
48
|
-
name: 'scope',
|
|
49
|
-
parameter: {
|
|
50
|
-
in: 'query',
|
|
51
|
-
name: 'scope',
|
|
52
|
-
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',
|
|
53
|
-
required: false,
|
|
54
|
-
schema: {
|
|
55
|
-
type: 'string'
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
export const stateParameter = {
|
|
60
|
-
name: 'state',
|
|
61
|
-
parameter: {
|
|
62
|
-
in: 'query',
|
|
63
|
-
name: 'state',
|
|
64
|
-
required: true,
|
|
65
|
-
schema: {
|
|
66
|
-
type: 'string'
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
export const definition = {
|
|
71
|
-
method: 'get',
|
|
72
|
-
path: '/oauth2/authorize',
|
|
73
|
-
operation: {
|
|
74
|
-
operationId: 'getOAuth2Authorize',
|
|
75
|
-
summary: `Implements the [Authorization Endpoint](https://tools.ietf.org/html/rfc6749#section-3.1)
|
|
76
|
-
as defined per the OAuth2 RFC.`,
|
|
77
|
-
tags: ['oauth2'],
|
|
78
|
-
parameters: [refersTo(responseTypeParameter), refersTo(clientIdParameter), refersTo(redirectURIParameter), refersTo(scopeParameter), refersTo(stateParameter)],
|
|
79
|
-
responses: {
|
|
80
|
-
'302': {
|
|
81
|
-
description: 'Redirects the user to the authorization interface.'
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
export default _handler(getOAuth2Authorize, "getOAuth2Authorize", ["OAUTH2", "ERRORS_DESCRIPTORS", "oAuth2Granters", "log"]);
|
|
87
|
-
|
|
88
|
-
async function getOAuth2Authorize({
|
|
89
|
-
OAUTH2,
|
|
90
|
-
ERRORS_DESCRIPTORS,
|
|
91
|
-
oAuth2Granters,
|
|
92
|
-
log
|
|
93
|
-
}, {
|
|
94
|
-
response_type: responseType,
|
|
95
|
-
client_id: clientId,
|
|
96
|
-
redirect_uri: demandedRedirectURI = '',
|
|
97
|
-
scope: demandedScope = '',
|
|
98
|
-
state,
|
|
99
|
-
...authorizeParameters
|
|
100
|
-
}) {
|
|
101
|
-
const url = new URL(OAUTH2.authenticateURL);
|
|
102
|
-
|
|
103
|
-
try {
|
|
104
|
-
const granter = oAuth2Granters.find(granter => granter.authorizer && granter.authorizer.responseType === responseType);
|
|
105
|
-
|
|
106
|
-
if (!granter) {
|
|
107
|
-
throw new YError('E_UNKNOWN_AUTHORIZER_TYPE', responseType);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const {
|
|
111
|
-
applicationId,
|
|
112
|
-
redirectURI,
|
|
113
|
-
scope
|
|
114
|
-
} = await granter.authorizer.authorize({
|
|
115
|
-
clientId,
|
|
116
|
-
redirectURI: demandedRedirectURI,
|
|
117
|
-
scope: demandedScope
|
|
118
|
-
}, camelCaseObjectProperties(authorizeParameters));
|
|
119
|
-
url.searchParams.set('type', responseType);
|
|
120
|
-
url.searchParams.set('redirect_uri', redirectURI);
|
|
121
|
-
url.searchParams.set('scope', scope);
|
|
122
|
-
url.searchParams.set('client_id', applicationId);
|
|
123
|
-
} catch (err) {
|
|
124
|
-
log('debug', '👫 - OAuth2 initialization error', err.code);
|
|
125
|
-
log('debug-stack', err.stack);
|
|
126
|
-
url.searchParams.set('redirect_uri', demandedRedirectURI);
|
|
127
|
-
setURLError(url, err, ERRORS_DESCRIPTORS[err.code] || ERRORS_DESCRIPTORS.E_OAUTH2);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (state) {
|
|
131
|
-
url.searchParams.set('state', state);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return {
|
|
135
|
-
status: 302,
|
|
136
|
-
headers: {
|
|
137
|
-
location: url.href
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
} // The OAuth2 standard uses snake case names so we are
|
|
141
|
-
// converting them to the project standards asap
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
export function camelCaseObjectProperties(object) {
|
|
145
|
-
return Object.keys(object).reduce((camelCasedObject, key) => {
|
|
146
|
-
const newKey = key === 'redirect_uri' ? 'redirectURI' : camelCase(key);
|
|
147
|
-
camelCasedObject[newKey] = object[key];
|
|
148
|
-
return camelCasedObject;
|
|
149
|
-
}, {});
|
|
150
|
-
}
|
|
151
|
-
export function setURLError(url, err, oAuth2Error) {
|
|
152
|
-
url.searchParams.set('error', oAuth2Error.code);
|
|
153
|
-
|
|
154
|
-
if (oAuth2Error.description) {
|
|
155
|
-
url.searchParams.set('error_decription', oAuth2Error.description.replace(/\$([0-9]+)/g, (_, paramIndex) => {
|
|
156
|
-
return (err.params || [])[parseInt(paramIndex, 10)];
|
|
157
|
-
}));
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
//# sourceMappingURL=getOAuth2Authorize.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/handlers/getOAuth2Authorize.ts"],"names":["autoHandler","camelCase","YError","refersTo","responseTypeParameter","name","parameter","in","required","schema","type","enum","clientIdParameter","redirectURIParameter","pattern","format","scopeParameter","description","stateParameter","definition","method","path","operation","operationId","summary","tags","parameters","responses","getOAuth2Authorize","OAUTH2","ERRORS_DESCRIPTORS","oAuth2Granters","log","response_type","responseType","client_id","clientId","redirect_uri","demandedRedirectURI","scope","demandedScope","state","authorizeParameters","url","URL","authenticateURL","granter","find","authorizer","applicationId","redirectURI","authorize","camelCaseObjectProperties","searchParams","set","err","code","stack","setURLError","E_OAUTH2","status","headers","location","href","object","Object","keys","reduce","camelCasedObject","key","newKey","oAuth2Error","replace","_","paramIndex","params","parseInt"],"mappings":"AAAA,SAASA,OAAW,IAAXA,QAAT,QAA4B,YAA5B;AACA,OAAOC,SAAP,MAAsB,WAAtB;AACA,OAAOC,MAAP,MAAmB,QAAnB;AACA,SAASC,QAAT,QAAyB,cAAzB;;AAcA;AACA;AACA;AACA;AACA;AAEA,OAAO,MAAMC,qBAAkD,GAAG;AAChEC,EAAAA,IAAI,EAAE,cAD0D;AAEhEC,EAAAA,SAAS,EAAE;AACTC,IAAAA,EAAE,EAAE,OADK;AAETF,IAAAA,IAAI,EAAE,eAFG;AAGTG,IAAAA,QAAQ,EAAE,IAHD;AAITC,IAAAA,MAAM,EAAE;AACNC,MAAAA,IAAI,EAAE,QADA;AAENC,MAAAA,IAAI,EAAE,CAAC,MAAD,EAAS,OAAT;AAFA;AAJC;AAFqD,CAA3D;AAYP,OAAO,MAAMC,iBAA8C,GAAG;AAC5DP,EAAAA,IAAI,EAAE,UADsD;AAE5DC,EAAAA,SAAS,EAAE;AACTC,IAAAA,EAAE,EAAE,OADK;AAETF,IAAAA,IAAI,EAAE,WAFG;AAGTG,IAAAA,QAAQ,EAAE,IAHD;AAITC,IAAAA,MAAM,EAAE;AACNC,MAAAA,IAAI,EAAE;AADA;AAJC;AAFiD,CAAvD;AAWP,OAAO,MAAMG,oBAAiD,GAAG;AAC/DR,EAAAA,IAAI,EAAE,aADyD;AAE/DC,EAAAA,SAAS,EAAE;AACTC,IAAAA,EAAE,EAAE,OADK;AAETF,IAAAA,IAAI,EAAE,cAFG;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qB,OAArB,EAA8BiB,WAAW,CAACf,IAA1C;;AACA,MAAIe,WAAW,CAACtD,WAAhB,EAA6B;AAC3B0B,IAAAA,GAAG,CAACU,YAAJ,CAAiBC,GAAjB,CACE,kBADF,EAEEiB,WAAW,CAACtD,WAAZ,CAAwBuD,OAAxB,CAAgC,aAAhC,EAA+C,CAACC,CAAD,EAAIC,UAAJ,KAAmB;AAChE,aAAO,CAAEnB,GAAD,CAAgBoB,MAAhB,IAA0B,EAA3B,EAA+BC,QAAQ,CAACF,UAAD,EAAa,EAAb,CAAvC,CAAP;AACD,KAFD,CAFF;AAMD;AACF","sourcesContent":["import { autoHandler } from 'knifecycle';\nimport camelCase from 'camelcase';\nimport YError from 'yerror';\nimport { refersTo } from '@whook/whook';\nimport type {\n WhookAPIHandlerDefinition,\n WhookAPIParameterDefinition,\n WhookResponse,\n WhookErrorsDescriptors,\n WhookErrorDescriptor,\n} from '@whook/whook';\nimport type {\n OAuth2Options,\n OAuth2GranterService,\n} from '../services/oAuth2Granters';\nimport type { LogService } from 'common-services';\n\n/* Architecture Note #1: OAuth2 authorize\nThis endpoint simply redirect the user to the authentication\n server page by first checking the application details are\n fine.\n*/\n\nexport const responseTypeParameter: WhookAPIParameterDefinition = {\n name: 'responseType',\n parameter: {\n in: 'query',\n name: 'response_type',\n required: true,\n schema: {\n type: 'string',\n enum: ['code', 'token'],\n },\n },\n};\nexport const clientIdParameter: WhookAPIParameterDefinition = {\n name: 'clientId',\n parameter: {\n in: 'query',\n name: 'client_id',\n required: true,\n schema: {\n type: 'string',\n },\n },\n};\nexport const redirectURIParameter: WhookAPIParameterDefinition = {\n name: 'redirectURI',\n parameter: {\n in: 'query',\n name: 'redirect_uri',\n required: false,\n schema: {\n type: 'string',\n pattern: '^https?://',\n format: 'uri',\n },\n },\n};\nexport const scopeParameter: WhookAPIParameterDefinition = {\n name: 'scope',\n parameter: {\n in: 'query',\n name: 'scope',\n description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',\n required: false,\n schema: {\n type: 'string',\n },\n },\n};\nexport const stateParameter: WhookAPIParameterDefinition = {\n name: 'state',\n parameter: {\n in: 'query',\n name: 'state',\n required: true,\n schema: {\n type: 'string',\n },\n },\n};\n\nexport const definition: WhookAPIHandlerDefinition = {\n method: 'get',\n path: '/oauth2/authorize',\n operation: {\n operationId: 'getOAuth2Authorize',\n summary: `Implements the [Authorization Endpoint](https://tools.ietf.org/html/rfc6749#section-3.1)\n as defined per the OAuth2 RFC.`,\n tags: ['oauth2'],\n parameters: [\n refersTo(responseTypeParameter),\n refersTo(clientIdParameter),\n refersTo(redirectURIParameter),\n refersTo(scopeParameter),\n refersTo(stateParameter),\n ],\n responses: {\n '302': {\n description: 'Redirects the user to the authorization interface.',\n },\n },\n },\n};\n\nexport default autoHandler(getOAuth2Authorize);\n\nasync function getOAuth2Authorize(\n {\n OAUTH2,\n ERRORS_DESCRIPTORS,\n oAuth2Granters,\n log,\n }: {\n OAUTH2: OAuth2Options;\n ERRORS_DESCRIPTORS: WhookErrorsDescriptors;\n oAuth2Granters: OAuth2GranterService[];\n log: LogService;\n },\n {\n response_type: responseType,\n client_id: clientId,\n redirect_uri: demandedRedirectURI = '',\n scope: demandedScope = '',\n state,\n ...authorizeParameters\n }: {\n response_type: string;\n client_id: string;\n redirect_uri?: string;\n scope?: string;\n state: string;\n } & Record<string, unknown>,\n): Promise<WhookResponse> {\n const url = new URL(OAUTH2.authenticateURL);\n\n try {\n const granter = oAuth2Granters.find(\n (granter) =>\n granter.authorizer && granter.authorizer.responseType === responseType,\n );\n\n if (!granter) {\n throw new YError('E_UNKNOWN_AUTHORIZER_TYPE', responseType);\n }\n\n const { applicationId, redirectURI, scope } =\n await granter.authorizer.authorize(\n {\n clientId,\n redirectURI: demandedRedirectURI,\n scope: demandedScope,\n },\n camelCaseObjectProperties(authorizeParameters),\n );\n\n url.searchParams.set('type', responseType);\n url.searchParams.set('redirect_uri', redirectURI);\n url.searchParams.set('scope', scope);\n url.searchParams.set('client_id', applicationId);\n } catch (err) {\n log('debug', '👫 - OAuth2 initialization error', err.code);\n log('debug-stack', err.stack);\n\n url.searchParams.set('redirect_uri', demandedRedirectURI);\n setURLError(\n url,\n err,\n ERRORS_DESCRIPTORS[err.code] || ERRORS_DESCRIPTORS.E_OAUTH2,\n );\n }\n\n if (state) {\n url.searchParams.set('state', state);\n }\n\n return {\n status: 302,\n headers: {\n location: url.href,\n },\n };\n}\n\n// The OAuth2 standard uses snake case names so we are\n// converting them to the project standards asap\nexport function camelCaseObjectProperties<T>(\n object: Record<string, T>,\n): Record<string, T> {\n return Object.keys(object).reduce((camelCasedObject, key) => {\n const newKey = key === 'redirect_uri' ? 'redirectURI' : camelCase(key);\n\n camelCasedObject[newKey] = object[key];\n return camelCasedObject;\n }, {});\n}\n\nexport function setURLError(\n url: URL,\n err: YError | Error,\n oAuth2Error: WhookErrorDescriptor,\n): void {\n url.searchParams.set('error', oAuth2Error.code);\n if (oAuth2Error.description) {\n url.searchParams.set(\n 'error_decription',\n oAuth2Error.description.replace(/\\$([0-9]+)/g, (_, paramIndex) => {\n return ((err as YError).params || [])[parseInt(paramIndex, 10)];\n }),\n );\n }\n}\n"],"file":"getOAuth2Authorize.mjs"}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import initGetOAuth2Authorize from './getOAuth2Authorize';
|
|
2
|
-
import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
|
|
3
|
-
import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters';
|
|
4
|
-
import YError from 'yerror';
|
|
5
|
-
describe('getOAuth2Authorize', () => {
|
|
6
|
-
const OAUTH2 = {
|
|
7
|
-
authenticateURL: 'https://auth.example.com/sign_in'
|
|
8
|
-
};
|
|
9
|
-
const ERRORS_DESCRIPTORS = { ...DEFAULT_ERRORS_DESCRIPTORS,
|
|
10
|
-
...OAUTH2_ERRORS_DESCRIPTORS
|
|
11
|
-
};
|
|
12
|
-
const log = jest.fn();
|
|
13
|
-
const codeGranter = {
|
|
14
|
-
type: 'code',
|
|
15
|
-
authorizer: {
|
|
16
|
-
responseType: 'code',
|
|
17
|
-
authorize: jest.fn()
|
|
18
|
-
},
|
|
19
|
-
acknowledger: {
|
|
20
|
-
acknowledgmentType: 'code',
|
|
21
|
-
acknowledge: jest.fn()
|
|
22
|
-
},
|
|
23
|
-
authenticator: {
|
|
24
|
-
grantType: 'authorization_code',
|
|
25
|
-
authenticate: jest.fn()
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
const tokenGranter = {
|
|
29
|
-
type: 'token',
|
|
30
|
-
authorizer: {
|
|
31
|
-
responseType: 'token',
|
|
32
|
-
authorize: jest.fn()
|
|
33
|
-
},
|
|
34
|
-
acknowledger: {
|
|
35
|
-
acknowledgmentType: 'token',
|
|
36
|
-
acknowledge: jest.fn()
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
const oAuth2Granters = [codeGranter, tokenGranter];
|
|
40
|
-
beforeEach(() => {
|
|
41
|
-
log.mockReset();
|
|
42
|
-
[codeGranter.authorizer.authorize, codeGranter.acknowledger.acknowledge, codeGranter.authenticator.authenticate, tokenGranter.authorizer.authorize, tokenGranter.acknowledger.acknowledge].forEach(mock => mock.mockReset());
|
|
43
|
-
});
|
|
44
|
-
test('should redirect', async () => {
|
|
45
|
-
[codeGranter.acknowledger.acknowledge, codeGranter.authenticator.authenticate, tokenGranter.authorizer.authorize, tokenGranter.acknowledger.acknowledge].forEach(mock => mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')));
|
|
46
|
-
codeGranter.authorizer.authorize.mockResolvedValueOnce({
|
|
47
|
-
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
48
|
-
redirectURI: 'http://lol',
|
|
49
|
-
scope: 'user'
|
|
50
|
-
});
|
|
51
|
-
const getOAuth2Authorize = await initGetOAuth2Authorize({
|
|
52
|
-
OAUTH2,
|
|
53
|
-
ERRORS_DESCRIPTORS,
|
|
54
|
-
oAuth2Granters,
|
|
55
|
-
log
|
|
56
|
-
});
|
|
57
|
-
const response = await getOAuth2Authorize({
|
|
58
|
-
response_type: 'code',
|
|
59
|
-
client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
60
|
-
redirect_uri: 'https://www.example.com',
|
|
61
|
-
scope: 'user',
|
|
62
|
-
state: 'bancal'
|
|
63
|
-
});
|
|
64
|
-
expect(response).toMatchInlineSnapshot(`
|
|
65
|
-
Object {
|
|
66
|
-
"headers": Object {
|
|
67
|
-
"location": "https://auth.example.com/sign_in?type=code&redirect_uri=http%3A%2F%2Flol&scope=user&client_id=abbacaca-abba-caca-abba-cacaabbacaca&state=bancal",
|
|
68
|
-
},
|
|
69
|
-
"status": 302,
|
|
70
|
-
}
|
|
71
|
-
`);
|
|
72
|
-
expect({
|
|
73
|
-
logCalls: log.mock.calls.filter(args => args[0].endsWith('stack')),
|
|
74
|
-
codeGranterAuthorizerAuthorizeCalls: codeGranter.authorizer.authorize.mock.calls,
|
|
75
|
-
codeGranterAcknowledgerAcknowledgeCalls: codeGranter.acknowledger.acknowledge.mock.calls,
|
|
76
|
-
codeGranterAuthenticatorAuthenticateCalls: codeGranter.authenticator.authenticate.mock.calls,
|
|
77
|
-
tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorizer.authorize.mock.calls,
|
|
78
|
-
tokenGranterAcknowledgerAcknowledgeCalls: tokenGranter.acknowledger.acknowledge.mock.calls
|
|
79
|
-
}).toMatchSnapshot();
|
|
80
|
-
});
|
|
81
|
-
test('should redirect with an error when some', async () => {
|
|
82
|
-
[codeGranter.authorizer.authorize, codeGranter.acknowledger.acknowledge, codeGranter.authenticator.authenticate, tokenGranter.authorizer.authorize, tokenGranter.acknowledger.acknowledge].forEach(mock => mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')));
|
|
83
|
-
const getOAuth2Authorize = await initGetOAuth2Authorize({
|
|
84
|
-
OAUTH2,
|
|
85
|
-
ERRORS_DESCRIPTORS,
|
|
86
|
-
oAuth2Granters,
|
|
87
|
-
log
|
|
88
|
-
});
|
|
89
|
-
const response = await getOAuth2Authorize({
|
|
90
|
-
response_type: 'yolo',
|
|
91
|
-
client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
92
|
-
redirect_uri: 'https://www.example.com',
|
|
93
|
-
scope: 'user',
|
|
94
|
-
state: 'bancal'
|
|
95
|
-
});
|
|
96
|
-
expect(response).toMatchInlineSnapshot(`
|
|
97
|
-
Object {
|
|
98
|
-
"headers": Object {
|
|
99
|
-
"location": "https://auth.example.com/sign_in?redirect_uri=https%3A%2F%2Fwww.example.com&error=unsupported_response_type&error_decription=The+type+%22yolo%22+is+not+supported.&state=bancal",
|
|
100
|
-
},
|
|
101
|
-
"status": 302,
|
|
102
|
-
}
|
|
103
|
-
`);
|
|
104
|
-
expect({
|
|
105
|
-
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
|
|
106
|
-
codeGranterAuthorizerAuthorizeCalls: codeGranter.authorizer.authorize.mock.calls,
|
|
107
|
-
codeGranterAcknowledgerAcknowledgeCalls: codeGranter.acknowledger.acknowledge.mock.calls,
|
|
108
|
-
codeGranterAuthenticatorAuthenticateCalls: codeGranter.authenticator.authenticate.mock.calls,
|
|
109
|
-
tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorizer.authorize.mock.calls,
|
|
110
|
-
tokenGranterAcknowledgerAcknowledgeCalls: tokenGranter.acknowledger.acknowledge.mock.calls
|
|
111
|
-
}).toMatchSnapshot();
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
//# sourceMappingURL=getOAuth2Authorize.test.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/handlers/getOAuth2Authorize.test.ts"],"names":["initGetOAuth2Authorize","DEFAULT_ERRORS_DESCRIPTORS","OAUTH2_ERRORS_DESCRIPTORS","YError","describe","OAUTH2","authenticateURL","ERRORS_DESCRIPTORS","log","jest","fn","codeGranter","type","authorizer","responseType","authorize","acknowledger","acknowledgmentType","acknowledge","authenticator","grantType","authenticate","tokenGranter","oAuth2Granters","beforeEach","mockReset","forEach","mock","test","mockRejectedValueOnce","mockResolvedValueOnce","applicationId","redirectURI","scope","getOAuth2Authorize","response","response_type","client_id","redirect_uri","state","expect","toMatchInlineSnapshot","logCalls","calls","filter","args","endsWith","codeGranterAuthorizerAuthorizeCalls","codeGranterAcknowledgerAcknowledgeCalls","codeGranterAuthenticatorAuthenticateCalls","tokenGranterAuthorizerAuthorizeCalls","tokenGranterAcknowledgerAcknowledgeCalls","toMatchSnapshot"],"mappings":"AAAA,OAAOA,sBAAP,MAAmC,sBAAnC;AACA,SAASC,0BAAT,QAA2C,cAA3C;AACA,SAASC,yBAAT,QAA0C,4BAA1C;AACA,OAAOC,MAAP,MAAmB,QAAnB;AAGAC,QAAQ,CAAC,oBAAD,EAAuB,MAAM;AACnC,QAAMC,MAAqB,GAAG;AAC5BC,IAAAA,eAAe,EAAE;AADW,GAA9B;AAGA,QAAMC,kBAAkB,GAAG,EACzB,GAAGN,0BADsB;AAEzB,OAAGC;AAFsB,GAA3B;AAIA,QAAMM,GAAG,GAAGC,IAAI,CAACC,EAAL,EAAZ;AACA,QAAMC,WAAW,GAAG;AAClBC,IAAAA,IAAI,EAAE,MADY;AAElBC,IAAAA,UAAU,EAAE;AAAEC,MAAAA,YAAY,EAAE,MAAhB;AAAwBC,MAAAA,SAAS,EAAEN,IAAI,CAACC,EAAL;AAAnC,KAFM;AAGlBM,IAAAA,YAAY,EAAE;AAAEC,MAAAA,kBAAkB,EAAE,MAAtB;AAA8BC,MAAAA,WAAW,EAAET,IAAI,CAACC,EAAL;AAA3C,KAHI;AAIlBS,IAAAA,aAAa,EAAE;AACbC,MAAAA,SAAS,EAAE,oBADE;AAEbC,MAAAA,YAAY,EAAEZ,IAAI,CAACC,EAAL;AAFD;AAJG,GAApB;AASA,QAAMY,YAAY,GAAG;AACnBV,IAAAA,IAAI,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E,YAA1B,CAAuCM,IAAvC,CAA4CgB,KAPzC;AAQLO,MAAAA,oCAAoC,EAClC5B,YAAY,CAACT,UAAb,CAAwBE,SAAxB,CAAkCY,IAAlC,CAAuCgB,KATpC;AAULQ,MAAAA,wCAAwC,EACtC7B,YAAY,CAACN,YAAb,CAA0BE,WAA1B,CAAsCS,IAAtC,CAA2CgB;AAXxC,KAAD,CAAN,CAYGS,eAZH;AAaD,GA9CG,CAAJ;AA+CD,CAvIO,CAAR","sourcesContent":["import initGetOAuth2Authorize from './getOAuth2Authorize';\nimport { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';\nimport { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters';\nimport YError from 'yerror';\nimport type { OAuth2Options, OAuth2GranterService } from '..';\n\ndescribe('getOAuth2Authorize', () => {\n const OAUTH2: OAuth2Options = {\n authenticateURL: 'https://auth.example.com/sign_in',\n };\n const ERRORS_DESCRIPTORS = {\n ...DEFAULT_ERRORS_DESCRIPTORS,\n ...OAUTH2_ERRORS_DESCRIPTORS,\n };\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 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.acknowledger.acknowledge,\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.authorizer.authorize.mockResolvedValueOnce({\n applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',\n redirectURI: 'http://lol',\n scope: 'user',\n });\n\n const getOAuth2Authorize = await initGetOAuth2Authorize({\n OAUTH2,\n ERRORS_DESCRIPTORS,\n oAuth2Granters,\n log,\n });\n const response = await getOAuth2Authorize({\n response_type: 'code',\n client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',\n redirect_uri: 'https://www.example.com',\n scope: 'user',\n state: 'bancal',\n });\n\n expect(response).toMatchInlineSnapshot(`\n Object {\n \"headers\": Object {\n \"location\": \"https://auth.example.com/sign_in?type=code&redirect_uri=http%3A%2F%2Flol&scope=user&client_id=abbacaca-abba-caca-abba-cacaabbacaca&state=bancal\",\n },\n \"status\": 302,\n }\n `);\n expect({\n logCalls: log.mock.calls.filter((args) => args[0].endsWith('stack')),\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 with an error when some', async () => {\n [\n codeGranter.authorizer.authorize,\n codeGranter.acknowledger.acknowledge,\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\n const getOAuth2Authorize = await initGetOAuth2Authorize({\n OAUTH2,\n ERRORS_DESCRIPTORS,\n oAuth2Granters,\n log,\n });\n const response = await getOAuth2Authorize({\n response_type: 'yolo',\n client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',\n redirect_uri: 'https://www.example.com',\n scope: 'user',\n state: 'bancal',\n });\n\n expect(response).toMatchInlineSnapshot(`\n Object {\n \"headers\": Object {\n \"location\": \"https://auth.example.com/sign_in?redirect_uri=https%3A%2F%2Fwww.example.com&error=unsupported_response_type&error_decription=The+type+%22yolo%22+is+not+supported.&state=bancal\",\n },\n \"status\": 302,\n }\n `);\n expect({\n logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),\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":"getOAuth2Authorize.test.mjs"}
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { handler as _handler } from 'knifecycle';
|
|
2
|
-
import YHTTPError from 'yhttperror';
|
|
3
|
-
import { AUTH_API_PREFIX } from '../services/authCookies';
|
|
4
|
-
export const definition = {
|
|
5
|
-
method: 'post',
|
|
6
|
-
path: `${AUTH_API_PREFIX}/login`,
|
|
7
|
-
operation: {
|
|
8
|
-
operationId: 'postAuthLogin',
|
|
9
|
-
summary: 'Logs a user in',
|
|
10
|
-
tags: ['auth'],
|
|
11
|
-
'x-whook': {
|
|
12
|
-
disabled: true
|
|
13
|
-
},
|
|
14
|
-
parameters: [],
|
|
15
|
-
requestBody: {
|
|
16
|
-
required: true,
|
|
17
|
-
content: {
|
|
18
|
-
'application/json': {
|
|
19
|
-
schema: {
|
|
20
|
-
type: 'object',
|
|
21
|
-
required: ['username', 'password'],
|
|
22
|
-
properties: {
|
|
23
|
-
username: {
|
|
24
|
-
type: 'string'
|
|
25
|
-
},
|
|
26
|
-
password: {
|
|
27
|
-
type: 'string'
|
|
28
|
-
},
|
|
29
|
-
scope: {
|
|
30
|
-
type: 'string'
|
|
31
|
-
},
|
|
32
|
-
remember: {
|
|
33
|
-
type: 'boolean'
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
responses: {
|
|
41
|
-
200: {
|
|
42
|
-
description: 'Successfuly logged in.',
|
|
43
|
-
content: {
|
|
44
|
-
'application/json': {
|
|
45
|
-
schema: {
|
|
46
|
-
type: 'object',
|
|
47
|
-
properties: {
|
|
48
|
-
access_token: {
|
|
49
|
-
type: 'string'
|
|
50
|
-
},
|
|
51
|
-
expiration_date: {
|
|
52
|
-
type: 'string'
|
|
53
|
-
},
|
|
54
|
-
expires_in: {
|
|
55
|
-
type: 'string'
|
|
56
|
-
},
|
|
57
|
-
token_type: {
|
|
58
|
-
type: 'string'
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
export default _handler(postAuthLogin, "postAuthLogin", ["ROOT_AUTHENTICATION_DATA", "authCookies", "postOAuth2Token"]);
|
|
69
|
-
|
|
70
|
-
async function postAuthLogin({
|
|
71
|
-
ROOT_AUTHENTICATION_DATA,
|
|
72
|
-
authCookies,
|
|
73
|
-
postOAuth2Token
|
|
74
|
-
}, {
|
|
75
|
-
body
|
|
76
|
-
}) {
|
|
77
|
-
try {
|
|
78
|
-
const response = await postOAuth2Token({
|
|
79
|
-
body: {
|
|
80
|
-
grant_type: 'password',
|
|
81
|
-
scope: body.scope,
|
|
82
|
-
username: body.username,
|
|
83
|
-
password: body.password
|
|
84
|
-
},
|
|
85
|
-
authenticationData: ROOT_AUTHENTICATION_DATA
|
|
86
|
-
});
|
|
87
|
-
return { ...response,
|
|
88
|
-
headers: { ...(response.headers || {}),
|
|
89
|
-
'Set-Cookie': authCookies.build(response.status === 200 ? response.body : undefined, {
|
|
90
|
-
session: !body.remember
|
|
91
|
-
})
|
|
92
|
-
},
|
|
93
|
-
body: {
|
|
94
|
-
access_token: response.body.access_token,
|
|
95
|
-
expiration_date: response.body.expiration_date,
|
|
96
|
-
expires_in: response.body.expires_in,
|
|
97
|
-
token_type: response.body.token_type
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
} catch (err) {
|
|
101
|
-
const newErr = YHTTPError.wrap(err);
|
|
102
|
-
newErr.headers = { ...(err.headers || {}),
|
|
103
|
-
'Set-Cookie': authCookies.build()
|
|
104
|
-
};
|
|
105
|
-
throw newErr;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
//# sourceMappingURL=postAuthLogin.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/handlers/postAuthLogin.ts"],"names":["autoHandler","YHTTPError","AUTH_API_PREFIX","definition","method","path","operation","operationId","summary","tags","disabled","parameters","requestBody","required","content","schema","type","properties","username","password","scope","remember","responses","description","access_token","expiration_date","expires_in","token_type","postAuthLogin","ROOT_AUTHENTICATION_DATA","authCookies","postOAuth2Token","body","response","grant_type","authenticationData","headers","build","status","undefined","session","err","newErr","wrap"],"mappings":"AAAA,SAASA,OAAW,IAAXA,QAAT,QAA4B,YAA5B;AACA,OAAOC,UAAP,MAAuB,YAAvB;AAEA,SAASC,eAAT,QAAgC,yBAAhC;AASA,OAAO,MAAMC,UAAqC,GAAG;AACnDC,EAAAA,MAAM,EAAE,MAD2C;AAEnDC,EAAAA,IAAI,EAAG,GAAEH,eAAgB,QAF0B;AAGnDI,EAAAA,SAAS,EAAE;AACTC,IAAAA,WAAW,EAAE,eADJ;AAETC,IAAAA,OAAO,E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W,CAACO,KAAZ;AAFC,KAAjB;AAKA,UAAMK,MAAN;AACD;AACF","sourcesContent":["import { autoHandler } from 'knifecycle';\nimport YHTTPError from 'yhttperror';\nimport initPostOauth2Token from './postOAuth2Token';\nimport { AUTH_API_PREFIX } from '../services/authCookies';\nimport type { WhookAPIHandlerDefinition } from '@whook/whook';\nimport type {\n AuthCookiesService,\n AuthHandlersConfig,\n} from '../services/authCookies';\nimport type { AsyncReturnType } from 'type-fest';\nimport type { BaseAuthenticationData } from '@whook/authorization';\n\nexport const definition: WhookAPIHandlerDefinition = {\n method: 'post',\n path: `${AUTH_API_PREFIX}/login`,\n operation: {\n operationId: 'postAuthLogin',\n summary: 'Logs a user in',\n tags: ['auth'],\n 'x-whook': {\n disabled: true,\n },\n parameters: [],\n requestBody: {\n required: true,\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n required: ['username', 'password'],\n properties: {\n username: { type: 'string' },\n password: { type: 'string' },\n scope: { type: 'string' },\n remember: { type: 'boolean' },\n },\n },\n },\n },\n },\n responses: {\n 200: {\n description: 'Successfuly logged in.',\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n properties: {\n access_token: { type: 'string' },\n expiration_date: { type: 'string' },\n expires_in: { type: 'string' },\n token_type: { type: 'string' },\n },\n },\n },\n },\n },\n },\n },\n};\n\nexport default autoHandler(postAuthLogin);\n\nasync function postAuthLogin<\n AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,\n>(\n {\n ROOT_AUTHENTICATION_DATA,\n authCookies,\n postOAuth2Token,\n }: AuthHandlersConfig<AUTHENTICATION_DATA> & {\n authCookies: Pick<AuthCookiesService, 'build'>;\n postOAuth2Token: AsyncReturnType<typeof initPostOauth2Token>;\n },\n {\n body,\n }: {\n body: {\n username: string;\n password: string;\n scope: string;\n remember: boolean;\n };\n },\n) {\n try {\n const response = await postOAuth2Token({\n body: {\n grant_type: 'password',\n scope: body.scope,\n username: body.username,\n password: body.password,\n },\n authenticationData: ROOT_AUTHENTICATION_DATA,\n });\n\n return {\n ...response,\n headers: {\n ...(response.headers || {}),\n 'Set-Cookie': authCookies.build(\n response.status === 200 ? response.body : undefined,\n { session: !body.remember },\n ),\n },\n body: {\n access_token: response.body.access_token,\n expiration_date: response.body.expiration_date,\n expires_in: response.body.expires_in,\n token_type: response.body.token_type,\n },\n };\n } catch (err) {\n const newErr = YHTTPError.wrap(err);\n\n newErr.headers = {\n ...(err.headers || {}),\n 'Set-Cookie': authCookies.build(),\n };\n\n throw newErr;\n }\n}\n"],"file":"postAuthLogin.mjs"}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import initPostAuthLogin from './postAuthLogin';
|
|
2
|
-
describe('postAuthLogin', () => {
|
|
3
|
-
test('should work', async () => {
|
|
4
|
-
const authCookies = {
|
|
5
|
-
build: jest.fn()
|
|
6
|
-
};
|
|
7
|
-
const postOAuth2Token = jest.fn();
|
|
8
|
-
const postAuthLogin = await initPostAuthLogin({
|
|
9
|
-
ROOT_AUTHENTICATION_DATA: {
|
|
10
|
-
scope: 'user',
|
|
11
|
-
applicationId: 'root_app_id'
|
|
12
|
-
},
|
|
13
|
-
authCookies,
|
|
14
|
-
postOAuth2Token
|
|
15
|
-
});
|
|
16
|
-
authCookies.build.mockReturnValueOnce('the_build_cookies');
|
|
17
|
-
postOAuth2Token.mockResolvedValueOnce({
|
|
18
|
-
status: 200,
|
|
19
|
-
body: {
|
|
20
|
-
access_token: 'an_access_token',
|
|
21
|
-
expiration_date: '2020-02-02T20:22:02Z',
|
|
22
|
-
expires_in: 3600,
|
|
23
|
-
token_type: 'bearer'
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
const response = await postAuthLogin({
|
|
27
|
-
body: {
|
|
28
|
-
username: 'a_username',
|
|
29
|
-
password: 'a_password',
|
|
30
|
-
remember: false,
|
|
31
|
-
scope: 'user'
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
expect({
|
|
35
|
-
response,
|
|
36
|
-
authCookiesBuildCalls: authCookies.build.mock.calls,
|
|
37
|
-
postOAuth2TokenCalls: postOAuth2Token.mock.calls
|
|
38
|
-
}).toMatchInlineSnapshot(`
|
|
39
|
-
Object {
|
|
40
|
-
"authCookiesBuildCalls": Array [
|
|
41
|
-
Array [
|
|
42
|
-
Object {
|
|
43
|
-
"access_token": "an_access_token",
|
|
44
|
-
"expiration_date": "2020-02-02T20:22:02Z",
|
|
45
|
-
"expires_in": 3600,
|
|
46
|
-
"token_type": "bearer",
|
|
47
|
-
},
|
|
48
|
-
Object {
|
|
49
|
-
"session": true,
|
|
50
|
-
},
|
|
51
|
-
],
|
|
52
|
-
],
|
|
53
|
-
"postOAuth2TokenCalls": Array [
|
|
54
|
-
Array [
|
|
55
|
-
Object {
|
|
56
|
-
"authenticationData": Object {
|
|
57
|
-
"applicationId": "root_app_id",
|
|
58
|
-
"scope": "user",
|
|
59
|
-
},
|
|
60
|
-
"body": Object {
|
|
61
|
-
"grant_type": "password",
|
|
62
|
-
"password": "a_password",
|
|
63
|
-
"scope": "user",
|
|
64
|
-
"username": "a_username",
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
],
|
|
68
|
-
],
|
|
69
|
-
"response": Object {
|
|
70
|
-
"body": Object {
|
|
71
|
-
"access_token": "an_access_token",
|
|
72
|
-
"expiration_date": "2020-02-02T20:22:02Z",
|
|
73
|
-
"expires_in": 3600,
|
|
74
|
-
"token_type": "bearer",
|
|
75
|
-
},
|
|
76
|
-
"headers": Object {
|
|
77
|
-
"Set-Cookie": "the_build_cookies",
|
|
78
|
-
},
|
|
79
|
-
"status": 200,
|
|
80
|
-
},
|
|
81
|
-
}
|
|
82
|
-
`);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
//# sourceMappingURL=postAuthLogin.test.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/handlers/postAuthLogin.test.ts"],"names":["initPostAuthLogin","describe","test","authCookies","build","jest","fn","postOAuth2Token","postAuthLogin","ROOT_AUTHENTICATION_DATA","scope","applicationId","mockReturnValueOnce","mockResolvedValueOnce","status","body","access_token","expiration_date","expires_in","token_type","response","username","password","remember","expect","authCookiesBuildCalls","mock","calls","postOAuth2TokenCalls","toMatchInlineSnapshot"],"mappings":"AAAA,OAAOA,iBAAP,MAA8B,iBAA9B;AAEAC,QAAQ,CAAC,eAAD,EAAkB,MAAM;AAC9BC,EAAAA,IAAI,CAAC,aAAD,EAAgB,YAAY;AAC9B,UAAMC,WAAW,GAAG;AAClBC,MAAAA,KAAK,EAAEC,IAAI,CAACC,EAAL;AADW,KAApB;AAGA,UAAMC,eAAe,GAAGF,IAAI,CAACC,EAAL,EAAxB;AACA,UAAME,aAAa,GAAG,MAAMR,iBAAiB,CAAC;AAC5CS,MAAAA,wBAAwB,EAAE;AACxBC,QAAAA,KAAK,EAAE,MADiB;AAExBC,QAAAA,aAAa,EAAE;AAFS,OADkB;AAK5CR,MAAAA,WAL4C;AAM5CI,MAAAA;AAN4C,KAAD,CAA7C;AASAJ,IAAAA,WAAW,CAACC,KAAZ,CAAkBQ,mBAAlB,CAAsC,mBAAtC;AACAL,IAAAA,eAAe,CAACM,qBAAhB,CAAsC;AACpCC,MAAAA,MAAM,EAAE,GAD4B;AAEpCC,MAAAA,IAAI,EAAE;AACJC,QAAAA,YAAY,EAAE,iBADV;AAEJC,QAAAA,eAAe,EAAE,sBAFb;AAGJC,QAAAA,UAAU,EAAE,IAHR;AAIJC,QAAAA,UAAU,EAAE;AAJR;AAF8B,KAAtC;AAUA,UAAMC,QAAQ,GAAG,MAAMZ,aAAa,CAAC;AACnCO,MAAAA,IAAI,EAAE;AACJM,QAAAA,QAAQ,EAAE,YADN;AAEJC,QAAAA,QAAQ,EAAE,YAFN;AAGJC,QAAAA,QAAQ,EAAE,KAHN;AAIJb,QAAAA,KAAK,EAAE;AAJH;AAD6B,KAAD,CAApC;AASAc,IAAAA,MAAM,CAAC;AACLJ,MAAAA,QADK;AAELK,MAAAA,qBAAqB,EAAEtB,WAAW,CAACC,KAAZ,CAAkBsB,IAAlB,CAAuBC,KAFzC;AAGLC,MAAAA,oBAAoB,EAAErB,eAAe,CAACmB,IAAhB,CAAqBC;AAHtC,KAAD,CAAN,CAIGE,qBAJH,CAI0B;AAC9B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,KAhDI;AAiDD,GAnFG,CAAJ;AAoFD,CArFO,CAAR","sourcesContent":["import initPostAuthLogin from './postAuthLogin';\n\ndescribe('postAuthLogin', () => {\n test('should work', async () => {\n const authCookies = {\n build: jest.fn(),\n };\n const postOAuth2Token = jest.fn();\n const postAuthLogin = await initPostAuthLogin({\n ROOT_AUTHENTICATION_DATA: {\n scope: 'user',\n applicationId: 'root_app_id',\n },\n authCookies,\n postOAuth2Token,\n });\n\n authCookies.build.mockReturnValueOnce('the_build_cookies');\n postOAuth2Token.mockResolvedValueOnce({\n status: 200,\n body: {\n access_token: 'an_access_token',\n expiration_date: '2020-02-02T20:22:02Z',\n expires_in: 3600,\n token_type: 'bearer',\n },\n });\n\n const response = await postAuthLogin({\n body: {\n username: 'a_username',\n password: 'a_password',\n remember: false,\n scope: 'user',\n },\n });\n\n expect({\n response,\n authCookiesBuildCalls: authCookies.build.mock.calls,\n postOAuth2TokenCalls: postOAuth2Token.mock.calls,\n }).toMatchInlineSnapshot(`\n Object {\n \"authCookiesBuildCalls\": Array [\n Array [\n Object {\n \"access_token\": \"an_access_token\",\n \"expiration_date\": \"2020-02-02T20:22:02Z\",\n \"expires_in\": 3600,\n \"token_type\": \"bearer\",\n },\n Object {\n \"session\": true,\n },\n ],\n ],\n \"postOAuth2TokenCalls\": Array [\n Array [\n Object {\n \"authenticationData\": Object {\n \"applicationId\": \"root_app_id\",\n \"scope\": \"user\",\n },\n \"body\": Object {\n \"grant_type\": \"password\",\n \"password\": \"a_password\",\n \"scope\": \"user\",\n \"username\": \"a_username\",\n },\n },\n ],\n ],\n \"response\": Object {\n \"body\": Object {\n \"access_token\": \"an_access_token\",\n \"expiration_date\": \"2020-02-02T20:22:02Z\",\n \"expires_in\": 3600,\n \"token_type\": \"bearer\",\n },\n \"headers\": Object {\n \"Set-Cookie\": \"the_build_cookies\",\n },\n \"status\": 200,\n },\n }\n `);\n });\n});\n"],"file":"postAuthLogin.test.mjs"}
|