forked from Code-4-Community/scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 0
Role based auth backend #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dburkhart07
wants to merge
26
commits into
main
Choose a base branch
from
role-based-auth-backend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
34edd92
Initial JWT Auth for backend
dburkhart07 2011b66
Added guards for jwt auth
dburkhart07 bb180d4
Updated auth
dburkhart07 1a95f15
Tried fixing JWT Strategy
dburkhart07 8587514
Updated auth
dburkhart07 8f251c8
Finished general authentication for both frontend and backend pages
dburkhart07 89677e3
Final commit for this branch
dburkhart07 267b6eb
Revisions made with Sam!!!
dburkhart07 44f537d
Resolved merge conflicts
dburkhart07 a5a7852
Resolved merge conflicts
dburkhart07 20a26ba
Another main merge
dburkhart07 88f8d3c
Fixed all errors with modules
dburkhart07 5421fe6
prettier
dburkhart07 6aea768
Fixed module importing
dburkhart07 caa33e9
prettier
dburkhart07 390b380
Added back in donation migration
dburkhart07 f1bad91
Full implementation of backend role-based auth
dburkhart07 f7621f5
prettier
dburkhart07 1331bbb
[SSF 17] - environment variables updates (#44)
dburkhart07 d69b3c8
Fixed user flow to use a cognito id hardcoded into the database
dburkhart07 fe466d5
prettier
dburkhart07 5f9f97b
Resolved merge conflicts
dburkhart07 3267bb5
Added requested changes
dburkhart07 34f86e0
Added in decorator and guard for a bypass gaurd
dburkhart07 99c018a
Final commit
dburkhart07 86e7364
Merged main
dburkhart07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,15 +1,17 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { forwardRef, Module } from '@nestjs/common'; | ||
| import { TypeOrmModule } from '@nestjs/typeorm'; | ||
| import { Allocation } from './allocations.entity'; | ||
| import { AllocationsController } from './allocations.controller'; | ||
| import { AllocationsService } from './allocations.service'; | ||
| import { AuthService } from '../auth/auth.service'; | ||
| import { JwtStrategy } from '../auth/jwt.strategy'; | ||
| import { AuthModule } from '../auth/auth.module'; | ||
|
|
||
| @Module({ | ||
| imports: [TypeOrmModule.forFeature([Allocation])], | ||
| imports: [ | ||
| TypeOrmModule.forFeature([Allocation]), | ||
| forwardRef(() => AuthModule), | ||
| ], | ||
| controllers: [AllocationsController], | ||
| providers: [AllocationsService, AuthService, JwtStrategy], | ||
| providers: [AllocationsService], | ||
| exports: [AllocationsService], | ||
| }) | ||
| export class AllocationModule {} |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,14 +1,17 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { Module, forwardRef } from '@nestjs/common'; | ||
| import { PassportModule } from '@nestjs/passport'; | ||
|
|
||
| import { AuthController } from './auth.controller'; | ||
| import { AuthService } from './auth.service'; | ||
| import { JwtStrategy } from './jwt.strategy'; | ||
| import { UsersModule } from '../users/users.module'; | ||
|
|
||
| @Module({ | ||
| imports: [UsersModule, PassportModule.register({ defaultStrategy: 'jwt' })], | ||
| imports: [ | ||
| forwardRef(() => UsersModule), | ||
| PassportModule.register({ defaultStrategy: 'jwt' }), | ||
| ], | ||
| controllers: [AuthController], | ||
| providers: [AuthService, JwtStrategy], | ||
| exports: [AuthService, JwtStrategy], | ||
| }) | ||
| export class AuthModule {} |
This file contains hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| import { Injectable, ExecutionContext } from '@nestjs/common'; | ||
| import { AuthGuard } from '@nestjs/passport'; | ||
| import { Reflector } from '@nestjs/core'; | ||
| import { IS_PUBLIC_KEY } from './public.decorator'; | ||
|
|
||
| // Extension onto AuthGuard to add public route handling | ||
| @Injectable() | ||
| export class JwtAuthGuard extends AuthGuard('jwt') { | ||
| constructor(private reflector: Reflector) { | ||
| super(); | ||
| } | ||
|
|
||
| canActivate(context: ExecutionContext) { | ||
| const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [ | ||
| context.getHandler(), | ||
| context.getClass(), | ||
| ]); | ||
|
|
||
| if (isPublic) { | ||
| return true; | ||
| } | ||
|
|
||
| return super.canActivate(context); | ||
| } | ||
| } |
This file contains hidden or 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,9 @@ | ||
| export interface CognitoJwtPayload { | ||
| sub: string; | ||
| email?: string; | ||
| username?: string; | ||
| aud?: string; | ||
| iss?: string; | ||
| exp?: number; | ||
| iat?: number; | ||
| } |
This file contains hidden or 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 hidden or 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,4 @@ | ||
| import { SetMetadata } from '@nestjs/common'; | ||
|
|
||
| export const IS_PUBLIC_KEY = 'isPublic'; | ||
| export const Public = () => SetMetadata(IS_PUBLIC_KEY, true); |
This file contains hidden or 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,7 @@ | ||
| import { SetMetadata } from '@nestjs/common'; | ||
| import { Role } from '../users/types'; | ||
|
|
||
dburkhart07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Key used to store roles metadata | ||
| export const ROLES_KEY = 'roles'; | ||
| // Custom decorator to set roles metadata on route handlers for proper parsing by RolesGuard | ||
| export const Roles = (...roles: Role[]) => SetMetadata(ROLES_KEY, roles); | ||
This file contains hidden or 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,37 @@ | ||
| import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; | ||
| import { Reflector } from '@nestjs/core'; | ||
| import { Role } from '../users/types'; | ||
| import { ROLES_KEY } from './roles.decorator'; | ||
|
|
||
| // Guard to enforce role-based access control on route handlers | ||
| // Applies logic to get us our user, and compare it with the required roles | ||
| // Interacts with the metadata that we attach in the @Roles() decorator | ||
| @Injectable() | ||
| export class RolesGuard implements CanActivate { | ||
dburkhart07 marked this conversation as resolved.
Show resolved
Hide resolved
dburkhart07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| constructor(private reflector: Reflector) {} | ||
|
|
||
| // If this returns false, Nest will deny access to the route handler | ||
| // Automatically throwing a Forbidden Exception (403 status code) | ||
| canActivate(context: ExecutionContext): boolean { | ||
dburkhart07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Look for the metadata we set with the @Roles() decorator | ||
| // Checks in the route handler, then the controller, and makes it undefined if nothing found | ||
| // Routes take priority over controllers in terms of overriding | ||
| const requiredRoles = this.reflector.getAllAndOverride<Role[]>(ROLES_KEY, [ | ||
| context.getHandler(), // method-level | ||
| context.getClass(), // controller-level | ||
| ]); | ||
|
|
||
| if (!requiredRoles || requiredRoles.length === 0) { | ||
| return true; | ||
| } | ||
|
|
||
| const request = context.switchToHttp().getRequest(); | ||
| const user = request.user; | ||
|
|
||
| if (!user || !user.role) { | ||
| return false; | ||
| } | ||
|
|
||
| return requiredRoles.includes(user.role); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.