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

Commit

Permalink
authz: don't treat unauthenticated routes any different
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus committed Jul 3, 2024
1 parent 7aef7ef commit addc6cd
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
16 changes: 9 additions & 7 deletions policies/cats.rego
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package cats

import rego.v1

roles := {
"maria": "admin",
}
roles := {"maria": "admin"}

default allow := false
allow := allowed(input.user, input.action, input.resource)

allowed(user, _, _) if user, "admin" in roles
allowed(_, "get", _)
allowed(_, "list", _)
allow := allowed(input.user, input.action, input.resource, roles)

allowed(user, _, _, roles) if user, "admin" in roles

allowed(_, "get", _, _)

allowed(_, "list", _, _)
5 changes: 5 additions & 0 deletions policies/hello.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package hello

import rego.v1

default allow := true
5 changes: 5 additions & 0 deletions policies/login.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package login

import rego.v1

default allow := true
1 change: 1 addition & 0 deletions policies/profile.rego
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package profile

import rego.v1

default allow := true
2 changes: 2 additions & 0 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class AppController {
@UseGuards(LocalAuthGuard)
@Unauthenticated() // So global JWT guard doesn't prohibit logins
@Post('auth/login')
@AuthzQuery('login/allow')
async login(@Request() req) {
return this.authService.login(req.user);
}
Expand All @@ -24,6 +25,7 @@ export class AppController {

@Get('hello')
@Unauthenticated()
@AuthzQuery('hello/allow')
getInfo() {
return { hello: 'world' };
}
Expand Down
11 changes: 1 addition & 10 deletions src/authz/authz.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Reflector } from '@nestjs/core';

import { IS_UNAUTHENTICATED_KEY } from '../authn/decorators/public';
import { AuthzService } from './authz.service';
import {
AUTHZ_EXTRA,
Expand All @@ -17,7 +16,7 @@ class InputPayload implements ToInput {
constructor(extra: ((_: Request) => Record<string, any>)[], req: Request) {
this.input = extra.reduce(
(acc, add) => (add ? { ...acc, ...add(req) } : acc),
{ user: req.user.username },
{ user: req.user?.username },
);
}

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

async canActivate(context: ExecutionContext) {
const isUnauthenticated = this.reflector.getAllAndOverride<boolean>(
IS_UNAUTHENTICATED_KEY,
[context.getHandler(), context.getClass()],
);
if (isUnauthenticated) {
return true;
}

const request = context.switchToHttp().getRequest();
const inp = new InputPayload(
this.reflector.getAll<((_: Request) => Record<string, any>)[]>(
Expand Down

0 comments on commit addc6cd

Please sign in to comment.