-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): Polish api custom-widget controller, service and autor…
…ization
- Loading branch information
1 parent
72c2a74
commit 89ff63f
Showing
9 changed files
with
109 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
|
||
/** | ||
* @description Decorator to inject a IS_PUBLIC_KEY metadata to the handler, which will be read by the JwtAuthGuard to allow public access to the handler. | ||
*/ | ||
|
||
export const AUTHORIZATION_CHECKS = 'AUTHORIZATION_CHECKS'; | ||
|
||
export const AUTHORIZATION_STRATEGIES = { | ||
USER_ID: 'userId', | ||
ROLE: 'role', | ||
} as const; | ||
|
||
export type AuthorizationCheck = [ | ||
(typeof AUTHORIZATION_STRATEGIES)[keyof typeof AUTHORIZATION_STRATEGIES], | ||
params?: unknown[], | ||
]; | ||
|
||
export const CheckAuthorization = ( | ||
...authorizationChecks: AuthorizationCheck[] | ||
) => SetMetadata(AUTHORIZATION_CHECKS, authorizationChecks); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// auth-user.guard.ts | ||
import { | ||
Injectable, | ||
CanActivate, | ||
ExecutionContext, | ||
ForbiddenException, | ||
} from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
|
||
@Injectable() | ||
export class CheckAuthentication implements CanActivate { | ||
constructor(private reflector: Reflector) {} | ||
|
||
public canActivate(context: ExecutionContext): boolean { | ||
const request = context.switchToHttp().getRequest(); | ||
const user = request.user; | ||
const userIdFromParams = request.params.userId; | ||
|
||
// Retrieve roles metadata set by @CheckAuthorization | ||
const requiredRoles = this.reflector.get<string[]>( | ||
'roles', | ||
context.getHandler(), | ||
); | ||
|
||
// Check if the user ID from the request matches the one in the params | ||
if (userIdFromParams && user.id !== userIdFromParams) { | ||
throw new ForbiddenException('Access denied: User ID mismatch.'); | ||
} | ||
|
||
// If roles are required, check that the user has at least one of the required roles | ||
if (requiredRoles && requiredRoles.length > 0) { | ||
const hasRole = requiredRoles.some((role) => user.roles?.includes(role)); | ||
if (!hasRole) { | ||
throw new ForbiddenException('Access denied: Insufficient role.'); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters