@commercetools/connect-payments-sdk 0.0.5 → 0.0.7

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @commercetools/connect-payments-sdk
2
2
 
3
+ ## 0.0.7
4
+
5
+ ### Patch Changes
6
+
7
+ - 8110cc2: Enable AuthorityAuthorizationHook in Payment SDK
8
+
9
+ ## 0.0.6
10
+
11
+ ### Patch Changes
12
+
13
+ - 11ae367: Export authorization hook AuthorityAuthorizationHook in the root path
14
+
3
15
  ## 0.0.5
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;
@@ -6,3 +6,4 @@ export * from './hooks/jwt-auth.hook';
6
6
  export * from './hooks/oauth2-auth.hook';
7
7
  export * from './hooks/session-auth.hook';
8
8
  export * from './hooks/types/hook.type';
9
+ export * from './hooks/authorize.hook';
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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { JWTAuthenticationHook, Oauth2AuthenticationHook, RequestContextData, RequestContextProvider, SessionAuthenticationHook } from './api';
1
+ import { JWTAuthenticationHook, Oauth2AuthenticationHook, RequestContextData, RequestContextProvider, SessionAuthenticationHook, AuthorityAuthorizationHook } from './api';
2
2
  import { DefaultCommercetoolsAPI } from './commercetools/api/root-api';
3
3
  import { DefaultAuthorizationService } from './commercetools/services/ct-authorization.service';
4
4
  import { DefaultCartService } from './commercetools/services/ct-cart.service';
@@ -30,4 +30,5 @@ export declare const setupPaymentSDK: (opts: {
30
30
  sessionAuthHookFn: SessionAuthenticationHook;
31
31
  jwtAuthHookFn: JWTAuthenticationHook;
32
32
  oauth2AuthHookFn: Oauth2AuthenticationHook;
33
+ authorityAuthorizationHookFn: AuthorityAuthorizationHook;
33
34
  };
package/dist/index.js CHANGED
@@ -70,6 +70,7 @@ const setupPaymentSDK = (opts) => {
70
70
  clientSecret: opts.clientSecret,
71
71
  authUrl: opts.authUrl,
72
72
  });
73
+ const authorityAuthorizationManager = new security_1.AuthorityAuthorizationManager();
73
74
  const jwtAuthenticationManager = new security_1.JWTAuthenticationManager({
74
75
  jwtService,
75
76
  iss: opts.jwtIssuer,
@@ -87,6 +88,10 @@ const setupPaymentSDK = (opts) => {
87
88
  authenticationManager: oauth2AuthenticationManager,
88
89
  contextProvider,
89
90
  });
91
+ const authorityAuthorizationHookFn = new api_1.AuthorityAuthorizationHook({
92
+ authorizationManager: authorityAuthorizationManager,
93
+ contextProvider,
94
+ });
90
95
  return {
91
96
  ctAPI,
92
97
  ctCartService,
@@ -96,6 +101,7 @@ const setupPaymentSDK = (opts) => {
96
101
  sessionAuthHookFn,
97
102
  jwtAuthHookFn,
98
103
  oauth2AuthHookFn,
104
+ authorityAuthorizationHookFn,
99
105
  };
100
106
  };
101
107
  exports.setupPaymentSDK = setupPaymentSDK;
@@ -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
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -5,3 +5,4 @@ export * from './authn/session-authn-manager';
5
5
  export * from './authn/types/authn.type';
6
6
  export * from './services/jwt.service';
7
7
  export * from './services/oauth2.service';
8
+ export * from './authz/authorization-manager';
@@ -21,3 +21,4 @@ __exportStar(require("./authn/session-authn-manager"), exports);
21
21
  __exportStar(require("./authn/types/authn.type"), exports);
22
22
  __exportStar(require("./services/jwt.service"), exports);
23
23
  __exportStar(require("./services/oauth2.service"), exports);
24
+ __exportStar(require("./authz/authorization-manager"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercetools/connect-payments-sdk",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Payment SDK for commercetools payment connectors",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",