-
Notifications
You must be signed in to change notification settings - Fork 14
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
#3881 - Validate user account for all routes #3985
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
dae437d
iniital commit
dheepak-aot 62bb0ef
iniital commit
dheepak-aot 838259e
Merge branch 'main' into fix/#3881-validate-user-account
dheepak-aot e4feaa1
Updated student page container
dheepak-aot e832ce9
Updated container and replaced the deprecated Vue extension in worksp…
dheepak-aot 0f05543
Added the new decorator to escape validation.
dheepak-aot 74b878a
Added the new decorator to escape validation.
dheepak-aot a5e4141
Added the new decorator to escape validation.
dheepak-aot 25e3c20
Added the new decorator to escape validation.
dheepak-aot a2d28ad
Adjusted comments and fixed the import for E2E test.
dheepak-aot 59cb3fb
fixed the import for E2E test.
dheepak-aot e51618b
fixed the import for E2E test.
dheepak-aot 4239133
fixed the import for E2E test.
dheepak-aot 9648b60
fixed the import for E2E test.
dheepak-aot 5ae78e6
fixed the import for E2E test.
dheepak-aot 76fd726
fixed the import for E2E test.
dheepak-aot 67d9545
E2E Tests
dheepak-aot ab22b7c
E2E Tests
dheepak-aot 1d28189
E2E Tests
dheepak-aot c870916
E2E Tests
dheepak-aot edfc8bf
Review Comments
dheepak-aot 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
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
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
5 changes: 5 additions & 0 deletions
5
sources/packages/backend/apps/api/src/auth/decorators/common.decorator.ts
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,5 @@ | ||
import { Reflector } from "@nestjs/core"; | ||
/** | ||
* Specifies when a user account must be already created in order to access a route. | ||
*/ | ||
export const RequiresUserAccount = Reflector.createDecorator<boolean>(); |
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
55 changes: 55 additions & 0 deletions
55
sources/packages/backend/apps/api/src/auth/guards/requires-user-account.guard.ts
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,55 @@ | ||
import { | ||
Injectable, | ||
CanActivate, | ||
ExecutionContext, | ||
ForbiddenException, | ||
} from "@nestjs/common"; | ||
import { Reflector } from "@nestjs/core"; | ||
import { IS_PUBLIC_KEY, RequiresUserAccount } from "../decorators"; | ||
import { IUserToken } from "apps/api/src/auth/userToken.interface"; | ||
import { MISSING_USER_ACCOUNT } from "../../constants"; | ||
import { ApiProcessError } from "../../types"; | ||
|
||
/** | ||
* Validates that a user account must be already created in order to access a route. | ||
* Public routes and routes that do not require a user account are skipped. | ||
*/ | ||
@Injectable() | ||
export class RequiresUserAccountGuard implements CanActivate { | ||
andrewsignori-aot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
constructor(private readonly reflector: Reflector) {} | ||
|
||
canActivate(context: ExecutionContext): boolean { | ||
// Check if the route is public. | ||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [ | ||
context.getHandler(), | ||
context.getClass(), | ||
]); | ||
// If the route is public, no validation is required. | ||
if (isPublic) { | ||
return true; | ||
} | ||
|
||
const requiresUserAccount = this.reflector.getAllAndOverride( | ||
RequiresUserAccount, | ||
[context.getHandler(), context.getClass()], | ||
); | ||
|
||
if (requiresUserAccount === false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return true; | ||
} | ||
|
||
const { user } = context.switchToHttp().getRequest(); | ||
const userToken = user as IUserToken; | ||
|
||
if (!userToken?.userId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
throw new ForbiddenException( | ||
new ApiProcessError( | ||
"No user account has been associated to the user token.", | ||
MISSING_USER_ACCOUNT, | ||
), | ||
); | ||
} | ||
|
||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍