Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add authorization docs #3067

Merged
merged 43 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
973aa97
Add universal required email config for authentication
apedroferreira Jan 3, 2024
ba2399d
Must have at least 1 verified email in Github
apedroferreira Jan 3, 2024
ebcbf55
Merge remote-tracking branch 'upstream/master' into auth-required-email
apedroferreira Jan 3, 2024
08791c6
Address review comments
apedroferreira Jan 4, 2024
ef5731b
Merge remote-tracking branch 'upstream/master' into auth-required-email
apedroferreira Jan 4, 2024
39c0f00
Refactor (review comments)
apedroferreira Jan 5, 2024
d7fc87c
Small refactor
apedroferreira Jan 5, 2024
07015a0
Load env before imports
apedroferreira Jan 8, 2024
f730414
Add spacing to navigation
apedroferreira Jan 8, 2024
ebce000
Azure AD auth provider (without role mapping)
apedroferreira Jan 12, 2024
60f031d
Use just size property in icon
apedroferreira Jan 12, 2024
f58dcc9
Add role mapping
apedroferreira Jan 16, 2024
43c9af0
Update schemas
apedroferreira Jan 16, 2024
1c06826
Better name
apedroferreira Jan 16, 2024
f6fdf95
Fix Azure icon
apedroferreira Jan 16, 2024
761f0ed
Merge remote-tracking branch 'upstream/master' into auth-azure-ad-pro…
apedroferreira Jan 16, 2024
71ea8a7
Disable feature flag
apedroferreira Jan 16, 2024
9cffb6a
Self-review
apedroferreira Jan 16, 2024
8f150de
Merge remote-tracking branch 'upstream/master' into auth-azure-ad-pro…
apedroferreira Jan 16, 2024
f550a02
Fix page blocking logic and default page
apedroferreira Jan 16, 2024
f725a21
More fixes
apedroferreira Jan 16, 2024
7d921c2
Better signout experience
apedroferreira Jan 16, 2024
78337e2
Write authorization docs (authentication)
apedroferreira Jan 9, 2024
2af7023
Bump prettier (major) (#2320)
renovate[bot] Jan 17, 2024
68c27a2
[code-infra] Add monorepo update script (#3091)
Janpot Jan 17, 2024
54b3456
Bump MUI Core (#3089)
renovate[bot] Jan 17, 2024
aa370d0
[code-infra] Replace hardcoded repository in monorepo update script (…
Janpot Jan 17, 2024
4698dfa
Bump MUI Core (#3093)
renovate[bot] Jan 17, 2024
f61ce2d
Bump @mui/monorepo digest to 35a1472 (#3095)
renovate[bot] Jan 18, 2024
145e9a3
[release] v0.1.46 (#3094)
bharatkashyap Jan 18, 2024
15a7510
Bump @mui/toolpad to 0.1.46 (#3096)
renovate[bot] Jan 18, 2024
0a99b10
Roles docs without images
apedroferreira Jan 18, 2024
6c84d3f
Add documentation for Azure AD and roles/RBAC
apedroferreira Jan 18, 2024
4331732
Merge remote-tracking branch 'upstream/master' into auth-docs
apedroferreira Jan 18, 2024
9b6ea5c
Merge remote-tracking branch 'upstream/master' into auth-docs
apedroferreira Jan 18, 2024
2e271be
Run prettier, disable feature flag
apedroferreira Jan 18, 2024
b5a5b0d
Merge remote-tracking branch 'upstream/master' into auth-docs
apedroferreira Jan 29, 2024
04c406a
Fix merge
apedroferreira Jan 29, 2024
b150070
Fix merge more
apedroferreira Jan 29, 2024
8cf02ea
Remove feature flag
apedroferreira Jan 31, 2024
9d4f81d
Merge remote-tracking branch 'upstream/master' into auth-docs
apedroferreira Jan 31, 2024
b26127f
Prettier
apedroferreira Jan 31, 2024
dd63fe6
Add recommended warnings
apedroferreira Feb 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/data/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ const pages: MuiPage[] = [
{
pathname: '/toolpad/concepts/page-properties',
},
{
pathname: '/toolpad/concepts/authorization',
title: 'Authorization',
children: [
{
pathname: '/toolpad/concepts/authentication',
title: 'Authentication',
},
{
pathname: '/toolpad/concepts/rbac',
title: 'Role-based access control',
},
],
},
{ pathname: '/toolpad/concepts/custom-server' },
],
},
Expand Down
95 changes: 95 additions & 0 deletions docs/data/toolpad/concepts/authentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Authentication

<p class="description">Restrict a Toolpad application to authenticated users only, under certain authentication providers.</p>

You can configure a Toolpad application so that users have to sign in with specific authentication providers in order to access it.

{{"component": "modules/components/DocsImage.tsx", "src": "/static/toolpad/docs/concepts/authorization/sign-in-page.png", "alt": "Toolpad sign-in page", "caption": "Toolpad sign-in page", "indent": 1, "aspectRatio": 1 }}

Authentication settings can be accessed through the **Authorization** option in the app editor header.

{{"component": "modules/components/DocsImage.tsx", "src": "/static/toolpad/docs/concepts/authorization/authentication-settings.png", "alt": "Authentication settings", "caption": "Authentication settings", "indent": 1, "aspectRatio": 6 }}

## Authentication secret
Copy link
Member

@Janpot Janpot Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add a note here that it's important in production to keep this secret safe and not share it with anyone?


To enable any authorization features, you must set a random string to be used as a secret in the `TOOLPAD_AUTH_SECRET` environment variable.

This secret will be used to hash tokens, sign/encrypt cookies and generate cryptographic keys.

You can quickly create a good value on the command line with this `openssl` command:

```bash
openssl rand -base64 32
```

:::warning
Please make sure to keep this secret safe and do not share it with anyone!
:::

## Authentication providers

In the authentication settings, you can set up one or more authentication providers for users to be able to sign in with, such as Github and Google.

If any authentication providers are set, only authenticated users are able to access your Toolpad application.

Each authentication provider has its own configuration options, to be set with certain environment variables.

### Github

| environment variable name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | description |
| :------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------ |
| `TOOLPAD_GITHUB_CLIENT_ID` | Github OAuth app client ID. |
| `TOOLPAD_GITHUB_CLIENT_SECRET` | Github OAuth app client secret. |

Take a look at the following official instructions to [create a Github OAuth app](https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/creating-an-oauth-app).

In the Github OAuth app settings screen, in the **Authorization callback URL** option, use the production path of your application followed by `/api/auth/callback/github`, as in `http://mui.com/api/auth/callback/github`.

{{"component": "modules/components/DocsImage.tsx", "src": "/static/toolpad/docs/concepts/authorization/github-callback-url.png", "alt": "Github callback URL configuration", "caption": "Github callback URL configuration", "zoom": false, "width": 460 }}

### Google

| environment variable name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | description |
| :------------------------------------------------------------------------------------------------------------------------- | :-------------------------- |
| `TOOLPAD_GOOGLE_CLIENT_ID` | Google OAuth client ID. |
| `TOOLPAD_GOOGLE_CLIENT_SECRET` | Google OAuth client secret. |

Take a look at the following official instructions to [create a Google OAuth client ID](https://developers.google.com/workspace/guides/create-credentials#oauth-client-id).

In the Google OAuth client settings screen, under the **Authorized redirect URIs** option, make sure to include the production path of your application followed by `/api/auth/callback/google`, as in `http://mui.com/api/auth/callback/google`.

{{"component": "modules/components/DocsImage.tsx", "src": "/static/toolpad/docs/concepts/authorization/google-redirect-url.png", "alt": "Google redirect URIs configuration", "caption": "Google redirect URIs configuration", "zoom": false, "width": 460 }}

### Azure Active Directory (now Entra ID)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add a warning here that it's only going to be available on a plan?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points, I've added a few small warnings: dd63fe6


:::warning
The Azure AD authentication provider will be a paid feature of Toolpad very soon, so it's only available for free for a limited time.
:::

| environment variable name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | description |
| :------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------- |
| `TOOLPAD_AZURE_AD_CLIENT_ID` | Azure AD application (client) ID. |
| `TOOLPAD_AZURE_AD_CLIENT_SECRET` | Azure AD application client secret. |
| `TOOLPAD_AZURE_AD_TENANT_ID` | Azure AD application directory (tenant) ID |

Follow these steps to configure your Azure AD client and get the necessary environment variables:

1. Go to https://portal.azure.com, search for "Microsoft Entra ID" and go to it.

2. In the left-side menu, go to **App registrations** and create a new app by choosing **New registration**.

3. When registering your application, under the **Redirect URI** option, make sure to include the production path of your application followed by `/api/auth/callback/azure-ad`, as in `http://mui.com/api/auth/callback/azure-ad`.

{{"component": "modules/components/DocsImage.tsx", "src": "/static/toolpad/docs/concepts/authorization/azure-ad-redirect-url.png", "alt": "Azure AD redirect URI configuration", "caption": "Azure AD redirect URI configuration", "aspectRatio": 6 }}

4. Once your application has ben created, go to its page in **App registrations** where you can find the client and tenant IDs under the **Overview** option in the left-side menu.

5. Go to **Certificates & secrets** and use the option **New client secret** to generate a client secret.

With the Azure AD provider, only existing users of your Azure AD application will be able to sign in.

## Restricted domains

In the authentication settings, you can specify one or more domains (such as `mui.com`) to restrict user authentication based on the signed-in user's email address.

If any restricted domains are set, the user must have at least one email address assigned to their current authentication provider that belongs to one of those domains, otherwise they will not be able to sign in.
51 changes: 51 additions & 0 deletions docs/data/toolpad/concepts/rbac.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Role-based access control

<p class="description">Restrict pages in Toolpad application to users with certain roles only.</p>

:::warning
Role-based access control will be a paid feature of Toolpad very soon, so it's only available for free for a limited time.
:::

You can configure pages in a Toolpad application so that users must have certain roles within specific authentication providers in order to access them.

Before using these features, make sure that you have [authentication](/toolpad/concepts/authentication/) enabled for the authenticated providers you are using.

## Defining roles

Role settings can be accessed through the **Authorization** option in the app editor header, under the **Roles** tab, where you can view, create, edit and delete roles for the whole application.

{{"component": "modules/components/DocsImage.tsx", "src": "/static/toolpad/docs/concepts/authorization/roles-settings.png", "alt": "Roles settings", "caption": "Roles settings", "indent": 1, "aspectRatio": 6 }}

## Restricting pages

By default, all pages are viewable by users with any roles (or even no roles).

You can change this for any individual page in the editor screen, under the **Page** settings in the right sidebar, where you can select/deselect **Allow access to all roles** and add one or multiple roles under the list of **Allowed roles** for the page.

{{"component": "modules/components/DocsImage.tsx", "src": "/static/toolpad/docs/concepts/authorization/restricting-pages.png", "alt": "Page authorization settings", "caption": "Page authorization settings", "zoom": false, "width": 380 }}

## Authorization providers

In order for Toolpad users to have roles, you need to have those roles configured in the authentication provider they sign in with. Each authentication provider has its own configuration settings for roles.

Not all available authentication providers in Toolpad support roles, so you must use one that does if you want to use this feature.

### Azure Active Directory (now Entra ID)

Follow these steps to assign roles to users in an Azure AD application:

1. From the Azure AD home screen, go to **App registrations** in the left-side menu, then in your application go to **App roles** on the left, where you can create new roles with the **Create app role** option.

{{"component": "modules/components/DocsImage.tsx", "src": "/static/toolpad/docs/concepts/authorization/azure-ad-roles.png", "alt": "Azure AD app roles", "caption": "Azure AD app roles", "indent": 1, "aspectRatio": 6 }}

2. To assign your created roles to a user, from the Azure AD home screen go to **Enterprise applications** in the left-side menu, then in your application go to **Users and groups** on the left, where you can assign/unassign roles to selected users with the **Edit assignment** option.

## Role mapping

By default, Toolpad will expect an exact match between the names of your Toolpad roles and the roles in the authentication provider being used.

If you don't want these to match, you can assign each Toolpad role to one or more provider roles under the **Role mappings** tab in the Authorization options in the editor.

To assign more than one provider role to a Toolpad role, you can separate the values with commas under the **Provider role** column (as in `viewer, editor, admin`).

{{"component": "modules/components/DocsImage.tsx", "src": "/static/toolpad/docs/concepts/authorization/role-mapping-settings.png", "alt": "Role mapping settings", "caption": "Role mapping settings", "indent": 1, "aspectRatio": 6 }}
7 changes: 7 additions & 0 deletions docs/pages/toolpad/concepts/authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import * as pageProps from '../../../data/toolpad/concepts/authentication.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs disableAd {...pageProps} />;
}
7 changes: 7 additions & 0 deletions docs/pages/toolpad/concepts/rbac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import * as pageProps from '../../../data/toolpad/concepts/rbac.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs disableAd {...pageProps} />;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions packages/toolpad-app/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ export const VERSION_CHECK_INTERVAL = 1000 * 60 * 10;

// TODO: Remove once global functions UI is ready
export const FEATURE_FLAG_GLOBAL_FUNCTIONS = false;

export const FEATURE_FLAG_AUTHORIZATION = false;
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import UrlQueryEditor from './UrlQueryEditor';
import NodeNameEditor from '../NodeNameEditor';
import PageTitleEditor from '../PageTitleEditor';
import PageDisplayNameEditor from '../PageDisplayNameEditor';
import { FEATURE_FLAG_AUTHORIZATION } from '../../../constants';

const PAGE_DISPLAY_OPTIONS: { value: appDom.PageDisplayMode; label: string }[] = [
{ value: 'shell', label: 'App shell' },
Expand Down Expand Up @@ -119,27 +118,25 @@ export default function PageOptionsPanel() {
</ToggleButtonGroup>
</Tooltip>
</div>
{FEATURE_FLAG_AUTHORIZATION ? (
<div>
<Typography variant="body2">Authorization:</Typography>
<FormControlLabel
control={<Checkbox checked={allowAll} onChange={handleAllowAllChange} />}
label="Allow access to all roles"
/>
<Autocomplete
multiple
options={Array.from(availableRoles.keys())}
value={allowAll ? [] : allowedRoles}
onChange={handleAllowedRolesChange}
disabled={allowAll}
fullWidth
noOptionsText="No roles defined"
renderInput={(params) => (
<TextField {...params} label="Allowed roles" placeholder="Roles" />
)}
/>
</div>
) : null}
<div>
<Typography variant="body2">Authorization:</Typography>
<FormControlLabel
control={<Checkbox checked={allowAll} onChange={handleAllowAllChange} />}
label="Allow access to all roles"
/>
<Autocomplete
multiple
options={Array.from(availableRoles.keys())}
value={allowAll ? [] : allowedRoles}
onChange={handleAllowedRolesChange}
disabled={allowAll}
fullWidth
noOptionsText="No roles defined"
renderInput={(params) => (
<TextField {...params} label="Allowed roles" placeholder="Roles" />
)}
/>
</div>
{appDom.isCodePage(page) ? null : (
<div>
<Divider variant="middle" sx={{ alignSelf: 'stretch' }} />
Expand Down
10 changes: 4 additions & 6 deletions packages/toolpad-app/src/toolpad/Toolpad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { APP_FUNCTIONS_ROUTE } from '../routes';
import ToolpadShell from './ToolpadShell';
import { getViewFromPathname } from '../utils/domView';
import AppProvider, { AppState, useAppStateContext } from './AppState';
import { FEATURE_FLAG_AUTHORIZATION, FEATURE_FLAG_GLOBAL_FUNCTIONS } from '../constants';
import { FEATURE_FLAG_GLOBAL_FUNCTIONS } from '../constants';
import { ProjectProvider } from '../project';
import AppAuthorizationDialog from './AppEditor/AppAuthorizationEditor';

Expand Down Expand Up @@ -104,11 +104,9 @@ function EditorShell({ children }: EditorShellProps) {
return (
<ToolpadShell
navigation={
FEATURE_FLAG_AUTHORIZATION ? (
<Stack sx={{ ml: 3 }}>
<Button onClick={handleAuthorizationDialogOpen}>Authorization</Button>
</Stack>
) : null
<Stack sx={{ ml: 3 }}>
<Button onClick={handleAuthorizationDialogOpen}>Authorization</Button>
</Stack>
}
actions={
previewPath ? (
Expand Down
Loading