Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
start of auth layer
  • Loading branch information
bryopsida committed Jan 7, 2023
commit fd4e74ce3e4f2e85589296a50876aa3773e6448f
294 changes: 289 additions & 5 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@nestjs/graphql": "^10.1.7",
"@nestjs/mercurius": "^10.1.7",
"@nestjs/mongoose": "^9.2.1",
"@nestjs/passport": "^9.0.0",
"@nestjs/platform-fastify": "^9.2.1",
"@nestjs/terminus": "^9.1.4",
"class-validator": "^0.14.0",
Expand All @@ -31,6 +32,8 @@
"mercurius": "^10.5.1",
"mongoose": "^6.8.2",
"nestjs-pino": "^3.1.1",
"passport": "^0.6.0",
"passport-local": "^1.0.0",
"pino-http": "^8.3.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0"
Expand All @@ -41,6 +44,7 @@
"@nestjs/testing": "^9.0.0",
"@types/jest": "29.2.5",
"@types/node": "18.11.18",
"@types/passport-local": "^1.0.35",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
Expand Down
4 changes: 4 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { GraphQLModule } from '@nestjs/graphql';
import { MongooseModule } from '@nestjs/mongoose';
import { MessageModule } from './messages/message.module';
import { CommonModule } from './common/common.module';
import { UsersModule } from './users/users.module';
import { AuthenticationModule } from './authentication/authentication.module';

@Module({
imports: [
CommonModule,
AuthenticationModule,
HealthModule,
MessageModule,
LoggerModule.forRoot(),
Expand All @@ -20,6 +23,7 @@ import { CommonModule } from './common/common.module';
graphiql: true,
}),
MongooseModule.forRoot('mongodb://localhost:27017/syslog'),
UsersModule,
],
controllers: [],
providers: [],
Expand Down
15 changes: 15 additions & 0 deletions src/authentication/authentication.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Module } from '@nestjs/common';
import { AuthenticationService } from './authentication.service';
import { UsersModule } from 'src/users/users.module';
import { LocalStrategy } from './strategies/local.strategy';
import { PassportModule } from '@nestjs/passport';
import { LoginController } from './controllers/login.controller';
import { LocalAuthGuard } from './guards/local-auth.guard';

@Module({
controllers: [LoginController],
exports: [LocalAuthGuard],
imports: [UsersModule, PassportModule],
providers: [AuthenticationService, LocalStrategy],
})
export class AuthenticationModule {}
16 changes: 16 additions & 0 deletions src/authentication/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Injectable } from '@nestjs/common';
import { UsersService } from 'src/users/users.service';

@Injectable()
export class AuthenticationService {
constructor(private usersService: UsersService) {}

async validateUser(username: string, pass: string): Promise<any> {
const user = await this.usersService.findOne(username);
if (user && user.password === pass) {
const { password, ...result } = user;
return result;
}
return null;
}
}
11 changes: 11 additions & 0 deletions src/authentication/controllers/login.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Controller, Request, Post, UseGuards } from '@nestjs/common';
import { LocalAuthGuard } from '../guards/local-auth.guard';

@Controller()
export class LoginController {
@UseGuards(LocalAuthGuard)
@Post('auth/login')
async login(@Request() req) {
return req.user;
}
}
5 changes: 5 additions & 0 deletions src/authentication/guards/local-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {}
19 changes: 19 additions & 0 deletions src/authentication/strategies/local.strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthenticationService } from '../authentication.service';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthenticationService) {
super();
}

async validate(username: string, password: string): Promise<any> {
const user = await this.authService.validateUser(username, password);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
12 changes: 6 additions & 6 deletions src/messages/dto/message.args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ import { FACILITY, SEVERITY } from '../schemas/message.schemas';

@ArgsType()
export class MessageArgs {
@Field((type) => Int)
@Field((_type) => Int)
@Min(0)
skip = 0;

@Field((type) => Int)
@Field((_type) => Int)
@Min(1)
@Max(50)
take = 25;

@Field((type) => Int, {
@Field((_type) => Int, {
nullable: true,
})
@Min(0)
@Max(23)
facility?: FACILITY;

@Field((type) => Int, {
@Field((_type) => Int, {
nullable: true,
})
@Min(0)
@Max(7)
severity?: SEVERITY;

@Field((type) => [String], {
@Field((_type) => [String], {
nullable: true,
})
apps?: string[];

@Field((type) => [String], {
@Field((_type) => [String], {
nullable: true,
})
hostnames?: string[];
Expand Down
7 changes: 3 additions & 4 deletions src/messages/message.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { NotFoundException } from '@nestjs/common';
import { Args, Context, Query, Resolver, Subscription } from '@nestjs/graphql';
import { PubSub } from 'mercurius';
import { MessageArgs } from './dto/message.args';
import { Message } from './models/message.model';
import { MessageService } from './message.service';

@Resolver((of) => Message)
@Resolver((_of) => Message)
export class MessageResolver {
constructor(private readonly service: MessageService) {}

@Query((returns) => [Message])
@Query((_returns) => [Message])
messages(@Args() args: MessageArgs): Promise<Message[]> {
return this.service.findAll(args);
}

@Subscription((returns) => Message)
@Subscription((_returns) => Message)
messagesAdded(@Context('pubsub') pubSub: PubSub) {
return pubSub.subscribe('messagesAdded');
}
Expand Down
8 changes: 8 additions & 0 deletions src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { UsersService } from './users.service';

@Module({
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}
21 changes: 21 additions & 0 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@nestjs/common';
export type User = any;
@Injectable()
export class UsersService {
private readonly users = [
{
userId: 1,
username: 'john',
password: 'changeme',
},
{
userId: 2,
username: 'maria',
password: 'guess',
},
];

async findOne(username: string): Promise<User | undefined> {
return this.users.find((user) => user.username === username);
}
}
18 changes: 18 additions & 0 deletions test/authentication/authentication.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthenticationService } from '../../src/authentication/authentication.service';

describe('AuthenticationService', () => {
let service: AuthenticationService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthenticationService],
}).compile();

service = module.get<AuthenticationService>(AuthenticationService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
18 changes: 18 additions & 0 deletions test/users/users.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UsersService } from '../../src/users/users.service';

describe('UsersService', () => {
let service: UsersService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UsersService],
}).compile();

service = module.get<UsersService>(UsersService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});