Skip to content

Commit

Permalink
allow overriding default allowedRoles
Browse files Browse the repository at this point in the history
If for e.g. the complete app should be guarded by authentication it's necessary to
apply an authenticated role to every route.

Signed-off-by: Tobias Kohlbau <tobias@kohlbau.de>
  • Loading branch information
tobiaskohlbau committed Aug 23, 2022
1 parent 2b62b01 commit d2868be
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
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.

```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"
}
}
}
}
}
})
}
}
```
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']
} = {}) {
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
}
);

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

0 comments on commit d2868be

Please sign in to comment.