clearMessage & update on modifyChat & support for browser credentials

This commit is contained in:
Adhiraj Singh
2020-07-09 13:00:36 +05:30
parent 583c2adf28
commit 7a2a5e03a8
7 changed files with 112 additions and 29 deletions

View File

@@ -4,7 +4,7 @@ import WS from 'ws'
import * as Utils from './Utils'
import Encoder from '../Binary/Encoder'
import Decoder from '../Binary/Decoder'
import { AuthenticationCredentials, UserMetaData, WANode, AuthenticationCredentialsBase64, WATag, MessageLogLevel } from './Constants'
import { AuthenticationCredentials, UserMetaData, WANode, AuthenticationCredentialsBase64, WATag, MessageLogLevel, AuthenticationCredentialsBrowser } from './Constants'
/** Generate a QR code from the ref & the curve public key. This is scanned by the phone */
@@ -82,6 +82,27 @@ export default class WAConnectionBase {
macKey: Buffer.from(authInfo.macKey, 'base64'), // decode from base64
}
}
/**
* Load in the authentication credentials
* @param authInfo the authentication credentials or path to browser credentials JSON
*/
loadAuthInfoFromBrowser(authInfo: AuthenticationCredentialsBrowser | string) {
if (!authInfo) {
throw 'given authInfo is null'
}
if (typeof authInfo === 'string') {
this.log(`loading authentication credentials from ${authInfo}`)
const file = fs.readFileSync(authInfo, { encoding: 'utf-8' }) // load a closed session back if it exists
authInfo = JSON.parse(file) as AuthenticationCredentialsBrowser
}
this.authInfo = {
clientID: authInfo.WABrowserId.replace (/\"/g, ''),
serverToken: authInfo.WAToken2.replace (/\"/g, ''),
clientToken: authInfo.WAToken1.replace (/\"/g, ''),
encKey: Buffer.from(authInfo.WASecretBundle.encKey, 'base64'), // decode from base64
macKey: Buffer.from(authInfo.WASecretBundle.macKey, 'base64'), // decode from base64
}
}
/**
* Register for a callback for a certain function, will cancel automatically after one execution
* @param {[string, object, string] | string} parameters name of the function along with some optional specific parameters

View File

@@ -20,6 +20,12 @@ export interface AuthenticationCredentialsBase64 {
encKey: string
macKey: string
}
export interface AuthenticationCredentialsBrowser {
WABrowserId: string
WASecretBundle: {encKey: string, macKey: string}
WAToken1: string
WAToken2: string
}
export interface UserMetaData {
id: string
name: string
@@ -58,6 +64,8 @@ export interface WAChat {
count: number
archive?: 'true' | 'false'
read_only?: 'true' | 'false'
mute?: string
pin?: string
spam: 'false' | 'true'
jid: string
modify_tag: string

View File

@@ -16,7 +16,6 @@ export default class WAConnectionValidator extends WAConnectionBase {
macKey: null,
}
}
const data = ['admin', 'init', this.version, this.browserDescription, this.authInfo.clientID, true]
return this.query(data)
.then((json) => {
@@ -42,15 +41,17 @@ export default class WAConnectionValidator extends WAConnectionBase {
}
})
.then((json) => {
switch (json.status) {
case 401: // if the phone was unpaired
throw [json.status, 'unpaired from phone', json]
case 429: // request to login was denied, don't know why it happens
throw [json.status, 'request denied, try reconnecting', json]
case 304: // request to generate a new key for a QR code was denied
throw [json.status, 'request for new key denied', json]
default:
break
if ('status' in json) {
switch (json.status) {
case 401: // if the phone was unpaired
throw [json.status, 'unpaired from phone', json]
case 429: // request to login was denied, don't know why it happens
throw [json.status, 'request denied, try reconnecting', json]
case 304: // request to generate a new key for a QR code was denied
throw [json.status, 'request for new key denied', json]
default:
throw [json.status, 'unknown error status', json]
}
}
if (json[1] && json[1].challenge) {
// if its a challenge request (we get it when logging in)