From f0fc6c183c404ae5ff63462cb416906202b162ab Mon Sep 17 00:00:00 2001 From: balzack Date: Fri, 12 Jul 2024 22:30:36 -0700 Subject: [PATCH] adding access endpoint --- app/sdk/src/index.ts | 21 ++++++++++----------- app/sdk/src/net/addAccount.ts | 2 -- app/sdk/src/net/setAccess.ts | 12 ++++++++++++ 3 files changed, 22 insertions(+), 13 deletions(-) create mode 100644 app/sdk/src/net/setAccess.ts diff --git a/app/sdk/src/index.ts b/app/sdk/src/index.ts index 6775f54c..0e6c350d 100644 --- a/app/sdk/src/index.ts +++ b/app/sdk/src/index.ts @@ -4,6 +4,7 @@ import { BotModule } from './bot'; import { ConsoleLogging } from './logging'; import { type Store, OfflineStore, OnlineStore, NoStore } from './store'; import { setLogin } from './net/setLogin'; +import { setAccess } from './net/setAccess'; import { addAccount } from './net/addAccount'; import type { Session, Node, Bot, SqlStore, WebStore, Crypto, Logging } from './api'; import type { SessionParams } from './types'; @@ -28,31 +29,29 @@ export class DatabagSDK { public async initOfflineStore(sql: SqlStore): Promise { this.store = new OfflineStore(this.log, sql); const login = await this.store.init(); - if (!login) { - return null; - } - return new SessionModule(this.store, this.crypto, this.log, login.token, login.url, login.timestamp); + return login ? new SessionModule(this.store, this.crypto, this.log, login.token, login.url, login.timestamp) : null } public async initOnlineStore(web: WebStore): Promise { this.store = new OnlineStore(this.log, web); const login = await this.store.init(); - if (!login) { - return null; - } - return new SessionModule(this.store, this.crypto, this.log, login.token, login.url, login.timestamp); + return login ? new SessionModule(this.store, this.crypto, this.log, login.token, login.url, login.timestamp) : null } public async login(handle: string, password: string, url: string, mfaCode: string | null, params: SessionParams): Promise { const { appName, version, deviceId, deviceToken, pushType, notifications } = params; const { guid, appToken, created, pushSupported } = await setLogin(url, handle, password, mfaCode, appName, version, deviceId, deviceToken, pushType, notifications); const login: Login = { guid, url, token: appToken, timestamp: created, pushSupported }; - this.store.setLogin(login); + await this.store.setLogin(login); return new SessionModule(this.store, this.crypto, this.log, appToken, url, created); } public async access(url: string, token: string, params: SessionParams): Promise { - return new SessionModule(this.store, this.crypto, this.log, '', '', 0); + const { appName, version, deviceId, deviceToken, pushType, notifications } = params; + const { guid, appToken, created, pushSupported } = await setAccess(url, token, appName, version, deviceId, deviceToken, pushType, notifications); + const login: Login = { guid, url, token: appToken, timestamp: created, pushSupported }; + await this.store.setLogin(login); + return new SessionModule(this.store, this.crypto, this.log, appToken, url, created); } public async create(handle: string, password: string, url: string, token: string | null, params: SessionParams): Promise { @@ -60,7 +59,7 @@ export class DatabagSDK { const { appName, version, deviceId, deviceToken, pushType, notifications } = params; const { guid, appToken, created, pushSupported } = await setLogin(url, handle, password, null, appName, version, deviceId, deviceToken, pushType, notifications); const login: Login = { guid, url, token: appToken, timestamp: created, pushSupported }; - this.store.setLogin(login); + await this.store.setLogin(login); return new SessionModule(this.store, this.crypto, this.log, appToken, url, created); } diff --git a/app/sdk/src/net/addAccount.ts b/app/sdk/src/net/addAccount.ts index b26d9e5b..50600113 100644 --- a/app/sdk/src/net/addAccount.ts +++ b/app/sdk/src/net/addAccount.ts @@ -6,8 +6,6 @@ export async function addAccount(url: string, username: string, password: string const endpoint = `${url}/account/profile${access}` const auth = encode(`${username}:${password}`) -console.log("ADD ACCOUNT: ", endpoint); - const response = await Promise.race<{ status: number }>([ axios.post(endpoint, null, { headers: { 'Credentials' : `Basic ${auth}` } }).catch(err => { throw new Error('addAccount failed') }), new Promise((_, reject) => setTimeout(() => reject(new Error('addAccount timeout')), 60000)) diff --git a/app/sdk/src/net/setAccess.ts b/app/sdk/src/net/setAccess.ts new file mode 100644 index 00000000..fa901cea --- /dev/null +++ b/app/sdk/src/net/setAccess.ts @@ -0,0 +1,12 @@ +import axios from 'redaxios'; +import { encode } from './base64'; + +export async function setAccess(url: string, token: string, appName: string, appVersion: string, platform: string, deviceToken: string, pushType: string, notifications: { event: string, messageTitle: string }[]): Promise<{ guid: string, appToken: string, created: number, pushSupported: boolean }> { + const endpoint = `${url}/account/access?token=${token}&appName=${appName}&appVersion=${appVersion}&platform=${platform}&deviceToken=${deviceToken}&pushType=${pushType}` + const response = await axios.put(endpoint, notifications) + if (response.status >= 400 && response.status < 600) { + throw new Error('setAccess failed') + } + return response.data; +} +