@commercetools/connect-payments-sdk 0.0.5 → 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 +6 -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/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
|
@@ -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);
|
|
@@ -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
|
+
}
|