AI-native design editor. Open-source Figma alternative.
import type { CloudDiscovery } from '#cloud/contract'
import { joinURL } from 'ufo'
import * as v from 'valibot'
import type { CloudFetch } from './discovery'
const deviceCodeSchema = v.object({
device_code: v.string(),
user_code: v.string(),
verification_uri: v.pipe(v.string(), v.url()),
verification_uri_complete: v.pipe(v.string(), v.url()),
expires_in: v.pipe(v.number(), v.integer(), v.minValue(1)),
interval: v.pipe(v.number(), v.integer(), v.minValue(1))
})
const deviceTokenSchema = v.object({
access_token: v.string(),
token_type: v.literal('Bearer'),
expires_in: v.pipe(v.number(), v.integer(), v.minValue(1)),
scope: v.string()
})
const deviceErrorSchema = v.object({ error: v.string(), error_description: v.optional(v.string()) })
/** RFC 8628: each `slow_down` adds five seconds to the polling interval. */
const SLOW_DOWN_MS = 5000
export type CloudDeviceErrorCode = 'denied' | 'expired' | 'unavailable'
export class CloudDeviceAuthorizationError extends Error {
override readonly name = 'CloudDeviceAuthorizationError'
constructor(readonly code: CloudDeviceErrorCode) {
super(`Cloud device authorization ${code}`)
}
}
export type CloudDeviceAuthorization = v.InferOutput<typeof deviceCodeSchema>
export type CloudDeviceToken = v.InferOutput<typeof deviceTokenSchema>
export type CloudDevicePollingOptions = {
signal?: AbortSignal
fetch?: CloudFetch
now?: () => number
sleep?: (milliseconds: number, signal?: AbortSignal) => Promise<void>
}
function abortableSleep(milliseconds: number, signal?: AbortSignal): Promise<void> {
return new Promise<void>((resolve, reject) => {
const timeout = setTimeout(resolve, milliseconds)
signal?.addEventListener(
'abort',
() => {
clearTimeout(timeout)
reject(
signal.reason instanceof Error
? signal.reason
: new DOMException('Authorization cancelled', 'AbortError')
)
},
{ once: true }
)
})
}
function authEndpoint(discovery: CloudDiscovery, path: string): string {
return joinURL(discovery.authURL, path)
}
export type CloudDeviceAuthorizationOptions = {
fetch?: CloudFetch
}
export async function requestCloudDeviceAuthorization(
discovery: CloudDiscovery,
connectionId: string,
options: CloudDeviceAuthorizationOptions = {}
): Promise<CloudDeviceAuthorization> {
const request = options.fetch ?? globalThis.fetch
const response = await request(authEndpoint(discovery, 'device/code'), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
client_id: `openpencil-desktop:${connectionId}`,
scope: 'openid profile'
})
})
if (!response.ok) throw new CloudDeviceAuthorizationError('unavailable')
return v.parse(deviceCodeSchema, await response.json())
}
export async function pollCloudDeviceToken(
discovery: CloudDiscovery,
connectionId: string,
authorization: CloudDeviceAuthorization,
options: CloudDevicePollingOptions = {}
): Promise<CloudDeviceToken> {
const request = options.fetch ?? globalThis.fetch
const now = options.now ?? Date.now
const sleep = options.sleep ?? abortableSleep
const expiresAt = now() + authorization.expires_in * 1000
let interval = authorization.interval * 1000
while (now() < expiresAt) {
options.signal?.throwIfAborted()
await sleep(interval, options.signal)
if (now() >= expiresAt) break
const response = await request(authEndpoint(discovery, 'device/token'), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
device_code: authorization.device_code,
client_id: `openpencil-desktop:${connectionId}`
}),
signal: options.signal
})
const body = await response.text()
if (response.ok) return v.parse(v.pipe(v.string(), v.parseJson(), deviceTokenSchema), body)
// A rate limit asks for the same as `slow_down`, whatever its body says.
if (response.status === 429) {
interval += SLOW_DOWN_MS
continue
}
const parsed = v.safeParse(v.pipe(v.string(), v.parseJson(), deviceErrorSchema), body)
if (!parsed.success) throw new CloudDeviceAuthorizationError('unavailable')
const error = parsed.output
if (error.error === 'authorization_pending') continue
if (error.error === 'slow_down') {
interval += SLOW_DOWN_MS
continue
}
let code: CloudDeviceErrorCode = 'unavailable'
if (error.error === 'access_denied') code = 'denied'
else if (error.error === 'expired_token') code = 'expired'
throw new CloudDeviceAuthorizationError(code)
}
throw new CloudDeviceAuthorizationError('expired')
}