|
import { OAuthResult } from "@huggingface/hub" |
|
|
|
|
|
export function getValidOAuth(rawInput?: any): OAuthResult | undefined { |
|
try { |
|
let untypedOAuthResult: any |
|
try { |
|
untypedOAuthResult = JSON.parse(rawInput) |
|
if (!untypedOAuthResult) { throw new Error("no valid serialized oauth result") } |
|
} catch (err) { |
|
untypedOAuthResult = rawInput |
|
} |
|
|
|
const maybeValidOAuth = untypedOAuthResult as OAuthResult |
|
|
|
const accessTokenExpiresAt = new Date(maybeValidOAuth.accessTokenExpiresAt) |
|
|
|
|
|
const currentDate = new Date() |
|
|
|
if (accessTokenExpiresAt.getTime() < currentDate.getTime()) { |
|
throw new Error("the serialized oauth result has expired") |
|
} |
|
|
|
return maybeValidOAuth |
|
} catch (err) { |
|
|
|
return undefined |
|
} |
|
} |