Skip to content

Commit cae49ac

Browse files
authored
Merge pull request #222274 from chcomley/users/chcomley/swa-authentication-authorization-12-14-2022
[SWA] Refresh/peer review Authentication-Authorization
2 parents 660b001 + b71e9e2 commit cae49ac

File tree

7 files changed

+244
-239
lines changed

7 files changed

+244
-239
lines changed

articles/static-web-apps/assign-roles-microsoft-graph.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ In this tutorial, you learn to:
1919
- Deploy a static web app.
2020
- Create an Azure Active Directory app registration.
2121
- Set up custom authentication with Azure Active Directory.
22-
- Configure a [serverless function](authentication-authorization.md?tabs=function#role-management) that queries the user's Active Directory group membership and returns a list of custom roles.
22+
- Configure a [serverless function](authentication-custom.md#manage-roles) that queries the user's Active Directory group membership and returns a list of custom roles.
2323

2424
> [!NOTE]
25-
> This tutorial requires you to [use a function to assign roles](authentication-authorization.md?tabs=function#role-management). Function-based role management is currently in preview.
25+
> This tutorial requires you to [use a function to assign roles](authentication-custom.md#manage-roles). Function-based role management is currently in preview.
2626
2727
## Prerequisites
2828

articles/static-web-apps/authentication-authorization.md

Lines changed: 55 additions & 232 deletions
Large diffs are not rendered by default.

articles/static-web-apps/authentication-custom.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,188 @@ To use a custom identity provider, use the following URL patterns.
317317

318318
If you are using Azure Active Directory, use `aad` as the value for the `<PROVIDER_NAME_IN_CONFIG>` placeholder.
319319

320+
## Manage roles
321+
322+
Every user who accesses a static web app belongs to one or more roles. There are two built-in roles that users can belong to:
323+
324+
- **anonymous**: All users automatically belong to the _anonymous_ role.
325+
- **authenticated**: All users who are signed in belong to the _authenticated_ role.
326+
327+
Beyond the built-in roles, you can assign custom roles to users, and reference them in the _staticwebapp.config.json_ file.
328+
329+
# [Invitations](#tab/invitations)
330+
331+
### Add a user to a role
332+
333+
To add a user to a role, you generate invitations that allow you to associate users to specific roles. Roles are defined and maintained in the _staticwebapp.config.json_ file.
334+
335+
<a name="invitations" id="invitations"></a>
336+
337+
#### Create an invitation
338+
339+
Invitations are specific to individual authorization-providers, so consider the needs of your app as you select which providers to support. Some providers expose a user's email address, while others only provide the site's username.
340+
341+
<a name="provider-user-details" id="provider-user-details"></a>
342+
343+
| Authorization provider | Exposes |
344+
| ---------------------- | ---------------- |
345+
| Azure AD | email address |
346+
| GitHub | username |
347+
| Twitter | username |
348+
349+
Do the following steps to create an invitation.
350+
351+
1. Go to a Static Web Apps resource in the [Azure portal](https://portal.azure.com).
352+
2. Under _Settings_, select **Role Management**.
353+
3. Select **Invite**.
354+
4. Select an _Authorization provider_ from the list of options.
355+
5. Add either the username or email address of the recipient in the _Invitee details_ box.
356+
- For GitHub and Twitter, enter the username. For all others, enter the recipient's email address.
357+
6. Select the domain of your static site from the _Domain_ drop-down menu.
358+
- The domain you select is the domain that appears in the invitation. If you have a custom domain associated with your site, choose the custom domain.
359+
7. Add a comma-separated list of role names in the _Role_ box.
360+
8. Enter the maximum number of hours you want the invitation to remain valid.
361+
- The maximum possible limit is 168 hours, which is seven days.
362+
9. Select **Generate**.
363+
10. Copy the link from the _Invite link_ box.
364+
11. Email the invitation link to the user that you're granting access to.
365+
366+
When the user selects the link in the invitation, they're prompted to sign in with their corresponding account. Once successfully signed in, the user is associated with the selected roles.
367+
368+
> [!CAUTION]
369+
> Make sure your route rules don't conflict with your selected authentication providers. Blocking a provider with a route rule prevents users from accepting invitations.
370+
371+
### Update role assignments
372+
373+
1. Go to a Static Web Apps resource in the [Azure portal](https://portal.azure.com).
374+
1. Under _Settings_, select **Role Management**.
375+
2. Select the user in the list.
376+
3. Edit the list of roles in the _Role_ box.
377+
4. Select **Update**.
378+
379+
### Remove user
380+
381+
1. Go to a Static Web Apps resource in the [Azure portal](https://portal.azure.com).
382+
1. Under _Settings_, select **Role Management**.
383+
1. Locate the user in the list.
384+
1. Check the checkbox on the user's row.
385+
2. Select **Delete**.
386+
387+
As you remove a user, keep in mind the following items:
388+
389+
- Removing a user invalidates their permissions.
390+
- Worldwide propagation may take a few minutes.
391+
- If the user is added back to the app, the [`userId` changes](user-information.md).
392+
393+
# [Function (preview)](#tab/function)
394+
395+
Instead of using the built-in invitations system, you can use a serverless function to programmatically assign roles to users when they sign in.
396+
397+
To assign custom roles in a function, you can define an API function that is automatically called after each time a user successfully authenticates with an identity provider. The function is passed the user's information from the provider. It must return a list of custom roles that are assigned to the user.
398+
399+
Example uses of this function include:
400+
401+
- Query a database to determine which roles a user should be assigned
402+
- Call the [Microsoft Graph API](https://developer.microsoft.com/graph) to determine a user's roles based on their Active Directory group membership
403+
- Determine a user's roles based on claims returned by the identity provider
404+
405+
> [!NOTE]
406+
> The ability to assign roles via a function is only available when [custom authentication](authentication-custom.md) is configured.
407+
>
408+
> When this feature is enabled, any roles assigned via the built-in invitations system are ignored.
409+
410+
### Configure a function for assigning roles
411+
412+
To configure Static Web Apps to use an API function as the role assignment function, add a `rolesSource` property to the `auth` section of your app's [configuration file](configuration.md). The value of the `rolesSource` property is the path to the API function.
413+
414+
```json
415+
{
416+
"auth": {
417+
"rolesSource": "/api/GetRoles",
418+
"identityProviders": {
419+
// ...
420+
}
421+
}
422+
}
423+
```
424+
425+
> [!NOTE]
426+
> Once configured, the role assignment function can no longer be accessed by external HTTP requests.
427+
428+
### Create a function for assigning roles
429+
430+
After you define the `rolesSource` property in your app's configuration, add an [API function](apis-functions.md) in your static web app at the specified path. You can use a managed function app or [bring your own function app](functions-bring-your-own.md).
431+
432+
Each time a user successfully authenticates with an identity provider, the POST method calls the specified function. The function passes a JSON object in the request body that contains the user's information from the provider. For some identity providers, the user information also includes an `accessToken` that the function can use to make API calls using the user's identity.
433+
434+
See the following example payload from Azure AD:
435+
436+
```json
437+
{
438+
"identityProvider": "aad",
439+
"userId": "72137ad3-ae00-42b5-8d54-aacb38576d76",
440+
"userDetails": "[email protected]",
441+
"claims": [
442+
{
443+
"typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress",
444+
445+
},
446+
{
447+
"typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname",
448+
"val": "Contoso"
449+
},
450+
{
451+
"typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname",
452+
"val": "Ellen"
453+
},
454+
{
455+
"typ": "name",
456+
"val": "Ellen Contoso"
457+
},
458+
{
459+
"typ": "http://schemas.microsoft.com/identity/claims/objectidentifier",
460+
"val": "7da753ff-1c8e-4b5e-affe-d89e5a57fe2f"
461+
},
462+
{
463+
"typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
464+
"val": "72137ad3-ae00-42b5-8d54-aacb38576d76"
465+
},
466+
{
467+
"typ": "http://schemas.microsoft.com/identity/claims/tenantid",
468+
"val": "3856f5f5-4bae-464a-9044-b72dc2dcde26"
469+
},
470+
{
471+
"typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
472+
473+
},
474+
{
475+
"typ": "ver",
476+
"val": "1.0"
477+
}
478+
],
479+
"accessToken": "eyJ0eXAiOiJKV..."
480+
}
481+
```
482+
483+
The function can use the user's information to determine which roles to assign to the user. It must return an HTTP 200 response with a JSON body containing a list of custom role names to assign to the user.
484+
485+
For example, to assign the user to the `Reader` and `Contributor` roles, return the following response:
486+
487+
```json
488+
{
489+
"roles": [
490+
"Reader",
491+
"Contributor"
492+
]
493+
}
494+
```
495+
496+
If you don't want to assign any other roles to the user, return an empty `roles` array.
497+
498+
To learn more, see [Tutorial: Assign custom roles with a function and Microsoft Graph](assign-roles-microsoft-graph.md).
499+
500+
---
501+
320502
## Next steps
321503

322504
> [!div class="nextstepaction"]

articles/static-web-apps/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ In addition to IP address blocks, you can also specify [service tags](../virtual
353353

354354
## Authentication
355355

356-
* [Default authentication providers](authentication-authorization.md#login), don't require settings in the configuration file.
356+
* [Default authentication providers](authentication-authorization.md#set-up-sign-in), don't require settings in the configuration file.
357357
* [Custom authentication providers](authentication-custom.md) use the `auth` section of the settings file.
358358

359359
For details on how to restrict routes to authenticated users, see [Securing routes with roles](#securing-routes-with-roles).

articles/static-web-apps/faq.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ sections:
157157
- question: |
158158
How many users can log in to my static web app?
159159
answer:
160-
Static Web Apps doesn't have a limit on the number of users that can log in to your app. You can assign custom roles to up to 25 users using the built-in invitations system. If you need to assign custom roles to more users, you can use an [API function to programmatically assign roles](authentication-authorization.md?tabs=function#role-management).
160+
Static Web Apps doesn't have a limit on the number of users that can log in to your app. You can assign custom roles to up to 25 users using the built-in invitations system. If you need to assign custom roles to more users, you can use an [API function to programmatically assign roles](authentication-custom.md#manage-roles).
161161
162162
- question: |
163163
How do I use the retrieve a user's access token or claims from the identity provider?
164164
answer: |
165-
You can retrieve the user's access token and claims when you use an [API function for role management](authentication-authorization.md?tabs=function#role-management).
165+
You can retrieve the user's access token and claims when you use an [API function for role management](authentication-custom.md#manage-roles).
166166
167167
- question: |
168168
Am I limited to using a single identity provider?

articles/static-web-apps/plans.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Azure Static Web Apps is available through two different plans, Free and Standar
2727
| Custom domains | 2 per app | 5 per app |
2828
| APIs via Azure Functions | Managed | Managed or<br>[Bring your own Functions app](functions-bring-your-own.md) |
2929
| Authentication provider integration | [Pre-configured](authentication-authorization.md)<br>(Service defined) | [Custom registrations](authentication-custom.md) |
30-
| [Assign custom roles with a function](authentication-authorization.md?tabs=function#role-management) | - ||
30+
| [Assign custom roles with a function](authentication-custom.md#manage-roles) | - ||
3131
| Private endpoints | - ||
3232
| [Service Level Agreement (SLA)](https://azure.microsoft.com/support/legal/sla/app-service-static/v1_0/) | None ||
3333

articles/static-web-apps/quotas.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The following quotas exist for Azure Static Web Apps.
2424
| Custom domains | 2 per app | 5 per app |
2525
| Allowed IP ranges | Unavailable | 25 |
2626
| Authorization (built-in roles) | Unlimited end-users that may authenticate with built-in `authenticated` role | Unlimited end-users that may authenticate with built-in `authenticated` role |
27-
| Authorization (custom roles) | Maximum of 25 end-users that may belong to custom roles via [invitations](authentication-authorization.md?tabs=invitations#role-management) | Maximum of 25 end-users that may belong to custom roles via [invitations](authentication-authorization.md?tabs=invitations#role-management), or unlimited end-users that may be assigned custom roles via [serverless function](authentication-authorization.md?tabs=function#role-management) |
27+
| Authorization (custom roles) | Maximum of 25 end-users that may belong to custom roles via [invitations](authentication-custom.md#manage-roles) | Maximum of 25 end-users that may belong to custom roles via [invitations](authentication-custom.md#manage-roles), or unlimited end-users that may be assigned custom roles via [serverless function](authentication-custom.md#manage-roles) |
2828
| Request Size Limit | 30 MB | 30 MB |
2929

3030
## GitHub storage

0 commit comments

Comments
 (0)