@webiny/app-security 0.0.0-mt-1
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 +115 -0
- package/components/SecureRoute.d.ts +6 -0
- package/components/SecureRoute.js +27 -0
- package/components/SecureView.d.ts +12 -0
- package/components/SecureView.js +28 -0
- package/components/index.d.ts +2 -0
- package/components/index.js +2 -0
- package/contexts/Security.d.ts +9 -0
- package/contexts/Security.js +44 -0
- package/hooks/useSecurity.d.ts +2 -0
- package/hooks/useSecurity.js +5 -0
- package/index.d.ts +3 -0
- package/index.js +3 -0
- package/package.json +59 -0
- package/types.d.ts +21 -0
- package/types.js +1 -0
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://www.npmjs.com/package/@webiny/app-security)
|
|
3
|
+
[](https://www.npmjs.com/package/@webiny/app-security)
|
|
4
|
+
[](https://github.com/prettier/prettier)
|
|
5
|
+
[](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
|
+
```
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useSecurity } from "..";
|
|
2
|
+
import { plugins } from "@webiny/plugins";
|
|
3
|
+
export default (function (_ref) {
|
|
4
|
+
var children = _ref.children,
|
|
5
|
+
permission = _ref.permission;
|
|
6
|
+
|
|
7
|
+
var _useSecurity = useSecurity(),
|
|
8
|
+
identity = _useSecurity.identity;
|
|
9
|
+
|
|
10
|
+
var hasPermission = false;
|
|
11
|
+
|
|
12
|
+
if (identity) {
|
|
13
|
+
hasPermission = permission ? Boolean(identity.getPermission(permission)) : true;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (hasPermission) {
|
|
17
|
+
return children;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
var plugin = plugins.byName("secure-route-error");
|
|
21
|
+
|
|
22
|
+
if (!plugin) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return plugin.render();
|
|
27
|
+
});
|
|
@@ -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;
|
|
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;
|
|
12
|
+
export default SecureView;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useSecurity } from "..";
|
|
2
|
+
|
|
3
|
+
function SecureView(_ref) {
|
|
4
|
+
var children = _ref.children,
|
|
5
|
+
permission = _ref.permission;
|
|
6
|
+
|
|
7
|
+
var _useSecurity = useSecurity(),
|
|
8
|
+
identity = _useSecurity.identity;
|
|
9
|
+
|
|
10
|
+
var hasPermission = false;
|
|
11
|
+
var matchedPermission = null;
|
|
12
|
+
|
|
13
|
+
if (identity) {
|
|
14
|
+
matchedPermission = identity.getPermission(permission);
|
|
15
|
+
hasPermission = Boolean(matchedPermission);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (typeof children === "function") {
|
|
19
|
+
return children({
|
|
20
|
+
hasPermission: hasPermission,
|
|
21
|
+
permission: matchedPermission
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return hasPermission ? children : null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default SecureView;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React, { Dispatch, SetStateAction } from "react";
|
|
2
|
+
import { SecurityIdentity, SecurityPermission } from "../types";
|
|
3
|
+
export declare const SecurityContext: React.Context<SecurityContextValue>;
|
|
4
|
+
export interface SecurityContextValue {
|
|
5
|
+
identity: SecurityIdentity | null;
|
|
6
|
+
setIdentity: Dispatch<SetStateAction<SecurityIdentity>>;
|
|
7
|
+
getPermission<T extends SecurityPermission = SecurityPermission>(name: string): T;
|
|
8
|
+
}
|
|
9
|
+
export declare const SecurityProvider: (props: any) => JSX.Element;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
|
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
3
|
+
import minimatch from "minimatch";
|
|
4
|
+
import React, { useState, useMemo, useCallback } from "react";
|
|
5
|
+
export var SecurityContext = /*#__PURE__*/React.createContext(null);
|
|
6
|
+
export var SecurityProvider = function SecurityProvider(props) {
|
|
7
|
+
var _useState = useState(null),
|
|
8
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
9
|
+
identity = _useState2[0],
|
|
10
|
+
setIdentity = _useState2[1];
|
|
11
|
+
|
|
12
|
+
var getPermission = useCallback(function (name) {
|
|
13
|
+
if (!identity) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
var perms = identity.permissions || [];
|
|
18
|
+
var exactMatch = perms.find(function (p) {
|
|
19
|
+
return p.name === name;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
if (exactMatch) {
|
|
23
|
+
return exactMatch;
|
|
24
|
+
} // Try matching using patterns
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
return perms.find(function (p) {
|
|
28
|
+
return minimatch(name, p.name);
|
|
29
|
+
});
|
|
30
|
+
}, [identity]);
|
|
31
|
+
var value = useMemo(function () {
|
|
32
|
+
return {
|
|
33
|
+
identity: identity ? _objectSpread(_objectSpread({}, identity), {}, {
|
|
34
|
+
// For backwards compatibility, expose the `getPermission` method on the `identity` object.
|
|
35
|
+
getPermission: getPermission
|
|
36
|
+
}) : null,
|
|
37
|
+
setIdentity: setIdentity,
|
|
38
|
+
getPermission: getPermission
|
|
39
|
+
};
|
|
40
|
+
}, [identity]);
|
|
41
|
+
return /*#__PURE__*/React.createElement(SecurityContext.Provider, {
|
|
42
|
+
value: value
|
|
43
|
+
}, props.children);
|
|
44
|
+
};
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webiny/app-security",
|
|
3
|
+
"version": "0.0.0-mt-1",
|
|
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-mt-1",
|
|
17
|
+
"@webiny/plugins": "0.0.0-mt-1",
|
|
18
|
+
"minimatch": "3.0.4",
|
|
19
|
+
"react": "16.14.0",
|
|
20
|
+
"react-dom": "16.14.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@babel/cli": "^7.5.5",
|
|
24
|
+
"@babel/core": "^7.5.5",
|
|
25
|
+
"@babel/plugin-proposal-class-properties": "^7.5.5",
|
|
26
|
+
"@babel/preset-env": "^7.5.5",
|
|
27
|
+
"@babel/preset-react": "^7.0.0",
|
|
28
|
+
"@babel/preset-typescript": "^7.8.3",
|
|
29
|
+
"@webiny/cli": "^0.0.0-mt-1",
|
|
30
|
+
"@webiny/project-utils": "^0.0.0-mt-1",
|
|
31
|
+
"babel-plugin-emotion": "^9.2.8",
|
|
32
|
+
"babel-plugin-lodash": "^3.3.4",
|
|
33
|
+
"babel-plugin-named-asset-import": "^1.0.0-next.3e165448",
|
|
34
|
+
"rimraf": "^3.0.2",
|
|
35
|
+
"ttypescript": "^1.5.12",
|
|
36
|
+
"typescript": "^4.1.3"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public",
|
|
40
|
+
"directory": "dist"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "yarn webiny run build",
|
|
44
|
+
"watch": "yarn webiny run watch"
|
|
45
|
+
},
|
|
46
|
+
"svgo": {
|
|
47
|
+
"plugins": {
|
|
48
|
+
"removeViewBox": false
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"adio": {
|
|
52
|
+
"ignore": {
|
|
53
|
+
"peerDependencies": [
|
|
54
|
+
"react-dom"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"gitHead": "37736d8456a6ecb342a6c3645060bd9a3f2d4bb0"
|
|
59
|
+
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
logout(): void;
|
|
19
|
+
getPermission?<T extends SecurityPermission = SecurityPermission>(name: string): T;
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
}
|
package/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|