Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit addc6cd

Browse files
committed
authz: don't treat unauthenticated routes any different
Signed-off-by: Stephan Renatus <[email protected]>
1 parent 7aef7ef commit addc6cd

File tree

6 files changed

+23
-17
lines changed

6 files changed

+23
-17
lines changed

policies/cats.rego

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package cats
2+
23
import rego.v1
34

4-
roles := {
5-
"maria": "admin",
6-
}
5+
roles := {"maria": "admin"}
76

87
default allow := false
9-
allow := allowed(input.user, input.action, input.resource)
108

11-
allowed(user, _, _) if user, "admin" in roles
12-
allowed(_, "get", _)
13-
allowed(_, "list", _)
9+
allow := allowed(input.user, input.action, input.resource, roles)
10+
11+
allowed(user, _, _, roles) if user, "admin" in roles
12+
13+
allowed(_, "get", _, _)
14+
15+
allowed(_, "list", _, _)

policies/hello.rego

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package hello
2+
3+
import rego.v1
4+
5+
default allow := true

policies/login.rego

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package login
2+
3+
import rego.v1
4+
5+
default allow := true

policies/profile.rego

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package profile
2+
23
import rego.v1
34

45
default allow := true

src/app.controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class AppController {
1111
@UseGuards(LocalAuthGuard)
1212
@Unauthenticated() // So global JWT guard doesn't prohibit logins
1313
@Post('auth/login')
14+
@AuthzQuery('login/allow')
1415
async login(@Request() req) {
1516
return this.authService.login(req.user);
1617
}
@@ -24,6 +25,7 @@ export class AppController {
2425

2526
@Get('hello')
2627
@Unauthenticated()
28+
@AuthzQuery('hello/allow')
2729
getInfo() {
2830
return { hello: 'world' };
2931
}

src/authz/authz.guard.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
22
import { ConfigService } from '@nestjs/config';
33
import { Reflector } from '@nestjs/core';
44

5-
import { IS_UNAUTHENTICATED_KEY } from '../authn/decorators/public';
65
import { AuthzService } from './authz.service';
76
import {
87
AUTHZ_EXTRA,
@@ -17,7 +16,7 @@ class InputPayload implements ToInput {
1716
constructor(extra: ((_: Request) => Record<string, any>)[], req: Request) {
1817
this.input = extra.reduce(
1918
(acc, add) => (add ? { ...acc, ...add(req) } : acc),
20-
{ user: req.user.username },
19+
{ user: req.user?.username },
2120
);
2221
}
2322

@@ -35,14 +34,6 @@ export class AuthzGuard implements CanActivate {
3534
) {}
3635

3736
async canActivate(context: ExecutionContext) {
38-
const isUnauthenticated = this.reflector.getAllAndOverride<boolean>(
39-
IS_UNAUTHENTICATED_KEY,
40-
[context.getHandler(), context.getClass()],
41-
);
42-
if (isUnauthenticated) {
43-
return true;
44-
}
45-
4637
const request = context.switchToHttp().getRequest();
4738
const inp = new InputPayload(
4839
this.reflector.getAll<((_: Request) => Record<string, any>)[]>(

0 commit comments

Comments
 (0)