Skip to content

Commit 437fca2

Browse files
refactor(api): migrate DTOs and decorators to REST/Swagger
1 parent f48af78 commit 437fca2

27 files changed

Lines changed: 94 additions & 413 deletions

src/auth/auth.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Body, Controller, Post, HttpCode, HttpStatus, BadRequestException } from '@nestjs/common';
22
import { AuthService } from './auth.service';
3-
import { GoogleAuthInput } from './dto/google-auth.input';
3+
import { GoogleAuthInput } from './dto/oauth-google.dto';
44
import { ApiTags } from '@nestjs/swagger';
55
import { Api } from '../common/decorators/api.decorator';
66

src/auth/dto/oauth-google.dto.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { IsIn, IsNotEmpty, IsOptional, IsString } from 'class-validator';
2+
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
3+
4+
export class GoogleAuthInput {
5+
@ApiProperty({ description: 'Google ID token' })
6+
@IsString()
7+
@IsNotEmpty()
8+
idToken: string;
9+
10+
@ApiPropertyOptional({ description: 'Build type', enum: ['development', 'production'] })
11+
@IsString()
12+
@IsOptional()
13+
@IsIn(['development', 'production'])
14+
buildType?: string;
15+
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { ArgsType, Field } from '@nestjs/graphql';
21
import { IsJWT, IsNotEmpty } from 'class-validator';
3-
import { GraphQLJWT } from 'graphql-scalars';
2+
import { ApiProperty } from '@nestjs/swagger';
43

5-
@ArgsType()
64
export class RefreshTokenInput {
5+
@ApiProperty({ description: 'JWT refresh token' })
76
@IsNotEmpty()
87
@IsJWT()
9-
@Field(() => GraphQLJWT)
108
token: string;
119
}

src/auth/dto/signup.input.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import { IsEmail, IsNotEmpty, MinLength } from 'class-validator';
2-
import { InputType, Field } from '@nestjs/graphql';
1+
import { IsEmail, IsNotEmpty, MinLength, IsOptional } from 'class-validator';
2+
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
33

4-
@InputType()
54
export class SignupInput {
6-
@Field()
5+
@ApiProperty({ description: 'User email address' })
76
@IsEmail()
87
email: string;
98

10-
@Field()
9+
@ApiProperty({ description: 'User password', minLength: 8 })
1110
@IsNotEmpty()
1211
@MinLength(8)
1312
password: string;
1413

15-
@Field({ nullable: true })
14+
@ApiPropertyOptional({ description: 'User first name' })
15+
@IsOptional()
1616
firstname?: string;
1717

18-
@Field({ nullable: true })
18+
@ApiPropertyOptional({ description: 'User last name' })
19+
@IsOptional()
1920
lastname?: string;
2021
}

src/auth/models/auth.model.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { ObjectType } from '@nestjs/graphql';
1+
import { ApiProperty } from '@nestjs/swagger';
22
import { User } from '../../users/models/user.model';
3-
import { Token } from './token.model';
43

5-
@ObjectType()
6-
export class Auth extends Token {
4+
export class Auth {
5+
@ApiProperty({ type: User })
76
user: User;
7+
8+
@ApiProperty({ description: 'JWT access token' })
9+
accessToken: string;
10+
11+
@ApiProperty({ description: 'JWT refresh token' })
12+
refreshToken: string;
813
}

src/auth/models/token.model.ts

Lines changed: 6 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,12 @@
1-
import { Field, ObjectType } from '@nestjs/graphql';
2-
import { GraphQLJWT } from 'graphql-scalars';
3-
import { getAddress } from 'viem';
4-
const J = require('joi');
1+
//import { Field, ObjectType } from '@nestjs/graphql';
2+
//import { GraphQLJWT } from 'graphql-scalars';
3+
//const J = require('joi');
54

6-
@ObjectType()
5+
//@ObjectType()
76
export class Token {
8-
@Field(() => GraphQLJWT, { description: 'JWT access token' })
7+
//@Field(() => GraphQLJWT, { description: 'JWT access token' })
98
accessToken: string;
109

11-
@Field(() => GraphQLJWT, { description: 'JWT refresh token' })
10+
//@Field(() => GraphQLJWT, { description: 'JWT refresh token' })
1211
refreshToken: string;
1312
}
14-
15-
/* Old Webserve Authentication Evgy Code (Temporarily being used until can build out proper auth system) */
16-
17-
function checksum(address) {
18-
return getAddress(address);
19-
}
20-
const _checksum_anti_access = (value, helper) => {
21-
try {
22-
if (BigInt(value) > MIN_ADDRESS && value === checksum(value)) {
23-
if (helper.state.ancestors[0].access_token === ZERO_HASH) {
24-
return IMAGINARY_PEG;
25-
}
26-
return value;
27-
}
28-
} catch (e) {}
29-
30-
return helper.message('Invalid address');
31-
};
32-
33-
const ALLOWED_CHAINS = ['BSC'];
34-
const ALLOWED_QUERIES = ['BS', 'TX', 'HOT', 'NEW'];
35-
const ALLOWED_NFT_TYPES = ['JBU', 'JBS', 'JBW', 'JBD', 'JBA', 'JBR'];
36-
const DEFAULT_CHAIN = 'BSC';
37-
const MIN_ADDRESS = BigInt(2 ** 20); // Above 0xffff
38-
const ZERO_HASH = '0x0000000000000000000000000000000000000000000000000000000000000000';
39-
const IMAGINARY_PEG = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
40-
41-
const _checksum = (value, helper) => {
42-
try {
43-
if (BigInt(value) > MIN_ADDRESS && value === checksum(value)) {
44-
return value;
45-
}
46-
} catch (e) {}
47-
48-
return helper.message('Invalid address');
49-
};
50-
51-
const _hex = (value, helper) => {
52-
return (/^(0x)?[0-9a-f]+$/.test(value) && value) || helper.message('Invalid hex');
53-
};
54-
55-
const _bool = (value, helper) => {
56-
return !!value;
57-
};
58-
59-
const nonce_scheme = J.object({
60-
wallet: J.required().custom(_checksum),
61-
chain: J.string()
62-
.allow('')
63-
.default(DEFAULT_CHAIN)
64-
.valid(...ALLOWED_CHAINS),
65-
})
66-
.rename('w', 'wallet')
67-
.rename('c', 'chain');
68-
69-
const auth_scheme = nonce_scheme
70-
.keys({
71-
signature: J.string().min(64).max(1024).custom(_hex).required(),
72-
affiliate: J.number().min(0).max(1000000000), // JBU ID
73-
is_nft: J.custom(_bool),
74-
})
75-
.rename('s', 'signature')
76-
.rename('AF', 'affiliate')
77-
.rename('_n', 'is_nft');
78-
79-
const authed_scheme = J.object({
80-
access_token: J.string().allow('').default(ZERO_HASH).min(64).max(66).custom(_hex),
81-
wallet: J.string().allow('').default(IMAGINARY_PEG).custom(_checksum_anti_access),
82-
chain: J.string()
83-
.allow('')
84-
.default(DEFAULT_CHAIN)
85-
.valid(...ALLOWED_CHAINS),
86-
})
87-
.rename('w', 'wallet')
88-
.rename('c', 'chain')
89-
.rename('a', 'access_token');

src/common/database/pagination/pagination-query.model.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/common/database/pagination/pagination.interceptor.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/common/decorators/api-nested-query.decorator.ts

Lines changed: 0 additions & 98 deletions
This file was deleted.

src/common/decorators/api.decorator.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { applyDecorators, Type as NestType } from '@nestjs/common';
2-
import { UseGuards } from '@nestjs/common';
32
import {
43
ApiOperation,
54
ApiUnauthorizedResponse,
@@ -14,7 +13,7 @@ import {
1413
ApiQuery,
1514
getSchemaPath,
1615
} from '@nestjs/swagger';
17-
import { RedisAuthGuard } from '../../auth/redis-auth.guard';
16+
//import { RedisAuthGuard } from '../../auth/redis-auth.guard';
1817

1918
/**
2019
* API Decorator Response Type
@@ -213,7 +212,7 @@ export function Api(options: ApiOptions): MethodDecorator {
213212

214213
// Add authentication guards if required
215214
if (options.authenticationRequired) {
216-
decorators.push(UseGuards(RedisAuthGuard), ApiBearerAuth());
215+
decorators.push(ApiBearerAuth());
217216
}
218217

219218
decorators.push(ApiOperation(op));

0 commit comments

Comments
 (0)