-
Notifications
You must be signed in to change notification settings - Fork 0
EDFIAL-352-EDFIAL-353-Auth setup #7
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
rtavernaea
wants to merge
5
commits into
development
Choose a base branch
from
EDFIAL-352-EDFIAL-353-Auth-setup
base: development
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
5 commits
Select commit
Hold shift + click to select a range
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 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,50 @@ | ||
| import { sessionData } from '../helpers/session/session-factory'; | ||
| import { userA } from '../fixtures/user-fixtures'; | ||
| import { tenantA } from '../fixtures/context-fixtures/tenant-fixtures'; | ||
| import { allBundles } from '../fixtures/em-bundle-fixtures'; | ||
| import { sessionCookie } from '../helpers/session/session-cookie'; | ||
| import sessionStore from '../helpers/session/session-store'; | ||
| import request from 'supertest'; | ||
| import { EarthbeamBundlesService } from 'api/src/earthbeam/earthbeam-bundles.service'; | ||
| import { SessionData } from 'express-session'; | ||
|
|
||
| describe('POST /partners', () => { | ||
| const endpoint = '/partners/assessments/test'; | ||
| it('should reject unauthenticated requests', async () => { | ||
| const res = await request(app.getHttpServer()).post(endpoint); | ||
| expect(res.status).toBe(401); | ||
| }); | ||
|
|
||
| describe('authenticated requests', () => { | ||
| const sessA = sessionCookie('partners-spec-a'); | ||
| let mockGetBundles = jest | ||
| .spyOn(EarthbeamBundlesService.prototype, 'getBundles') | ||
| .mockResolvedValue(allBundles); | ||
|
|
||
| beforeEach(async () => { | ||
| jest.spyOn(EarthbeamBundlesService.prototype, 'getBundles').mockResolvedValue(allBundles); | ||
| await sessionStore.set(sessA.sid, sessionData(userA, tenantA)); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await sessionStore.destroy(sessA.sid); | ||
| mockGetBundles.mockRestore(); | ||
| }); | ||
|
|
||
| it('should reject requests from user without the PartnerAdmin role', async () => { | ||
| const resA = await request(app.getHttpServer()) | ||
| .post('/partners/assessments/test') | ||
| .set('Cookie', [sessA.cookie]); | ||
| expect(resA.status).toBe(403); | ||
| }); | ||
| it('allow partner admins to call this route', async () => { | ||
| const session = await sessionStore.get(sessA.sid); | ||
| session?.passport?.user.roles.push('PartnerAdmin'); | ||
| await sessionStore.set(sessA.sid, session as SessionData); | ||
| const resA = await request(app.getHttpServer()) | ||
| .post('/partners/assessments/test') | ||
| .set('Cookie', [sessA.cookie]); | ||
| expect(resA.status).toBe(201); | ||
| }); | ||
| }); | ||
| }); |
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,6 @@ | ||
| import { SetMetadata } from '@nestjs/common'; | ||
| import { PrivilegeKey } from 'models/src/dtos/privileges'; | ||
|
|
||
| export const AUTHORIZE_KEY = 'authorize_rule'; | ||
|
|
||
| export const Authorize = (privilege: PrivilegeKey | null) => SetMetadata(AUTHORIZE_KEY, privilege); |
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,54 @@ | ||
| import { GetSessionDataDto } from '@edanalytics/models'; | ||
| import { | ||
| CanActivate, | ||
| ExecutionContext, | ||
| HttpException, | ||
| Inject, | ||
| Injectable, | ||
| Logger, | ||
| } from '@nestjs/common'; | ||
| import { Reflector } from '@nestjs/core'; | ||
| import { PrivilegeKey } from 'models/src/dtos/privileges'; | ||
| import { AUTHORIZE_KEY } from '../helpers/authorize.decorator'; | ||
| import { IS_PUBLIC_KEY } from './public.decorator'; | ||
| import { Request } from 'express'; | ||
| import { plainToInstance } from 'class-transformer'; | ||
| import { AuthService } from '../auth.service'; | ||
| @Injectable() | ||
| export class AuthorizedGuard implements CanActivate { | ||
| constructor( | ||
| private reflector: Reflector, | ||
| @Inject(AuthService) private readonly authService: AuthService | ||
| ) {} | ||
|
|
||
| async canActivate(context: ExecutionContext): Promise<boolean> { | ||
| const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [ | ||
| context.getHandler(), | ||
| context.getClass(), | ||
| ]); | ||
|
|
||
| if (isPublic) return true; | ||
|
|
||
| const privilege = this.reflector.getAllAndOverride<PrivilegeKey | null>(AUTHORIZE_KEY, [ | ||
| context.getHandler(), | ||
| context.getClass(), | ||
| ]); | ||
|
|
||
| if (privilege === undefined) return true; | ||
|
|
||
| const request: Request = context.switchToHttp().getRequest(); | ||
| if (privilege === null) { | ||
| Logger.verbose('Authorization explicitly skipped for route ' + request.url); | ||
| return true; | ||
| } | ||
|
|
||
| if (!request.isAuthenticated()) return false; | ||
| try { | ||
| const userDto = plainToInstance(GetSessionDataDto, request['user']); | ||
| return userDto.privileges.has(privilege); | ||
| } catch (err) { | ||
| Logger.log(err); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
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,13 @@ | ||
| import { Controller, Post } from '@nestjs/common'; | ||
| import { ApiTags } from '@nestjs/swagger'; | ||
| import { Authorize } from '../auth/helpers/authorize.decorator'; | ||
|
|
||
| @Controller() | ||
| @ApiTags('Partners') | ||
| export class PartnersController { | ||
| constructor() {} | ||
|
|
||
| @Authorize('partner-earthmover-bundle.create') | ||
| @Post(':type/:bundleKey') | ||
| async enableBundle() {} | ||
| } |
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 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { PartnersController } from './partners.controller'; | ||
|
|
||
| @Module({ | ||
| imports: [], | ||
| providers: [], | ||
| controllers: [PartnersController], | ||
| }) | ||
| export class PartnersModule {} |
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 @@ | ||
| export type PrivilegeKey = | ||
| | 'partner-earthmover-bundle.read' | ||
| | 'partner-earthmover-bundle.create' | ||
| | 'partner-earthmover-bundle.delete'; |
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,13 @@ | ||
| import { PrivilegeKey } from './privileges'; | ||
|
|
||
| export type AppRoles = 'PartnerAdmin'; | ||
|
|
||
| export const rolePrivileges: Record<AppRoles, Set<PrivilegeKey>> = Object.freeze({ | ||
| PartnerAdmin: Object.freeze( | ||
| new Set<PrivilegeKey>([ | ||
| 'partner-earthmover-bundle.read', | ||
| 'partner-earthmover-bundle.create', | ||
| 'partner-earthmover-bundle.delete', | ||
| ]) | ||
| ), | ||
| }); |
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
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.