forked from heusalagroup/fi.hg.backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmsAuthControllerImpl.ts
265 lines (227 loc) · 10.1 KB
/
SmsAuthControllerImpl.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
256
257
258
259
260
261
262
263
264
265
// Copyright (c) 2022-2023. <[email protected]>. All rights reserved.
import { SmsAuthController } from "../core/auth/SmsAuthController";
import { trim } from "../core/functions/trim";
import { ReadonlyJsonAny } from "../core/Json";
import { JwtDecodeService } from "../core/jwt/JwtDecodeService";
import { PhoneNumberUtils } from "../core/PhoneNumberUtils";
import { ResponseEntity } from "../core/request/types/ResponseEntity";
import { createErrorDTO, ErrorDTO } from "../core/types/ErrorDTO";
import { Language, parseLanguage } from "../core/types/Language";
import { SmsAuthMessageService } from "../core/auth/SmsAuthMessageService";
import { SmsTokenService } from "../core/auth/SmsTokenService";
import { SmsVerificationService } from "../core/auth/SmsVerificationService";
import { LogService } from "../core/LogService";
import { isAuthenticateSmsDTO } from "../core/auth/sms/types/AuthenticateSmsDTO";
import { isVerifySmsTokenDTO } from "../core/auth/sms/types/VerifySmsTokenDTO";
import { isVerifySmsCodeDTO } from "../core/auth/sms/types/VerifySmsCodeDTO";
import { SmsTokenDTO } from "../core/auth/sms/types/SmsTokenDTO";
import { JwtDecodeServiceImpl } from "./JwtDecodeServiceImpl";
import { isString } from "../core/types/String";
import { LogLevel } from "../core/types/LogLevel";
const LOG = LogService.createLogger('SmsAuthControllerImpl');
/**
* This HTTP backend controller can be used to validate the ownership of user's
* sms address.
*
* 1. Call .authenticateSms(body, lang) to send the authentication sms
* 2. Call .verifySmsCode(body) to verify user supplied code from the sms and create a session JWT
* 3. Call .verifySmsToken(body) to verify validity of the previously created session JWT and to refresh the session
*
* The .verifyTokenAndReturnSubject(token) can be used to validate internally API calls in your own APIs.
*/
export class SmsAuthControllerImpl implements SmsAuthController {
public static setLogLevel (level: LogLevel) {
LOG.setLogLevel(level);
}
private _defaultLanguage : Language;
private _defaultPhonePrefix : string;
private _smsTokenService : SmsTokenService;
private _smsVerificationService : SmsVerificationService;
private _smsAuthMessageService : SmsAuthMessageService;
private _jwtDecodeService: JwtDecodeService;
protected constructor (
defaultLanguage: Language = Language.ENGLISH,
defaultPhonePrefix: string,
smsTokenService: SmsTokenService,
smsVerificationService: SmsVerificationService,
smsAuthMessageService: SmsAuthMessageService,
jwtDecodeService: JwtDecodeService,
) {
this._defaultLanguage = defaultLanguage;
this._defaultPhonePrefix = defaultPhonePrefix;
this._smsTokenService = smsTokenService;
this._smsVerificationService = smsVerificationService;
this._smsAuthMessageService = smsAuthMessageService;
this._jwtDecodeService = jwtDecodeService;
}
public static create (
defaultLanguage: Language = Language.ENGLISH,
defaultPhonePrefix: string,
smsTokenService: SmsTokenService,
smsVerificationService: SmsVerificationService,
smsAuthMessageService: SmsAuthMessageService,
jwtDecodeService: JwtDecodeService = JwtDecodeServiceImpl,
) {
return new SmsAuthControllerImpl(
defaultLanguage,
defaultPhonePrefix,
smsTokenService,
smsVerificationService,
smsAuthMessageService,
jwtDecodeService,
);
}
/**
* Set default language for messages sent to the user by sms.
* @param value
*/
public setDefaultLanguage (value: Language) {
this._defaultLanguage = value;
}
/**
* @inheritDoc
*/
public setDefaultPhonePrefix (value: string) : void {
this._defaultPhonePrefix = value;
}
/**
* Handles POST HTTP request to initiate an sms address authentication by
* sending one time code to the user as a sms message.
*
* The message should be in format `AuthenticateSmsDTO`.
*
* @param body {AuthenticateSmsDTO}
* @param langString {Language} The optional language of the message
*/
public async authenticateSms (
body: ReadonlyJsonAny,
langString: string = ""
): Promise<ResponseEntity<SmsTokenDTO | ErrorDTO>> {
try {
const lang: Language = parseLanguage(langString) ?? this._defaultLanguage;
if ( !isAuthenticateSmsDTO(body) ) {
return ResponseEntity.badRequest<ErrorDTO>().body(
createErrorDTO(`Body not AuthenticateSmsDTO`, 400)
).status(400);
}
let sms : string = trim(body.sms);
LOG.debug('authenticateSms: body = ', body);
if ( !sms ) {
return ResponseEntity.badRequest<ErrorDTO>().body(
createErrorDTO(`body.sms required`, 400)
).status(400);
}
sms = PhoneNumberUtils.normalizePhoneAddress(sms, this._defaultPhonePrefix);
const code: string = this._smsVerificationService.createVerificationCode(sms);
const smsToken: SmsTokenDTO = this._smsTokenService.createUnverifiedSmsToken(sms);
try {
await this._smsAuthMessageService.sendAuthenticationCode(lang, sms, code);
} catch (err) {
LOG.error(`authenticateSms: Could not send sms to '${sms}': `, err);
return ResponseEntity.internalServerError<ErrorDTO>().body(
createErrorDTO('Internal error', 500)
).status(500);
}
return ResponseEntity.ok<SmsTokenDTO>(smsToken);
} catch (err) {
LOG.error(`ERROR: `, err);
return ResponseEntity.internalServerError<ErrorDTO>().body(
createErrorDTO('Internal Server Error', 500)
).status(500);
}
}
/**
* Handles HTTP POST request which validates the user supplied code and
* generates a valid JWT token, which can be used to keep the session active.
*
* @param body {VerifySmsCodeDTO}
*/
public async verifySmsCode (
body: ReadonlyJsonAny
): Promise<ResponseEntity<SmsTokenDTO | ErrorDTO>> {
try {
if ( !isVerifySmsCodeDTO(body) ) {
LOG.debug(`Access denied:`, body);
return ResponseEntity.badRequest<ErrorDTO>().body(
createErrorDTO(`Body not VerifySmsCodeDTO`, 400)
).status(400);
}
LOG.debug('verifySmsCode: body = ', body);
const tokenDto: SmsTokenDTO = body?.token;
const token: string = tokenDto?.token;
const sms: string = tokenDto?.sms;
const code: string = body?.code;
if ( !(sms && code && this._smsVerificationService.verifyCode(sms, code)) ) {
LOG.info(`Access denied for "${sms}" since code is not correct`);
return ResponseEntity.internalServerError<ErrorDTO>().body(
createErrorDTO('Access denied', 403)
).status(403);
}
if ( !(token && sms && this._smsTokenService.verifyToken(sms, token, false)) ) {
LOG.info(`Access denied for "${sms}" since token is not valid`);
return ResponseEntity.internalServerError<ErrorDTO>().body(
createErrorDTO('Access denied', 403)
).status(403);
}
const smsToken: SmsTokenDTO = this._smsTokenService.createVerifiedSmsToken(sms);
LOG.debug('verifySmsCode: smsToken = ', smsToken);
return ResponseEntity.ok<SmsTokenDTO>(smsToken);
} catch (err) {
LOG.error(`ERROR: `, err);
return ResponseEntity.internalServerError<ErrorDTO>().body(
createErrorDTO('Internal Server Error', 500)
).status(500);
}
}
/**
* Handles HTTP POST request which validates previously validated session and
* if valid, generates a new refreshed session token.
*
* @param body {VerifySmsTokenDTO}
*/
public async verifySmsToken (
body: ReadonlyJsonAny
): Promise<ResponseEntity<SmsTokenDTO | ErrorDTO>> {
try {
if ( !isVerifySmsTokenDTO(body) ) {
return ResponseEntity.badRequest<ErrorDTO>().body(
createErrorDTO(`Body not VerifySmsTokenDTO`, 400)
).status(400);
}
LOG.debug('verifySmsToken: body = ', body);
const token: string = body?.token?.token ?? '';
const sms: string = body?.token?.sms ?? '';
if ( !(token && sms && this._smsTokenService.verifyToken(sms, token, true)) ) {
return ResponseEntity.badRequest<ErrorDTO>().body(
createErrorDTO('Access denied', 403)
).status(403);
}
const smsToken: SmsTokenDTO = this._smsTokenService.createVerifiedSmsToken(sms);
return ResponseEntity.ok<SmsTokenDTO>(smsToken);
} catch (err) {
LOG.error(`ERROR: `, err);
return ResponseEntity.internalServerError<ErrorDTO>().body(
createErrorDTO('Internal Server Error', 500)
).status(500);
}
}
/**
* Can be used internally in APIs to validate and return the subject of this token.
*/
public async verifyTokenAndReturnSubject (
token: string
): Promise<string> {
LOG.debug('verifyTokenAndReturnSubject: token = ', token);
if ( !isString(token) ) {
throw new TypeError('SmsAuthController.verifyTokenAndReturnSubject: Argument must be string');
}
if ( !this._smsTokenService.isTokenValid(token) ) {
throw new TypeError('SmsAuthController.verifyTokenAndReturnSubject: Token was invalid: ' + token);
}
const subject : string = this._jwtDecodeService.decodePayloadSubject(token);
if (!subject) {
throw new TypeError(`SmsAuthController.verifyTokenAndReturnSubject: Token was not verified: ${token}`);
}
return subject;
}
}