Skip to content
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

feat: support for user linked project permissions #147

Merged
merged 5 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/pr-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: PR Release

on:
pull_request:

jobs:
build_and_publish_branch:
name: Build and publish replication-backend package
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 20

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v5
with:
platforms: linux/amd64
context: ./
push: true
tags: ghcr.io/aam-digital/replication-backend:pr-${{ github.event.number }}
17 changes: 15 additions & 2 deletions src/auth/guards/jwt-bearer/jwt-bearer.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Injectable } from '@nestjs/common';
import { UserInfo } from '../../../restricted-endpoints/session/user-auth.dto';
import { ConfigService } from '@nestjs/config';
import { AuthModule } from '../../auth.module';
import { CouchdbService } from '../../../couchdb/couchdb.service';
import { firstValueFrom } from 'rxjs';

/**
* Authenticate a user with a foreign bearer JWT using the {@link AuthModule.JWT_PUBLIC_KEY}.
Expand All @@ -13,14 +15,25 @@ export class JwtBearerStrategy extends PassportStrategy(
Strategy,
'jwt-bearer',
) {
constructor(configService: ConfigService) {
constructor(
configService: ConfigService,
private couchdbService: CouchdbService,
) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: configService.get<string>(AuthModule.JWT_PUBLIC_KEY),
});
}

async validate(data: any): Promise<UserInfo> {
return new UserInfo(data.username, data['_couchdb.roles']);
const user = await firstValueFrom(
this.couchdbService.get('app', data['username']),
).catch(() => {});

return new UserInfo(
data.username,
data['_couchdb.roles'],
user && user['projects'] ? user['projects'] : [],
);
Comment on lines +29 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small code block, but also for future proofing I would suggest we put the UserInfo creation in a service rather than duplicate it.

}
}
17 changes: 15 additions & 2 deletions src/auth/guards/jwt-cookie/jwt-cookie-strategy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { AuthModule } from '../../auth.module';
import { UserInfo } from '../../../restricted-endpoints/session/user-auth.dto';
import { TOKEN_KEY } from '../../cookie/cookie.service';
import { ConfigService } from '@nestjs/config';
import { firstValueFrom } from 'rxjs';
import { CouchdbService } from '../../../couchdb/couchdb.service';

/**
* Authenticate a user using an existing JWT from a cookie in the request.
Expand All @@ -14,7 +16,10 @@ export class JwtCookieStrategy extends PassportStrategy(
Strategy,
'jwt-cookie',
) {
constructor(configService: ConfigService) {
constructor(
configService: ConfigService,
private couchdbService: CouchdbService,
) {
super({
jwtFromRequest: (req) => req?.cookies[TOKEN_KEY],
ignoreExpiration: false,
Expand All @@ -23,6 +28,14 @@ export class JwtCookieStrategy extends PassportStrategy(
}

async validate(data: any): Promise<UserInfo> {
return new UserInfo(data.name, data.sub);
const user = await firstValueFrom(
this.couchdbService.get('app', data['username']),
).catch(() => {});

return new UserInfo(
data.name,
data.sub,
user && user['projects'] ? user['projects'] : [],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,10 @@ describe('DocumentController', () => {
});

it('should throw exception if the update permission is not given', () => {
const otherUser = {
const otherUser: UserInfo = {
name: 'anotherUser',
roles: [],
projects: [],
};
mockAbility([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('ChangesController', () => {
childDoc,
deletedChildDoc,
]);
const user: UserInfo = { name: 'username', roles: [] };
const user: UserInfo = { name: 'username', roles: [], projects: [] };
const mockCouchdbService = { get: () => undefined } as CouchdbService;
const getSpy = jest.spyOn(mockCouchdbService, 'get');
const mockRulesService = {
Expand Down
1 change: 1 addition & 0 deletions src/restricted-endpoints/session/user-auth.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class UserInfo {
constructor(
public name: string,
public roles: string[],
public projects: string[] = [],
) {}
}

Expand Down
Loading