Skip to content

Commit d2868be

Browse files
committed
allow overriding default allowedRoles
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 <[email protected]>
1 parent 2b62b01 commit d2868be

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,69 @@ export default {
136136
})
137137
}
138138
};
139-
```
139+
```
140+
141+
### allowedRoles
142+
143+
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.
144+
145+
```js
146+
export default {
147+
kit: {
148+
...
149+
adapter: azure({
150+
allowedRoles: ['authenticated'],
151+
customStaticWebAppConfig: {
152+
routes: [
153+
{
154+
route: "/.auth/login/facebook",
155+
statusCode: 404
156+
},
157+
{
158+
route: "/.auth/login/github",
159+
statusCode: 404
160+
},
161+
{
162+
route: "/.auth/login/google",
163+
statusCode: 404
164+
},
165+
{
166+
route: "/.auth/login/twitter",
167+
statusCode: 404
168+
},
169+
{
170+
route: "/.auth/*",
171+
allowedRoles: [
172+
"anonymous"
173+
]
174+
},
175+
{
176+
route: '/login',
177+
allowedRoles: [
178+
"anonymous"
179+
],
180+
rewrite: "/.auth/login/aad",
181+
}
182+
],
183+
responseOverrides: {
184+
'401': {
185+
'redirect': '/login',
186+
'statusCode': 302
187+
}
188+
},
189+
auth: {
190+
identityProviders: {
191+
azureActiveDirectory: {
192+
registration: {
193+
openIdIssuer: "AAD_ISSUER",
194+
clientIdSettingName: "AAD_CLIENT_ID",
195+
clientSecretSettingName: "AAD_CLIENT_SECRET"
196+
}
197+
}
198+
}
199+
}
200+
}
201+
})
202+
}
203+
}
204+
```

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type Options = {
66
debug?: boolean;
77
customStaticWebAppConfig?: CustomStaticWebAppConfig;
88
esbuildOptions?: Pick<esbuild.BuildOptions, 'external'>;
9+
allowedRoles?: string[];
910
};
1011

1112
export default function plugin(options?: Options): Adapter;

index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ function validateCustomConfig(config) {
2828
export default function ({
2929
debug = false,
3030
customStaticWebAppConfig = {},
31-
esbuildOptions = {}
31+
esbuildOptions = {},
32+
allowedRoles = ['anonymous']
3233
} = {}) {
3334
return {
3435
name: 'adapter-azure-swa',
@@ -48,13 +49,15 @@ export default function ({
4849
{
4950
route: '*',
5051
methods: ['POST', 'PUT', 'DELETE'],
51-
rewrite: ssrFunctionRoute
52+
rewrite: ssrFunctionRoute,
53+
allowedRoles: allowedRoles
5254
},
5355
{
5456
route: `/${builder.config.kit.appDir}/immutable/*`,
5557
headers: {
5658
'cache-control': 'public, immutable, max-age=31536000'
57-
}
59+
},
60+
allowedRoles: allowedRoles
5861
}
5962
],
6063
navigationFallback: {
@@ -130,15 +133,24 @@ export default function ({
130133
swaConfig.routes.push(
131134
{
132135
route: '/index.html',
133-
rewrite: ssrFunctionRoute
136+
rewrite: ssrFunctionRoute,
137+
allowedRoles: allowedRoles
134138
},
135139
{
136140
route: '/',
137-
rewrite: ssrFunctionRoute
141+
rewrite: ssrFunctionRoute,
142+
allowedRoles: allowedRoles
138143
}
139144
);
140145
}
141146

147+
swaConfig.routes.push(
148+
{
149+
route: '*',
150+
allowedRoles: allowedRoles
151+
}
152+
);
153+
142154
writeFileSync(`${publish}/staticwebapp.config.json`, JSON.stringify(swaConfig));
143155
}
144156
};

0 commit comments

Comments
 (0)