-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewbie-jwt.controller.ts
255 lines (224 loc) · 6.84 KB
/
newbie-jwt.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { EmailUserEntity } from '@/module/http/generated';
import { WebException } from '@/utils/exception';
import {
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
Param,
Post,
Put,
Query,
UseGuards,
} from '@nestjs/common';
import {
ApiBearerAuth,
ApiExcludeEndpoint,
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import { EmailUser } from '@prisma/client';
import { CurrentUser } from './newbie-jwt.decorator';
import {
EmailSignInBody,
EmailSignUpBody,
ResetPasswordBody,
SignupVerifyEmailBody,
SuccessResponse,
TokenResponse,
VerifyEmailVerifyCodeBody,
CharacterIdParam,
} from './newbie-jwt.dto';
import { NewbieJwtAuthGuard } from './newbie-jwt.guard';
import { NewbieJwtService } from './newbie-jwt.service';
import { Throttle, minutes } from '@nestjs/throttler';
@Controller({
path: '/',
version: '1',
})
@ApiTags('Newbie')
export class NewbieJwtController {
constructor(private jwtService: NewbieJwtService) {}
@Throttle({ default: { ttl: minutes(1), limit: 2 } })
@Post('/account/signup/email')
@HttpCode(HttpStatus.CREATED)
@ApiOperation({
summary:
'Ask for a verify-code to be sent to an email box for registration',
})
async signupVerifyEmail(
@Body() body: SignupVerifyEmailBody,
): Promise<SuccessResponse> {
const { email } = body;
const ok = await this.jwtService.signupVerifyEmail(email);
return { ok };
}
@Post('/account/signup/email/verify')
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary: 'Check if the verify-code is valid for email registration',
})
async signupVerifyEmailVerifyCode(
@Body() body: VerifyEmailVerifyCodeBody,
): Promise<SuccessResponse> {
const { email, code } = body;
const ok = await this.jwtService.checkEmailCode(email, code);
return { ok };
}
@Throttle({ default: { ttl: minutes(10), limit: 1 } })
@Put('/account/signup')
@HttpCode(HttpStatus.CREATED)
@ApiOperation({ summary: 'Register a new email user' })
async signup(@Body() body: EmailSignUpBody): Promise<TokenResponse> {
const { email, password, emailVerifyCode, characterName } = body;
const token = await this.jwtService.signup(
email,
password,
emailVerifyCode,
characterName,
);
return { token };
}
@Throttle({ default: { ttl: minutes(5), limit: 5 } })
@Post('/account/signin')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Log in as an email user' })
async signin(@Body() body: EmailSignInBody): Promise<TokenResponse> {
const { email, password } = body;
const token = await this.jwtService.signin(email, password);
return { token };
}
@Throttle({ default: { ttl: minutes(1), limit: 2 } })
@Post('/account/reset-password/email')
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary:
'Ask for a verify-code to be sent to an email box for password reset',
})
async resetPasswordVerifyEmail(
@Body() body: SignupVerifyEmailBody,
): Promise<SuccessResponse> {
const { email } = body;
const ok = await this.jwtService.resetPasswordVerifyEmail(email);
return { ok };
}
@Post('/account/reset-password/email/verify')
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary: 'Check if the verify-code is valid for password reset',
})
async resetPasswordVerifyEmailVerifyCode(
@Body() body: VerifyEmailVerifyCodeBody,
): Promise<SuccessResponse> {
const { email, code } = body;
const ok = await this.jwtService.checkEmailCode(email, code);
return { ok };
}
@Post('/account/reset-password')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Reset password' })
async resetPassword(
@Body() body: ResetPasswordBody,
): Promise<SuccessResponse> {
const { email, password, emailVerifyCode } = body;
const ok = await this.jwtService.resetPassword(
email,
password,
emailVerifyCode,
);
return { ok };
}
@Get('/account')
@UseGuards(NewbieJwtAuthGuard)
@HttpCode(HttpStatus.OK)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get current user information' })
async me(@CurrentUser() user: EmailUser): Promise<EmailUserEntity> {
if (user.passwordHash) {
// @ts-ignore
delete user.passwordHash;
}
return user;
}
@Delete('/account')
@UseGuards(NewbieJwtAuthGuard)
@HttpCode(HttpStatus.NO_CONTENT)
@ApiBearerAuth()
@ApiOperation({ summary: 'Delete current user account' })
async deleteAccount(@CurrentUser() user: EmailUser): Promise<void> {
await this.jwtService.delete(user.email);
}
@Get('/characters/:characterId/newbie')
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary:
"Get newbie email user's info by characterId; if not a newbie user, null will return",
})
async getNewbieInfo(
@Param() param: CharacterIdParam,
): Promise<Partial<EmailUserEntity | null>> {
const { characterId } = param;
const info = await this.jwtService.getUserByCharacterId(characterId);
return info;
}
/// special endpoint for mastodon
/// @nyacandy
@Put('/account/mastodon_signup')
@HttpCode(HttpStatus.CREATED)
@ApiExcludeEndpoint()
async mastodonSignup(
@Body() body: { email: string; characterName: string; adminToken: string },
): Promise<TokenResponse> {
const { email, characterName, adminToken } = body;
// hard code admin token
if (adminToken !== 'mAst0d0n!2O22@aDm1n') {
throw new WebException("Admin token doesn't match", { status: 403 });
}
const token = await this.jwtService.mastodonSignup(email, characterName);
return { token };
}
/// special endpoint for mastodon
/// @nyacandy
@Get('/account/mastodon_get_account')
@HttpCode(HttpStatus.OK)
@ApiExcludeEndpoint()
async mastodonGetAccount(
@Query() param: { email: string; adminToken: string },
): Promise<EmailUserEntity | null> {
const { email, adminToken } = param;
// hard code admin token
if (adminToken !== 'mAst0d0n!2O22@aDm1n') {
throw new WebException("Admin token doesn't match", { status: 403 });
}
const user = await this.jwtService.getAccountByEmail(email);
return user;
}
/// special endpoint for Wondera
/// https://www.notion.so/rss3/Wondera-x-Crossbell-ecd8e58dbff14d9eb493fcfe8e0acdaa?d=92c471aabc5e4e8b96b2dc279dec28f3#7ab410d6dcb646dc8a408f36b1816434
@Put('/account/wondera_signup')
@HttpCode(HttpStatus.CREATED)
@ApiExcludeEndpoint()
async wonderaSignup(
@Body()
body: {
email: string;
password: string;
characterName: string;
adminToken: string;
},
): Promise<TokenResponse> {
const { email, password, characterName, adminToken } = body;
// hard code admin token
if (adminToken !== 'wOnDeRa!2O23@aDm1n') {
throw new WebException("Admin token doesn't match", { status: 403 });
}
const token = await this.jwtService.wonderaSignup(
email,
password,
characterName,
);
return { token };
}
}