59 lines
1.1 KiB
TypeScript
59 lines
1.1 KiB
TypeScript
import { randomUUID } from 'crypto'
|
|
|
|
type DingtalkSessionTicket = {
|
|
id: string
|
|
redirectPath: string
|
|
cookies: string[]
|
|
expiresAt: number
|
|
}
|
|
|
|
const TICKET_TTL_MS = 60_000
|
|
const tickets = new Map<string, DingtalkSessionTicket>()
|
|
|
|
function cleanupExpiredTickets() {
|
|
const now = Date.now()
|
|
tickets.forEach((ticket, id) => {
|
|
if (ticket.expiresAt <= now) {
|
|
tickets.delete(id)
|
|
}
|
|
})
|
|
}
|
|
|
|
export function createDingtalkSessionTicket(input: {
|
|
redirectPath: string
|
|
cookies: string[]
|
|
}) {
|
|
cleanupExpiredTickets()
|
|
|
|
const id = randomUUID()
|
|
tickets.set(id, {
|
|
id,
|
|
redirectPath: input.redirectPath,
|
|
cookies: input.cookies,
|
|
expiresAt: Date.now() + TICKET_TTL_MS,
|
|
})
|
|
|
|
return id
|
|
}
|
|
|
|
export function consumeDingtalkSessionTicket(id: string | null | undefined) {
|
|
cleanupExpiredTickets()
|
|
|
|
const normalizedId = String(id || '').trim()
|
|
if (!normalizedId) {
|
|
return null
|
|
}
|
|
|
|
const ticket = tickets.get(normalizedId)
|
|
tickets.delete(normalizedId)
|
|
|
|
if (!ticket || ticket.expiresAt <= Date.now()) {
|
|
return null
|
|
}
|
|
|
|
return {
|
|
redirectPath: ticket.redirectPath,
|
|
cookies: ticket.cookies,
|
|
}
|
|
}
|