Skip to content
Merged
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
12 changes: 6 additions & 6 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true,
"printWidth": 80
}
"trailingComma": "es5",
"tabWidth": 4,
"semi": true,
"singleQuote": true,
"printWidth": 80
}
26 changes: 15 additions & 11 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { LoggerModule } from 'nestjs-pino'
import { AuthModule } from './auth/auth.module'
import { AuthDiscordModule } from './auth-discord/auth-discord.module'
import { AuthGoogleModule } from './auth-google/auth-google.module'
import { AuthSiweModule } from './auth-siwe/auth-siwe.module'
import { configModules, configValidationSchema } from './config'
import { pinoConfig } from './config/pino.config'
import { LitModule } from './lit/lit.module'
import { EasModule } from './eas/eas.module'
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { LoggerModule } from 'nestjs-pino';
import { AuthModule } from './auth/auth.module';
import { AuthDiscordModule } from './auth-discord/auth-discord.module';
import { AuthGoogleModule } from './auth-google/auth-google.module';
import { AuthSiweModule } from './auth-siwe/auth-siwe.module';
import { configModules, configValidationSchema } from './config';
import { pinoConfig } from './config/pino.config';
import { LitModule } from './lit/lit.module';
import { EasModule } from './eas/eas.module';
import { JwtModule } from './jwt/jwt.module';
import { DiscourseVerificationModule } from './discourse-verification/discourse-verification.module';

@Module({
imports: [
Expand All @@ -28,6 +30,8 @@ import { EasModule } from './eas/eas.module'
AuthSiweModule,
LitModule,
EasModule,
JwtModule,
DiscourseVerificationModule,
],
controllers: [],
providers: [],
Expand Down
70 changes: 35 additions & 35 deletions src/auth-discord/auth-discord-controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
import { Test, TestingModule } from '@nestjs/testing'
import { AuthDiscordController } from './auth-discord.controller'
import { OAuthService } from '../auth/oAuth.service'
import { CryptoUtilsService } from '../utils/crypto-utils.service'
import { HttpStatus, ForbiddenException } from '@nestjs/common'
import { AUTH_PROVIDERS } from '../auth/constants/provider.constants'
import { Test, TestingModule } from '@nestjs/testing';
import { AuthDiscordController } from './auth-discord.controller';
import { OAuthService } from '../auth/oAuth.service';
import { CryptoUtilsService } from '../utils/crypto-utils.service';
import { HttpStatus, ForbiddenException } from '@nestjs/common';
import { AUTH_PROVIDERS } from '../auth/constants/provider.constants';

describe('AuthDiscordController', () => {
let controller: AuthDiscordController
let controller: AuthDiscordController;

const mockCryptoService = {
generateState: jest.fn(),
validateState: jest.fn(),
}
};
const mockOAuthService = {
generateRedirectUrl: jest.fn(),
handleOAuth2Callback: jest.fn(),
}
};
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthDiscordController],
providers: [
{ provide: CryptoUtilsService, useValue: mockCryptoService },
{ provide: OAuthService, useValue: mockOAuthService },
],
}).compile()
}).compile();

controller = module.get<AuthDiscordController>(AuthDiscordController)
})
controller = module.get<AuthDiscordController>(AuthDiscordController);
});

describe('redirectToDiscord', () => {
it('should redirect to Discord authentication URL', () => {
mockCryptoService.generateState.mockReturnValue('mock-state')
mockOAuthService.generateRedirectUrl.mockReturnValue('mock-url')
const mockSession = { state: null }
const result = controller.redirectToDiscord(mockSession)
mockCryptoService.generateState.mockReturnValue('mock-state');
mockOAuthService.generateRedirectUrl.mockReturnValue('mock-url');
const mockSession = { state: null };
const result = controller.redirectToDiscord(mockSession);
expect(result).toEqual({
url: 'mock-url',
statusCode: HttpStatus.FOUND,
})
expect(mockSession.state).toEqual('mock-state')
expect(mockCryptoService.generateState).toHaveBeenCalled()
});
expect(mockSession.state).toEqual('mock-state');
expect(mockCryptoService.generateState).toHaveBeenCalled();
expect(mockOAuthService.generateRedirectUrl).toHaveBeenCalledWith(
AUTH_PROVIDERS.DISCORD,
'mock-state'
)
})
})
);
});
});

describe('handleOAuthCallback', () => {
it('should handle OAuth callback successfully', async () => {
mockOAuthService.handleOAuth2Callback.mockResolvedValue(
'mock-redirect-url'
)
const mockSession = { state: 'mock-state' }
);
const mockSession = { state: 'mock-state' };
const result = await controller.handleOAuthCallback(
{ code: 'valid-code', state: 'mock-state' },
mockSession
)
);
expect(result).toEqual({
url: 'mock-redirect-url',
statusCode: HttpStatus.FOUND,
})
});
expect(mockOAuthService.handleOAuth2Callback).toHaveBeenCalledWith(
'mock-state',
'mock-state',
'valid-code',
AUTH_PROVIDERS.DISCORD
)
})
);
});
it('should throw HttpException if state is invalid', async () => {
const mockSession = { state: 'invalid' }
const mockSession = { state: 'invalid' };
mockOAuthService.handleOAuth2Callback.mockImplementation(() => {
throw new ForbiddenException('Invalid state')
})
throw new ForbiddenException('Invalid state');
});
await expect(
controller.handleOAuthCallback(
{ code: 'valid-code', state: 'mock-state' },
mockSession
)
).rejects.toThrow(ForbiddenException)
})
})
})
).rejects.toThrow(ForbiddenException);
});
});
});
37 changes: 19 additions & 18 deletions src/auth-discord/auth-discord.controller.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import {
Controller,
Get,
HttpStatus,
Query,
Redirect,
HttpStatus,
Session,
} from '@nestjs/common'
} from '@nestjs/common';
import {
ApiTags,
ApiOperation,
ApiFoundResponse,
ApiOkResponse,
} from '@nestjs/swagger'
import { OAuthService } from '../auth/oAuth.service'
import { HandleOAuthCallback } from './dto/handle-oauth-callback-dto'
import { CryptoUtilsService } from '../utils/crypto-utils.service'
import { AUTH_PROVIDERS } from '../auth/constants/provider.constants'
import { JwtResponse } from '../auth//dto/jwt-response.dto'
ApiOperation,
ApiTags,
} from '@nestjs/swagger';

import { AUTH_PROVIDERS } from '../auth/constants/provider.constants';
import { JwtResponse } from '../auth/dto/jwt-response.dto';
import { OAuthService } from '../auth/oAuth.service';
import { CryptoUtilsService } from '../utils/crypto-utils.service';
import { HandleOAuthCallback } from './dto/handle-oauth-callback-dto';

@ApiTags(`${AUTH_PROVIDERS.DISCORD} Authentication`)
@Controller(`auth/${AUTH_PROVIDERS.DISCORD}`)
@ApiTags('Discord Authentication')
@Controller('auth/discord')
export class AuthDiscordController {
constructor(
private readonly oAuthService: OAuthService,
Expand All @@ -31,13 +32,13 @@ export class AuthDiscordController {
@ApiOperation({ summary: 'Redirect to Discord OAuth' })
@ApiFoundResponse({ description: 'Redirection to Discord OAuth.' })
redirectToDiscord(@Session() session: any) {
const state = this.cryptoService.generateState()
const state = this.cryptoService.generateState();
const url = this.oAuthService.generateRedirectUrl(
AUTH_PROVIDERS.DISCORD,
state
)
session.state = state
return { url, statusCode: HttpStatus.FOUND }
);
session.state = state;
return { url, statusCode: HttpStatus.FOUND };
}

@Get('authenticate/callback')
Expand All @@ -56,10 +57,10 @@ export class AuthDiscordController {
session.state,
code,
AUTH_PROVIDERS.DISCORD
)
);
return {
url: redirectUrl,
statusCode: HttpStatus.FOUND,
}
};
}
}
12 changes: 7 additions & 5 deletions src/auth-discord/auth-discord.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Module } from '@nestjs/common'
import { AuthDiscordController } from './auth-discord.controller'
import { AuthModule } from 'src/auth/auth.module'
import { UtilsModule } from '../utils/utils.module'
import { Module } from '@nestjs/common';

import { AuthModule } from '../auth/auth.module';
import { JwtModule } from '../jwt/jwt.module';
import { UtilsModule } from '../utils/utils.module';
import { AuthDiscordController } from './auth-discord.controller';

@Module({
imports: [AuthModule, UtilsModule],
imports: [AuthModule, UtilsModule, JwtModule],
providers: [],
controllers: [AuthDiscordController],
})
Expand Down
8 changes: 4 additions & 4 deletions src/auth-discord/config/auth-discord.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// config/discord.config.ts
import * as Joi from 'joi'
import { registerAs } from '@nestjs/config'
import * as Joi from 'joi';
import { registerAs } from '@nestjs/config';

export default registerAs('discord', () => ({
clientId: process.env.DISCORD_CLIENT_ID,
Expand All @@ -9,7 +9,7 @@ export default registerAs('discord', () => ({
scopes: process.env.DISCORD_SCOPES
? process.env.DISCORD_SCOPES.split(' ')
: ['identify'],
}))
}));

export const discordConfigSchema = {
DISCORD_CLIENT_ID: Joi.string().required().description('Discord client ID'),
Expand All @@ -23,4 +23,4 @@ export const discordConfigSchema = {
DISCORD_SCOPES: Joi.string()
.default('identify')
.description('Discord OAuth scopes'),
}
};
8 changes: 4 additions & 4 deletions src/auth-discord/dto/handle-oauth-callback-dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsString } from 'class-validator'
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
export class HandleOAuthCallback {
@ApiProperty()
@IsString()
readonly code: string
readonly code: string;

@ApiProperty()
@IsString()
readonly state: string
readonly state: string;
}
Empty file.
Empty file.
Empty file.
Loading
Loading