@whook/oauth2 25.2.0 → 26.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +147 -98
- package/dist/index.d.ts +30 -11
- package/dist/index.js +15 -8
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +1258 -399
- package/dist/index.test.js.map +1 -1
- package/dist/libs/clients.d.ts +2 -0
- package/dist/libs/clients.js +13 -0
- package/dist/libs/clients.js.map +1 -0
- package/dist/libs/errors.d.ts +113 -0
- package/dist/libs/errors.js +178 -0
- package/dist/libs/errors.js.map +1 -0
- package/dist/libs/grants.d.ts +2 -0
- package/dist/libs/grants.js +10 -0
- package/dist/libs/grants.js.map +1 -0
- package/dist/libs/redirectURI.d.ts +7 -0
- package/dist/libs/redirectURI.js +78 -0
- package/dist/libs/redirectURI.js.map +1 -0
- package/dist/libs/schemas.d.ts +23 -0
- package/dist/libs/schemas.js +25 -0
- package/dist/libs/schemas.js.map +1 -0
- package/dist/libs/scopes.d.ts +16 -0
- package/dist/libs/scopes.js +37 -0
- package/dist/libs/scopes.js.map +1 -1
- package/dist/libs/utils.d.ts +2 -0
- package/dist/libs/utils.js +17 -0
- package/dist/libs/utils.js.map +1 -0
- package/dist/libs/verifier.d.ts +7 -0
- package/dist/libs/verifier.js +37 -0
- package/dist/libs/verifier.js.map +1 -0
- package/dist/libs/verifier.test.js +21 -0
- package/dist/libs/verifier.test.js.map +1 -0
- package/dist/routes/getOAuth2Authorize.d.ts +13 -50
- package/dist/routes/getOAuth2Authorize.js +60 -86
- package/dist/routes/getOAuth2Authorize.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.test.js +147 -68
- package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.d.ts +8 -8
- package/dist/routes/getOAuth2WellKnownMetadata.js +20 -9
- package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.test.js +38 -37
- package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +3 -6
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +3 -6
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
- package/dist/routes/postAuthLogin.d.ts +14 -9
- package/dist/routes/postAuthLogin.js +10 -5
- package/dist/routes/postAuthLogin.js.map +1 -1
- package/dist/routes/postAuthLogin.test.js +79 -55
- package/dist/routes/postAuthLogin.test.js.map +1 -1
- package/dist/routes/postAuthLogout.d.ts +5 -2
- package/dist/routes/postAuthLogout.js +12 -7
- package/dist/routes/postAuthLogout.js.map +1 -1
- package/dist/routes/postAuthLogout.test.js +35 -17
- package/dist/routes/postAuthLogout.test.js.map +1 -1
- package/dist/routes/postAuthRefresh.d.ts +14 -9
- package/dist/routes/postAuthRefresh.js +10 -6
- package/dist/routes/postAuthRefresh.js.map +1 -1
- package/dist/routes/postAuthRefresh.test.js +90 -59
- package/dist/routes/postAuthRefresh.test.js.map +1 -1
- package/dist/routes/postOAuth2Acknowledge.d.ts +20 -10
- package/dist/routes/postOAuth2Acknowledge.js +54 -38
- package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
- package/dist/routes/postOAuth2Acknowledge.test.js +137 -72
- package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
- package/dist/routes/postOAuth2Revoke.d.ts +99 -0
- package/dist/routes/postOAuth2Revoke.js +102 -0
- package/dist/routes/postOAuth2Revoke.js.map +1 -0
- package/dist/routes/postOAuth2Revoke.test.d.ts +1 -0
- package/dist/routes/postOAuth2Revoke.test.js +161 -0
- package/dist/routes/postOAuth2Revoke.test.js.map +1 -0
- package/dist/routes/postOAuth2Token.d.ts +35 -108
- package/dist/routes/postOAuth2Token.js +53 -114
- package/dist/routes/postOAuth2Token.js.map +1 -1
- package/dist/routes/postOAuth2Token.test.js +493 -61
- package/dist/routes/postOAuth2Token.test.js.map +1 -1
- package/dist/services/authCookies.d.ts +9 -13
- package/dist/services/authCookies.js +1 -1
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/authCookies.test.js.map +1 -1
- package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +129 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js +221 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js +474 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.d.ts +28 -12
- package/dist/services/oAuth2ClientCredentialsGranter.js +39 -21
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
- package/dist/services/oAuth2ClientCredentialsGranter.test.d.ts +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.test.js +265 -0
- package/dist/services/oAuth2ClientCredentialsGranter.test.js.map +1 -0
- package/dist/services/oAuth2Granters.d.ts +81 -86
- package/dist/services/oAuth2Granters.js +2 -67
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2ImplicitGranter.d.ts +38 -0
- package/dist/services/oAuth2ImplicitGranter.js +72 -0
- package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.js +727 -0
- package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -0
- package/dist/services/oAuth2PasswordGranter.d.ts +43 -10
- package/dist/services/oAuth2PasswordGranter.js +53 -18
- package/dist/services/oAuth2PasswordGranter.js.map +1 -1
- package/dist/services/oAuth2PasswordGranter.test.d.ts +1 -0
- package/dist/services/oAuth2PasswordGranter.test.js +455 -0
- package/dist/services/oAuth2PasswordGranter.test.js.map +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +42 -9
- package/dist/services/oAuth2RefreshTokenGranter.js +60 -21
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.test.d.ts +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.test.js +403 -0
- package/dist/services/oAuth2RefreshTokenGranter.test.js.map +1 -0
- package/package.json +11 -11
- package/src/index.test.ts +1314 -404
- package/src/index.ts +46 -53
- package/src/libs/clients.ts +22 -0
- package/src/libs/errors.ts +325 -0
- package/src/libs/grants.ts +15 -0
- package/src/libs/redirectURI.ts +120 -0
- package/src/libs/schemas.ts +27 -0
- package/src/libs/scopes.ts +49 -0
- package/src/libs/utils.ts +24 -0
- package/src/libs/verifier.test.ts +37 -0
- package/src/libs/verifier.ts +57 -0
- package/src/routes/getOAuth2Authorize.test.ts +179 -96
- package/src/routes/getOAuth2Authorize.ts +97 -124
- package/src/routes/getOAuth2WellKnownMetadata.test.ts +58 -60
- package/src/routes/getOAuth2WellKnownMetadata.ts +26 -11
- package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +4 -6
- package/src/routes/postAuthLogin.test.ts +86 -57
- package/src/routes/postAuthLogin.ts +24 -14
- package/src/routes/postAuthLogout.test.ts +42 -18
- package/src/routes/postAuthLogout.ts +22 -8
- package/src/routes/postAuthRefresh.test.ts +98 -61
- package/src/routes/postAuthRefresh.ts +25 -13
- package/src/routes/postOAuth2Acknowledge.test.ts +169 -110
- package/src/routes/postOAuth2Acknowledge.ts +101 -61
- package/src/routes/postOAuth2Revoke.test.ts +179 -0
- package/src/routes/postOAuth2Revoke.ts +156 -0
- package/src/routes/postOAuth2Token.test.ts +570 -108
- package/src/routes/postOAuth2Token.ts +92 -136
- package/src/services/authCookies.test.ts +5 -5
- package/src/services/authCookies.ts +16 -18
- package/src/services/oAuth2AuthorizationCodeGranter.test.ts +564 -0
- package/src/services/oAuth2AuthorizationCodeGranter.ts +388 -0
- package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
- package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
- package/src/services/oAuth2Granters.ts +101 -195
- package/src/services/oAuth2ImplicitGranter.test.ts +811 -0
- package/src/services/oAuth2ImplicitGranter.ts +163 -0
- package/src/services/oAuth2PasswordGranter.test.ts +493 -0
- package/src/services/oAuth2PasswordGranter.ts +116 -40
- package/src/services/oAuth2RefreshTokenGranter.test.ts +441 -0
- package/src/services/oAuth2RefreshTokenGranter.ts +118 -51
- package/src/types.d.ts +1 -0
- package/dist/services/oAuth2CodeGranter.d.ts +0 -41
- package/dist/services/oAuth2CodeGranter.js +0 -105
- package/dist/services/oAuth2CodeGranter.js.map +0 -1
- package/dist/services/oAuth2CodeGranter.test.js +0 -123
- package/dist/services/oAuth2CodeGranter.test.js.map +0 -1
- package/dist/services/oAuth2TokenGranter.d.ts +0 -22
- package/dist/services/oAuth2TokenGranter.js +0 -64
- package/dist/services/oAuth2TokenGranter.js.map +0 -1
- package/src/__snapshots__/index.test.ts.snap +0 -401
- package/src/routes/__snapshots__/getOAuth2Authorize.test.ts.snap +0 -37
- package/src/routes/__snapshots__/postOAuth2Acknowledge.test.ts.snap +0 -61
- package/src/routes/__snapshots__/postOAuth2Token.test.ts.snap +0 -52
- package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +0 -52
- package/src/services/oAuth2CodeGranter.test.ts +0 -176
- package/src/services/oAuth2CodeGranter.ts +0 -210
- package/src/services/oAuth2TokenGranter.ts +0 -119
- /package/dist/{services/oAuth2CodeGranter.test.d.ts → libs/verifier.test.d.ts} +0 -0
|
@@ -1,83 +1,150 @@
|
|
|
1
1
|
import { autoService, location } from 'knifecycle';
|
|
2
|
-
import { noop } from '@whook/whook';
|
|
2
|
+
import { noop, refersTo, type WhookAPISchemaDefinition } from '@whook/whook';
|
|
3
3
|
import { pickYErrorWithCode, YError } from 'yerror';
|
|
4
4
|
import { type LogService } from 'common-services';
|
|
5
5
|
import {
|
|
6
|
-
type
|
|
7
|
-
type
|
|
8
|
-
type
|
|
6
|
+
type WhookOAuth2GranterService,
|
|
7
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
8
|
+
type WhookOAuth2Options,
|
|
9
|
+
type WhookOAuth2GranterDefinitions,
|
|
9
10
|
} from './oAuth2Granters.js';
|
|
11
|
+
import {
|
|
12
|
+
type WhookAuthenticationData,
|
|
13
|
+
type WhookAuthenticationScope,
|
|
14
|
+
} from '@whook/authorization';
|
|
15
|
+
import { checkGrantType } from '../libs/grants.js';
|
|
16
|
+
import { filterScopes } from '../libs/scopes.js';
|
|
17
|
+
import { type WhookOAuth2AccessTokenService } from './oAuth2ImplicitGranter.js';
|
|
18
|
+
import { scopeSchema } from '../libs/schemas.js';
|
|
19
|
+
import { toUsableClientId } from '../libs/clients.js';
|
|
20
|
+
|
|
21
|
+
export const REFRESH_TOKEN_GRANT_TYPE = 'refresh_token';
|
|
22
|
+
|
|
23
|
+
export const refreshTokenRequestBodySchema = {
|
|
24
|
+
name: 'RefreshTokenRequestBody',
|
|
25
|
+
schema: {
|
|
26
|
+
type: 'object',
|
|
27
|
+
description:
|
|
28
|
+
'Token refresh grant type, see https://tools.ietf.org/html/rfc6749#section-6 .',
|
|
29
|
+
required: ['grant_type', 'refresh_token'],
|
|
30
|
+
properties: {
|
|
31
|
+
grant_type: {
|
|
32
|
+
type: 'string',
|
|
33
|
+
enum: [REFRESH_TOKEN_GRANT_TYPE],
|
|
34
|
+
},
|
|
35
|
+
refresh_token: {
|
|
36
|
+
type: 'string',
|
|
37
|
+
},
|
|
38
|
+
scope: refersTo(scopeSchema),
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
} as const satisfies WhookAPISchemaDefinition;
|
|
10
42
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
43
|
+
/**
|
|
44
|
+
* A type allowing to override the refresh token type
|
|
45
|
+
*/
|
|
46
|
+
export type WhookOAuth2RefreshToken = string;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A service to create and check refresh tokens
|
|
50
|
+
*/
|
|
51
|
+
export type WhookOAuth2RefreshTokenService =
|
|
52
|
+
WhookOAuth2AccessTokenService<WhookOAuth2RefreshToken>;
|
|
53
|
+
|
|
54
|
+
export interface WhookOAuth2RefreshTokenGranterDependencies {
|
|
55
|
+
OAUTH2: WhookOAuth2Options;
|
|
56
|
+
readClientGrants: WhookOAuth2ReadClientGrantsService;
|
|
57
|
+
oAuth2RefreshToken: Pick<WhookOAuth2RefreshTokenService, 'check'>;
|
|
14
58
|
log?: LogService;
|
|
15
59
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
60
|
+
|
|
61
|
+
export interface WhookOAuth2RefreshTokenGranterDefinitions extends WhookOAuth2GranterDefinitions {
|
|
62
|
+
grantType: typeof REFRESH_TOKEN_GRANT_TYPE;
|
|
63
|
+
authenticateParameters: {
|
|
64
|
+
refreshToken: string;
|
|
65
|
+
demandedScopes: WhookAuthenticationScope[];
|
|
66
|
+
};
|
|
19
67
|
}
|
|
20
|
-
export type OAuth2RefreshTokenGranterService = OAuth2GranterService<
|
|
21
|
-
Record<string, unknown>,
|
|
22
|
-
Record<string, unknown>,
|
|
23
|
-
Record<string, unknown>,
|
|
24
|
-
OAuth2RefreshTokenGranterParameters
|
|
25
|
-
>;
|
|
26
68
|
|
|
27
|
-
export
|
|
28
|
-
|
|
29
|
-
import.meta.url,
|
|
30
|
-
);
|
|
69
|
+
export type WhookOAuth2RefreshTokenGranterService =
|
|
70
|
+
WhookOAuth2GranterService<WhookOAuth2RefreshTokenGranterDefinitions>;
|
|
31
71
|
|
|
32
72
|
// Refresh Token Grant
|
|
33
73
|
// https://tools.ietf.org/html/rfc6749#page-47
|
|
34
74
|
async function initOAuth2RefreshTokenGranter({
|
|
35
|
-
|
|
75
|
+
OAUTH2,
|
|
76
|
+
readClientGrants,
|
|
36
77
|
oAuth2RefreshToken,
|
|
37
78
|
log = noop,
|
|
38
|
-
}:
|
|
79
|
+
}: WhookOAuth2RefreshTokenGranterDependencies): Promise<WhookOAuth2RefreshTokenGranterService> {
|
|
39
80
|
const authenticateWithRefreshToken: NonNullable<
|
|
40
|
-
|
|
41
|
-
>
|
|
42
|
-
|
|
43
|
-
authenticationData,
|
|
44
|
-
) => {
|
|
45
|
-
try {
|
|
46
|
-
// The client must be authenticated
|
|
47
|
-
if (!authenticationData) {
|
|
48
|
-
throw new YError('E_UNAUTHORIZED');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
await checkApplication({
|
|
52
|
-
applicationId: authenticationData.applicationId,
|
|
53
|
-
type: 'refresh',
|
|
54
|
-
scope: demandedScope,
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
const newAuthenticationData = await oAuth2RefreshToken.check(
|
|
58
|
-
authenticationData,
|
|
59
|
-
refreshToken,
|
|
60
|
-
demandedScope,
|
|
61
|
-
);
|
|
81
|
+
WhookOAuth2RefreshTokenGranterService['authenticate']
|
|
82
|
+
> = async ({ refreshToken, demandedScopes }, optionalAuthenticationData) => {
|
|
83
|
+
let refreshTokenAuthenticationData: WhookAuthenticationData;
|
|
62
84
|
|
|
63
|
-
|
|
85
|
+
try {
|
|
86
|
+
refreshTokenAuthenticationData =
|
|
87
|
+
await oAuth2RefreshToken.check(refreshToken);
|
|
64
88
|
} catch (err) {
|
|
65
89
|
const castedErr = pickYErrorWithCode(err as Error, 'E_BAD_TOKEN');
|
|
66
90
|
|
|
67
91
|
if (castedErr) {
|
|
68
|
-
throw YError.wrap(castedErr, '
|
|
92
|
+
throw YError.wrap(castedErr, 'E_OAUTH2_BAD_REFRESH_TOKEN');
|
|
69
93
|
}
|
|
70
94
|
throw err;
|
|
71
95
|
}
|
|
96
|
+
|
|
97
|
+
const usableClientId = toUsableClientId([
|
|
98
|
+
optionalAuthenticationData?.clientId,
|
|
99
|
+
refreshTokenAuthenticationData.clientId,
|
|
100
|
+
]);
|
|
101
|
+
const grants = await readClientGrants(usableClientId);
|
|
102
|
+
|
|
103
|
+
if (!grants.isPublicClient) {
|
|
104
|
+
if (!optionalAuthenticationData) {
|
|
105
|
+
throw new YError('E_OAUTH2_AUTHENTICATION_REQUIRED', [
|
|
106
|
+
grants.authenticationData.clientId,
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (usableClientId !== grants.authenticationData.clientId) {
|
|
112
|
+
throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
|
|
113
|
+
usableClientId,
|
|
114
|
+
grants.authenticationData.clientId,
|
|
115
|
+
]);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
checkGrantType(grants.allowedGrantTypes, REFRESH_TOKEN_GRANT_TYPE);
|
|
119
|
+
|
|
120
|
+
const filteredScopes = filterScopes(
|
|
121
|
+
filterScopes(
|
|
122
|
+
demandedScopes.length
|
|
123
|
+
? demandedScopes
|
|
124
|
+
: refreshTokenAuthenticationData.scopes,
|
|
125
|
+
grants.allowedScopes,
|
|
126
|
+
!!OAUTH2.strictScopesChecks,
|
|
127
|
+
),
|
|
128
|
+
refreshTokenAuthenticationData.scopes,
|
|
129
|
+
!!OAUTH2.strictScopesChecks,
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
...refreshTokenAuthenticationData,
|
|
134
|
+
scopes: filteredScopes,
|
|
135
|
+
};
|
|
72
136
|
};
|
|
73
137
|
|
|
74
138
|
log('debug', '👫 - OAuth2RefreshTokenGranter Service Initialized!');
|
|
75
139
|
|
|
76
140
|
return {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
authenticate: authenticateWithRefreshToken,
|
|
81
|
-
},
|
|
141
|
+
grantType: REFRESH_TOKEN_GRANT_TYPE,
|
|
142
|
+
issuesRefreshToken: true,
|
|
143
|
+
authenticate: authenticateWithRefreshToken,
|
|
82
144
|
};
|
|
83
145
|
}
|
|
146
|
+
|
|
147
|
+
export default location(
|
|
148
|
+
autoService(initOAuth2RefreshTokenGranter),
|
|
149
|
+
import.meta.url,
|
|
150
|
+
);
|
package/src/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type WhookBaseAuthenticationData } from '@whook/authorization';
|
|
2
2
|
|
|
3
|
+
// Here for testing additional authentication data passthrough
|
|
3
4
|
declare module '@whook/authorization' {
|
|
4
5
|
export interface WhookAuthenticationData extends WhookBaseAuthenticationData {
|
|
5
6
|
userId: string;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { type LogService } from 'common-services';
|
|
2
|
-
import { type OAuth2GranterService, type CheckApplicationService } from './oAuth2Granters.js';
|
|
3
|
-
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
4
|
-
export declare const CODE_GRANTER_TYPE = "code";
|
|
5
|
-
export declare const PLAIN_CODE_CHALLENGE_METHOD = "plain";
|
|
6
|
-
export declare const S256_CODE_CHALLENGE_METHOD = "S256";
|
|
7
|
-
export declare const CODE_CHALLENGE_METHODS: readonly ["plain", "S256"];
|
|
8
|
-
export type CodeChallengeMethod = (typeof CODE_CHALLENGE_METHODS)[number];
|
|
9
|
-
export interface OAuth2CodeService<P extends object = Record<string, unknown>, C extends string = string> {
|
|
10
|
-
create: (authenticationData: WhookAuthenticationData, redirectURI: string, codeChallenge: string | undefined, codeChallengeMethod: CodeChallengeMethod | undefined, additionalParameters: P) => Promise<C>;
|
|
11
|
-
check: (authenticationData: WhookAuthenticationData, code: C, redirectURI: string, codeVerifier: string | undefined) => Promise<WhookAuthenticationData & {
|
|
12
|
-
redirectURI: string;
|
|
13
|
-
} & P>;
|
|
14
|
-
}
|
|
15
|
-
export interface OAuth2CodeGranterDependencies {
|
|
16
|
-
oAuth2Code: OAuth2CodeService;
|
|
17
|
-
checkApplication: CheckApplicationService;
|
|
18
|
-
log?: LogService;
|
|
19
|
-
}
|
|
20
|
-
export interface OAuth2CodeGranterAuthorizeParameters {
|
|
21
|
-
codeChallenge: string;
|
|
22
|
-
codeChallengeMethod: CodeChallengeMethod;
|
|
23
|
-
}
|
|
24
|
-
export interface OAuth2CodeGranterAcknowledgedData {
|
|
25
|
-
code: string;
|
|
26
|
-
}
|
|
27
|
-
export interface OAuth2CodeGranterAcknowledgeParameters {
|
|
28
|
-
codeChallenge: string;
|
|
29
|
-
codeChallengeMethod: CodeChallengeMethod;
|
|
30
|
-
}
|
|
31
|
-
export interface OAuth2CodeGranterGrantParameters {
|
|
32
|
-
code: string;
|
|
33
|
-
redirectURI: string;
|
|
34
|
-
clientId: string;
|
|
35
|
-
codeVerifier: string;
|
|
36
|
-
}
|
|
37
|
-
export type OAuth2CodeGranterService = OAuth2GranterService<OAuth2CodeGranterAuthorizeParameters, OAuth2CodeGranterAcknowledgedData, OAuth2CodeGranterAcknowledgeParameters, OAuth2CodeGranterGrantParameters>;
|
|
38
|
-
declare const _default: import("knifecycle").ServiceInitializer<OAuth2CodeGranterDependencies, OAuth2CodeGranterService>;
|
|
39
|
-
export default _default;
|
|
40
|
-
export declare function base64UrlEncode(buf: Buffer): string;
|
|
41
|
-
export declare function hashCodeVerifier(codeVerifier: Buffer, method: CodeChallengeMethod): Buffer;
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { autoService, location } from 'knifecycle';
|
|
2
|
-
import { noop } from '@whook/whook';
|
|
3
|
-
import { YError } from 'yerror';
|
|
4
|
-
import { createHash } from 'crypto';
|
|
5
|
-
export const CODE_GRANTER_TYPE = 'code';
|
|
6
|
-
export const PLAIN_CODE_CHALLENGE_METHOD = 'plain';
|
|
7
|
-
export const S256_CODE_CHALLENGE_METHOD = 'S256';
|
|
8
|
-
export const CODE_CHALLENGE_METHODS = [
|
|
9
|
-
PLAIN_CODE_CHALLENGE_METHOD,
|
|
10
|
-
S256_CODE_CHALLENGE_METHOD,
|
|
11
|
-
];
|
|
12
|
-
export default location(autoService(initOAuth2CodeGranter), import.meta.url);
|
|
13
|
-
// Authorization Code Grant
|
|
14
|
-
// https://tools.ietf.org/html/rfc6749#section-4.1
|
|
15
|
-
async function initOAuth2CodeGranter({ oAuth2Code, checkApplication, log = noop, }) {
|
|
16
|
-
const authorizeWithCode = async ({ clientId, redirectURI, scope = '' }) => {
|
|
17
|
-
const { redirectURI: finalRedirectURI } = await checkApplication({
|
|
18
|
-
applicationId: clientId,
|
|
19
|
-
type: CODE_GRANTER_TYPE,
|
|
20
|
-
redirectURI,
|
|
21
|
-
scope,
|
|
22
|
-
});
|
|
23
|
-
return {
|
|
24
|
-
applicationId: clientId,
|
|
25
|
-
redirectURI: finalRedirectURI,
|
|
26
|
-
scope,
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
// Authorization Code Response:
|
|
30
|
-
// https://tools.ietf.org/html/rfc6749#section-4.1.2
|
|
31
|
-
const acknowledgeWithCode = async (authenticationData, { clientId, redirectURI, scope }, { codeChallenge, codeChallengeMethod, ...additionalParameters }) => {
|
|
32
|
-
const code = await oAuth2Code.create({ ...authenticationData, applicationId: clientId, scope }, redirectURI, codeChallenge, codeChallengeMethod, additionalParameters);
|
|
33
|
-
return {
|
|
34
|
-
authenticationData: {
|
|
35
|
-
...authenticationData,
|
|
36
|
-
applicationId: clientId,
|
|
37
|
-
scope,
|
|
38
|
-
},
|
|
39
|
-
redirectURI,
|
|
40
|
-
acknowledgedData: {
|
|
41
|
-
code,
|
|
42
|
-
},
|
|
43
|
-
additionalParameters,
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
const authenticateWithCode = async ({ code, clientId, redirectURI, codeVerifier }, authenticationData) => {
|
|
47
|
-
// The client must be authenticated (for now, see below)
|
|
48
|
-
if (!authenticationData) {
|
|
49
|
-
throw new YError('E_UNAUTHORIZED');
|
|
50
|
-
}
|
|
51
|
-
// This check is not really necessary atm but it acts
|
|
52
|
-
// as a reminder that this grant type could be used
|
|
53
|
-
// without authenticating the client. In this
|
|
54
|
-
// scenario, the authenticationData should be deducted
|
|
55
|
-
// from the clientId and the code given in parameters
|
|
56
|
-
// See https://tools.ietf.org/html/rfc6749#section-4.1.3
|
|
57
|
-
if (clientId && clientId !== authenticationData.applicationId) {
|
|
58
|
-
throw new YError('E_APPLICATION_ID_MISMATCH', [
|
|
59
|
-
clientId,
|
|
60
|
-
authenticationData.applicationId,
|
|
61
|
-
]);
|
|
62
|
-
}
|
|
63
|
-
await checkApplication({
|
|
64
|
-
applicationId: authenticationData.applicationId,
|
|
65
|
-
type: CODE_GRANTER_TYPE,
|
|
66
|
-
scope: '',
|
|
67
|
-
});
|
|
68
|
-
const newAuthenticationData = await oAuth2Code.check(authenticationData, code, redirectURI, codeVerifier);
|
|
69
|
-
return newAuthenticationData;
|
|
70
|
-
};
|
|
71
|
-
log('debug', '👫 - OAuth2CodeGranter Service Initialized!');
|
|
72
|
-
return {
|
|
73
|
-
type: CODE_GRANTER_TYPE,
|
|
74
|
-
authorizer: {
|
|
75
|
-
responseType: 'code',
|
|
76
|
-
authorize: authorizeWithCode,
|
|
77
|
-
},
|
|
78
|
-
acknowledger: {
|
|
79
|
-
acknowledgmentType: 'code',
|
|
80
|
-
acknowledge: acknowledgeWithCode,
|
|
81
|
-
},
|
|
82
|
-
authenticator: {
|
|
83
|
-
grantType: 'authorization_code',
|
|
84
|
-
authenticate: authenticateWithCode,
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
// See https://tools.ietf.org/html/rfc7636#appendix-A
|
|
89
|
-
export function base64UrlEncode(buf) {
|
|
90
|
-
let s = buf.toString('base64');
|
|
91
|
-
s = s.split('=')[0];
|
|
92
|
-
s = s.replaceAll('+', '-');
|
|
93
|
-
s = s.replaceAll('/', '_');
|
|
94
|
-
return s;
|
|
95
|
-
}
|
|
96
|
-
export function hashCodeVerifier(codeVerifier, method) {
|
|
97
|
-
if (PLAIN_CODE_CHALLENGE_METHOD === method) {
|
|
98
|
-
return codeVerifier;
|
|
99
|
-
}
|
|
100
|
-
if (S256_CODE_CHALLENGE_METHOD === method) {
|
|
101
|
-
return createHash('sha256').update(codeVerifier).digest();
|
|
102
|
-
}
|
|
103
|
-
throw new YError('E_UNSUPPORTED_CODE_CHALLENGE_METHOD', [method]);
|
|
104
|
-
}
|
|
105
|
-
//# sourceMappingURL=oAuth2CodeGranter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"oAuth2CodeGranter.js","sourceRoot":"","sources":["../../src/services/oAuth2CodeGranter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAMhC,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAGpC,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC;AACxC,MAAM,CAAC,MAAM,2BAA2B,GAAG,OAAO,CAAC;AACnD,MAAM,CAAC,MAAM,0BAA0B,GAAG,MAAM,CAAC;AACjD,MAAM,CAAC,MAAM,sBAAsB,GAAG;IACpC,2BAA2B;IAC3B,0BAA0B;CAClB,CAAC;AAwDX,eAAe,QAAQ,CAAC,WAAW,CAAC,qBAAqB,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAE7E,2BAA2B;AAC3B,kDAAkD;AAClD,KAAK,UAAU,qBAAqB,CAAC,EACnC,UAAU,EACV,gBAAgB,EAChB,GAAG,GAAG,IAAI,GACoB;IAC9B,MAAM,iBAAiB,GAEN,KAAK,EAAE,EAAE,QAAQ,EAAE,WAAW,EAAE,KAAK,GAAG,EAAE,EAAE,EAAE,EAAE;QAC/D,MAAM,EAAE,WAAW,EAAE,gBAAgB,EAAE,GAAG,MAAM,gBAAgB,CAAC;YAC/D,aAAa,EAAE,QAAQ;YACvB,IAAI,EAAE,iBAAiB;YACvB,WAAW;YACX,KAAK;SACN,CAAC,CAAC;QAEH,OAAO;YACL,aAAa,EAAE,QAAQ;YACvB,WAAW,EAAE,gBAAgB;YAC7B,KAAK;SACN,CAAC;IACJ,CAAC,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
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
|
|
2
|
-
import initOAuth2CodeGranter, { base64UrlEncode, hashCodeVerifier, } from './oAuth2CodeGranter.js';
|
|
3
|
-
describe('OAuth2CodeGranter', () => {
|
|
4
|
-
const oAuth2Code = {
|
|
5
|
-
create: jest.fn(),
|
|
6
|
-
check: jest.fn(),
|
|
7
|
-
};
|
|
8
|
-
const checkApplication = jest.fn();
|
|
9
|
-
const log = jest.fn();
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
oAuth2Code.create.mockReset();
|
|
12
|
-
oAuth2Code.check.mockReset();
|
|
13
|
-
checkApplication.mockReset();
|
|
14
|
-
log.mockReset();
|
|
15
|
-
});
|
|
16
|
-
test('should work with a complete valid flow', async () => {
|
|
17
|
-
const oAuth2CodeGranter = await initOAuth2CodeGranter({
|
|
18
|
-
checkApplication,
|
|
19
|
-
oAuth2Code,
|
|
20
|
-
log,
|
|
21
|
-
});
|
|
22
|
-
checkApplication.mockResolvedValue({
|
|
23
|
-
redirectURI: 'https://www.example.com',
|
|
24
|
-
type: 'a_type',
|
|
25
|
-
scope: 'a_scope',
|
|
26
|
-
applicationId: 'abbacaca-abba-caca-abba-cacaabbac0c0',
|
|
27
|
-
});
|
|
28
|
-
oAuth2Code.create.mockResolvedValueOnce('yolo');
|
|
29
|
-
oAuth2Code.check.mockResolvedValueOnce({
|
|
30
|
-
redirectURI: 'https://www.example2.com',
|
|
31
|
-
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
32
|
-
scope: 'user',
|
|
33
|
-
});
|
|
34
|
-
const authorizerResult = await oAuth2CodeGranter.authorizer?.authorize({
|
|
35
|
-
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
36
|
-
redirectURI: 'https://www.example.com/oauth2/code',
|
|
37
|
-
scope: 'user',
|
|
38
|
-
}, {
|
|
39
|
-
codeChallenge: '',
|
|
40
|
-
codeChallengeMethod: 'plain',
|
|
41
|
-
});
|
|
42
|
-
const acknowledgerResult = await oAuth2CodeGranter.acknowledger?.acknowledge({
|
|
43
|
-
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
44
|
-
scope: 'user, admin',
|
|
45
|
-
}, {
|
|
46
|
-
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
47
|
-
redirectURI: 'https://www.example.com/oauth2/code',
|
|
48
|
-
scope: 'user',
|
|
49
|
-
}, {
|
|
50
|
-
codeChallenge: '',
|
|
51
|
-
codeChallengeMethod: 'plain',
|
|
52
|
-
});
|
|
53
|
-
const authenticatorResult = await oAuth2CodeGranter.authenticator?.authenticate({
|
|
54
|
-
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
55
|
-
redirectURI: 'https://www.example.com/oauth2/code',
|
|
56
|
-
code: 'yolo',
|
|
57
|
-
codeVerifier: '',
|
|
58
|
-
}, {
|
|
59
|
-
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
60
|
-
scope: 'user',
|
|
61
|
-
});
|
|
62
|
-
expect({
|
|
63
|
-
authorizerResult,
|
|
64
|
-
acknowledgerResult,
|
|
65
|
-
authenticatorResult,
|
|
66
|
-
}).toMatchInlineSnapshot(`
|
|
67
|
-
{
|
|
68
|
-
"acknowledgerResult": {
|
|
69
|
-
"acknowledgedData": {
|
|
70
|
-
"code": "yolo",
|
|
71
|
-
},
|
|
72
|
-
"additionalParameters": {},
|
|
73
|
-
"authenticationData": {
|
|
74
|
-
"applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
75
|
-
"scope": "user",
|
|
76
|
-
},
|
|
77
|
-
"redirectURI": "https://www.example.com/oauth2/code",
|
|
78
|
-
},
|
|
79
|
-
"authenticatorResult": {
|
|
80
|
-
"applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
81
|
-
"redirectURI": "https://www.example2.com",
|
|
82
|
-
"scope": "user",
|
|
83
|
-
},
|
|
84
|
-
"authorizerResult": {
|
|
85
|
-
"applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
86
|
-
"redirectURI": "https://www.example.com",
|
|
87
|
-
"scope": "user",
|
|
88
|
-
},
|
|
89
|
-
}
|
|
90
|
-
`);
|
|
91
|
-
expect({
|
|
92
|
-
oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
|
|
93
|
-
oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
|
|
94
|
-
checkApplicationCalls: checkApplication.mock.calls,
|
|
95
|
-
logCalls: log.mock.calls,
|
|
96
|
-
}).toMatchSnapshot();
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
describe('base64UrlEncode()', () => {
|
|
100
|
-
test('should work like here https://tools.ietf.org/html/rfc7636#appendix-A', () => {
|
|
101
|
-
expect(base64UrlEncode(Buffer.from([
|
|
102
|
-
116, 24, 223, 180, 151, 153, 224, 37, 79, 250, 96, 125, 216, 173, 187,
|
|
103
|
-
186, 22, 212, 37, 77, 105, 214, 191, 240, 91, 88, 5, 88, 83, 132, 141,
|
|
104
|
-
121,
|
|
105
|
-
]))).toEqual('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk');
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
describe('base64UrlEncode()', () => {
|
|
109
|
-
test('should work with plain method', () => {
|
|
110
|
-
expect(hashCodeVerifier(Buffer.from('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'), 'plain')).toEqual(Buffer.from('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'));
|
|
111
|
-
});
|
|
112
|
-
test('should work with S256 like here https://tools.ietf.org/html/rfc7636#appendix-A', () => {
|
|
113
|
-
expect(hashCodeVerifier(Buffer.from('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'), 'S256')).toEqual(Buffer.from([
|
|
114
|
-
19, 211, 30, 150, 26, 26, 216, 236, 47, 22, 177, 12, 76, 152, 46, 8,
|
|
115
|
-
118, 168, 120, 173, 109, 241, 68, 86, 110, 225, 137, 74, 203, 112, 249,
|
|
116
|
-
195,
|
|
117
|
-
]));
|
|
118
|
-
});
|
|
119
|
-
test('should work base64 url encode like here https://tools.ietf.org/html/rfc7636#appendix-A', () => {
|
|
120
|
-
expect(base64UrlEncode(hashCodeVerifier(Buffer.from('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'), 'S256'))).toEqual('E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM');
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
//# sourceMappingURL=oAuth2CodeGranter.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"oAuth2CodeGranter.test.js","sourceRoot":"","sources":["../../src/services/oAuth2CodeGranter.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAGzE,OAAO,qBAAqB,EAAE,EAC5B,eAAe,EACf,gBAAgB,GAEjB,MAAM,wBAAwB,CAAC;AAEhC,QAAQ,CAAC,mBAAmB,EAAE,GAAG,EAAE;IACjC,MAAM,UAAU,GAAG;QACjB,MAAM,EAAE,IAAI,CAAC,EAAE,EAA+B;QAC9C,KAAK,EAAE,IAAI,CAAC,EAAE,EAA8B;KAC7C,CAAC;IACF,MAAM,gBAAgB,GAAG,IAAI,CAAC,EAAE,EAA2B,CAAC;IAC5D,MAAM,GAAG,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC;IAEtB,UAAU,CAAC,GAAG,EAAE;QACd,UAAU,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;QAC9B,UAAU,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC;QAC7B,gBAAgB,CAAC,SAAS,EAAE,CAAC;QAC7B,GAAG,CAAC,SAAS,EAAE,CAAC;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+B,EAAE,GAAG,EAAE;QACzC,MAAM,CACJ,gBAAgB,CACd,MAAM,CAAC,IAAI,CAAC,6CAA6C,CAAC,EAC1D,OAAO,CACR,CACF,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,6CAA6C,CAAC,CAAC,CAAC;IACxE,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,gFAAgF,EAAE,GAAG,EAAE;QAC1F,MAAM,CACJ,gBAAgB,CACd,MAAM,CAAC,IAAI,CAAC,6CAA6C,CAAC,EAC1D,MAAM,CACP,CACF,CAAC,OAAO,CACP,MAAM,CAAC,IAAI,CAAC;YACV,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC;YACnE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG;YACtE,GAAG;SACJ,CAAC,CACH,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,wFAAwF,EAAE,GAAG,EAAE;QAClG,MAAM,CACJ,eAAe,CACb,gBAAgB,CACd,MAAM,CAAC,IAAI,CAAC,6CAA6C,CAAC,EAC1D,MAAM,CACP,CACF,CACF,CAAC,OAAO,CAAC,6CAA6C,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { type LogService, type TimeService } from 'common-services';
|
|
2
|
-
import { type OAuth2GranterService, type OAuth2AccessTokenService, type CheckApplicationService } from './oAuth2Granters.js';
|
|
3
|
-
import { type OAuth2CodeService } from './oAuth2CodeGranter.js';
|
|
4
|
-
export declare const IMPLICIT_GRANTER_TYPE = "token";
|
|
5
|
-
export interface OAuth2TokenGranterDependencies {
|
|
6
|
-
oAuth2Code: OAuth2CodeService;
|
|
7
|
-
checkApplication: CheckApplicationService;
|
|
8
|
-
oAuth2AccessToken: OAuth2AccessTokenService;
|
|
9
|
-
time?: TimeService;
|
|
10
|
-
log?: LogService;
|
|
11
|
-
}
|
|
12
|
-
export type OAuth2TokenGranterAuthorizeParameters = Record<string, unknown>;
|
|
13
|
-
export interface OAuth2TokenGranterAcknowledgedData {
|
|
14
|
-
accessToken: string;
|
|
15
|
-
tokenType: 'bearer';
|
|
16
|
-
expiresIn: number;
|
|
17
|
-
}
|
|
18
|
-
export type OAuth2TokenGranterAcknowledgeParameters = Record<string, unknown>;
|
|
19
|
-
export type OAuth2TokenGranterGrantParameters = Record<string, unknown>;
|
|
20
|
-
export type OAuth2TokenGranterService = OAuth2GranterService<OAuth2TokenGranterAuthorizeParameters, OAuth2TokenGranterAcknowledgedData, OAuth2TokenGranterAcknowledgeParameters, OAuth2TokenGranterGrantParameters>;
|
|
21
|
-
declare const _default: import("knifecycle").ServiceInitializer<OAuth2TokenGranterDependencies, OAuth2TokenGranterService>;
|
|
22
|
-
export default _default;
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { autoService, location } from 'knifecycle';
|
|
2
|
-
import { noop } from '@whook/whook';
|
|
3
|
-
export const IMPLICIT_GRANTER_TYPE = 'token';
|
|
4
|
-
export default location(autoService(initOAuth2TokenGranter), import.meta.url);
|
|
5
|
-
// Implicit Grant
|
|
6
|
-
// https://tools.ietf.org/html/rfc6749#section-4.2
|
|
7
|
-
async function initOAuth2TokenGranter({ checkApplication, oAuth2AccessToken, time = Date.now.bind(Date), log = noop, }) {
|
|
8
|
-
const authorizeWithToken = async ({ clientId, redirectURI, scope }) => {
|
|
9
|
-
const { redirectURI: finalRedirectURI } = await checkApplication({
|
|
10
|
-
applicationId: clientId,
|
|
11
|
-
type: IMPLICIT_GRANTER_TYPE,
|
|
12
|
-
redirectURI,
|
|
13
|
-
scope,
|
|
14
|
-
});
|
|
15
|
-
return {
|
|
16
|
-
applicationId: clientId,
|
|
17
|
-
redirectURI: finalRedirectURI,
|
|
18
|
-
scope,
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
// Access Token Response:
|
|
22
|
-
// https://tools.ietf.org/html/rfc6749#section-4.2.2
|
|
23
|
-
const acknowledgeWithToken = async (authenticationData, { clientId, redirectURI, scope: providedScope }, additionalParameters) => {
|
|
24
|
-
const { redirectURI: finalRedirectURI } = await checkApplication({
|
|
25
|
-
applicationId: clientId,
|
|
26
|
-
redirectURI,
|
|
27
|
-
scope: providedScope,
|
|
28
|
-
type: IMPLICIT_GRANTER_TYPE,
|
|
29
|
-
});
|
|
30
|
-
const { token: accessToken, expiresAt: accessTokenExpiresAt } = await oAuth2AccessToken.create(authenticationData, {
|
|
31
|
-
// TODO: check a way to avoid this by adding params
|
|
32
|
-
...authenticationData,
|
|
33
|
-
applicationId: clientId,
|
|
34
|
-
scope: providedScope,
|
|
35
|
-
}, additionalParameters);
|
|
36
|
-
return {
|
|
37
|
-
authenticationData: {
|
|
38
|
-
...authenticationData,
|
|
39
|
-
applicationId: clientId,
|
|
40
|
-
scope: providedScope,
|
|
41
|
-
},
|
|
42
|
-
redirectURI: finalRedirectURI,
|
|
43
|
-
acknowledgedData: {
|
|
44
|
-
accessToken,
|
|
45
|
-
tokenType: 'bearer',
|
|
46
|
-
expiresIn: Math.round((accessTokenExpiresAt - time()) / 1000),
|
|
47
|
-
},
|
|
48
|
-
additionalParameters,
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
log('debug', '👫 - OAuth2TokenGranter Service Initialized!');
|
|
52
|
-
return {
|
|
53
|
-
type: IMPLICIT_GRANTER_TYPE,
|
|
54
|
-
authorizer: {
|
|
55
|
-
responseType: 'token',
|
|
56
|
-
authorize: authorizeWithToken,
|
|
57
|
-
},
|
|
58
|
-
acknowledger: {
|
|
59
|
-
acknowledgmentType: 'token',
|
|
60
|
-
acknowledge: acknowledgeWithToken,
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
//# sourceMappingURL=oAuth2TokenGranter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"oAuth2TokenGranter.js","sourceRoot":"","sources":["../../src/services/oAuth2TokenGranter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AASpC,MAAM,CAAC,MAAM,qBAAqB,GAAG,OAAO,CAAC;AAwB7C,eAAe,QAAQ,CAAC,WAAW,CAAC,sBAAsB,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAE9E,iBAAiB;AACjB,kDAAkD;AAClD,KAAK,UAAU,sBAAsB,CAAC,EACpC,gBAAgB,EAChB,iBAAiB,EACjB,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,EAC1B,GAAG,GAAG,IAAI,GACqB;IAC/B,MAAM,kBAAkB,GAEP,KAAK,EAAE,EAAE,QAAQ,EAAE,WAAW,EAAE,KAAK,EAAE,EAAE,EAAE;QAC1D,MAAM,EAAE,WAAW,EAAE,gBAAgB,EAAE,GAAG,MAAM,gBAAgB,CAAC;YAC/D,aAAa,EAAE,QAAQ;YACvB,IAAI,EAAE,qBAAqB;YAC3B,WAAW;YACX,KAAK;SACN,CAAC,CAAC;QAEH,OAAO;YACL,aAAa,EAAE,QAAQ;YACvB,WAAW,EAAE,gBAAgB;YAC7B,KAAK;SACN,CAAC;IACJ,CAAC,CAAC;IAEF,yBAAyB;IACzB,oDAAoD;IACpD,MAAM,oBAAoB,GAEP,KAAK,EACtB,kBAAkB,EAClB,EAAE,QAAQ,EAAE,WAAW,EAAE,KAAK,EAAE,aAA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