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

allow overriding default allowedRoles #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,69 @@ export default {
})
}
};
```
```

### allowedRoles

An array of strings containing the roles allowed to access the app. For e.g. if the app needs to be restrictied it's possible to use a [custom role](https://docs.microsoft.com/en-us/azure/static-web-apps/authentication-authorization). *Notice* this only supports securing the complete app. If specific routes should be secured it's best to implement custom authentication within the app itself.
Copy link
Owner

Choose a reason for hiding this comment

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

I wonder if it would be better to link docs on role configuration instead of the general auth docs:

Suggested change
An array of strings containing the roles allowed to access the app. For e.g. if the app needs to be restrictied it's possible to use a [custom role](https://docs.microsoft.com/en-us/azure/static-web-apps/authentication-authorization). *Notice* this only supports securing the complete app. If specific routes should be secured it's best to implement custom authentication within the app itself.
An array of strings containing the roles allowed to access the app. For example, if the app needs to be restricted, it's possible to use a [custom role](https://docs.microsoft.com/en-us/azure/static-web-apps/configuration#securing-routes-with-roles). **Note:** this only supports securing the complete app. If specific routes should be secured, you should implement custom authentication within the app itself.


```js
export default {
kit: {
...
adapter: azure({
allowedRoles: ['authenticated'],
customStaticWebAppConfig: {
routes: [
{
route: "/.auth/login/facebook",
statusCode: 404
},
{
route: "/.auth/login/github",
statusCode: 404
},
{
route: "/.auth/login/google",
statusCode: 404
},
{
route: "/.auth/login/twitter",
statusCode: 404
},
{
route: "/.auth/*",
allowedRoles: [
"anonymous"
]
},
{
route: '/login',
allowedRoles: [
"anonymous"
],
rewrite: "/.auth/login/aad",
}
],
responseOverrides: {
'401': {
'redirect': '/login',
'statusCode': 302
}
},
auth: {
identityProviders: {
azureActiveDirectory: {
registration: {
openIdIssuer: "AAD_ISSUER",
clientIdSettingName: "AAD_CLIENT_ID",
clientSecretSettingName: "AAD_CLIENT_SECRET"
}
}
}
}
}
Comment on lines +151 to +200
Copy link
Owner

Choose a reason for hiding this comment

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

Do we need this entire config in the docs? Can we just show setting allowedRoles?

})
}
}
```
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Options = {
debug?: boolean;
customStaticWebAppConfig?: CustomStaticWebAppConfig;
esbuildOptions?: Pick<esbuild.BuildOptions, 'external'>;
allowedRoles?: string[];
};

export default function plugin(options?: Options): Adapter;
22 changes: 17 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function validateCustomConfig(config) {
export default function ({
debug = false,
customStaticWebAppConfig = {},
esbuildOptions = {}
esbuildOptions = {},
allowedRoles = ['anonymous']
Copy link
Owner

Choose a reason for hiding this comment

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

I think the default should be undefined:

Suggested change
allowedRoles = ['anonymous']
allowedRoles

} = {}) {
return {
name: 'adapter-azure-swa',
Expand All @@ -48,13 +49,15 @@ export default function ({
{
route: '*',
methods: ['POST', 'PUT', 'DELETE'],
rewrite: ssrFunctionRoute
rewrite: ssrFunctionRoute,
allowedRoles: allowedRoles
},
{
route: `/${builder.config.kit.appDir}/immutable/*`,
headers: {
'cache-control': 'public, immutable, max-age=31536000'
}
},
allowedRoles: allowedRoles
}
],
navigationFallback: {
Expand Down Expand Up @@ -130,15 +133,24 @@ export default function ({
swaConfig.routes.push(
{
route: '/index.html',
rewrite: ssrFunctionRoute
rewrite: ssrFunctionRoute,
allowedRoles: allowedRoles
},
{
route: '/',
rewrite: ssrFunctionRoute
rewrite: ssrFunctionRoute,
allowedRoles: allowedRoles
}
);
}

swaConfig.routes.push(
{
route: '*',
allowedRoles: allowedRoles
}
);

Copy link
Owner

Choose a reason for hiding this comment

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

Instead of remembering to set allowedRoles on each route, can we apply them all at once? e.g.

swaConfig.routes = swaConfig.routes.map((r) => ({ ...r, allowedRoles }));

writeFileSync(`${publish}/staticwebapp.config.json`, JSON.stringify(swaConfig));
}
};
Expand Down