-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the`AuthGuard` as part of a test implementation for SiWe authentication: - Add `AuthGuard` with tests.
- Loading branch information
Showing
9 changed files
with
370 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,17 +1,23 @@ | ||
import { SiweMessage } from '@/domain/auth/entities/siwe-message.entity'; | ||
import { JwtAccessTokenPayload } from '@/routes/auth/entities/jwt-access-token.payload.entity'; | ||
import { Request } from 'express'; | ||
|
||
export const IAuthRepository = Symbol('IAuthRepository'); | ||
|
||
export interface IAuthRepository { | ||
generateNonce(): Promise<{ nonce: string }>; | ||
|
||
verify(args: { message: SiweMessage; signature: `0x${string}` }): Promise<{ | ||
verifyMessage(args: { | ||
message: SiweMessage; | ||
signature: `0x${string}`; | ||
}): Promise<{ | ||
accessToken: string; | ||
tokenType: string; | ||
notBefore: number | null; | ||
expiresIn: number | null; | ||
}>; | ||
|
||
getAccessToken(request: Request, tokenType: string): string | null; | ||
|
||
verifyAccessToken(accessToken: string): JwtAccessTokenPayload; | ||
} |
This file contains 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 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 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,8 @@ | ||
import { AddressSchema } from '@/validation/entities/schemas/address.schema'; | ||
import { z } from 'zod'; | ||
|
||
export type JwtAccessTokenPayload = z.infer<typeof JwtAccessTokenPayloadSchema>; | ||
|
||
export const JwtAccessTokenPayloadSchema = z.object({ | ||
signer_address: AddressSchema, | ||
}); |
11 changes: 11 additions & 0 deletions
11
src/routes/auth/entities/schemas/__tests__/jwt-access-token.payload.builder.ts
This file contains 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,11 @@ | ||
import { IBuilder, Builder } from '@/__tests__/builder'; | ||
import { JwtAccessTokenPayload } from '@/routes/auth/entities/jwt-access-token.payload.entity'; | ||
import { faker } from '@faker-js/faker'; | ||
import { getAddress } from 'viem'; | ||
|
||
export function jwtAccessTokenPayloadBuilder(): IBuilder<JwtAccessTokenPayload> { | ||
return new Builder<JwtAccessTokenPayload>().with( | ||
'signer_address', | ||
getAddress(faker.finance.ethereumAddress()), | ||
); | ||
} |
65 changes: 65 additions & 0 deletions
65
src/routes/auth/entities/schemas/__tests__/jwt-access-token.payload.entity.spec.ts
This file contains 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,65 @@ | ||
import { JwtAccessTokenPayloadSchema } from '@/routes/auth/entities/jwt-access-token.payload.entity'; | ||
import { jwtAccessTokenPayloadBuilder } from '@/routes/auth/entities/schemas/__tests__/jwt-access-token.payload.builder'; | ||
import { faker } from '@faker-js/faker'; | ||
import { getAddress } from 'viem'; | ||
|
||
describe('JwtAccessTokenSchema', () => { | ||
it('should parse a valid JwtAccessTokenSchema', () => { | ||
const jwtAccessTokenPayload = jwtAccessTokenPayloadBuilder().build(); | ||
|
||
const result = JwtAccessTokenPayloadSchema.safeParse(jwtAccessTokenPayload); | ||
|
||
expect(result.success).toBe(true); | ||
// Address did not checksum as it already way | ||
expect(result.success && result.data).toStrictEqual(jwtAccessTokenPayload); | ||
}); | ||
|
||
it('should checksum the signer_address', () => { | ||
const nonChecksummedAddress = faker.finance.ethereumAddress().toLowerCase(); | ||
const jwtAccessTokenPayload = jwtAccessTokenPayloadBuilder() | ||
.with('signer_address', nonChecksummedAddress as `0x${string}`) | ||
.build(); | ||
|
||
const result = JwtAccessTokenPayloadSchema.safeParse(jwtAccessTokenPayload); | ||
|
||
expect(result.success && result.data.signer_address).toBe( | ||
getAddress(nonChecksummedAddress), | ||
); | ||
}); | ||
|
||
it('should not allow a non-address signer_address', () => { | ||
const jwtAccessTokenPayload = jwtAccessTokenPayloadBuilder() | ||
.with('signer_address', faker.lorem.word() as `0x${string}`) | ||
.build(); | ||
|
||
const result = JwtAccessTokenPayloadSchema.safeParse(jwtAccessTokenPayload); | ||
|
||
expect(result.success).toBe(false); | ||
expect(!result.success && result.error.issues).toStrictEqual([ | ||
{ | ||
code: 'custom', | ||
message: 'Invalid input', | ||
path: ['signer_address'], | ||
}, | ||
]); | ||
}); | ||
|
||
it('should not parse an invalid JwtAccessTokenSchema', () => { | ||
const jwtAccessTokenPayload = { | ||
unknown: 'payload', | ||
}; | ||
|
||
const result = JwtAccessTokenPayloadSchema.safeParse(jwtAccessTokenPayload); | ||
|
||
expect(result.success).toBe(false); | ||
expect(!result.success && result.error.issues).toStrictEqual([ | ||
{ | ||
code: 'invalid_type', | ||
expected: 'string', | ||
message: 'Required', | ||
path: ['signer_address'], | ||
received: 'undefined', | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains 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,208 @@ | ||
import { TestAppProvider } from '@/__tests__/test-app.provider'; | ||
import { ConfigurationModule } from '@/config/configuration.module'; | ||
import configuration from '@/config/entities/__tests__/configuration'; | ||
import { TestCacheModule } from '@/datasources/cache/__tests__/test.cache.module'; | ||
import { CacheModule } from '@/datasources/cache/cache.module'; | ||
import { IJwtService } from '@/datasources/jwt/jwt.service.interface'; | ||
import { AuthDomainModule } from '@/domain/auth/auth.domain.module'; | ||
import { jwtAccessTokenPayloadBuilder } from '@/routes/auth/entities/schemas/__tests__/jwt-access-token.payload.builder'; | ||
import { TestLoggingModule } from '@/logging/__tests__/test.logging.module'; | ||
import { AuthGuard } from '@/routes/auth/guards/auth.guard'; | ||
import { faker } from '@faker-js/faker'; | ||
import { Get, INestApplication } from '@nestjs/common'; | ||
import { Controller, UseGuards } from '@nestjs/common'; | ||
import { TestingModule, Test } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
|
||
function secondsUntil(date: Date): number { | ||
return Math.floor((date.getTime() - Date.now()) / 1000); | ||
} | ||
|
||
@Controller() | ||
class TestController { | ||
@Get('valid') | ||
@UseGuards(AuthGuard) | ||
async validRoute(): Promise<{ secret: string }> { | ||
return { secret: 'This is a secret message' }; | ||
} | ||
} | ||
|
||
describe('AuthGuard', () => { | ||
let app: INestApplication; | ||
let jwtService: IJwtService; | ||
|
||
beforeEach(async () => { | ||
jest.useFakeTimers(); | ||
|
||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
TestLoggingModule, | ||
ConfigurationModule.register(configuration), | ||
CacheModule, | ||
AuthDomainModule, | ||
], | ||
controllers: [TestController], | ||
}) | ||
.overrideModule(CacheModule) | ||
.useModule(TestCacheModule) | ||
.compile(); | ||
|
||
jwtService = moduleFixture.get<IJwtService>(IJwtService); | ||
app = await new TestAppProvider().provide(moduleFixture); | ||
await app.init(); | ||
}); | ||
|
||
afterEach(async () => { | ||
jest.useRealTimers(); | ||
await app.close(); | ||
}); | ||
|
||
it('should not allow access if there is no token', async () => { | ||
await request(app.getHttpServer()).get('/valid').expect(403).expect({ | ||
message: 'Forbidden resource', | ||
error: 'Forbidden', | ||
statusCode: 403, | ||
}); | ||
}); | ||
|
||
it('should not allow access if verification of the token fails', async () => { | ||
const accessToken = faker.string.alphanumeric(); | ||
|
||
expect(() => jwtService.verify(accessToken)).toThrow('jwt malformed'); | ||
|
||
await request(app.getHttpServer()) | ||
.get('/valid') | ||
.set('authorization', `Bearer ${accessToken}`) | ||
.expect(403) | ||
.expect({ | ||
message: 'Forbidden resource', | ||
error: 'Forbidden', | ||
statusCode: 403, | ||
}); | ||
}); | ||
|
||
it('should not allow access if a token is not yet valid', async () => { | ||
const jwtAccessTokenPayload = jwtAccessTokenPayloadBuilder().build(); | ||
const notBefore = faker.date.future(); | ||
const accessToken = jwtService.sign(jwtAccessTokenPayload, { | ||
notBefore: secondsUntil(notBefore), | ||
}); | ||
|
||
expect(() => jwtService.verify(accessToken)).toThrow('jwt not active'); | ||
|
||
await request(app.getHttpServer()) | ||
.get('/valid') | ||
.set('authorization', `Bearer ${accessToken}`) | ||
.expect(403) | ||
.expect({ | ||
message: 'Forbidden resource', | ||
error: 'Forbidden', | ||
statusCode: 403, | ||
}); | ||
}); | ||
|
||
it('should not allow access if a token has expired', async () => { | ||
const jwtAccessTokenPayload = jwtAccessTokenPayloadBuilder().build(); | ||
const expiresIn = 0; // Now | ||
const accessToken = jwtService.sign(jwtAccessTokenPayload, { | ||
expiresIn, | ||
}); | ||
jest.advanceTimersByTime(1); | ||
|
||
expect(() => jwtService.verify(accessToken)).toThrow('jwt expired'); | ||
|
||
await request(app.getHttpServer()) | ||
.get('/valid') | ||
.set('authorization', `Bearer ${accessToken}`) | ||
.expect(403) | ||
.expect({ | ||
message: 'Forbidden resource', | ||
error: 'Forbidden', | ||
statusCode: 403, | ||
}); | ||
}); | ||
|
||
it('should not allow access if a verified token is not that of a JwtAccessTokenPayload', async () => { | ||
const jwtAccessTokenPayload = { | ||
unknown: 'payload', | ||
}; | ||
const accessToken = jwtService.sign(jwtAccessTokenPayload); | ||
|
||
expect(() => jwtService.verify(accessToken)).not.toThrow(); | ||
|
||
await request(app.getHttpServer()) | ||
.get('/valid') | ||
.set('authorization', `Bearer ${accessToken}`) | ||
.expect(403) | ||
.expect({ | ||
message: 'Forbidden resource', | ||
error: 'Forbidden', | ||
statusCode: 403, | ||
}); | ||
}); | ||
|
||
describe('should allow access if the JwtAccessTokenSchema is valid', () => { | ||
it('when notBefore nor expiresIn is specified', async () => { | ||
const jwtAccessTokenPayload = jwtAccessTokenPayloadBuilder().build(); | ||
const accessToken = jwtService.sign(jwtAccessTokenPayload); | ||
|
||
expect(() => jwtService.verify(accessToken)).not.toThrow(); | ||
|
||
await request(app.getHttpServer()) | ||
.get('/valid') | ||
.set('Authorization', `Bearer ${accessToken}`) | ||
.expect(200) | ||
.expect({ secret: 'This is a secret message' }); | ||
}); | ||
|
||
it('when notBefore is and expirationTime is not specified', async () => { | ||
const jwtAccessTokenPayload = jwtAccessTokenPayloadBuilder().build(); | ||
const notBefore = faker.date.past(); | ||
const accessToken = jwtService.sign(jwtAccessTokenPayload, { | ||
notBefore: secondsUntil(notBefore), | ||
}); | ||
|
||
expect(() => jwtService.verify(accessToken)).not.toThrow(); | ||
|
||
await request(app.getHttpServer()) | ||
.get('/valid') | ||
.set('Authorization', `Bearer ${accessToken}`) | ||
.expect(200) | ||
.expect({ secret: 'This is a secret message' }); | ||
}); | ||
|
||
it('when expiresIn is and notBefore is not specified', async () => { | ||
const jwtAccessTokenPayload = jwtAccessTokenPayloadBuilder().build(); | ||
const expiresIn = faker.date.future(); | ||
const accessToken = jwtService.sign(jwtAccessTokenPayload, { | ||
expiresIn: secondsUntil(expiresIn), | ||
}); | ||
|
||
expect(() => jwtService.verify(accessToken)).not.toThrow(); | ||
|
||
await request(app.getHttpServer()) | ||
.get('/valid') | ||
.set('Authorization', `Bearer ${accessToken}`) | ||
.expect(200) | ||
.expect({ secret: 'This is a secret message' }); | ||
}); | ||
|
||
it('when notBefore and expirationTime are specified', async () => { | ||
const jwtAccessTokenPayload = jwtAccessTokenPayloadBuilder().build(); | ||
const notBefore = faker.date.past(); | ||
const expiresIn = faker.date.future(); | ||
const accessToken = jwtService.sign(jwtAccessTokenPayload, { | ||
notBefore: secondsUntil(notBefore), | ||
expiresIn: secondsUntil(expiresIn), | ||
}); | ||
|
||
expect(() => jwtService.verify(accessToken)).not.toThrow(); | ||
|
||
await request(app.getHttpServer()) | ||
.get('/valid') | ||
.set('Authorization', `Bearer ${accessToken}`) | ||
.expect(200) | ||
.expect({ secret: 'This is a secret message' }); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.