@commercetools/connect-payments-sdk 0.0.4 → 0.0.6
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/CHANGELOG.md +12 -0
- package/dist/api/hooks/authorize.hook.d.ts +12 -0
- package/dist/api/hooks/authorize.hook.js +22 -0
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/dist/index.js +1 -0
- package/dist/security/authn/jwt-authn-manager.d.ts +2 -0
- package/dist/security/authn/jwt-authn-manager.js +13 -1
- package/dist/security/authz/authorization-manager.d.ts +6 -0
- package/dist/security/authz/authorization-manager.js +29 -0
- package/dist/security/authz/types/authz.type.d.ts +11 -0
- package/dist/security/authz/types/authz.type.js +2 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @commercetools/connect-payments-sdk
|
|
2
2
|
|
|
3
|
+
## 0.0.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 11ae367: Export authorization hook AuthorityAuthorizationHook in the root path
|
|
8
|
+
|
|
9
|
+
## 0.0.5
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 22434c9: Enforce validation of projectkey on the jwt authentication.
|
|
14
|
+
|
|
3
15
|
## 0.0.4
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AuthorityAuthorizationManager } from '../../security/authz/authorization-manager';
|
|
2
|
+
import { ContextProvider, RequestContextData } from '../context/types/request-context.type';
|
|
3
|
+
import { AuthorizationHook } from './types/hook.type';
|
|
4
|
+
export declare class AuthorityAuthorizationHook implements AuthorizationHook {
|
|
5
|
+
private authorizationManager;
|
|
6
|
+
private contextProvider;
|
|
7
|
+
constructor(opts: {
|
|
8
|
+
authorizationManager: AuthorityAuthorizationManager;
|
|
9
|
+
contextProvider: ContextProvider<RequestContextData>;
|
|
10
|
+
});
|
|
11
|
+
authorize(...authorities: string[]): () => Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthorityAuthorizationHook = void 0;
|
|
4
|
+
const errorx_1 = require("../../errorx");
|
|
5
|
+
class AuthorityAuthorizationHook {
|
|
6
|
+
authorizationManager;
|
|
7
|
+
contextProvider;
|
|
8
|
+
constructor(opts) {
|
|
9
|
+
this.authorizationManager = opts.authorizationManager;
|
|
10
|
+
this.contextProvider = opts.contextProvider;
|
|
11
|
+
}
|
|
12
|
+
authorize(...authorities) {
|
|
13
|
+
return async () => {
|
|
14
|
+
const authn = this.contextProvider.getContextData().authentication;
|
|
15
|
+
if (!authn) {
|
|
16
|
+
throw new errorx_1.ErrorAuthErrorResponse('Authentication is required.');
|
|
17
|
+
}
|
|
18
|
+
this.authorizationManager.verify(authn, authorities);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.AuthorityAuthorizationHook = AuthorityAuthorizationHook;
|
package/dist/api/index.d.ts
CHANGED
package/dist/api/index.js
CHANGED
|
@@ -22,3 +22,4 @@ __exportStar(require("./hooks/jwt-auth.hook"), exports);
|
|
|
22
22
|
__exportStar(require("./hooks/oauth2-auth.hook"), exports);
|
|
23
23
|
__exportStar(require("./hooks/session-auth.hook"), exports);
|
|
24
24
|
__exportStar(require("./hooks/types/hook.type"), exports);
|
|
25
|
+
__exportStar(require("./hooks/authorize.hook"), exports);
|
package/dist/index.js
CHANGED
|
@@ -73,6 +73,7 @@ const setupPaymentSDK = (opts) => {
|
|
|
73
73
|
const jwtAuthenticationManager = new security_1.JWTAuthenticationManager({
|
|
74
74
|
jwtService,
|
|
75
75
|
iss: opts.jwtIssuer,
|
|
76
|
+
projectKey: opts.projectKey,
|
|
76
77
|
});
|
|
77
78
|
const sessionAuthHookFn = new api_1.SessionAuthenticationHook({
|
|
78
79
|
authenticationManager: sessionAuthenticationManager,
|
|
@@ -4,9 +4,11 @@ import { AuthenticationManager } from './types/authn.type';
|
|
|
4
4
|
export declare class JWTAuthenticationManager implements AuthenticationManager {
|
|
5
5
|
private jwtService;
|
|
6
6
|
private iss;
|
|
7
|
+
private projectKey;
|
|
7
8
|
constructor(opts: {
|
|
8
9
|
jwtService: JWTService;
|
|
9
10
|
iss: string;
|
|
11
|
+
projectKey: string;
|
|
10
12
|
});
|
|
11
13
|
authenticate(authentication: HeaderBasedAuthentication): Promise<JWTAuthentication>;
|
|
12
14
|
}
|
|
@@ -7,9 +7,11 @@ const bearer_utils_1 = require("./bearer-utils");
|
|
|
7
7
|
class JWTAuthenticationManager {
|
|
8
8
|
jwtService;
|
|
9
9
|
iss;
|
|
10
|
+
projectKey;
|
|
10
11
|
constructor(opts) {
|
|
11
12
|
this.jwtService = opts.jwtService;
|
|
12
13
|
this.iss = opts.iss;
|
|
14
|
+
this.projectKey = opts.projectKey;
|
|
13
15
|
}
|
|
14
16
|
async authenticate(authentication) {
|
|
15
17
|
const principal = authentication.getPrincipal();
|
|
@@ -18,13 +20,23 @@ class JWTAuthenticationManager {
|
|
|
18
20
|
token,
|
|
19
21
|
}));
|
|
20
22
|
if (decodedToken.iss !== this.iss) {
|
|
21
|
-
throw new errorx_1.ErrorAuthErrorResponse('Issuer in the token does not match the expected issuer', {
|
|
23
|
+
throw new errorx_1.ErrorAuthErrorResponse('Issuer in the token does not match the expected issuer.', {
|
|
22
24
|
privateFields: {
|
|
23
25
|
expectedIssuer: this.iss,
|
|
24
26
|
actualIssuer: decodedToken['iss'],
|
|
25
27
|
},
|
|
26
28
|
});
|
|
27
29
|
}
|
|
30
|
+
const projectKey = decodedToken[`${this.iss}/claims/project_key`];
|
|
31
|
+
if (projectKey !== this.projectKey) {
|
|
32
|
+
throw new errorx_1.ErrorAuthErrorResponse('Project key does not match.', {
|
|
33
|
+
privateFields: {
|
|
34
|
+
expectedIssuer: this.iss,
|
|
35
|
+
actualIssuer: decodedToken['iss'],
|
|
36
|
+
projectKey,
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
28
40
|
return new authns_1.JWTAuthentication(token, {
|
|
29
41
|
mcCustomerId: decodedToken['sub'],
|
|
30
42
|
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Authentication } from '../authn/types/authn.type';
|
|
2
|
+
import { AuthorizationManager } from './types/authz.type';
|
|
3
|
+
export declare class AuthorityAuthorizationManager implements AuthorizationManager<string[]> {
|
|
4
|
+
verify(authentication: Authentication, authorities: string[]): void;
|
|
5
|
+
check(authentication: Authentication, authorities: string[]): boolean;
|
|
6
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthorityAuthorizationManager = void 0;
|
|
4
|
+
const errorx_1 = require("../../errorx");
|
|
5
|
+
class AuthorityAuthorizationManager {
|
|
6
|
+
verify(authentication, authorities) {
|
|
7
|
+
const isAuthorized = this.check(authentication, authorities);
|
|
8
|
+
if (!isAuthorized) {
|
|
9
|
+
throw new errorx_1.ErrorAuthErrorResponse('Not authorized', {
|
|
10
|
+
skipLog: true,
|
|
11
|
+
fields: {
|
|
12
|
+
validAuthorities: authorities,
|
|
13
|
+
},
|
|
14
|
+
privateFields: {
|
|
15
|
+
grantedAuthorities: authentication.getAuthorities(),
|
|
16
|
+
requiredAuthorities: authorities,
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
check(authentication, authorities) {
|
|
22
|
+
const grantedAuthorities = authentication.getAuthorities();
|
|
23
|
+
const hasGrantedAuthorities = authorities.some((authority) => {
|
|
24
|
+
return grantedAuthorities.find((grantedAuthority) => grantedAuthority === authority);
|
|
25
|
+
});
|
|
26
|
+
return hasGrantedAuthorities;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.AuthorityAuthorizationManager = AuthorityAuthorizationManager;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Authentication } from '../../authn/types/authn.type';
|
|
2
|
+
export interface AuthorizationManager<T> {
|
|
3
|
+
/**
|
|
4
|
+
* Determines if access should be granted for a specific authentication and object.
|
|
5
|
+
*/
|
|
6
|
+
verify(authentication: Authentication, object: T): void;
|
|
7
|
+
/**
|
|
8
|
+
* Determines if access is granted for a specific object.
|
|
9
|
+
*/
|
|
10
|
+
check(authentication: Authentication, object: T): boolean;
|
|
11
|
+
}
|