From bebadbc165ddc992b222cc6c48efe53e658a3bd1 Mon Sep 17 00:00:00 2001 From: Adryan Alencar Date: Sat, 19 Nov 2022 20:18:53 -0300 Subject: [PATCH] Add method to get the login activity --- src/repositories/account.repository.ts | 34 ++++++++++++++----- ...ount.repository.login.activity.response.ts | 32 +++++++++++++++++ 2 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 src/responses/account.repository.login.activity.response.ts diff --git a/src/repositories/account.repository.ts b/src/repositories/account.repository.ts index 35e017f51..541c0d359 100644 --- a/src/repositories/account.repository.ts +++ b/src/repositories/account.repository.ts @@ -26,7 +26,7 @@ export class AccountRepository extends Repository { if (!this.client.state.passwordEncryptionPubKey) { await this.client.qe.syncLoginExperiments(); } - const {encrypted, time} = this.encryptPassword(password); + const { encrypted, time } = this.encryptPassword(password); const response = await Bluebird.try(() => this.client.request.send({ method: 'POST', @@ -76,14 +76,17 @@ export class AccountRepository extends Repository { return `2${sum}`; } - public encryptPassword(password: string): { time: string, encrypted: string } { + public encryptPassword(password: string): { time: string; encrypted: string } { const randKey = crypto.randomBytes(32); const iv = crypto.randomBytes(12); - const rsaEncrypted = crypto.publicEncrypt({ - key: Buffer.from(this.client.state.passwordEncryptionPubKey, 'base64').toString(), - // @ts-ignore - padding: crypto.constants.RSA_PKCS1_PADDING, - }, randKey); + const rsaEncrypted = crypto.publicEncrypt( + { + key: Buffer.from(this.client.state.passwordEncryptionPubKey, 'base64').toString(), + // @ts-ignore + padding: crypto.constants.RSA_PKCS1_PADDING, + }, + randKey, + ); const cipher = crypto.createCipheriv('aes-256-gcm', randKey, iv); const time = Math.floor(Date.now() / 1000).toString(); cipher.setAAD(Buffer.from(time)); @@ -97,8 +100,10 @@ export class AccountRepository extends Repository { Buffer.from([1, this.client.state.passwordEncryptionKeyId]), iv, sizeBuffer, - rsaEncrypted, authTag, aesEncrypted]) - .toString('base64'), + rsaEncrypted, + authTag, + aesEncrypted, + ]).toString('base64'), }; } @@ -315,6 +320,17 @@ export class AccountRepository extends Repository { return body; } + public async getLoginActivity() { + const { body } = await this.client.request.send({ + method: 'GET', + url: '/api/v1/session/login_activity/', + qs: { + device_id: this.client.state.uuid, + }, + }); + return body; + } + public async getPrefillCandidates() { const { body } = await this.client.request.send({ method: 'POST', diff --git a/src/responses/account.repository.login.activity.response.ts b/src/responses/account.repository.login.activity.response.ts new file mode 100644 index 000000000..c3e2844c1 --- /dev/null +++ b/src/responses/account.repository.login.activity.response.ts @@ -0,0 +1,32 @@ +export interface AccountRepositoryLoginActivityResponseRootObject { + sessions: AccountRepositoryLoginActivityResponseSession[]; + suspicious_logins: AccountRepositoryLoginActivityResponseSuspiciousLogin[]; + status: string; +} +export interface AccountRepositoryLoginActivityResponseSession { + id: string; + location: string; + latitude: number; + longitude: number; + device: string; + timestamp: number; + login_timestamp: number; + is_current: boolean; + login_id: string; + user_agent: string; + ip_address: string; + device_id: string; + device_id_uuid: string; + family_device_id: string; +} + +export interface AccountRepositoryLoginActivityResponseSuspiciousLogin { + id: string; + location: string; + latitude: number; + longitude: number; + device: string; + timestamp: number; + user_agent: string; + ip_address: string; +}