File size: 2,721 Bytes
f48f76d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d67e1c
f48f76d
 
 
 
 
 
696b526
 
 
f48f76d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextRequest, NextResponse } from 'next/server';

const TOKEN_ENDPOINT = 'https://huggingface.co/oauth/token';
const USERINFO_ENDPOINT = 'https://huggingface.co/oauth/userinfo';
const STATE_COOKIE = 'hf_oauth_state';

export async function POST(request: NextRequest) {
  const clientId = process.env.HF_OAUTH_CLIENT_ID || process.env.NEXT_PUBLIC_HF_OAUTH_CLIENT_ID;
  const clientSecret = process.env.HF_OAUTH_CLIENT_SECRET;

  if (!clientId || !clientSecret) {
    return NextResponse.json({ error: 'OAuth application is not configured' }, { status: 500 });
  }

  const { code, state } = await request.json().catch(() => ({}));

  if (!code) {
    return NextResponse.json({ error: 'Authorization code is missing' }, { status: 400 });
  }

  const storedState = request.cookies.get(STATE_COOKIE)?.value;
  if (storedState && state !== storedState) {
    const response = NextResponse.json({ error: 'Invalid or expired OAuth state' }, { status: 400 });
    response.cookies.delete(STATE_COOKIE);
    return response;
  }

  const origin = request.nextUrl.origin;
  const envRedirect =
    process.env.HF_OAUTH_REDIRECT_URI || process.env.NEXT_PUBLIC_HF_OAUTH_REDIRECT_URI || '';
  const redirectUri = envRedirect.trim() || `${origin}/auth/hf/callback`;

  try {
    const tokenResponse = await fetch(TOKEN_ENDPOINT, {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      body: new URLSearchParams({
        grant_type: 'authorization_code',
        code,
        redirect_uri: redirectUri,
        client_id: clientId,
        client_secret: clientSecret,
      }),
    });

    if (!tokenResponse.ok) {
      const errorPayload = await tokenResponse.json().catch(() => ({}));
      throw new Error(errorPayload?.error_description || 'Failed to exchange code for token');
    }

    const tokenData = await tokenResponse.json();
    const accessToken = tokenData?.access_token;
    if (!accessToken) {
      throw new Error('Access token missing in response');
    }

    const userResponse = await fetch(USERINFO_ENDPOINT, {
      headers: { Authorization: `Bearer ${accessToken}` },
    });

    if (!userResponse.ok) {
      throw new Error('Failed to fetch user info');
    }

    const profile = await userResponse.json();
    const namespace = profile?.preferred_username || profile?.name || 'user';

    const response = NextResponse.json({
      token: accessToken,
      namespace,
    });
    response.cookies.delete(STATE_COOKIE);
    return response;
  } catch (error: any) {
    const response = NextResponse.json({ error: error?.message || 'OAuth flow failed' }, { status: 500 });
    response.cookies.delete(STATE_COOKIE);
    return response;
  }
}