@celljs/oauth2-client 3.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/LICENSE +21 -0
- package/README.md +1 -0
- package/cell.yml +57 -0
- package/lib/node/authentication/authentication-protocol.d.ts +25 -0
- package/lib/node/authentication/authentication-protocol.d.ts.map +1 -0
- package/lib/node/authentication/authentication-protocol.js +11 -0
- package/lib/node/authentication/authentication-protocol.js.map +1 -0
- package/lib/node/authentication/index.d.ts +4 -0
- package/lib/node/authentication/index.d.ts.map +1 -0
- package/lib/node/authentication/index.js +20 -0
- package/lib/node/authentication/index.js.map +1 -0
- package/lib/node/authentication/oauth2-authentication-provider.d.ts +30 -0
- package/lib/node/authentication/oauth2-authentication-provider.d.ts.map +1 -0
- package/lib/node/authentication/oauth2-authentication-provider.js +170 -0
- package/lib/node/authentication/oauth2-authentication-provider.js.map +1 -0
- package/lib/node/authentication/oidc-authentication-provider.d.ts +18 -0
- package/lib/node/authentication/oidc-authentication-provider.d.ts.map +1 -0
- package/lib/node/authentication/oidc-authentication-provider.js +74 -0
- package/lib/node/authentication/oidc-authentication-provider.js.map +1 -0
- package/lib/node/authorization/authorization-code-authorized-client-provider.d.ts +5 -0
- package/lib/node/authorization/authorization-code-authorized-client-provider.d.ts.map +1 -0
- package/lib/node/authorization/authorization-code-authorized-client-provider.js +27 -0
- package/lib/node/authorization/authorization-code-authorized-client-provider.js.map +1 -0
- package/lib/node/authorization/authorization-code-grant-middleware.d.ts +26 -0
- package/lib/node/authorization/authorization-code-grant-middleware.d.ts.map +1 -0
- package/lib/node/authorization/authorization-code-grant-middleware.js +129 -0
- package/lib/node/authorization/authorization-code-grant-middleware.js.map +1 -0
- package/lib/node/authorization/authorization-failure-handler.d.ts +10 -0
- package/lib/node/authorization/authorization-failure-handler.d.ts.map +1 -0
- package/lib/node/authorization/authorization-failure-handler.js +35 -0
- package/lib/node/authorization/authorization-failure-handler.js.map +1 -0
- package/lib/node/authorization/authorization-protocol.d.ts +78 -0
- package/lib/node/authorization/authorization-protocol.d.ts.map +1 -0
- package/lib/node/authorization/authorization-protocol.js +34 -0
- package/lib/node/authorization/authorization-protocol.js.map +1 -0
- package/lib/node/authorization/authorization-request-manager.d.ts +13 -0
- package/lib/node/authorization/authorization-request-manager.d.ts.map +1 -0
- package/lib/node/authorization/authorization-request-manager.js +64 -0
- package/lib/node/authorization/authorization-request-manager.js.map +1 -0
- package/lib/node/authorization/authorization-request-redirect-middleware.d.ts +20 -0
- package/lib/node/authorization/authorization-request-redirect-middleware.d.ts.map +1 -0
- package/lib/node/authorization/authorization-request-redirect-middleware.js +113 -0
- package/lib/node/authorization/authorization-request-redirect-middleware.js.map +1 -0
- package/lib/node/authorization/authorization-request-resolver.d.ts +38 -0
- package/lib/node/authorization/authorization-request-resolver.d.ts.map +1 -0
- package/lib/node/authorization/authorization-request-resolver.js +155 -0
- package/lib/node/authorization/authorization-request-resolver.js.map +1 -0
- package/lib/node/authorization/authorization-success-handler.d.ts +7 -0
- package/lib/node/authorization/authorization-success-handler.d.ts.map +1 -0
- package/lib/node/authorization/authorization-success-handler.js +28 -0
- package/lib/node/authorization/authorization-success-handler.js.map +1 -0
- package/lib/node/authorization/authorized-client-manager.d.ts +11 -0
- package/lib/node/authorization/authorized-client-manager.d.ts.map +1 -0
- package/lib/node/authorization/authorized-client-manager.js +48 -0
- package/lib/node/authorization/authorized-client-manager.js.map +1 -0
- package/lib/node/authorization/authorized-client-service.d.ts +12 -0
- package/lib/node/authorization/authorized-client-service.d.ts.map +1 -0
- package/lib/node/authorization/authorized-client-service.js +87 -0
- package/lib/node/authorization/authorized-client-service.js.map +1 -0
- package/lib/node/authorization/client-credentials-authorized-client-provider.d.ts +12 -0
- package/lib/node/authorization/client-credentials-authorized-client-provider.d.ts.map +1 -0
- package/lib/node/authorization/client-credentials-authorized-client-provider.js +72 -0
- package/lib/node/authorization/client-credentials-authorized-client-provider.js.map +1 -0
- package/lib/node/authorization/context-attributes-mapper.d.ts +5 -0
- package/lib/node/authorization/context-attributes-mapper.d.ts.map +1 -0
- package/lib/node/authorization/context-attributes-mapper.js +28 -0
- package/lib/node/authorization/context-attributes-mapper.js.map +1 -0
- package/lib/node/authorization/index.d.ts +14 -0
- package/lib/node/authorization/index.d.ts.map +1 -0
- package/lib/node/authorization/index.js +30 -0
- package/lib/node/authorization/index.js.map +1 -0
- package/lib/node/authorization/password-authorized-client-provider.d.ts +12 -0
- package/lib/node/authorization/password-authorized-client-provider.d.ts.map +1 -0
- package/lib/node/authorization/password-authorized-client-provider.js +85 -0
- package/lib/node/authorization/password-authorized-client-provider.js.map +1 -0
- package/lib/node/authorization/refresh-token-authorized-client-provider.d.ts +12 -0
- package/lib/node/authorization/refresh-token-authorized-client-provider.d.ts.map +1 -0
- package/lib/node/authorization/refresh-token-authorized-client-provider.js +68 -0
- package/lib/node/authorization/refresh-token-authorized-client-provider.js.map +1 -0
- package/lib/node/constants.d.ts +5 -0
- package/lib/node/constants.d.ts.map +1 -0
- package/lib/node/constants.js +8 -0
- package/lib/node/constants.js.map +1 -0
- package/lib/node/endpoint/authorization-code-token-response-client.d.ts +12 -0
- package/lib/node/endpoint/authorization-code-token-response-client.d.ts.map +1 -0
- package/lib/node/endpoint/authorization-code-token-response-client.js +83 -0
- package/lib/node/endpoint/authorization-code-token-response-client.js.map +1 -0
- package/lib/node/endpoint/client-credentials-token-response-client.d.ts +12 -0
- package/lib/node/endpoint/client-credentials-token-response-client.d.ts.map +1 -0
- package/lib/node/endpoint/client-credentials-token-response-client.js +80 -0
- package/lib/node/endpoint/client-credentials-token-response-client.js.map +1 -0
- package/lib/node/endpoint/endpoint-protocol.d.ts +48 -0
- package/lib/node/endpoint/endpoint-protocol.d.ts.map +1 -0
- package/lib/node/endpoint/endpoint-protocol.js +9 -0
- package/lib/node/endpoint/endpoint-protocol.js.map +1 -0
- package/lib/node/endpoint/index.d.ts +7 -0
- package/lib/node/endpoint/index.d.ts.map +1 -0
- package/lib/node/endpoint/index.js +23 -0
- package/lib/node/endpoint/index.js.map +1 -0
- package/lib/node/endpoint/password-token-response-client.d.ts +12 -0
- package/lib/node/endpoint/password-token-response-client.d.ts.map +1 -0
- package/lib/node/endpoint/password-token-response-client.js +82 -0
- package/lib/node/endpoint/password-token-response-client.js.map +1 -0
- package/lib/node/endpoint/refresh-token-token-response-client.d.ts +12 -0
- package/lib/node/endpoint/refresh-token-token-response-client.d.ts.map +1 -0
- package/lib/node/endpoint/refresh-token-token-response-client.js +87 -0
- package/lib/node/endpoint/refresh-token-token-response-client.js.map +1 -0
- package/lib/node/endpoint/user-response-client.d.ts +9 -0
- package/lib/node/endpoint/user-response-client.d.ts.map +1 -0
- package/lib/node/endpoint/user-response-client.js +84 -0
- package/lib/node/endpoint/user-response-client.js.map +1 -0
- package/lib/node/error/error-hander.d.ts +11 -0
- package/lib/node/error/error-hander.d.ts.map +1 -0
- package/lib/node/error/error-hander.js +52 -0
- package/lib/node/error/error-hander.js.map +1 -0
- package/lib/node/error/error-protocol.d.ts +3 -0
- package/lib/node/error/error-protocol.d.ts.map +1 -0
- package/lib/node/error/error-protocol.js +7 -0
- package/lib/node/error/error-protocol.js.map +1 -0
- package/lib/node/error/error.d.ts +10 -0
- package/lib/node/error/error.d.ts.map +1 -0
- package/lib/node/error/error.js +23 -0
- package/lib/node/error/error.js.map +1 -0
- package/lib/node/error/index.d.ts +4 -0
- package/lib/node/error/index.d.ts.map +1 -0
- package/lib/node/error/index.js +20 -0
- package/lib/node/error/index.js.map +1 -0
- package/lib/node/index.d.ts +8 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +24 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/module.d.ts +4 -0
- package/lib/node/module.d.ts.map +1 -0
- package/lib/node/module.js +6 -0
- package/lib/node/module.js.map +1 -0
- package/lib/node/provider/index.d.ts +3 -0
- package/lib/node/provider/index.d.ts.map +1 -0
- package/lib/node/provider/index.js +19 -0
- package/lib/node/provider/index.js.map +1 -0
- package/lib/node/provider/provider-details-manager.d.ts +12 -0
- package/lib/node/provider/provider-details-manager.d.ts.map +1 -0
- package/lib/node/provider/provider-details-manager.js +50 -0
- package/lib/node/provider/provider-details-manager.js.map +1 -0
- package/lib/node/provider/provider-protocol.d.ts +21 -0
- package/lib/node/provider/provider-protocol.d.ts.map +1 -0
- package/lib/node/provider/provider-protocol.js +5 -0
- package/lib/node/provider/provider-protocol.js.map +1 -0
- package/lib/node/registration/id-token-jwt-decoder-facatory.d.ts +15 -0
- package/lib/node/registration/id-token-jwt-decoder-facatory.d.ts.map +1 -0
- package/lib/node/registration/id-token-jwt-decoder-facatory.js +89 -0
- package/lib/node/registration/id-token-jwt-decoder-facatory.js.map +1 -0
- package/lib/node/registration/index.d.ts +5 -0
- package/lib/node/registration/index.d.ts.map +1 -0
- package/lib/node/registration/index.js +21 -0
- package/lib/node/registration/index.js.map +1 -0
- package/lib/node/registration/jws-algorithm-resolver.d.ts +5 -0
- package/lib/node/registration/jws-algorithm-resolver.d.ts.map +1 -0
- package/lib/node/registration/jws-algorithm-resolver.js +22 -0
- package/lib/node/registration/jws-algorithm-resolver.js.map +1 -0
- package/lib/node/registration/registration-manager.d.ts +15 -0
- package/lib/node/registration/registration-manager.d.ts.map +1 -0
- package/lib/node/registration/registration-manager.js +64 -0
- package/lib/node/registration/registration-manager.js.map +1 -0
- package/lib/node/registration/registration-protocol.d.ts +22 -0
- package/lib/node/registration/registration-protocol.d.ts.map +1 -0
- package/lib/node/registration/registration-protocol.js +7 -0
- package/lib/node/registration/registration-protocol.js.map +1 -0
- package/lib/node/user/index.d.ts +4 -0
- package/lib/node/user/index.d.ts.map +1 -0
- package/lib/node/user/index.js +20 -0
- package/lib/node/user/index.js.map +1 -0
- package/lib/node/user/oauth2-user-service.d.ts +10 -0
- package/lib/node/user/oauth2-user-service.d.ts.map +1 -0
- package/lib/node/user/oauth2-user-service.js +61 -0
- package/lib/node/user/oauth2-user-service.js.map +1 -0
- package/lib/node/user/oidc-user-service.d.ts +13 -0
- package/lib/node/user/oidc-user-service.d.ts.map +1 -0
- package/lib/node/user/oidc-user-service.js +98 -0
- package/lib/node/user/oidc-user-service.js.map +1 -0
- package/lib/node/user/user-protocol.d.ts +6 -0
- package/lib/node/user/user-protocol.d.ts.map +1 -0
- package/lib/node/user/user-protocol.js +3 -0
- package/lib/node/user/user-protocol.js.map +1 -0
- package/lib/node/utils/authorization-grant-request-entity-util.d.ts +10 -0
- package/lib/node/utils/authorization-grant-request-entity-util.d.ts.map +1 -0
- package/lib/node/utils/authorization-grant-request-entity-util.js +28 -0
- package/lib/node/utils/authorization-grant-request-entity-util.js.map +1 -0
- package/lib/node/utils/authorization-response-util.d.ts +8 -0
- package/lib/node/utils/authorization-response-util.d.ts.map +1 -0
- package/lib/node/utils/authorization-response-util.js +53 -0
- package/lib/node/utils/authorization-response-util.js.map +1 -0
- package/lib/node/utils/index.d.ts +3 -0
- package/lib/node/utils/index.d.ts.map +1 -0
- package/lib/node/utils/index.js +19 -0
- package/lib/node/utils/index.js.map +1 -0
- package/lib/package.spec.d.ts +1 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +4 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +51 -0
- package/src/node/authentication/authentication-protocol.ts +26 -0
- package/src/node/authentication/index.ts +3 -0
- package/src/node/authentication/oauth2-authentication-provider.ts +163 -0
- package/src/node/authentication/oidc-authentication-provider.ts +60 -0
- package/src/node/authorization/authorization-code-authorized-client-provider.ts +16 -0
- package/src/node/authorization/authorization-code-grant-middleware.ts +110 -0
- package/src/node/authorization/authorization-failure-handler.ts +23 -0
- package/src/node/authorization/authorization-protocol.ts +104 -0
- package/src/node/authorization/authorization-request-manager.ts +54 -0
- package/src/node/authorization/authorization-request-redirect-middleware.ts +91 -0
- package/src/node/authorization/authorization-request-resolver.ts +149 -0
- package/src/node/authorization/authorization-success-handler.ts +15 -0
- package/src/node/authorization/authorized-client-manager.ts +35 -0
- package/src/node/authorization/authorized-client-service.ts +73 -0
- package/src/node/authorization/client-credentials-authorized-client-provider.ts +61 -0
- package/src/node/authorization/context-attributes-mapper.ts +18 -0
- package/src/node/authorization/index.ts +13 -0
- package/src/node/authorization/password-authorized-client-provider.ts +78 -0
- package/src/node/authorization/refresh-token-authorized-client-provider.ts +58 -0
- package/src/node/constants.ts +4 -0
- package/src/node/endpoint/authorization-code-token-response-client.ts +73 -0
- package/src/node/endpoint/client-credentials-token-response-client.ts +69 -0
- package/src/node/endpoint/endpoint-protocol.ts +55 -0
- package/src/node/endpoint/index.ts +6 -0
- package/src/node/endpoint/password-token-response-client.ts +71 -0
- package/src/node/endpoint/refresh-token-token-response-client.ts +77 -0
- package/src/node/endpoint/user-response-client.ts +72 -0
- package/src/node/error/error-hander.ts +39 -0
- package/src/node/error/error-protocol.ts +6 -0
- package/src/node/error/error.ts +19 -0
- package/src/node/error/index.ts +3 -0
- package/src/node/index.ts +7 -0
- package/src/node/module.ts +4 -0
- package/src/node/provider/index.ts +2 -0
- package/src/node/provider/provider-details-manager.ts +33 -0
- package/src/node/provider/provider-protocol.ts +24 -0
- package/src/node/registration/id-token-jwt-decoder-facatory.ts +71 -0
- package/src/node/registration/index.ts +4 -0
- package/src/node/registration/jws-algorithm-resolver.ts +12 -0
- package/src/node/registration/registration-manager.ts +47 -0
- package/src/node/registration/registration-protocol.ts +27 -0
- package/src/node/user/index.ts +3 -0
- package/src/node/user/oauth2-user-service.ts +50 -0
- package/src/node/user/oidc-user-service.ts +92 -0
- package/src/node/user/user-protocol.ts +6 -0
- package/src/node/utils/authorization-grant-request-entity-util.ts +26 -0
- package/src/node/utils/authorization-response-util.ts +50 -0
- package/src/node/utils/index.ts +2 -0
- package/src/package.spec.ts +4 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { AuthorizedClientProvider, AuthorizationContext, AuthorizedClient } from './authorization-protocol';
|
|
2
|
+
import { AuthorizationGrantType, OAuth2AuthorizationError, AccessToken } from '@celljs/oauth2-core';
|
|
3
|
+
import { Component, Autowired, Value } from '@celljs/core';
|
|
4
|
+
import { ClientCredentialsTokenResponseClient } from '../endpoint';
|
|
5
|
+
import { ClientRegistration } from '../registration';
|
|
6
|
+
import { ClientAuthorizationError } from '../error';
|
|
7
|
+
|
|
8
|
+
@Component(AuthorizedClientProvider)
|
|
9
|
+
export class ClientCredentialsAuthorizedClientProvider implements AuthorizedClientProvider {
|
|
10
|
+
|
|
11
|
+
@Value('cell.oauth2.client.clockSkew')
|
|
12
|
+
protected readonly clockSkew: number;
|
|
13
|
+
|
|
14
|
+
@Autowired(ClientCredentialsTokenResponseClient)
|
|
15
|
+
protected readonly accessTokenResponseClient: ClientCredentialsTokenResponseClient;
|
|
16
|
+
|
|
17
|
+
async provide(context: AuthorizationContext): Promise<AuthorizedClient | undefined> {
|
|
18
|
+
const { clientRegistration, authorizedClient } = context;
|
|
19
|
+
if (AuthorizationGrantType.ClientCredentials !== clientRegistration.authorizationGrantType) {
|
|
20
|
+
return ;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (authorizedClient && !this.hasTokenExpired(authorizedClient.accessToken)) {
|
|
24
|
+
// If client is already authorized but access token is NOT expired than no
|
|
25
|
+
// need for re-authorization
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// As per spec, in section 4.4.3 Access Token Response
|
|
30
|
+
// https://tools.ietf.org/html/rfc6749#section-4.4.3
|
|
31
|
+
// A refresh token SHOULD NOT be included.
|
|
32
|
+
//
|
|
33
|
+
// Therefore, renewing an expired access token (re-authorization)
|
|
34
|
+
// is the same as acquiring a new access token (authorization).
|
|
35
|
+
const tokenResponse = await this.getTokenResponse(clientRegistration);
|
|
36
|
+
return {
|
|
37
|
+
clientRegistration,
|
|
38
|
+
principalName: context.principal.name,
|
|
39
|
+
accessToken: tokenResponse.accessToken
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
hasTokenExpired(accessToken: AccessToken) {
|
|
44
|
+
return accessToken.expiresAt - Date.now() < this.clockSkew;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected getTokenResponse(clientRegistration: ClientRegistration) {
|
|
48
|
+
try {
|
|
49
|
+
return this.accessTokenResponseClient.getTokenResponse({
|
|
50
|
+
authorizationGrantType: AuthorizationGrantType.ClientCredentials,
|
|
51
|
+
clientRegistration
|
|
52
|
+
});
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error instanceof OAuth2AuthorizationError) {
|
|
55
|
+
throw new ClientAuthorizationError(error.oauth2Error, clientRegistration.registrationId, error);
|
|
56
|
+
}
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ContextAttributesMapper, AuthorizeRequest, AuthorizationContext } from './authorization-protocol';
|
|
2
|
+
import { Component } from '@celljs/core';
|
|
3
|
+
import { Context, AttributeScope } from '@celljs/web/lib/node';
|
|
4
|
+
import { OAuth2ParameterNames } from '@celljs/oauth2-core';
|
|
5
|
+
|
|
6
|
+
@Component(ContextAttributesMapper)
|
|
7
|
+
export class DefaultContextAttributesMapper implements ContextAttributesMapper {
|
|
8
|
+
|
|
9
|
+
async apply(authorizeRequest: AuthorizeRequest): Promise<Map<string, any>> {
|
|
10
|
+
const contextAttributes = new Map<string, any>();
|
|
11
|
+
const scopes = Context.getAttr<string>(OAuth2ParameterNames.SCOPE, AttributeScope.Request);
|
|
12
|
+
if (scopes) {
|
|
13
|
+
contextAttributes.set(AuthorizationContext.REQUEST_SCOPE_ATTRIBUTE_NAME, scopes.split(' '));
|
|
14
|
+
}
|
|
15
|
+
return contextAttributes;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './authorization-protocol';
|
|
2
|
+
export * from './authorization-request-resolver';
|
|
3
|
+
export * from './authorized-client-manager';
|
|
4
|
+
export * from './authorization-request-manager';
|
|
5
|
+
export * from './authorization-request-redirect-middleware';
|
|
6
|
+
export * from './authorization-code-grant-middleware';
|
|
7
|
+
export * from './authorization-code-authorized-client-provider';
|
|
8
|
+
export * from './client-credentials-authorized-client-provider';
|
|
9
|
+
export * from './password-authorized-client-provider';
|
|
10
|
+
export * from './refresh-token-authorized-client-provider';
|
|
11
|
+
export * from './context-attributes-mapper';
|
|
12
|
+
export * from './authorization-failure-handler';
|
|
13
|
+
export * from './authorization-success-handler';
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { AuthorizedClientProvider, AuthorizationContext, AuthorizedClient } from './authorization-protocol';
|
|
2
|
+
import { AuthorizationGrantType, OAuth2AuthorizationError, AccessToken } from '@celljs/oauth2-core';
|
|
3
|
+
import { Component, Autowired, Value } from '@celljs/core';
|
|
4
|
+
import { ClientRegistration } from '../registration';
|
|
5
|
+
import { ClientAuthorizationError } from '../error';
|
|
6
|
+
import { PasswordTokenResponseClient } from '../endpoint/password-token-response-client';
|
|
7
|
+
|
|
8
|
+
@Component(AuthorizedClientProvider)
|
|
9
|
+
export class PasswordAuthorizedClientProvider implements AuthorizedClientProvider {
|
|
10
|
+
|
|
11
|
+
@Value('cell.oauth2.client.clockSkew')
|
|
12
|
+
protected readonly clockSkew: number;
|
|
13
|
+
|
|
14
|
+
@Autowired(PasswordTokenResponseClient)
|
|
15
|
+
protected readonly accessTokenResponseClient: PasswordTokenResponseClient;
|
|
16
|
+
|
|
17
|
+
async provide(context: AuthorizationContext): Promise<AuthorizedClient | undefined> {
|
|
18
|
+
const { clientRegistration, authorizedClient, attributes } = context;
|
|
19
|
+
if (AuthorizationGrantType.Password !== clientRegistration.authorizationGrantType) {
|
|
20
|
+
return ;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const username = attributes.get(AuthorizationContext.USERNAME_ATTRIBUTE_NAME);
|
|
24
|
+
const password = attributes.get(AuthorizationContext.PASSWORD_ATTRIBUTE_NAME);
|
|
25
|
+
|
|
26
|
+
if (!username || !password) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (authorizedClient && !this.hasTokenExpired(authorizedClient.accessToken)) {
|
|
31
|
+
// If client is already authorized but access token is NOT expired than no
|
|
32
|
+
// need for re-authorization
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (authorizedClient && this.hasTokenExpired(authorizedClient.accessToken) && authorizedClient.refreshToken) {
|
|
37
|
+
// If client is already authorized and access token is expired and a refresh
|
|
38
|
+
// token is available, than return and allow
|
|
39
|
+
// RefreshTokenAuthorizedClientProvider to handle the refresh
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// As per spec, in section 4.4.3 Access Token Response
|
|
44
|
+
// https://tools.ietf.org/html/rfc6749#section-4.4.3
|
|
45
|
+
// A refresh token SHOULD NOT be included.
|
|
46
|
+
//
|
|
47
|
+
// Therefore, renewing an expired access token (re-authorization)
|
|
48
|
+
// is the same as acquiring a new access token (authorization).
|
|
49
|
+
const tokenResponse = await this.getTokenResponse(clientRegistration, username, password);
|
|
50
|
+
return {
|
|
51
|
+
clientRegistration,
|
|
52
|
+
principalName: context.principal.name,
|
|
53
|
+
accessToken: tokenResponse.accessToken
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
hasTokenExpired(accessToken: AccessToken) {
|
|
58
|
+
return accessToken.expiresAt - Date.now() < this.clockSkew;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
protected getTokenResponse(clientRegistration: ClientRegistration, username: string, password: string) {
|
|
62
|
+
try {
|
|
63
|
+
return this.accessTokenResponseClient.getTokenResponse({
|
|
64
|
+
authorizationGrantType: AuthorizationGrantType.Password,
|
|
65
|
+
clientRegistration,
|
|
66
|
+
username,
|
|
67
|
+
password
|
|
68
|
+
|
|
69
|
+
});
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (error instanceof OAuth2AuthorizationError) {
|
|
72
|
+
throw new ClientAuthorizationError(error.oauth2Error, clientRegistration.registrationId, error);
|
|
73
|
+
}
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { AuthorizedClientProvider, AuthorizationContext, AuthorizedClient } from './authorization-protocol';
|
|
2
|
+
import { AuthorizationGrantType, OAuth2AuthorizationError, AccessToken } from '@celljs/oauth2-core';
|
|
3
|
+
import { Component, Autowired, Value } from '@celljs/core';
|
|
4
|
+
import { RefreshTokenTokenResponseClient } from '../endpoint';
|
|
5
|
+
import { ClientRegistration } from '../registration';
|
|
6
|
+
import { ClientAuthorizationError } from '../error';
|
|
7
|
+
import { ok } from 'assert';
|
|
8
|
+
|
|
9
|
+
@Component(AuthorizedClientProvider)
|
|
10
|
+
export class RefreshTokenAuthorizedClientProvider implements AuthorizedClientProvider {
|
|
11
|
+
|
|
12
|
+
@Value('cell.oauth2.client.clockSkew')
|
|
13
|
+
protected readonly clockSkew: number;
|
|
14
|
+
|
|
15
|
+
@Autowired(RefreshTokenTokenResponseClient)
|
|
16
|
+
protected readonly accessTokenResponseClient: RefreshTokenTokenResponseClient;
|
|
17
|
+
|
|
18
|
+
async provide(context: AuthorizationContext): Promise<AuthorizedClient | undefined> {
|
|
19
|
+
const { clientRegistration, authorizedClient, attributes } = context;
|
|
20
|
+
if (!authorizedClient || !authorizedClient.refreshToken || !this.hasTokenExpired(authorizedClient.accessToken)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const scopes = attributes.get(AuthorizationContext.REQUEST_SCOPE_ATTRIBUTE_NAME);
|
|
25
|
+
|
|
26
|
+
ok(scopes || Array.isArray(scopes), `The context attribute must be of type Array '${AuthorizationContext.REQUEST_SCOPE_ATTRIBUTE_NAME}'`);
|
|
27
|
+
|
|
28
|
+
const tokenResponse = await this.getTokenResponse(clientRegistration, authorizedClient, scopes);
|
|
29
|
+
return {
|
|
30
|
+
clientRegistration,
|
|
31
|
+
principalName: context.principal.name,
|
|
32
|
+
accessToken: tokenResponse.accessToken,
|
|
33
|
+
refreshToken: tokenResponse.refreshToken
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
hasTokenExpired(accessToken: AccessToken) {
|
|
38
|
+
return accessToken.expiresAt - Date.now() < this.clockSkew;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
protected getTokenResponse(clientRegistration: ClientRegistration, authorizedClient: AuthorizedClient, scopes?: string[]) {
|
|
42
|
+
try {
|
|
43
|
+
return this.accessTokenResponseClient.getTokenResponse({
|
|
44
|
+
authorizationGrantType: AuthorizationGrantType.RefreshToken,
|
|
45
|
+
clientRegistration,
|
|
46
|
+
accessToken: authorizedClient.accessToken,
|
|
47
|
+
refreshToken: authorizedClient.refreshToken!,
|
|
48
|
+
scopes
|
|
49
|
+
});
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof OAuth2AuthorizationError) {
|
|
52
|
+
throw new ClientAuthorizationError(error.oauth2Error, clientRegistration.registrationId, error);
|
|
53
|
+
}
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export const AUTHORIZATION_REQUEST_BASE_URI = 'cell.oauth2.client.authorizationRequestBaseUri';
|
|
2
|
+
export const DEFAULT_REDIRECT_URI = 'cell.oauth2.client.defaultRedirectUri';
|
|
3
|
+
export const AUTHORIZATION_CODE_GRANT_URI = 'cell.oauth2.client.authorizationCodeGrantUri';
|
|
4
|
+
export const REDIRECT_LOGIN_PATH = 'cell.oauth2.client.redirectLoginPath';
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { AccessTokenResponseClient, AuthorizationCodeGrantRequest, INVALID_TOKEN_RESPONSE_ERROR_CODE } from './endpoint-protocol';
|
|
2
|
+
import { Component, Autowired } from '@celljs/core';
|
|
3
|
+
import { RestOperations } from '@celljs/http';
|
|
4
|
+
import { ProviderDetailsManager } from '../provider';
|
|
5
|
+
import { AccessTokenResponse, OAuth2ParameterNames, PkceParameterNames, ClientAuthenticationMethod, OAuth2Error, OAuth2AuthorizationError,
|
|
6
|
+
AccessTokenResponseConverter } from '@celljs/oauth2-core';
|
|
7
|
+
import { AuthorizationGrantRequestEntityUtil } from '../utils';
|
|
8
|
+
import * as qs from 'qs';
|
|
9
|
+
|
|
10
|
+
@Component()
|
|
11
|
+
export class AuthorizationCodeTokenResponseClient implements AccessTokenResponseClient<AuthorizationCodeGrantRequest> {
|
|
12
|
+
|
|
13
|
+
@Autowired(RestOperations)
|
|
14
|
+
protected readonly restOperations: RestOperations;
|
|
15
|
+
|
|
16
|
+
@Autowired(ProviderDetailsManager)
|
|
17
|
+
protected readonly providerDetailsManager: ProviderDetailsManager;
|
|
18
|
+
|
|
19
|
+
@Autowired(AccessTokenResponseConverter)
|
|
20
|
+
protected readonly accessTokenResponseConverter: AccessTokenResponseConverter;
|
|
21
|
+
|
|
22
|
+
async getTokenResponse(authorizationCodeGrantRequest: AuthorizationCodeGrantRequest): Promise<AccessTokenResponse> {
|
|
23
|
+
const { clientRegistration } = authorizationCodeGrantRequest;
|
|
24
|
+
const providerDetails = await this.providerDetailsManager.get(clientRegistration.provider);
|
|
25
|
+
|
|
26
|
+
let response;
|
|
27
|
+
try {
|
|
28
|
+
response = await this.restOperations.post<{ [key: string]: string }>(providerDetails!.tokenUri, this.buildFormParameters(authorizationCodeGrantRequest), {
|
|
29
|
+
headers: AuthorizationGrantRequestEntityUtil.getTokenRequestHeaders(clientRegistration)
|
|
30
|
+
});
|
|
31
|
+
} catch (error) {
|
|
32
|
+
const oauth2Error = <OAuth2Error>{
|
|
33
|
+
errorCode: INVALID_TOKEN_RESPONSE_ERROR_CODE,
|
|
34
|
+
description: `An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: ${error?.message || error}`
|
|
35
|
+
};
|
|
36
|
+
throw new OAuth2AuthorizationError(oauth2Error, error);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const { data } = response;
|
|
40
|
+
|
|
41
|
+
const acccessTokenResponse = this.accessTokenResponseConverter.convert(data);
|
|
42
|
+
|
|
43
|
+
if (!acccessTokenResponse.accessToken.scopes || acccessTokenResponse.accessToken.scopes.length === 0) {
|
|
44
|
+
// As per spec, in Section 5.1 Successful Access Token Response
|
|
45
|
+
// https://tools.ietf.org/html/rfc6749#section-5.1
|
|
46
|
+
// If AccessTokenResponse.scope is empty, then default to the scope
|
|
47
|
+
// originally requested by the client in the Token Request
|
|
48
|
+
acccessTokenResponse.accessToken.scopes = clientRegistration.scopes;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return acccessTokenResponse;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected buildFormParameters(authorizationCodeGrantRequest: AuthorizationCodeGrantRequest) {
|
|
55
|
+
const { authorizationExchange, clientRegistration, authorizationGrantType } = authorizationCodeGrantRequest;
|
|
56
|
+
const { authorizationRequest, authorizationResponse } = authorizationExchange;
|
|
57
|
+
const formParameters: { [key: string]: any } = {
|
|
58
|
+
[OAuth2ParameterNames.GRANT_TYPE]: authorizationGrantType,
|
|
59
|
+
[OAuth2ParameterNames.CODE]: authorizationResponse.code,
|
|
60
|
+
[OAuth2ParameterNames.REDIRECT_URI]: authorizationRequest.redirectUri,
|
|
61
|
+
[PkceParameterNames.CODE_VERIFIER]: authorizationRequest.attributes[PkceParameterNames.CODE_VERIFIER]
|
|
62
|
+
};
|
|
63
|
+
const { clientAuthenticationMethod, clientId, clientSecret } = clientRegistration;
|
|
64
|
+
if (ClientAuthenticationMethod.Basic !== clientAuthenticationMethod) {
|
|
65
|
+
formParameters[OAuth2ParameterNames.CLIENT_ID] = clientId;
|
|
66
|
+
}
|
|
67
|
+
if (ClientAuthenticationMethod.Post === clientAuthenticationMethod) {
|
|
68
|
+
formParameters[OAuth2ParameterNames.CLIENT_SECRET] = clientSecret;
|
|
69
|
+
}
|
|
70
|
+
return qs.stringify(formParameters);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { AccessTokenResponseClient, INVALID_TOKEN_RESPONSE_ERROR_CODE, ClientCredentialsGrantRequest } from './endpoint-protocol';
|
|
2
|
+
import { Component, Autowired } from '@celljs/core';
|
|
3
|
+
import { RestOperations } from '@celljs/http';
|
|
4
|
+
import { ProviderDetailsManager } from '../provider';
|
|
5
|
+
import { AccessTokenResponse, OAuth2ParameterNames, ClientAuthenticationMethod, OAuth2Error, OAuth2AuthorizationError, AccessTokenResponseConverter } from '@celljs/oauth2-core';
|
|
6
|
+
import { AuthorizationGrantRequestEntityUtil } from '../utils';
|
|
7
|
+
import * as qs from 'qs';
|
|
8
|
+
|
|
9
|
+
@Component()
|
|
10
|
+
export class ClientCredentialsTokenResponseClient implements AccessTokenResponseClient<ClientCredentialsGrantRequest> {
|
|
11
|
+
|
|
12
|
+
@Autowired(RestOperations)
|
|
13
|
+
protected readonly restOperations: RestOperations;
|
|
14
|
+
|
|
15
|
+
@Autowired(ProviderDetailsManager)
|
|
16
|
+
protected readonly providerDetailsManager: ProviderDetailsManager;
|
|
17
|
+
|
|
18
|
+
@Autowired(AccessTokenResponseConverter)
|
|
19
|
+
protected readonly accessTokenResponseConverter: AccessTokenResponseConverter;
|
|
20
|
+
|
|
21
|
+
async getTokenResponse(clientCredentialsGrantRequest: ClientCredentialsGrantRequest): Promise<AccessTokenResponse> {
|
|
22
|
+
const { clientRegistration } = clientCredentialsGrantRequest;
|
|
23
|
+
const providerDetails = await this.providerDetailsManager.get(clientRegistration.provider);
|
|
24
|
+
|
|
25
|
+
let response;
|
|
26
|
+
try {
|
|
27
|
+
response = await this.restOperations.post<{ [key: string]: string }>(providerDetails!.tokenUri, this.buildFormParameters(clientCredentialsGrantRequest), {
|
|
28
|
+
headers: AuthorizationGrantRequestEntityUtil.getTokenRequestHeaders(clientRegistration)
|
|
29
|
+
});
|
|
30
|
+
} catch (error) {
|
|
31
|
+
const oauth2Error = <OAuth2Error>{
|
|
32
|
+
errorCode: INVALID_TOKEN_RESPONSE_ERROR_CODE,
|
|
33
|
+
description: `An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: ${error?.message || error}`
|
|
34
|
+
};
|
|
35
|
+
throw new OAuth2AuthorizationError(oauth2Error, error);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const { data } = response;
|
|
39
|
+
|
|
40
|
+
const acccessTokenResponse = this.accessTokenResponseConverter.convert(data);
|
|
41
|
+
|
|
42
|
+
if (!acccessTokenResponse.accessToken.scopes || acccessTokenResponse.accessToken.scopes.length === 0) {
|
|
43
|
+
// As per spec, in Section 5.1 Successful Access Token Response
|
|
44
|
+
// https://tools.ietf.org/html/rfc6749#section-5.1
|
|
45
|
+
// If AccessTokenResponse.scope is empty, then default to the scope
|
|
46
|
+
// originally requested by the client in the Token Request
|
|
47
|
+
acccessTokenResponse.accessToken.scopes = clientRegistration.scopes;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return acccessTokenResponse;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected buildFormParameters(clientCredentialsGrantRequest: ClientCredentialsGrantRequest) {
|
|
54
|
+
const { clientRegistration, authorizationGrantType } = clientCredentialsGrantRequest;
|
|
55
|
+
const formParameters: { [key: string]: any } = {
|
|
56
|
+
[OAuth2ParameterNames.GRANT_TYPE]: authorizationGrantType,
|
|
57
|
+
};
|
|
58
|
+
const { clientAuthenticationMethod, clientId, clientSecret, scopes } = clientRegistration;
|
|
59
|
+
if (scopes) {
|
|
60
|
+
formParameters[OAuth2ParameterNames.SCOPE] = scopes.join(' ');
|
|
61
|
+
}
|
|
62
|
+
if (ClientAuthenticationMethod.Post === clientAuthenticationMethod) {
|
|
63
|
+
formParameters[OAuth2ParameterNames.CLIENT_ID] = clientId;
|
|
64
|
+
formParameters[OAuth2ParameterNames.CLIENT_SECRET] = clientSecret;
|
|
65
|
+
}
|
|
66
|
+
return qs.stringify(formParameters);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ClientRegistration } from '../registration';
|
|
2
|
+
import { AuthorizationExchange, AuthorizationGrantType, AccessToken, RefreshToken, AccessTokenResponse } from '@celljs/oauth2-core';
|
|
3
|
+
|
|
4
|
+
export const UserResponseClient = Symbol('UserResponseClient');
|
|
5
|
+
|
|
6
|
+
export const MISSING_USER_INFO_URI_ERROR_CODE = 'missing_user_info_uri';
|
|
7
|
+
export const MISSING_USER_NAME_ATTRIBUTE_ERROR_CODE = 'missing_user_name_attribute';
|
|
8
|
+
export const INVALID_USER_INFO_RESPONSE_ERROR_CODE = 'invalid_user_info_response';
|
|
9
|
+
|
|
10
|
+
export const INVALID_TOKEN_RESPONSE_ERROR_CODE = 'invalid_token_response';
|
|
11
|
+
|
|
12
|
+
export interface AuthorizationGrantRequest {
|
|
13
|
+
authorizationGrantType: AuthorizationGrantType;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface AuthorizationCodeGrantRequest extends AuthorizationGrantRequest {
|
|
17
|
+
clientRegistration: ClientRegistration;
|
|
18
|
+
authorizationExchange: AuthorizationExchange;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ClientCredentialsGrantRequest extends AuthorizationGrantRequest {
|
|
22
|
+
clientRegistration: ClientRegistration;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface PasswordGrantRequest extends AuthorizationGrantRequest {
|
|
26
|
+
clientRegistration: ClientRegistration;
|
|
27
|
+
username: string;
|
|
28
|
+
password: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface RefreshTokenGrantRequest extends AuthorizationGrantRequest {
|
|
32
|
+
clientRegistration: ClientRegistration;
|
|
33
|
+
accessToken: AccessToken;
|
|
34
|
+
refreshToken: RefreshToken;
|
|
35
|
+
scopes?: string[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface AccessTokenResponseClient<T extends AuthorizationGrantRequest> {
|
|
39
|
+
getTokenResponse(authorizationRequest: T): Promise<AccessTokenResponse>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface OAuth2UserRequest {
|
|
43
|
+
clientRegistration: ClientRegistration;
|
|
44
|
+
accessToken: AccessToken;
|
|
45
|
+
additionalParameters: { [key: string]: any };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface OAuth2UserResponse {
|
|
49
|
+
username: string;
|
|
50
|
+
userAttributes: { [key: string]: any }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface UserResponseClient<T extends OAuth2UserRequest> {
|
|
54
|
+
getUserResponse(authorizationRequest: T): Promise<OAuth2UserResponse>;
|
|
55
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './endpoint-protocol';
|
|
2
|
+
export * from './authorization-code-token-response-client';
|
|
3
|
+
export * from './client-credentials-token-response-client';
|
|
4
|
+
export * from './password-token-response-client';
|
|
5
|
+
export * from './refresh-token-token-response-client';
|
|
6
|
+
export * from './user-response-client';
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { AccessTokenResponseClient, INVALID_TOKEN_RESPONSE_ERROR_CODE, PasswordGrantRequest } from './endpoint-protocol';
|
|
2
|
+
import { Component, Autowired } from '@celljs/core';
|
|
3
|
+
import { RestOperations } from '@celljs/http';
|
|
4
|
+
import { ProviderDetailsManager } from '../provider';
|
|
5
|
+
import { AccessTokenResponse, OAuth2ParameterNames, ClientAuthenticationMethod, OAuth2Error, OAuth2AuthorizationError, AccessTokenResponseConverter } from '@celljs/oauth2-core';
|
|
6
|
+
import { AuthorizationGrantRequestEntityUtil } from '../utils';
|
|
7
|
+
import * as qs from 'qs';
|
|
8
|
+
|
|
9
|
+
@Component()
|
|
10
|
+
export class PasswordTokenResponseClient implements AccessTokenResponseClient<PasswordGrantRequest> {
|
|
11
|
+
|
|
12
|
+
@Autowired(RestOperations)
|
|
13
|
+
protected readonly restOperations: RestOperations;
|
|
14
|
+
|
|
15
|
+
@Autowired(ProviderDetailsManager)
|
|
16
|
+
protected readonly providerDetailsManager: ProviderDetailsManager;
|
|
17
|
+
|
|
18
|
+
@Autowired(AccessTokenResponseConverter)
|
|
19
|
+
protected readonly accessTokenResponseConverter: AccessTokenResponseConverter;
|
|
20
|
+
|
|
21
|
+
async getTokenResponse(passwordGrantRequest: PasswordGrantRequest): Promise<AccessTokenResponse> {
|
|
22
|
+
const { clientRegistration } = passwordGrantRequest;
|
|
23
|
+
const providerDetails = await this.providerDetailsManager.get(clientRegistration.provider);
|
|
24
|
+
|
|
25
|
+
let response;
|
|
26
|
+
try {
|
|
27
|
+
response = await this.restOperations.post<{ [key: string]: string }>(providerDetails!.tokenUri, this.buildFormParameters(passwordGrantRequest), {
|
|
28
|
+
headers: AuthorizationGrantRequestEntityUtil.getTokenRequestHeaders(clientRegistration)
|
|
29
|
+
});
|
|
30
|
+
} catch (error) {
|
|
31
|
+
const oauth2Error = <OAuth2Error>{
|
|
32
|
+
errorCode: INVALID_TOKEN_RESPONSE_ERROR_CODE,
|
|
33
|
+
description: `An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: ${error?.message || error}`
|
|
34
|
+
};
|
|
35
|
+
throw new OAuth2AuthorizationError(oauth2Error, error);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const { data } = response;
|
|
39
|
+
|
|
40
|
+
const acccessTokenResponse = this.accessTokenResponseConverter.convert(data);
|
|
41
|
+
|
|
42
|
+
if (!acccessTokenResponse.accessToken.scopes || acccessTokenResponse.accessToken.scopes.length === 0) {
|
|
43
|
+
// As per spec, in Section 5.1 Successful Access Token Response
|
|
44
|
+
// https://tools.ietf.org/html/rfc6749#section-5.1
|
|
45
|
+
// If AccessTokenResponse.scope is empty, then default to the scope
|
|
46
|
+
// originally requested by the client in the Token Request
|
|
47
|
+
acccessTokenResponse.accessToken.scopes = clientRegistration.scopes;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return acccessTokenResponse;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected buildFormParameters(passwordGrantRequest: PasswordGrantRequest) {
|
|
54
|
+
const { clientRegistration, authorizationGrantType, username, password } = passwordGrantRequest;
|
|
55
|
+
const formParameters: { [key: string]: any } = {
|
|
56
|
+
[OAuth2ParameterNames.GRANT_TYPE]: authorizationGrantType,
|
|
57
|
+
[OAuth2ParameterNames.USERNAME]: username,
|
|
58
|
+
[OAuth2ParameterNames.PASSWORD]: password
|
|
59
|
+
};
|
|
60
|
+
const { clientAuthenticationMethod, clientId, clientSecret, scopes } = clientRegistration;
|
|
61
|
+
if (scopes) {
|
|
62
|
+
formParameters[OAuth2ParameterNames.SCOPE] = scopes.join(' ');
|
|
63
|
+
}
|
|
64
|
+
if (ClientAuthenticationMethod.Post === clientAuthenticationMethod) {
|
|
65
|
+
formParameters[OAuth2ParameterNames.CLIENT_ID] = clientId;
|
|
66
|
+
formParameters[OAuth2ParameterNames.CLIENT_SECRET] = clientSecret;
|
|
67
|
+
}
|
|
68
|
+
return qs.stringify(formParameters);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { AccessTokenResponseClient, INVALID_TOKEN_RESPONSE_ERROR_CODE, RefreshTokenGrantRequest } from './endpoint-protocol';
|
|
2
|
+
import { Component, Autowired } from '@celljs/core';
|
|
3
|
+
import { RestOperations } from '@celljs/http';
|
|
4
|
+
import { ProviderDetailsManager } from '../provider';
|
|
5
|
+
import { AccessTokenResponse, OAuth2ParameterNames, ClientAuthenticationMethod, OAuth2Error, OAuth2AuthorizationError, AccessTokenResponseConverter } from '@celljs/oauth2-core';
|
|
6
|
+
import { AuthorizationGrantRequestEntityUtil } from '../utils';
|
|
7
|
+
import * as qs from 'qs';
|
|
8
|
+
|
|
9
|
+
@Component()
|
|
10
|
+
export class RefreshTokenTokenResponseClient implements AccessTokenResponseClient<RefreshTokenGrantRequest> {
|
|
11
|
+
|
|
12
|
+
@Autowired(RestOperations)
|
|
13
|
+
protected readonly restOperations: RestOperations;
|
|
14
|
+
|
|
15
|
+
@Autowired(ProviderDetailsManager)
|
|
16
|
+
protected readonly providerDetailsManager: ProviderDetailsManager;
|
|
17
|
+
|
|
18
|
+
@Autowired(AccessTokenResponseConverter)
|
|
19
|
+
protected readonly accessTokenResponseConverter: AccessTokenResponseConverter;
|
|
20
|
+
|
|
21
|
+
async getTokenResponse(refreshTokenGrantRequest: RefreshTokenGrantRequest): Promise<AccessTokenResponse> {
|
|
22
|
+
const { clientRegistration, refreshToken, accessToken } = refreshTokenGrantRequest;
|
|
23
|
+
const providerDetails = await this.providerDetailsManager.get(clientRegistration.provider);
|
|
24
|
+
|
|
25
|
+
let response;
|
|
26
|
+
try {
|
|
27
|
+
response = await this.restOperations.post<{ [key: string]: string }>(providerDetails!.tokenUri, this.buildFormParameters(refreshTokenGrantRequest), {
|
|
28
|
+
headers: AuthorizationGrantRequestEntityUtil.getTokenRequestHeaders(clientRegistration)
|
|
29
|
+
});
|
|
30
|
+
} catch (error) {
|
|
31
|
+
const oauth2Error = <OAuth2Error>{
|
|
32
|
+
errorCode: INVALID_TOKEN_RESPONSE_ERROR_CODE,
|
|
33
|
+
description: `An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: ${error?.message || error}`
|
|
34
|
+
};
|
|
35
|
+
throw new OAuth2AuthorizationError(oauth2Error, error);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const { data } = response;
|
|
39
|
+
|
|
40
|
+
const acccessTokenResponse = this.accessTokenResponseConverter.convert(data);
|
|
41
|
+
|
|
42
|
+
if (!acccessTokenResponse.accessToken.scopes || acccessTokenResponse.accessToken.scopes.length === 0) {
|
|
43
|
+
// As per spec, in Section 5.1 Successful Access Token Response
|
|
44
|
+
// https://tools.ietf.org/html/rfc6749#section-5.1
|
|
45
|
+
// If AccessTokenResponse.scope is empty, then default to the scope
|
|
46
|
+
// originally requested by the client in the Token Request
|
|
47
|
+
acccessTokenResponse.accessToken.scopes = accessToken.scopes;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!acccessTokenResponse.refreshToken) {
|
|
51
|
+
acccessTokenResponse.refreshToken = {
|
|
52
|
+
issuedAt: Date.now(),
|
|
53
|
+
tokenValue: refreshToken.tokenValue
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return acccessTokenResponse;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
protected buildFormParameters(refreshTokenGrantRequest: RefreshTokenGrantRequest) {
|
|
61
|
+
const { clientRegistration, authorizationGrantType, refreshToken, scopes } = refreshTokenGrantRequest;
|
|
62
|
+
const formParameters: { [key: string]: any } = {
|
|
63
|
+
[OAuth2ParameterNames.GRANT_TYPE]: authorizationGrantType,
|
|
64
|
+
[OAuth2ParameterNames.REFRESH_TOKEN]: refreshToken.tokenValue
|
|
65
|
+
};
|
|
66
|
+
if (scopes) {
|
|
67
|
+
formParameters[OAuth2ParameterNames.SCOPE] = scopes.join(' ');
|
|
68
|
+
}
|
|
69
|
+
const { clientAuthenticationMethod, clientId, clientSecret } = clientRegistration;
|
|
70
|
+
if (ClientAuthenticationMethod.Post === clientAuthenticationMethod) {
|
|
71
|
+
formParameters[OAuth2ParameterNames.CLIENT_ID] = clientId;
|
|
72
|
+
formParameters[OAuth2ParameterNames.CLIENT_SECRET] = clientSecret;
|
|
73
|
+
}
|
|
74
|
+
return qs.stringify(formParameters);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { UserResponseClient, OAuth2UserRequest, MISSING_USER_INFO_URI_ERROR_CODE,
|
|
2
|
+
MISSING_USER_NAME_ATTRIBUTE_ERROR_CODE, INVALID_USER_INFO_RESPONSE_ERROR_CODE, OAuth2UserResponse } from './endpoint-protocol';
|
|
3
|
+
import { Component, Autowired } from '@celljs/core';
|
|
4
|
+
import { RestOperations, HttpHeaders, MediaType, HttpMethod } from '@celljs/http';
|
|
5
|
+
import { ProviderDetailsManager } from '../provider';
|
|
6
|
+
import { OAuth2ParameterNames, OAuth2Error, OAuth2AuthorizationError, OAuth2AuthenticationError, AuthenticationMethod } from '@celljs/oauth2-core';
|
|
7
|
+
import * as qs from 'qs';
|
|
8
|
+
import { AxiosRequestConfig } from 'axios';
|
|
9
|
+
|
|
10
|
+
@Component(UserResponseClient)
|
|
11
|
+
export class DefaultUserResponseClient implements UserResponseClient<OAuth2UserRequest> {
|
|
12
|
+
|
|
13
|
+
@Autowired(RestOperations)
|
|
14
|
+
protected readonly restOperations: RestOperations;
|
|
15
|
+
|
|
16
|
+
@Autowired(ProviderDetailsManager)
|
|
17
|
+
protected readonly providerDetailsManager: ProviderDetailsManager;
|
|
18
|
+
|
|
19
|
+
async getUserResponse(userRequest: OAuth2UserRequest): Promise<OAuth2UserResponse> {
|
|
20
|
+
const { clientRegistration, accessToken } = userRequest;
|
|
21
|
+
const providerDetails = await this.providerDetailsManager.get(clientRegistration.provider);
|
|
22
|
+
|
|
23
|
+
if (!providerDetails?.userInfoEndpoint?.uri) {
|
|
24
|
+
throw new OAuth2AuthenticationError({
|
|
25
|
+
errorCode: MISSING_USER_INFO_URI_ERROR_CODE,
|
|
26
|
+
description: `Missing required UserInfo Uri in UserInfoEndpoint for Client Registration: ${clientRegistration.registrationId}`
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const userNameAttributeName = providerDetails?.userInfoEndpoint?.userNameAttributeName;
|
|
30
|
+
if (!userNameAttributeName) {
|
|
31
|
+
throw new OAuth2AuthenticationError({
|
|
32
|
+
errorCode: MISSING_USER_NAME_ATTRIBUTE_ERROR_CODE,
|
|
33
|
+
description: `Missing required \"user name\" attribute name in UserInfoEndpoint for Client Registration: ${clientRegistration.registrationId}`
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let response;
|
|
38
|
+
try {
|
|
39
|
+
const httpMethod = providerDetails.userInfoEndpoint.authenticationMethod === AuthenticationMethod.Form ? HttpMethod.POST : HttpMethod.GET;
|
|
40
|
+
const config: AxiosRequestConfig = {
|
|
41
|
+
url: providerDetails?.userInfoEndpoint?.uri,
|
|
42
|
+
method: httpMethod,
|
|
43
|
+
headers: {
|
|
44
|
+
[HttpHeaders.ACCEPT]: MediaType.APPLICATION_JSON
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
if (httpMethod === HttpMethod.POST) {
|
|
48
|
+
config.headers![HttpHeaders.CONTENT_TYPE] = `${MediaType.APPLICATION_FORM_URLENCODED};charset=UTF-8`;
|
|
49
|
+
config.data = qs.stringify({
|
|
50
|
+
[OAuth2ParameterNames.ACCESS_TOKEN]: accessToken.tokenValue
|
|
51
|
+
});
|
|
52
|
+
} else {
|
|
53
|
+
config.headers![HttpHeaders.AUTHORIZATION] = `Bearer ${accessToken.tokenValue}`;
|
|
54
|
+
|
|
55
|
+
}
|
|
56
|
+
response = await this.restOperations.request<{ [key: string]: any }>(config);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
const oauth2Error = <OAuth2Error>{
|
|
59
|
+
errorCode: INVALID_USER_INFO_RESPONSE_ERROR_CODE,
|
|
60
|
+
description: `An error occurred while attempting to retrieve the UserInfo Resource: ${error?.message || error}`
|
|
61
|
+
};
|
|
62
|
+
throw new OAuth2AuthorizationError(oauth2Error, error);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const { data } = response;
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
username: data[userNameAttributeName],
|
|
69
|
+
userAttributes: data
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|