From bde8f2abae771454a0793bbd04dd2b8f6395c278 Mon Sep 17 00:00:00 2001 From: Johnie Hjelm Date: Sat, 2 Jan 2021 19:04:31 +0100 Subject: [PATCH] Added function to update one profile field and updated the AccountEditProfileOptions interface to full_name --- ...s_account_repository_.accountrepository.md | 19 ++++++- src/repositories/account.repository.ts | 49 +++++++++++++++---- src/types/account.edit-profile.options.ts | 2 +- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/docs/classes/_repositories_account_repository_.accountrepository.md b/docs/classes/_repositories_account_repository_.accountrepository.md index e6a16f907..11842092c 100644 --- a/docs/classes/_repositories_account_repository_.accountrepository.md +++ b/docs/classes/_repositories_account_repository_.accountrepository.md @@ -24,6 +24,7 @@ * [create](_repositories_account_repository_.accountrepository.md#create) * [currentUser](_repositories_account_repository_.accountrepository.md#currentuser) * [editProfile](_repositories_account_repository_.accountrepository.md#editprofile) +* [setProfileField](_repositories_account_repository_.accountrepository.md#setProfileField) * [getPrefillCandidates](_repositories_account_repository_.accountrepository.md#getprefillcandidates) * [login](_repositories_account_repository_.accountrepository.md#login) * [logout](_repositories_account_repository_.accountrepository.md#logout) @@ -147,6 +148,22 @@ Name | Type | ___ +### setProfileField + +▸ **setProfileField**(`options`: [AccountEditProfileOptions](../interfaces/_types_account_edit_profile_options_.accounteditprofileoptions.md)): *`Promise`* + +*Defined in [repositories/account.repository.ts:243](https://github.com/dilame/instagram-private-api/blob/3e16058/src/repositories/account.repository.ts#L243)* + +**Parameters:** + +Name | Type | +------ | ------ | +`options` | [AccountEditProfileOptions](../interfaces/_types_account_edit_profile_options_.accounteditprofileoptions.md) | + +**Returns:** *`Promise`* + +___ + ### getPrefillCandidates ▸ **getPrefillCandidates**(): *`Promise`* @@ -284,4 +301,4 @@ Name | Type | ------ | ------ | `options` | [AccountTwoFactorLoginOptions](../interfaces/_types_account_two_factor_login_options_.accounttwofactorloginoptions.md) | -**Returns:** *`Promise`* \ No newline at end of file +**Returns:** *`Promise`* diff --git a/src/repositories/account.repository.ts b/src/repositories/account.repository.ts index 15d2a7cf6..4a6c114d9 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', @@ -77,14 +77,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)); @@ -98,8 +101,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'), }; } @@ -235,6 +240,32 @@ export class AccountRepository extends Repository { return body.user; } + public async setProfileField(options: AccountEditProfileOptions) { + const { username, full_name, biography, external_url, gender, phone_number, email } = await this.currentUser(); + const data = { + username, + full_name, + biography, + external_url, + gender, + phone_number, + email, + ...options, + }; + const { body } = await this.client.request.send({ + url: '/api/v1/accounts/edit_profile/', + method: 'POST', + form: this.client.request.sign({ + ...data, + _csrftoken: this.client.state.cookieCsrfToken, + _uid: this.client.state.cookieUserId, + device_id: this.client.state.deviceId, + _uuid: this.client.state.uuid, + }), + }); + return body.user; + } + public async changePassword(oldPassword: string, newPassword: string) { const { body } = await this.client.request.send({ url: '/api/v1/accounts/change_password/', diff --git a/src/types/account.edit-profile.options.ts b/src/types/account.edit-profile.options.ts index 1e8b78fb9..8c6fe7b2d 100644 --- a/src/types/account.edit-profile.options.ts +++ b/src/types/account.edit-profile.options.ts @@ -3,7 +3,7 @@ export interface AccountEditProfileOptions { gender: string; phone_number: string; username: string; - first_name: string; + full_name: string; biography: string; email: string; }