This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEmailAuthHttpService.ts
91 lines (80 loc) · 3.63 KB
/
EmailAuthHttpService.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
// Copyright (c) 2021-2023. Heusala Group Oy <[email protected]>. All rights reserved.
// Copyright (c) 2021-2023. Sendanor <[email protected]>. All rights reserved.
import { EmailTokenDTO, isEmailTokenDTO } from "./auth/email/types/EmailTokenDTO";
import { Language } from "./types/Language";
import { LanguageService } from "./LanguageService";
import { HttpService } from "./HttpService";
import { LogService } from "./LogService";
import { createVerifyEmailCodeDTO, VerifyEmailCodeDTO } from "./auth/email/types/VerifyEmailCodeDTO";
import { ReadonlyJsonAny } from "./Json";
import { CallbackWithLanguage, AUTHENTICATE_EMAIL_URL, VERIFY_EMAIL_CODE_URL, VERIFY_EMAIL_TOKEN_URL } from "./auth/email/email-auth-constants";
import { LogLevel } from "./types/LogLevel";
import { createVerifyEmailTokenDTO, VerifyEmailTokenDTO } from "./auth/email/types/VerifyEmailTokenDTO";
import { createAuthenticateEmailDTO } from "./auth/email/types/AuthenticateEmailDTO";
const LOG = LogService.createLogger('EmailAuthHttpService');
/**
* This is a client service for email address based user authentication over
* HTTP protocol.
*/
export class EmailAuthHttpService {
private static _authenticateEmailUrl : CallbackWithLanguage = AUTHENTICATE_EMAIL_URL;
private static _verifyEmailCodeUrl : CallbackWithLanguage = VERIFY_EMAIL_CODE_URL;
private static _verifyEmailTokenUrl : CallbackWithLanguage = VERIFY_EMAIL_TOKEN_URL;
public static setLogLevel (level: LogLevel) {
LOG.setLogLevel(level);
}
public static initialize (
authenticateEmailUrl : CallbackWithLanguage = AUTHENTICATE_EMAIL_URL,
verifyEmailCodeUrl : CallbackWithLanguage = VERIFY_EMAIL_CODE_URL,
verifyEmailTokenUrl : CallbackWithLanguage = VERIFY_EMAIL_TOKEN_URL
) {
this._authenticateEmailUrl = authenticateEmailUrl;
this._verifyEmailCodeUrl = verifyEmailCodeUrl;
this._verifyEmailTokenUrl = verifyEmailTokenUrl;
}
public static async authenticateEmailAddress (
email : string,
language ?: Language
) : Promise<EmailTokenDTO> {
const lang : Language = language ?? LanguageService.getCurrentLanguage();
const body = createAuthenticateEmailDTO(email);
return await this._postJson(
this._authenticateEmailUrl(lang),
body as unknown as ReadonlyJsonAny
);
}
public static async verifyEmailToken (
emailToken : EmailTokenDTO,
language ?: Language
) : Promise<EmailTokenDTO> {
const lang : Language = language ?? LanguageService.getCurrentLanguage();
const body : VerifyEmailTokenDTO = createVerifyEmailTokenDTO(emailToken);
return await this._postJson(
this._verifyEmailTokenUrl(lang),
body as unknown as ReadonlyJsonAny
);
}
public static async verifyEmailCode (
token : EmailTokenDTO,
code: string,
language ?: Language
) : Promise<EmailTokenDTO> {
const lang : Language = language ?? LanguageService.getCurrentLanguage();
const body : VerifyEmailCodeDTO = createVerifyEmailCodeDTO(token, code);
return await this._postJson(
this._verifyEmailCodeUrl(lang),
body as unknown as ReadonlyJsonAny
);
}
private static async _postJson (
url : string,
body : ReadonlyJsonAny
) : Promise<EmailTokenDTO> {
const response : unknown = await HttpService.postJson(url, body);
if (!isEmailTokenDTO(response)) {
LOG.debug(`Response: `, response);
throw new TypeError(`Response was not EmailTokenDTO`);
}
return response;
}
}