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

Guards first pass #293

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion apps/server/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const employees: EmployeeData[] = [
email: '[email protected]',
positionId: CHIEF_FIN_OFFICER_UUID,
signatureLink: DEV_SIGNATURE_LINK,
scope: EmployeeScope.BASE_USER,
scope: EmployeeScope.ADMIN,
},
{
id: ANGELA_WEIGL_UUID,
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export class AppController {
return this.appService.getHello();
}

@UseGuards(LocalAuthGuard)
@Post('/auth/login')
@UseGuards(LocalAuthGuard)
@ApiCreatedResponse({ type: JwtEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiUnprocessableEntityResponse({
Expand Down Expand Up @@ -80,8 +80,8 @@ export class AppController {
return tokens;
}

@UseGuards(JwtRefreshAuthGuard)
@Get('/auth/refresh')
@UseGuards(JwtRefreshAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: JwtEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
Expand Down
10 changes: 10 additions & 0 deletions apps/server/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ describe('AuthService', () => {
const result = await service.validateEmployee(email, password);
expect(result).toBeNull();
});

it('should return null on an invalid admin credential', async () => {
jest.spyOn(employeeService, 'findOneByEmail').mockResolvedValue(employee);

const result = await service.validateEmployeeScope(
email,
EmployeeScope.ADMIN,
);
expect(result).toBeNull();
});
});

describe('login', () => {
Expand Down
18 changes: 17 additions & 1 deletion apps/server/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EmployeeEntity } from '../employees/entities/employee.entity';
import { CreateEmployeeDto } from '../employees/dto/create-employee.dto';
import { DepartmentsService } from '../departments/departments.service';
import { PositionsService } from '../positions/positions.service';
import { Department, Position } from '@prisma/client';
import { Department, EmployeeScope, Position } from '@prisma/client';

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -37,6 +37,22 @@ export class AuthService {
return new EmployeeEntity(result);
}

/**
* Validate if employee has specified scope.
* @param email employee email
* @returns validated employee or null
*/
async validateEmployeeScope(
email: string,
scope: EmployeeScope,
): Promise<EmployeeEntity | null> {
const user = await this.employeesService.findOneByEmail(email);
if (user.scope == scope) {
return user;
}
return null;
}

/**
* Authenticate a user.
* @param request the incoming request
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/auth/strategies/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
}

async validate(payload: any) {
return { id: payload.sub, email: payload.email };
return { id: payload.sub, email: payload.email, scope: payload.scope };
}
}
9 changes: 9 additions & 0 deletions apps/server/src/departments/departments.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Delete,
NotFoundException,
Query,
UseGuards,
ValidationPipe,
} from '@nestjs/common';
import { DepartmentsService } from './departments.service';
Expand All @@ -27,6 +28,8 @@ import { Prisma } from '@prisma/client';
import { AppErrorMessage } from '../app.errors';
import { DepartmentsErrorMessage } from './departments.errors';
import { LoggerServiceImpl } from '../logger/logger.service';
import { AdminAuthGuard } from '../auth/guards/admin-auth.guard';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@ApiTags('departments')
@Controller('departments')
Expand All @@ -37,6 +40,7 @@ export class DepartmentsController {
) {}

@Post()
@UseGuards(AdminAuthGuard)
@ApiCreatedResponse({ type: DepartmentEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiUnprocessableEntityResponse({
Expand All @@ -54,6 +58,7 @@ export class DepartmentsController {
}

@Get()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: [DepartmentEntity] })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiBadRequestResponse({ description: AppErrorMessage.UNPROCESSABLE_ENTITY })
Expand All @@ -63,6 +68,7 @@ export class DepartmentsController {
}

@Get(':id')
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: DepartmentEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND })
Expand All @@ -81,6 +87,7 @@ export class DepartmentsController {
}

@Get('name/:name')
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: DepartmentEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND })
Expand All @@ -99,6 +106,7 @@ export class DepartmentsController {
}

@Patch(':id')
@UseGuards(AdminAuthGuard)
@ApiOkResponse({ type: DepartmentEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND })
Expand Down Expand Up @@ -133,6 +141,7 @@ export class DepartmentsController {
}

@Delete(':id')
@UseGuards(AdminAuthGuard)
@ApiOkResponse()
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND })
Expand Down
11 changes: 7 additions & 4 deletions apps/server/src/employees/employees.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { AuthUser } from '../auth/auth.decorators';
import { UserEntity } from '../auth/entities/user.entity';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { LoggerServiceImpl } from '../logger/logger.service';
import { AdminAuthGuard } from '../auth/guards/admin-auth.guard';

@ApiTags('employees')
@Controller('employees')
Expand All @@ -43,6 +44,7 @@ export class EmployeesController {
) {}

@Post()
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@ApiCreatedResponse({ type: EmployeeEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
Expand All @@ -60,6 +62,7 @@ export class EmployeesController {
}

@Get()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: [EmployeeEntity] })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiBadRequestResponse({ description: AppErrorMessage.UNPROCESSABLE_ENTITY })
Expand All @@ -75,9 +78,9 @@ export class EmployeesController {
return employees.map((employee) => new EmployeeEntity(employee));
}

@UseGuards(JwtAuthGuard)
@Get('me')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: EmployeeEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiBadRequestResponse({ description: AppErrorMessage.UNPROCESSABLE_ENTITY })
Expand All @@ -87,12 +90,12 @@ export class EmployeesController {
}

@Get(':id')
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: EmployeeEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND })
@ApiBadRequestResponse({ description: AppErrorMessage.UNPROCESSABLE_ENTITY })
async findOne(@Param('id') id: string) {
// TODO: Auth
const employee = await this.employeesService.findOne(id);
if (employee == null) {
this.loggerService.error(EmployeeErrorMessage.EMPLOYEE_NOT_FOUND);
Expand All @@ -104,6 +107,7 @@ export class EmployeesController {
}

@Patch(':id')
@UseGuards(AdminAuthGuard)
@ApiOkResponse({ type: EmployeeEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND })
Expand All @@ -116,7 +120,6 @@ export class EmployeesController {
@Body(new ValidationPipe({ transform: true }))
updateEmployeeDto: UpdateEmployeeDto,
) {
// TODO: Auth
try {
const updatedEmployee = await this.employeesService.update(
id,
Expand All @@ -137,12 +140,12 @@ export class EmployeesController {
}

@Delete(':id')
@UseGuards(AdminAuthGuard)
@ApiOkResponse()
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND })
@ApiBadRequestResponse({ description: AppErrorMessage.UNPROCESSABLE_ENTITY })
async remove(@Param('id') id: string) {
// TODO: Auth
try {
await this.employeesService.remove(id);
} catch (e) {
Expand Down
18 changes: 13 additions & 5 deletions apps/server/src/form-instances/form-instances.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { AuthUser } from '../auth/auth.decorators';
import { UserEntity } from '../auth/entities/user.entity';
import { LoggerServiceImpl } from '../logger/logger.service';
import { AdminAuthGuard } from '../auth/guards/admin-auth.guard';

@ApiTags('form-instances')
@Controller('form-instances')
Expand All @@ -43,6 +44,7 @@ export class FormInstancesController {
) {}

@Post()
@UseGuards(JwtAuthGuard)
@ApiCreatedResponse({ type: FormInstanceEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiUnprocessableEntityResponse({
Expand All @@ -59,13 +61,14 @@ export class FormInstancesController {
}

@Get()
@UseGuards(AdminAuthGuard)
@ApiOkResponse({ type: [FormInstanceEntity] })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiBadRequestResponse({ description: AppErrorMessage.UNPROCESSABLE_ENTITY })
@ApiQuery({
name: 'limit',
type: Number,
description: 'Limit on number of positions to return',
description: 'Limit on number of form instances to return',
required: false,
})
async findAll(@Query('limit') limit?: number) {
Expand All @@ -75,8 +78,8 @@ export class FormInstancesController {
);
}

@UseGuards(JwtAuthGuard)
@Get('me')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: [FormInstanceEntity] })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
Expand All @@ -90,8 +93,8 @@ export class FormInstancesController {
);
}

@UseGuards(JwtAuthGuard)
@Get('created/me')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: [FormInstanceEntity] })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
Expand All @@ -106,6 +109,7 @@ export class FormInstancesController {
}

@Get(':id')
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: FormInstanceEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND })
Expand All @@ -126,6 +130,7 @@ export class FormInstancesController {
}

@Patch(':id')
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: FormInstanceEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND })
Expand All @@ -138,6 +143,7 @@ export class FormInstancesController {
@Body(new ValidationPipe({ transform: true }))
updateFormInstanceDto: UpdateFormInstanceDto,
) {
// TODO: Who is alloed to update a form instance? Creator? Admin? etc?
try {
const updatedFormInstance = await this.formInstancesService.update(
id,
Expand All @@ -160,11 +166,13 @@ export class FormInstancesController {
}

@Delete(':id')
@UseGuards(JwtAuthGuard)
@ApiOkResponse()
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND })
@ApiBadRequestResponse({ description: AppErrorMessage.UNPROCESSABLE_ENTITY })
async remove(@Param('id') id: string) {
// TODO: Who is alloed to update a form instance? Creator? Admin? etc?
try {
await this.formInstancesService.remove(id);
} catch (e) {
Expand All @@ -182,8 +190,8 @@ export class FormInstancesController {
}
}

@UseGuards(JwtAuthGuard)
@Patch(':formInstanceId/sign/:signatureId')
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: FormInstanceEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
@ApiNotFoundResponse({ description: AppErrorMessage.NOT_FOUND })
Expand Down Expand Up @@ -218,8 +226,8 @@ export class FormInstancesController {
}
}

@UseGuards(JwtAuthGuard)
@Patch(':formInstanceId/complete')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: FormInstanceEntity })
@ApiForbiddenResponse({ description: AppErrorMessage.FORBIDDEN })
Expand Down
Loading