generated from Nodarx/template
22 lines
788 B
TypeScript
22 lines
788 B
TypeScript
import { createFetchWrapper } from '@KTXC'
|
|
|
|
export interface LaunchSession {
|
|
actionUrl: string
|
|
accessToken: string
|
|
accessTokenTtl: number
|
|
officeOrigin: string
|
|
mode: 'view'
|
|
}
|
|
|
|
const api = createFetchWrapper()
|
|
export const launch = (resource: string): Promise<LaunchSession> => api.post('/m/service_wopi/sessions', { resource })
|
|
|
|
export function validateSession(session: LaunchSession): void {
|
|
const action = new URL(session.actionUrl)
|
|
if (action.protocol !== 'https:' || action.origin !== session.officeOrigin || action.username || action.password
|
|
|| session.mode !== 'view' || !/^[a-f0-9]{64}$/.test(session.accessToken)
|
|
|| !Number.isSafeInteger(session.accessTokenTtl) || session.accessTokenTtl <= Date.now()) {
|
|
throw new Error('Invalid office session.')
|
|
}
|
|
}
|