@celljs/oauth2-client 4.0.0 → 4.1.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/esm/node/authentication/oauth2-authentication-provider.d.ts +1 -1
- package/esm/node/authentication/oauth2-authentication-provider.js +4 -3
- package/esm/node/authentication/oauth2-authentication-provider.js.map +1 -1
- package/esm/node/authentication/oidc-authentication-provider.d.ts +7 -2
- package/esm/node/authentication/oidc-authentication-provider.js +23 -3
- package/esm/node/authentication/oidc-authentication-provider.js.map +1 -1
- package/esm/node/authorization/authorization-request-resolver.js +6 -6
- package/esm/node/authorization/authorization-request-resolver.js.map +1 -1
- package/esm/node/endpoint/authorization-code-token-response-client.js +2 -2
- package/esm/node/endpoint/authorization-code-token-response-client.js.map +1 -1
- package/esm/node/endpoint/client-credentials-token-response-client.js +2 -2
- package/esm/node/endpoint/client-credentials-token-response-client.js.map +1 -1
- package/esm/node/endpoint/password-token-response-client.js +2 -2
- package/esm/node/endpoint/password-token-response-client.js.map +1 -1
- package/esm/node/endpoint/refresh-token-token-response-client.js +2 -2
- package/esm/node/endpoint/refresh-token-token-response-client.js.map +1 -1
- package/esm/node/endpoint/user-response-client.js +2 -2
- package/esm/node/endpoint/user-response-client.js.map +1 -1
- package/esm/node/error/error-handler.js +2 -4
- package/esm/node/error/error-handler.js.map +1 -1
- package/esm/node/test/test-config.d.ts +1 -0
- package/esm/node/test/test-config.js +9 -0
- package/esm/node/test/test-config.js.map +1 -0
- package/esm/node/utils/authorization-grant-request-entity-util.js +1 -4
- package/esm/node/utils/authorization-grant-request-entity-util.js.map +1 -1
- package/esm/node/utils/form-util.d.ts +7 -0
- package/esm/node/utils/form-util.js +19 -0
- package/esm/node/utils/form-util.js.map +1 -0
- package/esm/node/utils/index.d.ts +2 -0
- package/esm/node/utils/index.js +2 -0
- package/esm/node/utils/index.js.map +1 -1
- package/esm/node/utils/oidc-nonce-util.d.ts +6 -0
- package/esm/node/utils/oidc-nonce-util.js +20 -0
- package/esm/node/utils/oidc-nonce-util.js.map +1 -0
- package/lib/node/authentication/oauth2-authentication-provider.d.ts +1 -1
- package/lib/node/authentication/oauth2-authentication-provider.d.ts.map +1 -1
- package/lib/node/authentication/oauth2-authentication-provider.js +4 -3
- package/lib/node/authentication/oauth2-authentication-provider.js.map +1 -1
- package/lib/node/authentication/oidc-authentication-provider.d.ts +7 -2
- package/lib/node/authentication/oidc-authentication-provider.d.ts.map +1 -1
- package/lib/node/authentication/oidc-authentication-provider.js +22 -2
- package/lib/node/authentication/oidc-authentication-provider.js.map +1 -1
- package/lib/node/authentication/oidc-authentication-provider.spec.d.ts +2 -0
- package/lib/node/authentication/oidc-authentication-provider.spec.d.ts.map +1 -0
- package/lib/node/authentication/oidc-authentication-provider.spec.js +99 -0
- package/lib/node/authentication/oidc-authentication-provider.spec.js.map +1 -0
- package/lib/node/authorization/authorization-request-resolver.d.ts.map +1 -1
- package/lib/node/authorization/authorization-request-resolver.js +6 -38
- package/lib/node/authorization/authorization-request-resolver.js.map +1 -1
- package/lib/node/endpoint/authorization-code-token-response-client.js +2 -35
- package/lib/node/endpoint/authorization-code-token-response-client.js.map +1 -1
- package/lib/node/endpoint/client-credentials-token-response-client.js +2 -35
- package/lib/node/endpoint/client-credentials-token-response-client.js.map +1 -1
- package/lib/node/endpoint/password-token-response-client.js +2 -35
- package/lib/node/endpoint/password-token-response-client.js.map +1 -1
- package/lib/node/endpoint/refresh-token-token-response-client.js +2 -35
- package/lib/node/endpoint/refresh-token-token-response-client.js.map +1 -1
- package/lib/node/endpoint/user-response-client.js +2 -35
- package/lib/node/endpoint/user-response-client.js.map +1 -1
- package/lib/node/error/error-handler.d.ts.map +1 -1
- package/lib/node/error/error-handler.js +2 -3
- package/lib/node/error/error-handler.js.map +1 -1
- package/lib/node/test/test-config.d.ts +2 -0
- package/lib/node/test/test-config.d.ts.map +1 -0
- package/lib/node/test/test-config.js +10 -0
- package/lib/node/test/test-config.js.map +1 -0
- package/lib/node/utils/authorization-grant-request-entity-util.d.ts.map +1 -1
- package/lib/node/utils/authorization-grant-request-entity-util.js +1 -3
- package/lib/node/utils/authorization-grant-request-entity-util.js.map +1 -1
- package/lib/node/utils/form-util.d.ts +8 -0
- package/lib/node/utils/form-util.d.ts.map +1 -0
- package/lib/node/utils/form-util.js +22 -0
- package/lib/node/utils/form-util.js.map +1 -0
- package/lib/node/utils/form-util.spec.d.ts +2 -0
- package/lib/node/utils/form-util.spec.d.ts.map +1 -0
- package/lib/node/utils/form-util.spec.js +24 -0
- package/lib/node/utils/form-util.spec.js.map +1 -0
- package/lib/node/utils/index.d.ts +2 -0
- package/lib/node/utils/index.d.ts.map +1 -1
- package/lib/node/utils/index.js +2 -0
- package/lib/node/utils/index.js.map +1 -1
- package/lib/node/utils/oidc-nonce-util.d.ts +7 -0
- package/lib/node/utils/oidc-nonce-util.d.ts.map +1 -0
- package/lib/node/utils/oidc-nonce-util.js +23 -0
- package/lib/node/utils/oidc-nonce-util.js.map +1 -0
- package/package.json +9 -15
- package/src/node/authentication/oauth2-authentication-provider.ts +5 -4
- package/src/node/authentication/oidc-authentication-provider.spec.ts +104 -0
- package/src/node/authentication/oidc-authentication-provider.ts +25 -4
- package/src/node/authorization/authorization-request-resolver.ts +6 -5
- package/src/node/endpoint/authorization-code-token-response-client.ts +2 -2
- package/src/node/endpoint/client-credentials-token-response-client.ts +2 -2
- package/src/node/endpoint/password-token-response-client.ts +2 -2
- package/src/node/endpoint/refresh-token-token-response-client.ts +2 -2
- package/src/node/endpoint/user-response-client.ts +2 -2
- package/src/node/error/error-handler.ts +2 -3
- package/src/node/test/test-config.ts +6 -0
- package/src/node/utils/authorization-grant-request-entity-util.ts +1 -3
- package/src/node/utils/form-util.spec.ts +25 -0
- package/src/node/utils/form-util.ts +17 -0
- package/src/node/utils/index.ts +2 -0
- package/src/node/utils/oidc-nonce-util.ts +18 -0
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { Component, Autowired } from '@celljs/core';
|
|
2
|
-
import { OAuth2AuthenticationError, OidcScopes, AccessTokenResponse, OidcParameterNames} from '@celljs/oauth2-core';
|
|
2
|
+
import { OAuth2AuthenticationError, OidcScopes, AccessTokenResponse, OidcParameterNames, AuthorizationRequest } from '@celljs/oauth2-core';
|
|
3
3
|
import { IdTokenJwtDecoderFactory, ClientRegistration } from '../registration';
|
|
4
4
|
import { Jwt } from '@celljs/oauth2-jose';
|
|
5
5
|
import { errors } from 'jose';
|
|
6
6
|
import { OidcUserRequest, OidcUserService } from '../user';
|
|
7
7
|
import { UserService, AuthenticationProvider } from '@celljs/security/lib/node';
|
|
8
8
|
import { User } from '@celljs/security';
|
|
9
|
-
import { OIDC_AUTHENTICATION_PROVIDER_PRIORITY, INVALID_ID_TOKEN_ERROR_CODE } from './authentication-protocol';
|
|
9
|
+
import { OIDC_AUTHENTICATION_PROVIDER_PRIORITY, INVALID_ID_TOKEN_ERROR_CODE, INVALID_NONCE_ERROR_CODE } from './authentication-protocol';
|
|
10
|
+
import { OidcNonceUtil } from '../utils/oidc-nonce-util';
|
|
10
11
|
import { OAuth2AuthenticationProvider } from './oauth2-authentication-provider';
|
|
11
12
|
|
|
12
13
|
@Component(AuthenticationProvider)
|
|
@@ -32,8 +33,10 @@ export class OidcAuthenticationProvider extends OAuth2AuthenticationProvider {
|
|
|
32
33
|
return true;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
protected override async getUserinfo(clientRegistration: ClientRegistration, tokenResponse: AccessTokenResponse, additionalParameters: { [key: string]: string }
|
|
36
|
+
protected override async getUserinfo(clientRegistration: ClientRegistration, tokenResponse: AccessTokenResponse, additionalParameters: { [key: string]: string },
|
|
37
|
+
authorizationRequest: AuthorizationRequest) {
|
|
36
38
|
const idToken = await this.createOidcToken(clientRegistration, tokenResponse);
|
|
39
|
+
this.validateNonce(authorizationRequest, idToken);
|
|
37
40
|
|
|
38
41
|
return this.userService.load({
|
|
39
42
|
idToken,
|
|
@@ -43,10 +46,28 @@ export class OidcAuthenticationProvider extends OAuth2AuthenticationProvider {
|
|
|
43
46
|
});
|
|
44
47
|
}
|
|
45
48
|
|
|
49
|
+
/**
|
|
50
|
+
* OpenID Connect Core 1.0 §3.1.3.7: when the authorization request sent a nonce, the ID Token must
|
|
51
|
+
* carry it back (as sent, the hash of the stored nonce).
|
|
52
|
+
*/
|
|
53
|
+
protected validateNonce(authorizationRequest: AuthorizationRequest, idToken: Jwt): void {
|
|
54
|
+
const nonce = authorizationRequest.attributes?.[OidcParameterNames.NONCE];
|
|
55
|
+
if (nonce === undefined) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const claim = idToken.payload?.[OidcParameterNames.NONCE];
|
|
59
|
+
if (!OidcNonceUtil.matches(nonce, claim)) {
|
|
60
|
+
throw new OAuth2AuthenticationError({
|
|
61
|
+
errorCode: INVALID_NONCE_ERROR_CODE,
|
|
62
|
+
description: claim === undefined ? 'The ID Token has no nonce.' : 'The nonce of the ID Token does not match the authorization request.'
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
46
67
|
protected async createOidcToken(clientRegistration: ClientRegistration, tokenResponse: AccessTokenResponse): Promise<Jwt> {
|
|
47
68
|
const jwtDecoder = await this.idTokenJwtDecoderFactory.create(clientRegistration);
|
|
48
69
|
try {
|
|
49
|
-
return jwtDecoder.decode(tokenResponse.additionalParameters[OidcParameterNames.ID_TOKEN]);
|
|
70
|
+
return await jwtDecoder.decode(tokenResponse.additionalParameters[OidcParameterNames.ID_TOKEN]);
|
|
50
71
|
} catch (error) {
|
|
51
72
|
if (error instanceof errors.JOSEError) {
|
|
52
73
|
throw new OAuth2AuthenticationError({
|
|
@@ -6,9 +6,9 @@ import { ClientRegistrationManager, ClientRegistration } from '../registration';
|
|
|
6
6
|
import { RequestMatcher, Context } from '@celljs/web/lib/node';
|
|
7
7
|
import { AUTHORIZATION_REQUEST_BASE_URI, DEFAULT_REDIRECT_URI } from '../constants';
|
|
8
8
|
import { Base64StringKeyGenerator } from '@celljs/security/lib/node';
|
|
9
|
-
import { SHA256, enc } from 'crypto-js';
|
|
10
9
|
import { ProviderDetailsManager } from '../provider';
|
|
11
|
-
import
|
|
10
|
+
import { FormUtil } from '../utils/form-util';
|
|
11
|
+
import { OidcNonceUtil } from '../utils/oidc-nonce-util';
|
|
12
12
|
import { PathResolver, ENDPOINT, UrlUtil } from '@celljs/web';
|
|
13
13
|
|
|
14
14
|
export const REGISTRATION_ID_URI_VARIABLE_NAME = 'registrationId';
|
|
@@ -59,7 +59,7 @@ export class DefaultAuthorizationRequestResolver implements AuthorizationRequest
|
|
|
59
59
|
if (AuthorizationGrantType.AuthorizationCode === clientRegistration.authorizationGrantType) {
|
|
60
60
|
const additionalParameters: { [key: string]: any } = {};
|
|
61
61
|
const scopes = clientRegistration.scopes;
|
|
62
|
-
if (scopes.indexOf(OidcScopes.OPENID)) {
|
|
62
|
+
if (scopes.indexOf(OidcScopes.OPENID) !== -1) {
|
|
63
63
|
await this.addNonceParameters(attributes, additionalParameters);
|
|
64
64
|
}
|
|
65
65
|
if (ClientAuthenticationMethod.None === clientRegistration.clientAuthenticationMethod) {
|
|
@@ -80,7 +80,7 @@ export class DefaultAuthorizationRequestResolver implements AuthorizationRequest
|
|
|
80
80
|
attributes,
|
|
81
81
|
additionalParameters
|
|
82
82
|
};
|
|
83
|
-
authorizationRequest.authorizationRequestUri = `${authorizationRequest.authorizationUri}?${
|
|
83
|
+
authorizationRequest.authorizationRequestUri = `${authorizationRequest.authorizationUri}?${FormUtil.stringify(this.getParameters(authorizationRequest))}`;
|
|
84
84
|
return authorizationRequest;
|
|
85
85
|
}
|
|
86
86
|
|
|
@@ -132,7 +132,8 @@ export class DefaultAuthorizationRequestResolver implements AuthorizationRequest
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
protected createHash(nonce: string) {
|
|
135
|
-
|
|
135
|
+
// base64url without padding: what S256 requires for the PKCE code_challenge (RFC 7636 §4.2).
|
|
136
|
+
return OidcNonceUtil.hash(nonce);
|
|
136
137
|
}
|
|
137
138
|
|
|
138
139
|
protected async resolveRegistrationId(): Promise<string | undefined> {
|
|
@@ -5,7 +5,7 @@ import { ProviderDetailsManager } from '../provider';
|
|
|
5
5
|
import { AccessTokenResponse, OAuth2ParameterNames, PkceParameterNames, ClientAuthenticationMethod, OAuth2Error, OAuth2AuthorizationError,
|
|
6
6
|
AccessTokenResponseConverter } from '@celljs/oauth2-core';
|
|
7
7
|
import { AuthorizationGrantRequestEntityUtil } from '../utils';
|
|
8
|
-
import
|
|
8
|
+
import { FormUtil } from '../utils/form-util';
|
|
9
9
|
|
|
10
10
|
@Component()
|
|
11
11
|
export class AuthorizationCodeTokenResponseClient implements AccessTokenResponseClient<AuthorizationCodeGrantRequest> {
|
|
@@ -67,7 +67,7 @@ export class AuthorizationCodeTokenResponseClient implements AccessTokenResponse
|
|
|
67
67
|
if (ClientAuthenticationMethod.Post === clientAuthenticationMethod) {
|
|
68
68
|
formParameters[OAuth2ParameterNames.CLIENT_SECRET] = clientSecret;
|
|
69
69
|
}
|
|
70
|
-
return
|
|
70
|
+
return FormUtil.stringify(formParameters);
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
}
|
|
@@ -4,7 +4,7 @@ import { RestOperations } from '@celljs/http';
|
|
|
4
4
|
import { ProviderDetailsManager } from '../provider';
|
|
5
5
|
import { AccessTokenResponse, OAuth2ParameterNames, ClientAuthenticationMethod, OAuth2Error, OAuth2AuthorizationError, AccessTokenResponseConverter } from '@celljs/oauth2-core';
|
|
6
6
|
import { AuthorizationGrantRequestEntityUtil } from '../utils';
|
|
7
|
-
import
|
|
7
|
+
import { FormUtil } from '../utils/form-util';
|
|
8
8
|
|
|
9
9
|
@Component()
|
|
10
10
|
export class ClientCredentialsTokenResponseClient implements AccessTokenResponseClient<ClientCredentialsGrantRequest> {
|
|
@@ -63,7 +63,7 @@ export class ClientCredentialsTokenResponseClient implements AccessTokenResponse
|
|
|
63
63
|
formParameters[OAuth2ParameterNames.CLIENT_ID] = clientId;
|
|
64
64
|
formParameters[OAuth2ParameterNames.CLIENT_SECRET] = clientSecret;
|
|
65
65
|
}
|
|
66
|
-
return
|
|
66
|
+
return FormUtil.stringify(formParameters);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
}
|
|
@@ -4,7 +4,7 @@ import { RestOperations } from '@celljs/http';
|
|
|
4
4
|
import { ProviderDetailsManager } from '../provider';
|
|
5
5
|
import { AccessTokenResponse, OAuth2ParameterNames, ClientAuthenticationMethod, OAuth2Error, OAuth2AuthorizationError, AccessTokenResponseConverter } from '@celljs/oauth2-core';
|
|
6
6
|
import { AuthorizationGrantRequestEntityUtil } from '../utils';
|
|
7
|
-
import
|
|
7
|
+
import { FormUtil } from '../utils/form-util';
|
|
8
8
|
|
|
9
9
|
@Component()
|
|
10
10
|
export class PasswordTokenResponseClient implements AccessTokenResponseClient<PasswordGrantRequest> {
|
|
@@ -65,7 +65,7 @@ export class PasswordTokenResponseClient implements AccessTokenResponseClient<Pa
|
|
|
65
65
|
formParameters[OAuth2ParameterNames.CLIENT_ID] = clientId;
|
|
66
66
|
formParameters[OAuth2ParameterNames.CLIENT_SECRET] = clientSecret;
|
|
67
67
|
}
|
|
68
|
-
return
|
|
68
|
+
return FormUtil.stringify(formParameters);
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
}
|
|
@@ -4,7 +4,7 @@ import { RestOperations } from '@celljs/http';
|
|
|
4
4
|
import { ProviderDetailsManager } from '../provider';
|
|
5
5
|
import { AccessTokenResponse, OAuth2ParameterNames, ClientAuthenticationMethod, OAuth2Error, OAuth2AuthorizationError, AccessTokenResponseConverter } from '@celljs/oauth2-core';
|
|
6
6
|
import { AuthorizationGrantRequestEntityUtil } from '../utils';
|
|
7
|
-
import
|
|
7
|
+
import { FormUtil } from '../utils/form-util';
|
|
8
8
|
|
|
9
9
|
@Component()
|
|
10
10
|
export class RefreshTokenTokenResponseClient implements AccessTokenResponseClient<RefreshTokenGrantRequest> {
|
|
@@ -71,7 +71,7 @@ export class RefreshTokenTokenResponseClient implements AccessTokenResponseClien
|
|
|
71
71
|
formParameters[OAuth2ParameterNames.CLIENT_ID] = clientId;
|
|
72
72
|
formParameters[OAuth2ParameterNames.CLIENT_SECRET] = clientSecret;
|
|
73
73
|
}
|
|
74
|
-
return
|
|
74
|
+
return FormUtil.stringify(formParameters);
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
}
|
|
@@ -4,7 +4,7 @@ import { Component, Autowired } from '@celljs/core';
|
|
|
4
4
|
import { RestOperations, HttpHeaders, MediaType, HttpMethod } from '@celljs/http';
|
|
5
5
|
import { ProviderDetailsManager } from '../provider';
|
|
6
6
|
import { OAuth2ParameterNames, OAuth2Error, OAuth2AuthorizationError, OAuth2AuthenticationError, AuthenticationMethod } from '@celljs/oauth2-core';
|
|
7
|
-
import
|
|
7
|
+
import { FormUtil } from '../utils/form-util';
|
|
8
8
|
import { AxiosRequestConfig } from 'axios';
|
|
9
9
|
|
|
10
10
|
@Component(UserResponseClient)
|
|
@@ -46,7 +46,7 @@ export class DefaultUserResponseClient implements UserResponseClient<OAuth2UserR
|
|
|
46
46
|
};
|
|
47
47
|
if (httpMethod === HttpMethod.POST) {
|
|
48
48
|
config.headers![HttpHeaders.CONTENT_TYPE] = `${MediaType.APPLICATION_FORM_URLENCODED};charset=UTF-8`;
|
|
49
|
-
config.data =
|
|
49
|
+
config.data = FormUtil.stringify({
|
|
50
50
|
[OAuth2ParameterNames.ACCESS_TOKEN]: accessToken.tokenValue
|
|
51
51
|
});
|
|
52
52
|
} else {
|
|
@@ -3,7 +3,6 @@ import { ErrorHandler, Context, RedirectStrategy } from '@celljs/web/lib/node';
|
|
|
3
3
|
import { Component, Autowired } from '@celljs/core';
|
|
4
4
|
import { OAuth2AuthorizationError, AuthorizationRequest, OAuth2ParameterNames } from '@celljs/oauth2-core';
|
|
5
5
|
import { AuthorizationRequestManager } from '../authorization';
|
|
6
|
-
import { enc } from 'crypto-js';
|
|
7
6
|
|
|
8
7
|
@Component(ErrorHandler)
|
|
9
8
|
export class OAuth2AuthorizationErrorHandler implements ErrorHandler {
|
|
@@ -25,11 +24,11 @@ export class OAuth2AuthorizationErrorHandler implements ErrorHandler {
|
|
|
25
24
|
|
|
26
25
|
let redirectUri = `${authorizationRequest.redirectUri}?${OAuth2ParameterNames.ERROR}=${oauth2Error.errorCode}`;
|
|
27
26
|
if (oauth2Error.description) {
|
|
28
|
-
redirectUri = `${redirectUri}&${OAuth2ParameterNames.ERROR_DESCRIPTION}=${
|
|
27
|
+
redirectUri = `${redirectUri}&${OAuth2ParameterNames.ERROR_DESCRIPTION}=${Buffer.from(oauth2Error.description, 'utf8').toString('base64')}`;
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
if (oauth2Error.uri) {
|
|
32
|
-
redirectUri = `${redirectUri}&${OAuth2ParameterNames.ERROR_URI}=${
|
|
31
|
+
redirectUri = `${redirectUri}&${OAuth2ParameterNames.ERROR_URI}=${Buffer.from(oauth2Error.uri, 'utf8').toString('base64')}`;
|
|
33
32
|
|
|
34
33
|
}
|
|
35
34
|
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Import first in specs that load @celljs/security: some of its modules read `cell.security` of the
|
|
3
|
+
* configuration while they load, which the generated configuration provides in an application.
|
|
4
|
+
*/
|
|
5
|
+
(globalThis as Record<string, any>).cellProps ??= { cell: { security: {} } };
|
|
6
|
+
export {};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ClientAuthenticationMethod } from '@celljs/oauth2-core';
|
|
2
2
|
import { ClientRegistration } from '../registration';
|
|
3
3
|
import { MediaType, HttpHeaders } from '@celljs/http';
|
|
4
|
-
import { enc } from 'crypto-js';
|
|
5
4
|
|
|
6
5
|
export namespace AuthorizationGrantRequestEntityUtil {
|
|
7
6
|
|
|
@@ -10,8 +9,7 @@ export namespace AuthorizationGrantRequestEntityUtil {
|
|
|
10
9
|
const { clientAuthenticationMethod, clientId, clientSecret } = clientRegistration;
|
|
11
10
|
if (clientAuthenticationMethod === ClientAuthenticationMethod.Basic) {
|
|
12
11
|
const credentialsStr = `${clientId}:${clientSecret}`;
|
|
13
|
-
|
|
14
|
-
headers[HttpHeaders.AUTHORIZATION] = `${clientAuthenticationMethod} ${enc.Base64.stringify(encoded)}`;
|
|
12
|
+
headers[HttpHeaders.AUTHORIZATION] = `${clientAuthenticationMethod} ${Buffer.from(credentialsStr, 'latin1').toString('base64')}`;
|
|
15
13
|
}
|
|
16
14
|
return headers;
|
|
17
15
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { FormUtil } from './form-util';
|
|
4
|
+
|
|
5
|
+
/** The expected strings are what qs.stringify (6.17) produced for the same parameters. */
|
|
6
|
+
describe('FormUtil.stringify', () => {
|
|
7
|
+
it('encodes like qs.stringify and leaves undefined values out', () => {
|
|
8
|
+
expect(FormUtil.stringify({ grant_type: 'authorization_code', code: 'a/b+c=d', redirect_uri: 'https://x.test/cb?x=1&y=2', client_id: 'id', client_secret: undefined }))
|
|
9
|
+
.to.equal('grant_type=authorization_code&code=a%2Fb%2Bc%3Dd&redirect_uri=https%3A%2F%2Fx.test%2Fcb%3Fx%3D1%26y%3D2&client_id=id');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('encodes spaces as %20, the RFC 3986 sub-delimiters, and null as an empty value', () => {
|
|
13
|
+
expect(FormUtil.stringify({ scope: 'openid profile email', state: "abc-_~.*'()!", empty: '', nil: null }))
|
|
14
|
+
.to.equal('scope=openid%20profile%20email&state=abc-_~.%2A%27%28%29%21&empty=&nil=');
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('encodes UTF-8', () => {
|
|
18
|
+
expect(FormUtil.stringify({ text: '中文 ✓' })).to.equal('text=%E4%B8%AD%E6%96%87%20%E2%9C%93');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('decodes back to the parameters with URLSearchParams', () => {
|
|
22
|
+
const parameters = { scope: 'a b', redirect_uri: 'https://x.test/?q=1&r=2', text: '中文' };
|
|
23
|
+
expect(Object.fromEntries(new URLSearchParams(FormUtil.stringify(parameters)))).to.deep.equal(parameters);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** RFC 3986 percent-encoding: encodeURIComponent, plus the sub-delimiters it leaves as they are. */
|
|
2
|
+
function encode(text: string): string {
|
|
3
|
+
return encodeURIComponent(text).replace(/[!'()*]/g, c => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export namespace FormUtil {
|
|
7
|
+
/**
|
|
8
|
+
* `application/x-www-form-urlencoded` of flat parameters, as qs.stringify wrote it: spaces as
|
|
9
|
+
* `%20`, undefined values left out, null as an empty value.
|
|
10
|
+
*/
|
|
11
|
+
export function stringify(parameters: Record<string, unknown>): string {
|
|
12
|
+
return Object.entries(parameters)
|
|
13
|
+
.filter(([, value]) => value !== undefined)
|
|
14
|
+
.map(([key, value]) => `${encode(key)}=${value === null ? '' : encode(String(value))}`)
|
|
15
|
+
.join('&');
|
|
16
|
+
}
|
|
17
|
+
}
|
package/src/node/utils/index.ts
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
export namespace OidcNonceUtil {
|
|
4
|
+
/** What the authorization request sends as `nonce` for a stored nonce, and the ID Token must carry back. */
|
|
5
|
+
export function hash(nonce: string): string {
|
|
6
|
+
return createHash('sha256').update(nonce).digest('base64url');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Whether `claim` (the `nonce` of an ID Token) belongs to the stored `nonce`; constant time. */
|
|
10
|
+
export function matches(nonce: string, claim: unknown): boolean {
|
|
11
|
+
if (typeof claim !== 'string') {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
const expected = Buffer.from(hash(nonce));
|
|
15
|
+
const actual = Buffer.from(claim);
|
|
16
|
+
return actual.length === expected.length && timingSafeEqual(actual, expected);
|
|
17
|
+
}
|
|
18
|
+
}
|