Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/renderer/src/hooks/usePhoneIsland.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ export const usePhoneIsland = () => {
const { NethVoiceAPI } = useLoggedNethVoiceAPI()

const createDataConfig = async (account: Account): Promise<[Extension, string]> => {
const phoneIslandTokenLoginResponse = (await NethVoiceAPI.Authentication.phoneIslandTokenLogin()).token
const tokenResponse = await NethVoiceAPI.Authentication.phoneIslandTokenLogin()
const phoneIslandToken = tokenResponse?.token
if (!phoneIslandToken) {
throw new Error('Unable to retrieve dedicated Phone Island token')
}

const deviceInformationObject: Extension | undefined = account.data!.endpoints.extension.find((e) => e.type === 'nethlink')
if (deviceInformationObject) {
const hostname = account!.host
const config: PhoneIslandConfig = {
hostname,
username: account.username,
authToken: phoneIslandTokenLoginResponse,
authToken: phoneIslandToken,
sipExten: deviceInformationObject.id,
sipSecret: deviceInformationObject.secret,
sipHost: account.sipHost || '',
Expand Down
43 changes: 35 additions & 8 deletions src/shared/useNethVoiceAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const PRIMARY_API_BASE_PATH = '/api'
const FALLBACK_API_BASE_PATH = '/webrest'

export const useNethVoiceAPI = (loggedAccount: Account | undefined = undefined) => {
const { GET, POST } = useNetwork()
const { GET, POST, DELETE } = useNetwork()
let isFirstHeartbeat = true
let account: Account | undefined = loggedAccount || undefined
// Use account's stored API path preference, or default to primary
Expand All @@ -46,9 +46,6 @@ export const useNethVoiceAPI = (loggedAccount: Account | undefined = undefined)
if (endpoint === '/logout') {
return `${FALLBACK_API_BASE_PATH}/authentication/logout`
}
if (endpoint === '/authentication/phone_island_token_login') {
return `${FALLBACK_API_BASE_PATH}/authentication/phone_island_token_login`
}
// For other endpoints, use webrest format
return `${FALLBACK_API_BASE_PATH}${endpoint}`
}
Expand Down Expand Up @@ -147,6 +144,17 @@ export const useNethVoiceAPI = (loggedAccount: Account | undefined = undefined)
}
}

async function _DELETE(path: string, hasAuth = true): Promise<any> {
try {
return (await DELETE(_joinUrl(path), _getHeaders(hasAuth)))
} catch (e) {
if (!path.includes('login') && !path.includes('2fa/verify-otp')) {
console.error(e)
}
throw e
}
}

function shouldTryFallback(path: string, error: any): boolean {
// Only try fallback if we're using primary path
if (currentApiBasePath !== PRIMARY_API_BASE_PATH) {
Expand Down Expand Up @@ -393,11 +401,30 @@ export const useNethVoiceAPI = (loggedAccount: Account | undefined = undefined)
})
},

phoneIslandTokenLogin: async (): Promise<{ username: string, token: string }> =>
await _POST(buildApiPath('/authentication/phone_island_token_login'), { subtype: 'nethlink' }),
// Dedicated token for Phone Island in NethLink (kept separate by design).
phoneIslandTokenLogin: async (): Promise<{ username: string, token: string }> => {
try {
return await _POST(buildApiPath('/tokens/persistent/nethlink'))
} catch (reason: any) {
if (reason?.response?.status === 404) {
// Legacy middleware fallback.
return await _POST(buildApiPath('/authentication/phone_island_token_login'), { subtype: 'nethlink' })
}
throw reason
}
},

phoneIslandTokenLogout: async (): Promise<{ username: string, token: string }> =>
await _POST(buildApiPath('/authentication/persistent_token_remove'), { type: 'phone-island', subtype: 'nethlink' }),
phoneIslandTokenLogout: async (): Promise<void> => {
try {
return await _DELETE(buildApiPath('/tokens/persistent/nethlink'))
} catch (reason: any) {
if (reason?.response?.status === 404) {
// Legacy middleware fallback.
return await _POST(buildApiPath('/authentication/persistent_token_remove'), { type: 'phone-island', subtype: 'nethlink' })
}
throw reason
}
},
}

const CustCard = {}
Expand Down
12 changes: 12 additions & 0 deletions src/shared/useNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ export const useNetwork = () => {
}
}

async function DELETE(path: string, config: { headers: { Authorization?: string | undefined; 'Content-Type': string } } | undefined = { headers: { 'Content-Type': 'application/json' } }): Promise<any> {
try {
const response = await axios.delete(path, config)
return response.data
} catch (e: any) {
const err: AxiosError = e
Log.error('during fetch DELETE', err.name, err.code, err.message, path, config)
throw e
}
}

async function HEAD(path: string, timeoutMs: number = 5000): Promise<boolean> {
try {
await axios.head(path, {
Expand All @@ -43,6 +54,7 @@ export const useNetwork = () => {
return {
GET,
POST,
DELETE,
HEAD
}
}