@webiny/app-security 0.0.0-ee-vpcs.549378cf03

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # @webiny/app-security
2
+ [![](https://img.shields.io/npm/dw/@webiny/app-security.svg)](https://www.npmjs.com/package/@webiny/app-security)
3
+ [![](https://img.shields.io/npm/v/@webiny/app-security.svg)](https://www.npmjs.com/package/@webiny/app-security)
4
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
+
7
+ Exposes a simple `SecurityProvider` React provider component and enables you to quickly retrieve the currently signed-in user via the `useSecurity` React hook.
8
+
9
+ ## Install
10
+ ```
11
+ npm install --save @webiny/app-security
12
+ ```
13
+
14
+ Or if you prefer yarn:
15
+ ```
16
+ yarn add @webiny/app-security
17
+ ```
18
+
19
+ ## Quick Example
20
+
21
+ First, make sure you mount the `SecurityProvider` React provider component in your application's entrypoint component, for example the `App` component:
22
+
23
+ ```tsx
24
+ import React from "react";
25
+ import { Routes } from "@webiny/app/components/Routes";
26
+ import { BrowserRouter } from "@webiny/react-router";
27
+ import { SecurityProvider } from "@webiny/app-security";
28
+ import Authenticator from "./components/Authenticator";
29
+
30
+ export const App = () => (
31
+ <>
32
+ {/*
33
+ <SecurityProvider> is a generic provider of identity information. 3rd party identity providers (like Cognito,
34
+ Okta, Auth0) will handle the authentication, and set the information about the user into this provider,
35
+ so other parts of the system have a centralized place to fetch user information from.
36
+ */}
37
+ <SecurityProvider>
38
+ {/* This is the component that might trigger the initial authentication
39
+ process and set the retrieved user data into the Security provider.*/}
40
+ <Authenticator>
41
+ <BrowserRouter basename={process.env.PUBLIC_URL}>
42
+ <Routes />
43
+ </BrowserRouter>
44
+ </Authenticator>
45
+ </SecurityProvider>
46
+ </>
47
+ );
48
+ ```
49
+
50
+ A simple `Authenticator` React component (uses Amazon Cognito and AWS Amplify's [`Auth`](https://github.com/aws-amplify/amplify-js/blob/main/packages/auth/src/Auth.ts#L100) class):
51
+
52
+ ```tsx
53
+ import React, { useEffect } from "react";
54
+ import Auth from "@aws-amplify/auth";
55
+ import { useSecurity, SecurityIdentity } from "@webiny/app-security";
56
+
57
+ // Apart from the React component, we also configure the Auth class here.
58
+ Auth.configure({
59
+ region: process.env.REACT_APP_USER_POOL_REGION,
60
+ userPoolId: process.env.REACT_APP_USER_POOL_ID,
61
+ userPoolWebClientId: process.env.REACT_APP_USER_POOL_WEB_CLIENT_ID,
62
+ oauth: {
63
+ domain: process.env.REACT_APP_USER_POOL_DOMAIN,
64
+ redirectSignIn: `${location.origin}?signIn`,
65
+ redirectSignOut: `${location.origin}?signOut`,
66
+ responseType: "token"
67
+ }
68
+ });
69
+
70
+ // The `Authenticator` component.
71
+ const Authenticator: React.FC = props => {
72
+ const { setIdentity } = useSecurity();
73
+
74
+ useEffect(() => {
75
+ // Get the currently signed-in user.
76
+ Auth.currentSession().then(response => {
77
+ const user = response.getIdToken().payload;
78
+ setIdentity(
79
+ new SecurityIdentity({
80
+ login: user.email,
81
+ firstName: user.given_name,
82
+ lastName: user.family_name,
83
+ logout: () => {
84
+ Auth.signOut();
85
+ setIdentity(null);
86
+ }
87
+ })
88
+ );
89
+ }).catch(() => { /* Do nothing. */ });
90
+ }, []);
91
+
92
+ return <>{props.children}</>;
93
+ };
94
+
95
+ export default Authenticator;
96
+ ```
97
+
98
+ Finally, use the `useSecurity` React hook in any of your components:
99
+
100
+ ```tsx
101
+ import React from "react";
102
+ import { useSecurity } from "@webiny/app-security";
103
+
104
+ const MyComponent: React.FC = () => {
105
+ const { identity } = useSecurity();
106
+
107
+ if (identity) {
108
+ return <>Logged in.</>;
109
+ }
110
+
111
+ return <>Not logged in.</>;
112
+ };
113
+
114
+ export default MyComponent;
115
+ ```
package/Security.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import React from "react";
2
+ export declare const Security: React.FC;
package/Security.js ADDED
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.Security = void 0;
9
+
10
+ var _react = _interopRequireDefault(require("react"));
11
+
12
+ var _app = require("@webiny/app");
13
+
14
+ var _Security = require("./contexts/Security");
15
+
16
+ // Importing from `app-core` and NOT from `app-admin`, to avoid circular dependency.
17
+ // This can be resolved in a different way, by changing the location of `AppInstaller` component (currently in `app-admin`).
18
+ // But this is a faster solution, as I'm really short on time :)
19
+ var SecurityProviderHOC = function SecurityProviderHOC(Component) {
20
+ return function SecurityProvider(_ref) {
21
+ var children = _ref.children;
22
+ return /*#__PURE__*/_react.default.createElement(_Security.SecurityProvider, null, /*#__PURE__*/_react.default.createElement(Component, null, children));
23
+ };
24
+ };
25
+
26
+ var Security = function Security() {
27
+ return /*#__PURE__*/_react.default.createElement(_app.Provider, {
28
+ hoc: SecurityProviderHOC
29
+ });
30
+ };
31
+
32
+ exports.Security = Security;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["SecurityProviderHOC","Component","SecurityProvider","children","Security"],"sources":["Security.tsx"],"sourcesContent":["// Importing from `app-core` and NOT from `app-admin`, to avoid circular dependency.\n// This can be resolved in a different way, by changing the location of `AppInstaller` component (currently in `app-admin`).\n// But this is a faster solution, as I'm really short on time :)\nimport React from \"react\";\nimport { Provider } from \"@webiny/app\";\nimport { SecurityProvider as ContextProvider } from \"./contexts/Security\";\n\nconst SecurityProviderHOC = (Component: React.FC<any>): React.FC<any> => {\n return function SecurityProvider({ children }) {\n return (\n <ContextProvider>\n <Component>{children}</Component>\n </ContextProvider>\n );\n };\n};\n\nexport const Security: React.FC = () => {\n return <Provider hoc={SecurityProviderHOC} />;\n};\n"],"mappings":";;;;;;;;;AAGA;;AACA;;AACA;;AALA;AACA;AACA;AAKA,IAAMA,mBAAmB,GAAG,SAAtBA,mBAAsB,CAACC,SAAD,EAA6C;EACrE,OAAO,SAASC,gBAAT,OAAwC;IAAA,IAAZC,QAAY,QAAZA,QAAY;IAC3C,oBACI,6BAAC,0BAAD,qBACI,6BAAC,SAAD,QAAYA,QAAZ,CADJ,CADJ;EAKH,CAND;AAOH,CARD;;AAUO,IAAMC,QAAkB,GAAG,SAArBA,QAAqB,GAAM;EACpC,oBAAO,6BAAC,aAAD;IAAU,GAAG,EAAEJ;EAAf,EAAP;AACH,CAFM"}
@@ -0,0 +1,9 @@
1
+ import React from "react";
2
+ interface HasPermissionProps {
3
+ any?: string[];
4
+ all?: string[];
5
+ name?: string;
6
+ children: React.ReactNode;
7
+ }
8
+ export declare const HasPermission: ({ children, ...props }: HasPermissionProps) => JSX.Element | null;
9
+ export {};
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
5
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
6
+
7
+ Object.defineProperty(exports, "__esModule", {
8
+ value: true
9
+ });
10
+ exports.HasPermission = void 0;
11
+
12
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
+
14
+ var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
15
+
16
+ var _react = _interopRequireWildcard(require("react"));
17
+
18
+ var _useSecurity2 = require("../hooks/useSecurity");
19
+
20
+ var _excluded = ["children"];
21
+
22
+ var HasPermission = function HasPermission(_ref) {
23
+ var children = _ref.children,
24
+ props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
25
+
26
+ var _useSecurity = (0, _useSecurity2.useSecurity)(),
27
+ getPermission = _useSecurity.getPermission;
28
+
29
+ if (props.any && props.all) {
30
+ throw new Error("You can use either \"any\" or \"all\", but not both at the same time.");
31
+ }
32
+
33
+ if (props.name) {
34
+ return getPermission(props.name) ? /*#__PURE__*/_react.default.createElement(_react.Fragment, null, children) : null;
35
+ }
36
+
37
+ var permissions = [].concat((0, _toConsumableArray2.default)(props.any || []), (0, _toConsumableArray2.default)(props.all || [])).map(function (name) {
38
+ return getPermission(name);
39
+ });
40
+ var hasPermission = props.any ? permissions.some(function (p) {
41
+ return Boolean(p);
42
+ }) : permissions.every(function (p) {
43
+ return Boolean(p);
44
+ });
45
+ return hasPermission ? /*#__PURE__*/_react.default.createElement(_react.Fragment, null, children) : null;
46
+ };
47
+
48
+ exports.HasPermission = HasPermission;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["HasPermission","children","props","useSecurity","getPermission","any","all","Error","name","permissions","map","hasPermission","some","p","Boolean","every"],"sources":["HasPermission.tsx"],"sourcesContent":["import React, { Fragment } from \"react\";\nimport { useSecurity } from \"~/hooks/useSecurity\";\n\ninterface HasPermissionProps {\n any?: string[];\n all?: string[];\n name?: string;\n children: React.ReactNode;\n}\n\nexport const HasPermission = ({ children, ...props }: HasPermissionProps) => {\n const { getPermission } = useSecurity();\n\n if (props.any && props.all) {\n throw new Error(`You can use either \"any\" or \"all\", but not both at the same time.`);\n }\n\n if (props.name) {\n return getPermission(props.name) ? <Fragment>{children}</Fragment> : null;\n }\n\n const permissions = [...(props.any || []), ...(props.all || [])].map(name =>\n getPermission(name)\n );\n\n const hasPermission = props.any\n ? permissions.some(p => Boolean(p))\n : permissions.every(p => Boolean(p));\n\n return hasPermission ? <Fragment>{children}</Fragment> : null;\n};\n"],"mappings":";;;;;;;;;;;;;;;AAAA;;AACA;;;;AASO,IAAMA,aAAa,GAAG,SAAhBA,aAAgB,OAAgD;EAAA,IAA7CC,QAA6C,QAA7CA,QAA6C;EAAA,IAAhCC,KAAgC;;EACzE,mBAA0B,IAAAC,yBAAA,GAA1B;EAAA,IAAQC,aAAR,gBAAQA,aAAR;;EAEA,IAAIF,KAAK,CAACG,GAAN,IAAaH,KAAK,CAACI,GAAvB,EAA4B;IACxB,MAAM,IAAIC,KAAJ,yEAAN;EACH;;EAED,IAAIL,KAAK,CAACM,IAAV,EAAgB;IACZ,OAAOJ,aAAa,CAACF,KAAK,CAACM,IAAP,CAAb,gBAA4B,6BAAC,eAAD,QAAWP,QAAX,CAA5B,GAA8D,IAArE;EACH;;EAED,IAAMQ,WAAW,GAAG,2CAAKP,KAAK,CAACG,GAAN,IAAa,EAAlB,oCAA2BH,KAAK,CAACI,GAAN,IAAa,EAAxC,GAA6CI,GAA7C,CAAiD,UAAAF,IAAI;IAAA,OACrEJ,aAAa,CAACI,IAAD,CADwD;EAAA,CAArD,CAApB;EAIA,IAAMG,aAAa,GAAGT,KAAK,CAACG,GAAN,GAChBI,WAAW,CAACG,IAAZ,CAAiB,UAAAC,CAAC;IAAA,OAAIC,OAAO,CAACD,CAAD,CAAX;EAAA,CAAlB,CADgB,GAEhBJ,WAAW,CAACM,KAAZ,CAAkB,UAAAF,CAAC;IAAA,OAAIC,OAAO,CAACD,CAAD,CAAX;EAAA,CAAnB,CAFN;EAIA,OAAOF,aAAa,gBAAG,6BAAC,eAAD,QAAWV,QAAX,CAAH,GAAqC,IAAzD;AACH,CApBM"}
@@ -0,0 +1,7 @@
1
+ import React from "react";
2
+ interface SecureRouteProps {
3
+ children: React.ReactNode;
4
+ permission?: string;
5
+ }
6
+ declare const _default: ({ children, permission }: SecureRouteProps) => React.ReactElement | null;
7
+ export default _default;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _useSecurity = require("../hooks/useSecurity");
9
+
10
+ var _plugins = require("@webiny/plugins");
11
+
12
+ var _default = function _default(_ref) {
13
+ var children = _ref.children,
14
+ permission = _ref.permission;
15
+ var security = (0, _useSecurity.useSecurity)();
16
+
17
+ if (!security) {
18
+ return null;
19
+ }
20
+
21
+ var identity = security.identity,
22
+ getPermission = security.getPermission;
23
+
24
+ if (!identity) {
25
+ return null;
26
+ }
27
+
28
+ var hasPermission = false;
29
+
30
+ if (identity) {
31
+ hasPermission = permission ? Boolean(getPermission(permission)) : true;
32
+ }
33
+
34
+ if (hasPermission) {
35
+ return children;
36
+ }
37
+
38
+ var plugin = _plugins.plugins.byName("secure-route-error");
39
+
40
+ if (!plugin) {
41
+ return null;
42
+ }
43
+
44
+ return plugin.render();
45
+ };
46
+
47
+ exports.default = _default;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["children","permission","security","useSecurity","identity","getPermission","hasPermission","Boolean","plugin","plugins","byName","render"],"sources":["SecureRoute.tsx"],"sourcesContent":["import React from \"react\";\nimport { useSecurity } from \"~/hooks/useSecurity\";\nimport { SecureRouteErrorPlugin } from \"~/types\";\nimport { plugins } from \"@webiny/plugins\";\n\ninterface SecureRouteProps {\n children: React.ReactNode;\n permission?: string;\n}\nexport default ({ children, permission }: SecureRouteProps): React.ReactElement | null => {\n const security = useSecurity();\n\n if (!security) {\n return null;\n }\n\n const { identity, getPermission } = security;\n\n if (!identity) {\n return null;\n }\n\n let hasPermission = false;\n if (identity) {\n hasPermission = permission ? Boolean(getPermission(permission)) : true;\n }\n\n if (hasPermission) {\n return children as unknown as React.ReactElement;\n }\n\n const plugin = plugins.byName<SecureRouteErrorPlugin>(\"secure-route-error\");\n if (!plugin) {\n return null;\n }\n\n return plugin.render();\n};\n"],"mappings":";;;;;;;AACA;;AAEA;;eAMe,wBAA2E;EAAA,IAAxEA,QAAwE,QAAxEA,QAAwE;EAAA,IAA9DC,UAA8D,QAA9DA,UAA8D;EACtF,IAAMC,QAAQ,GAAG,IAAAC,wBAAA,GAAjB;;EAEA,IAAI,CAACD,QAAL,EAAe;IACX,OAAO,IAAP;EACH;;EAED,IAAQE,QAAR,GAAoCF,QAApC,CAAQE,QAAR;EAAA,IAAkBC,aAAlB,GAAoCH,QAApC,CAAkBG,aAAlB;;EAEA,IAAI,CAACD,QAAL,EAAe;IACX,OAAO,IAAP;EACH;;EAED,IAAIE,aAAa,GAAG,KAApB;;EACA,IAAIF,QAAJ,EAAc;IACVE,aAAa,GAAGL,UAAU,GAAGM,OAAO,CAACF,aAAa,CAACJ,UAAD,CAAd,CAAV,GAAwC,IAAlE;EACH;;EAED,IAAIK,aAAJ,EAAmB;IACf,OAAON,QAAP;EACH;;EAED,IAAMQ,MAAM,GAAGC,gBAAA,CAAQC,MAAR,CAAuC,oBAAvC,CAAf;;EACA,IAAI,CAACF,MAAL,EAAa;IACT,OAAO,IAAP;EACH;;EAED,OAAOA,MAAM,CAACG,MAAP,EAAP;AACH,C"}
@@ -0,0 +1,12 @@
1
+ import * as React from "react";
2
+ import { SecurityPermission } from "../types";
3
+ interface ChildrenRenderFunctionArgs<T extends SecurityPermission> {
4
+ hasPermission: boolean;
5
+ permission: T | null;
6
+ }
7
+ interface Props<T extends SecurityPermission> {
8
+ children: ((args: ChildrenRenderFunctionArgs<T>) => React.ReactElement) | React.ReactElement;
9
+ permission?: string;
10
+ }
11
+ declare function SecureView<T extends SecurityPermission>({ children, permission }: Props<T>): React.ReactElement | null;
12
+ export default SecureView;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _useSecurity2 = require("../hooks/useSecurity");
9
+
10
+ function SecureView(_ref) {
11
+ var children = _ref.children,
12
+ permission = _ref.permission;
13
+
14
+ var _useSecurity = (0, _useSecurity2.useSecurity)(),
15
+ getPermission = _useSecurity.getPermission;
16
+
17
+ var hasPermission = false;
18
+ var matchedPermission = null;
19
+
20
+ if (permission) {
21
+ matchedPermission = getPermission(permission);
22
+ hasPermission = Boolean(matchedPermission);
23
+ }
24
+
25
+ if (typeof children === "function") {
26
+ return children({
27
+ hasPermission: hasPermission,
28
+ permission: matchedPermission
29
+ });
30
+ }
31
+
32
+ return hasPermission ? children : null;
33
+ }
34
+
35
+ var _default = SecureView;
36
+ exports.default = _default;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["SecureView","children","permission","useSecurity","getPermission","hasPermission","matchedPermission","Boolean"],"sources":["SecureView.tsx"],"sourcesContent":["import * as React from \"react\";\nimport { useSecurity } from \"~/hooks/useSecurity\";\nimport { SecurityPermission } from \"~/types\";\n\ninterface ChildrenRenderFunctionArgs<T extends SecurityPermission> {\n hasPermission: boolean;\n permission: T | null;\n}\n\ninterface Props<T extends SecurityPermission> {\n children: ((args: ChildrenRenderFunctionArgs<T>) => React.ReactElement) | React.ReactElement;\n permission?: string;\n}\n\nfunction SecureView<T extends SecurityPermission>({\n children,\n permission\n}: Props<T>): React.ReactElement | null {\n const { getPermission } = useSecurity();\n\n let hasPermission = false;\n let matchedPermission: T | null = null;\n if (permission) {\n matchedPermission = getPermission<T>(permission);\n hasPermission = Boolean(matchedPermission);\n }\n\n if (typeof children === \"function\") {\n return children({\n hasPermission,\n permission: matchedPermission\n });\n }\n\n return hasPermission ? children : null;\n}\n\nexport default SecureView;\n"],"mappings":";;;;;;;AACA;;AAaA,SAASA,UAAT,OAGwC;EAAA,IAFpCC,QAEoC,QAFpCA,QAEoC;EAAA,IADpCC,UACoC,QADpCA,UACoC;;EACpC,mBAA0B,IAAAC,yBAAA,GAA1B;EAAA,IAAQC,aAAR,gBAAQA,aAAR;;EAEA,IAAIC,aAAa,GAAG,KAApB;EACA,IAAIC,iBAA2B,GAAG,IAAlC;;EACA,IAAIJ,UAAJ,EAAgB;IACZI,iBAAiB,GAAGF,aAAa,CAAIF,UAAJ,CAAjC;IACAG,aAAa,GAAGE,OAAO,CAACD,iBAAD,CAAvB;EACH;;EAED,IAAI,OAAOL,QAAP,KAAoB,UAAxB,EAAoC;IAChC,OAAOA,QAAQ,CAAC;MACZI,aAAa,EAAbA,aADY;MAEZH,UAAU,EAAEI;IAFA,CAAD,CAAf;EAIH;;EAED,OAAOD,aAAa,GAAGJ,QAAH,GAAc,IAAlC;AACH;;eAEcD,U"}
@@ -0,0 +1,3 @@
1
+ export { default as SecureView } from "./SecureView";
2
+ export { default as SecureRoute } from "./SecureRoute";
3
+ export { HasPermission } from "./HasPermission";
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ Object.defineProperty(exports, "HasPermission", {
9
+ enumerable: true,
10
+ get: function get() {
11
+ return _HasPermission.HasPermission;
12
+ }
13
+ });
14
+ Object.defineProperty(exports, "SecureRoute", {
15
+ enumerable: true,
16
+ get: function get() {
17
+ return _SecureRoute.default;
18
+ }
19
+ });
20
+ Object.defineProperty(exports, "SecureView", {
21
+ enumerable: true,
22
+ get: function get() {
23
+ return _SecureView.default;
24
+ }
25
+ });
26
+
27
+ var _SecureView = _interopRequireDefault(require("./SecureView"));
28
+
29
+ var _SecureRoute = _interopRequireDefault(require("./SecureRoute"));
30
+
31
+ var _HasPermission = require("./HasPermission");
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export { default as SecureView } from \"./SecureView\";\nexport { default as SecureRoute } from \"./SecureRoute\";\nexport { HasPermission } from \"./HasPermission\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA"}
@@ -0,0 +1,9 @@
1
+ import React, { Dispatch, SetStateAction } from "react";
2
+ import { SecurityIdentity, SecurityPermission } from "../types";
3
+ export interface SecurityContext {
4
+ identity: SecurityIdentity | null;
5
+ setIdentity: Dispatch<SetStateAction<SecurityIdentity | null>>;
6
+ getPermission<T extends SecurityPermission = SecurityPermission>(name: string): T | null;
7
+ }
8
+ export declare const SecurityContext: React.Context<SecurityContext>;
9
+ export declare const SecurityProvider: React.FC;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+
3
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
+
5
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
6
+
7
+ Object.defineProperty(exports, "__esModule", {
8
+ value: true
9
+ });
10
+ exports.SecurityProvider = exports.SecurityContext = void 0;
11
+
12
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
13
+
14
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
15
+
16
+ var _minimatch = _interopRequireDefault(require("minimatch"));
17
+
18
+ var _react = _interopRequireWildcard(require("react"));
19
+
20
+ var SecurityContext = /*#__PURE__*/_react.default.createContext({
21
+ identity: null,
22
+ setIdentity: function setIdentity() {
23
+ return void 0;
24
+ },
25
+ getPermission: function getPermission() {
26
+ return null;
27
+ }
28
+ });
29
+
30
+ exports.SecurityContext = SecurityContext;
31
+
32
+ var SecurityProvider = function SecurityProvider(props) {
33
+ var _useState = (0, _react.useState)(null),
34
+ _useState2 = (0, _slicedToArray2.default)(_useState, 2),
35
+ identity = _useState2[0],
36
+ setIdentity = _useState2[1];
37
+
38
+ var getPermission = (0, _react.useCallback)(function (name) {
39
+ if (!identity) {
40
+ return null;
41
+ }
42
+
43
+ var perms = identity.permissions || [];
44
+ var exactMatch = perms.find(function (p) {
45
+ return p.name === name;
46
+ });
47
+
48
+ if (exactMatch) {
49
+ return exactMatch;
50
+ } // Try matching using patterns
51
+
52
+
53
+ return perms.find(function (p) {
54
+ return (0, _minimatch.default)(name, p.name);
55
+ });
56
+ }, [identity]);
57
+ var value = (0, _react.useMemo)(function () {
58
+ return {
59
+ identity: identity ? (0, _objectSpread2.default)((0, _objectSpread2.default)({}, identity), {}, {
60
+ // For backwards compatibility, expose the `getPermission` method on the `identity` object.
61
+ getPermission: getPermission
62
+ }) : null,
63
+ setIdentity: setIdentity,
64
+ getPermission: getPermission
65
+ };
66
+ }, [identity]);
67
+ return /*#__PURE__*/_react.default.createElement(SecurityContext.Provider, {
68
+ value: value
69
+ }, props.children);
70
+ };
71
+
72
+ exports.SecurityProvider = SecurityProvider;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["SecurityContext","React","createContext","identity","setIdentity","getPermission","SecurityProvider","props","useState","useCallback","name","perms","permissions","exactMatch","find","p","minimatch","value","useMemo","children"],"sources":["Security.tsx"],"sourcesContent":["import minimatch from \"minimatch\";\nimport React, { useState, useMemo, Dispatch, SetStateAction, useCallback } from \"react\";\nimport { SecurityIdentity, SecurityPermission } from \"~/types\";\n\nexport interface SecurityContext {\n identity: SecurityIdentity | null;\n setIdentity: Dispatch<SetStateAction<SecurityIdentity | null>>;\n getPermission<T extends SecurityPermission = SecurityPermission>(name: string): T | null;\n}\n\nexport const SecurityContext = React.createContext<SecurityContext>({\n identity: null,\n setIdentity: () => {\n return void 0;\n },\n getPermission: () => {\n return null;\n }\n});\n\nexport const SecurityProvider: React.FC = props => {\n const [identity, setIdentity] = useState<SecurityIdentity | null>(null);\n\n const getPermission = useCallback(\n <T extends SecurityPermission = SecurityPermission>(name: string): T | null => {\n if (!identity) {\n return null;\n }\n\n const perms = identity.permissions || [];\n const exactMatch = perms.find(p => p.name === name);\n if (exactMatch) {\n return exactMatch as T;\n }\n\n // Try matching using patterns\n return perms.find(p => minimatch(name, p.name)) as any;\n },\n [identity]\n );\n\n const value = useMemo(() => {\n return {\n identity: identity\n ? {\n ...identity,\n // For backwards compatibility, expose the `getPermission` method on the `identity` object.\n getPermission\n }\n : null,\n setIdentity,\n getPermission\n };\n }, [identity]);\n\n return <SecurityContext.Provider value={value}>{props.children}</SecurityContext.Provider>;\n};\n"],"mappings":";;;;;;;;;;;;;;;AAAA;;AACA;;AASO,IAAMA,eAAe,gBAAGC,cAAA,CAAMC,aAAN,CAAqC;EAChEC,QAAQ,EAAE,IADsD;EAEhEC,WAAW,EAAE,uBAAM;IACf,OAAO,KAAK,CAAZ;EACH,CAJ+D;EAKhEC,aAAa,EAAE,yBAAM;IACjB,OAAO,IAAP;EACH;AAP+D,CAArC,CAAxB;;;;AAUA,IAAMC,gBAA0B,GAAG,SAA7BA,gBAA6B,CAAAC,KAAK,EAAI;EAC/C,gBAAgC,IAAAC,eAAA,EAAkC,IAAlC,CAAhC;EAAA;EAAA,IAAOL,QAAP;EAAA,IAAiBC,WAAjB;;EAEA,IAAMC,aAAa,GAAG,IAAAI,kBAAA,EAClB,UAAoDC,IAApD,EAA+E;IAC3E,IAAI,CAACP,QAAL,EAAe;MACX,OAAO,IAAP;IACH;;IAED,IAAMQ,KAAK,GAAGR,QAAQ,CAACS,WAAT,IAAwB,EAAtC;IACA,IAAMC,UAAU,GAAGF,KAAK,CAACG,IAAN,CAAW,UAAAC,CAAC;MAAA,OAAIA,CAAC,CAACL,IAAF,KAAWA,IAAf;IAAA,CAAZ,CAAnB;;IACA,IAAIG,UAAJ,EAAgB;MACZ,OAAOA,UAAP;IACH,CAT0E,CAW3E;;;IACA,OAAOF,KAAK,CAACG,IAAN,CAAW,UAAAC,CAAC;MAAA,OAAI,IAAAC,kBAAA,EAAUN,IAAV,EAAgBK,CAAC,CAACL,IAAlB,CAAJ;IAAA,CAAZ,CAAP;EACH,CAdiB,EAelB,CAACP,QAAD,CAfkB,CAAtB;EAkBA,IAAMc,KAAK,GAAG,IAAAC,cAAA,EAAQ,YAAM;IACxB,OAAO;MACHf,QAAQ,EAAEA,QAAQ,+DAELA,QAFK;QAGR;QACAE,aAAa,EAAbA;MAJQ,KAMZ,IAPH;MAQHD,WAAW,EAAXA,WARG;MASHC,aAAa,EAAbA;IATG,CAAP;EAWH,CAZa,EAYX,CAACF,QAAD,CAZW,CAAd;EAcA,oBAAO,6BAAC,eAAD,CAAiB,QAAjB;IAA0B,KAAK,EAAEc;EAAjC,GAAyCV,KAAK,CAACY,QAA/C,CAAP;AACH,CApCM"}
@@ -0,0 +1,2 @@
1
+ import { SecurityPermission } from "../types";
2
+ export declare function usePermission<T extends SecurityPermission = SecurityPermission>(name: string): T | null;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.usePermission = usePermission;
7
+
8
+ var _useSecurity2 = require("./useSecurity");
9
+
10
+ function usePermission(name) {
11
+ var _useSecurity = (0, _useSecurity2.useSecurity)(),
12
+ getPermission = _useSecurity.getPermission;
13
+
14
+ return getPermission(name);
15
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"names":["usePermission","name","useSecurity","getPermission"],"sources":["usePermission.ts"],"sourcesContent":["import { useSecurity } from \"~/hooks/useSecurity\";\nimport { SecurityPermission } from \"~/types\";\n\nexport function usePermission<T extends SecurityPermission = SecurityPermission>(name: string) {\n const { getPermission } = useSecurity();\n return getPermission<T>(name);\n}\n"],"mappings":";;;;;;;AAAA;;AAGO,SAASA,aAAT,CAA0EC,IAA1E,EAAwF;EAC3F,mBAA0B,IAAAC,yBAAA,GAA1B;EAAA,IAAQC,aAAR,gBAAQA,aAAR;;EACA,OAAOA,aAAa,CAAIF,IAAJ,CAApB;AACH"}
@@ -0,0 +1,2 @@
1
+ import { SecurityContext } from "../contexts/Security";
2
+ export declare function useSecurity(): SecurityContext;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useSecurity = useSecurity;
7
+
8
+ var _react = require("react");
9
+
10
+ var _Security = require("../contexts/Security");
11
+
12
+ function useSecurity() {
13
+ return (0, _react.useContext)(_Security.SecurityContext);
14
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"names":["useSecurity","useContext","SecurityContext"],"sources":["useSecurity.ts"],"sourcesContent":["import { useContext } from \"react\";\nimport { SecurityContext } from \"~/contexts/Security\";\n\nexport function useSecurity() {\n return useContext(SecurityContext);\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;AAEO,SAASA,WAAT,GAAuB;EAC1B,OAAO,IAAAC,iBAAA,EAAWC,yBAAX,CAAP;AACH"}
package/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./components";
2
+ export * from "./contexts/Security";
3
+ export { SecurityContext } from "./contexts/Security";
4
+ export * from "./hooks/useSecurity";
5
+ export * from "./hooks/usePermission";
6
+ export * from "./Security";
package/index.js ADDED
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {
7
+ SecurityContext: true
8
+ };
9
+ Object.defineProperty(exports, "SecurityContext", {
10
+ enumerable: true,
11
+ get: function get() {
12
+ return _Security.SecurityContext;
13
+ }
14
+ });
15
+
16
+ var _components = require("./components");
17
+
18
+ Object.keys(_components).forEach(function (key) {
19
+ if (key === "default" || key === "__esModule") return;
20
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
21
+ if (key in exports && exports[key] === _components[key]) return;
22
+ Object.defineProperty(exports, key, {
23
+ enumerable: true,
24
+ get: function get() {
25
+ return _components[key];
26
+ }
27
+ });
28
+ });
29
+
30
+ var _Security = require("./contexts/Security");
31
+
32
+ Object.keys(_Security).forEach(function (key) {
33
+ if (key === "default" || key === "__esModule") return;
34
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
35
+ if (key in exports && exports[key] === _Security[key]) return;
36
+ Object.defineProperty(exports, key, {
37
+ enumerable: true,
38
+ get: function get() {
39
+ return _Security[key];
40
+ }
41
+ });
42
+ });
43
+
44
+ var _useSecurity = require("./hooks/useSecurity");
45
+
46
+ Object.keys(_useSecurity).forEach(function (key) {
47
+ if (key === "default" || key === "__esModule") return;
48
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
49
+ if (key in exports && exports[key] === _useSecurity[key]) return;
50
+ Object.defineProperty(exports, key, {
51
+ enumerable: true,
52
+ get: function get() {
53
+ return _useSecurity[key];
54
+ }
55
+ });
56
+ });
57
+
58
+ var _usePermission = require("./hooks/usePermission");
59
+
60
+ Object.keys(_usePermission).forEach(function (key) {
61
+ if (key === "default" || key === "__esModule") return;
62
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
63
+ if (key in exports && exports[key] === _usePermission[key]) return;
64
+ Object.defineProperty(exports, key, {
65
+ enumerable: true,
66
+ get: function get() {
67
+ return _usePermission[key];
68
+ }
69
+ });
70
+ });
71
+
72
+ var _Security2 = require("./Security");
73
+
74
+ Object.keys(_Security2).forEach(function (key) {
75
+ if (key === "default" || key === "__esModule") return;
76
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
77
+ if (key in exports && exports[key] === _Security2[key]) return;
78
+ Object.defineProperty(exports, key, {
79
+ enumerable: true,
80
+ get: function get() {
81
+ return _Security2[key];
82
+ }
83
+ });
84
+ });
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./components\";\nexport * from \"./contexts/Security\";\nexport { SecurityContext } from \"./contexts/Security\";\nexport * from \"./hooks/useSecurity\";\nexport * from \"./hooks/usePermission\";\nexport * from \"./Security\";\n"],"mappings":";;;;;;;;;;;;;;;AAAA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AAEA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA"}
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@webiny/app-security",
3
+ "version": "0.0.0-ee-vpcs.549378cf03",
4
+ "main": "index.js",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/webiny/webiny-js.git"
8
+ },
9
+ "contributors": [
10
+ "Pavel Denisjuk <pavel@webiny.com>",
11
+ "Sven Al Hamad <sven@webiny.com>",
12
+ "Adrian Smijulj <adrian@webiny.com>"
13
+ ],
14
+ "license": "MIT",
15
+ "dependencies": {
16
+ "@webiny/app": "0.0.0-ee-vpcs.549378cf03",
17
+ "@webiny/plugins": "0.0.0-ee-vpcs.549378cf03",
18
+ "minimatch": "3.1.2",
19
+ "react": "17.0.2",
20
+ "react-dom": "17.0.2"
21
+ },
22
+ "devDependencies": {
23
+ "@babel/cli": "^7.19.3",
24
+ "@babel/core": "^7.19.3",
25
+ "@babel/plugin-proposal-class-properties": "^7.16.0",
26
+ "@babel/preset-env": "^7.19.4",
27
+ "@babel/preset-react": "^7.16.0",
28
+ "@babel/preset-typescript": "^7.18.6",
29
+ "@webiny/cli": "^0.0.0-ee-vpcs.549378cf03",
30
+ "@webiny/project-utils": "^0.0.0-ee-vpcs.549378cf03",
31
+ "babel-plugin-emotion": "^9.2.8",
32
+ "babel-plugin-lodash": "^3.3.4",
33
+ "rimraf": "^3.0.2",
34
+ "ttypescript": "^1.5.12",
35
+ "typescript": "4.7.4"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "directory": "dist"
40
+ },
41
+ "scripts": {
42
+ "build": "yarn webiny run build",
43
+ "watch": "yarn webiny run watch"
44
+ },
45
+ "adio": {
46
+ "ignore": {
47
+ "peerDependencies": [
48
+ "react-dom"
49
+ ]
50
+ }
51
+ },
52
+ "gitHead": "549378cf03fcd27845fc3fa23d1dc6b32896f630"
53
+ }
package/types.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ /// <reference types="react" />
2
+ import { Plugin } from "@webiny/app/types";
3
+ export declare type SecureRouteErrorPlugin = Plugin & {
4
+ render(): React.ReactElement;
5
+ };
6
+ export interface FullAccessPermission {
7
+ name: "*";
8
+ }
9
+ export interface SecurityPermission {
10
+ name: string;
11
+ [key: string]: any;
12
+ }
13
+ export interface SecurityIdentity {
14
+ id: string;
15
+ type: string;
16
+ displayName: string;
17
+ permissions?: SecurityPermission[];
18
+ /**
19
+ * TODO @ts-refactor @pavel
20
+ * Verify that login can be present in here.
21
+ */
22
+ login?: string;
23
+ /**
24
+ * TODO @ts-refactor @pavel
25
+ * Verify that profile can be present in here.
26
+ */
27
+ profile?: {
28
+ email?: string;
29
+ firstName?: string;
30
+ lastName?: string;
31
+ avatar?: {
32
+ src?: string;
33
+ };
34
+ gravatar?: string;
35
+ };
36
+ logout(): void;
37
+ getPermission?<T extends SecurityPermission = SecurityPermission>(name: string): T | null;
38
+ [key: string]: any;
39
+ }
package/types.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
package/types.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import { Plugin } from \"@webiny/app/types\";\n\nexport type SecureRouteErrorPlugin = Plugin & {\n render(): React.ReactElement;\n};\n\nexport interface FullAccessPermission {\n name: \"*\";\n}\n\nexport interface SecurityPermission {\n name: string;\n [key: string]: any;\n}\n\nexport interface SecurityIdentity {\n id: string;\n type: string;\n displayName: string;\n permissions?: SecurityPermission[];\n /**\n * TODO @ts-refactor @pavel\n * Verify that login can be present in here.\n */\n login?: string;\n /**\n * TODO @ts-refactor @pavel\n * Verify that profile can be present in here.\n */\n profile?: {\n email?: string;\n firstName?: string;\n lastName?: string;\n avatar?: {\n src?: string;\n };\n gravatar?: string;\n };\n logout(): void;\n // For backwards compatibility, expose the `getPermission` method on the `identity` object.\n getPermission?<T extends SecurityPermission = SecurityPermission>(name: string): T | null;\n [key: string]: any;\n}\n"],"mappings":""}