-
Notifications
You must be signed in to change notification settings - Fork 18
/
auth.ts
184 lines (159 loc) · 4.87 KB
/
auth.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
import { CLIENT_SECRET } from "./constants";
import { Context } from "./context";
import {
generateKeyPair,
parseAuthenticationHeader,
KeyPair,
parseCertificate,
generateSelfSignedCertificate,
serializePublicKey,
pkcs12ToBuffer,
} from "./utils/cert";
type RequestAuthenticationCodeInput = {
cpf: string;
password: string;
deviceId: string;
};
type ExchangeCertificatesInput = RequestAuthenticationCodeInput & {
authCode: string;
};
export class Auth {
private _keyPair: KeyPair = generateKeyPair();
private _keyPairCrypto: KeyPair = generateKeyPair();
private _encryptedCode: string = "";
public get context(): Context {
return this._context;
}
public constructor(private _context: Context) {}
private async authenticate(cpf: string, password: string): Promise<void> {
const data = await this._context.http.request("post", "login", {
client_id: "other.conta",
client_secret: CLIENT_SECRET,
grant_type: "password",
login: cpf,
password,
});
this.updateAuthState(data);
}
public async authenticateWithQrCode(
cpf: string,
password: string,
qrCodeId: string
): Promise<void> {
await this.authenticate(cpf, password);
const data = await this._context.http.request("post", "lift", {
qr_code_id: qrCodeId,
type: "login-webapp",
});
this.updateAuthState(data);
}
public async authenticateWithCertificate(
cpf: string,
password: string,
cert?: Buffer
): Promise<void> {
if (cert) {
this._context.http.cert = cert;
}
const data = await this._context.http.request("post", "token", {
client_id: "legacy_client_id",
client_secret: "legacy_client_secret",
grant_type: "password",
login: cpf,
password,
});
this.updateAuthState(data);
}
public async authenticateWithRefreshToken(
refreshToken: string
): Promise<void> {
const data = await this._context.http.request("post", "token", {
client_id: "legacy_client_id",
client_secret: "legacy_client_secret",
grant_type: "refresh_token",
refresh_token: refreshToken,
});
this.updateAuthState(data);
}
public async requestAuthenticationCode({
cpf,
password,
deviceId,
}: RequestAuthenticationCodeInput): Promise<string> {
const { headers } = await this._context.http
.rawRequest("post", "gen_certificate", {
login: cpf,
password,
device_id: deviceId,
model: [this._context.http.clientName, deviceId].join(" - "),
public_key: serializePublicKey(this._keyPair.publicKey),
public_key_crypto: serializePublicKey(this._keyPairCrypto.publicKey),
})
.catch(({ response }) => {
if (response.status !== 401 || !response.headers["www-authenticate"]) {
throw new Error("Authentication code request failed.");
}
return response;
});
const parsed = parseAuthenticationHeader(headers["www-authenticate"]);
this._encryptedCode =
parsed.get("device-authorization_encrypted-code") ?? "";
return parsed.get("sent-to") ?? "";
}
public async exchangeCertificates({
cpf,
password,
deviceId,
authCode,
}: ExchangeCertificatesInput): Promise<{
cert: Buffer;
certCrypto: Buffer;
}> {
if (!this._encryptedCode) {
throw new Error(
"No encrypted code found. Did you call `requestAuthenticationCode` before exchanging certs?"
);
}
const payload = {
login: cpf,
password,
device_id: deviceId,
model: [this._context.http.clientName, deviceId].join(" - "),
public_key: serializePublicKey(this._keyPair?.publicKey),
public_key_crypto: serializePublicKey(this._keyPairCrypto?.publicKey),
code: authCode,
"encrypted-code": this._encryptedCode,
};
const data = await this._context.http.request(
"post",
"gen_certificate",
payload
);
const cert = parseCertificate(data.certificate);
const selfSignedCert = generateSelfSignedCertificate(this._keyPair, cert);
const certCrypto = parseCertificate(data?.certificate_crypto);
const selfSignedCertCrypto = generateSelfSignedCertificate(
this._keyPairCrypto,
certCrypto
);
return {
cert: pkcs12ToBuffer(selfSignedCert),
certCrypto: pkcs12ToBuffer(selfSignedCertCrypto),
};
}
private updateAuthState(data: Record<string, any>): void {
this._context.http.accessToken =
data?.access_token ?? this._context.http.accessToken;
this._context.http.refreshToken =
data?.refresh_token ?? this._context.http.refreshToken;
this._context.http.refreshBefore =
data?.refresh_before ?? this._context.http.refreshBefore;
this._context.http.privateUrls = {
...this._context.http.authState.privateUrls,
...data?._links,
};
}
public revokeAccess(): void {
this._context.http.clearSession();
}
}