File size: 3,560 Bytes
f52d137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { UserInfo } from "../types";
import { sha256, randomString } from "./crypto";

const OAUTH_BASE = "https://huggingface.co";
const CONFIG_URL = "/config.json";

export const getSpaceUrl = (): string => {
  return window.location.origin;
};

export const readFragmentParams = () => {
  const hash = window.location.hash.replace(/^#/, "");
  const params = new URLSearchParams(hash);
  return Object.fromEntries(params.entries());
};

export const getPageSignFromUrl = (): string | null => {
  try {
    const params = new URLSearchParams(window.location.search);
    const sign = params.get("__sign");
    return sign && sign.trim().length > 0 ? sign.trim() : null;
  } catch (_e) {
    return null;
  }
};

// Configuration loading
export const loadConfig = async () => {
  try {
    const res = await fetch(CONFIG_URL, { cache: "no-store" });
    if (res.ok) return await res.json();
  } catch {}
  return {};
};

export const getClientId = async (): Promise<string> => {
  const params = new URLSearchParams(window.location.search);
  const override = params.get("client_id");
  if (override && override.trim().length > 0) return override.trim();

  const cfg = await loadConfig();
  if (
    cfg &&
    typeof cfg.client_id === "string" &&
    cfg.client_id.trim().length > 0
  ) {
    return cfg.client_id.trim();
  }
  return "spaces_oauth_client";
};

export const getRedirectUri = async (): Promise<string> => {
  const cfg = await loadConfig();
  if (
    cfg &&
    typeof cfg.redirect_uri === "string" &&
    cfg.redirect_uri.trim().length > 0
  ) {
    return cfg.redirect_uri.trim();
  }
  return getSpaceUrl();
};

// OAuth functions
export const fetchUserInfo = async (token: string): Promise<UserInfo> => {
  const res = await fetch(`${OAUTH_BASE}/oauth/userinfo`, {
    headers: { Authorization: `Bearer ${token}` },
  });
  if (!res.ok) throw new Error("Failed to fetch user profile");
  return res.json();
};

export const startLogin = async () => {
  const clientId = await getClientId();
  const redirectUri = await getRedirectUri();
  const scope = "openid profile email inference-api read-repos";

  const state = randomString(16);
  const codeVerifier = randomString(64);
  const codeChallenge = await sha256(codeVerifier);

  sessionStorage.setItem("hf_oauth_state", state);
  sessionStorage.setItem("hf_code_verifier", codeVerifier);

  const authUrl = new URL(`${OAUTH_BASE}/oauth/authorize`);
  authUrl.searchParams.set("client_id", clientId);
  authUrl.searchParams.set("redirect_uri", redirectUri);
  authUrl.searchParams.set("response_type", "code");
  authUrl.searchParams.set("scope", scope);
  authUrl.searchParams.set("state", state);
  authUrl.searchParams.set("code_challenge", codeChallenge);
  authUrl.searchParams.set("code_challenge_method", "S256");

  window.location.assign(authUrl.toString());
};

export const exchangeCodeForToken = async (code: string) => {
  const clientId = await getClientId();
  const redirectUri = await getRedirectUri();
  const codeVerifier = sessionStorage.getItem("hf_code_verifier");
  if (!codeVerifier) throw new Error("Missing code verifier");

  const body = new URLSearchParams({
    grant_type: "authorization_code",
    client_id: clientId,
    code,
    redirect_uri: redirectUri,
    code_verifier: codeVerifier,
  });

  const res = await fetch(`${OAUTH_BASE}/oauth/token`, {
    method: "POST",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    body,
  });
  if (!res.ok) throw new Error("Code→token exchange failed");
  return res.json();
};