| import { NextResponse } from "next/server"; |
| import { createProviderConnection } from "@/models"; |
| import { extractCodexAccountInfo } from "@/lib/oauth/providers"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| export async function POST(request) { |
| try { |
| const { accessToken, name } = await request.json(); |
|
|
| if (!accessToken || typeof accessToken !== "string") { |
| return NextResponse.json( |
| { error: "Access token is required" }, |
| { status: 400 } |
| ); |
| } |
|
|
| const token = accessToken.trim(); |
|
|
| |
| let email = null; |
| let providerSpecificData = { authMethod: "access_token" }; |
|
|
| |
| try { |
| const parts = token.split("."); |
| if (parts.length === 3) { |
| const base64 = parts[1].replace(/-/g, "+").replace(/_/g, "/"); |
| const missingPadding = (4 - (base64.length % 4)) % 4; |
| const padded = base64 + "=".repeat(missingPadding); |
| const payload = JSON.parse(Buffer.from(padded, "base64").toString("utf8")); |
|
|
| |
| const auth = payload["https://api.openai.com/auth"] || {}; |
| const profile = payload["https://api.openai.com/profile"] || {}; |
| email = profile.email || payload.email || payload.preferred_username || null; |
|
|
| if (auth.chatgpt_account_id) { |
| providerSpecificData.chatgptAccountId = auth.chatgpt_account_id; |
| } |
| if (auth.chatgpt_plan_type) { |
| providerSpecificData.chatgptPlanType = auth.chatgpt_plan_type; |
| } |
|
|
| |
| if (payload.exp) { |
| providerSpecificData.jwtExp = payload.exp; |
| } |
| } |
| } catch { |
| |
| } |
|
|
| |
| |
| if (!email) { |
| const info = extractCodexAccountInfo(token); |
| if (info.email) email = info.email; |
| if (info.chatgptAccountId) providerSpecificData.chatgptAccountId = info.chatgptAccountId; |
| if (info.chatgptPlanType) providerSpecificData.chatgptPlanType = info.chatgptPlanType; |
| } |
|
|
| const connectionName = name || email || "ChatGPT Access Token"; |
|
|
| |
| const connection = await createProviderConnection({ |
| provider: "codex", |
| authType: "access_token", |
| accessToken: token, |
| name: connectionName, |
| email: email, |
| providerSpecificData, |
| testStatus: "active", |
| }); |
|
|
| return NextResponse.json({ |
| success: true, |
| connection: { |
| id: connection.id, |
| provider: connection.provider, |
| email: connection.email, |
| name: connection.name, |
| workspace: providerSpecificData.chatgptAccountId || null, |
| plan: providerSpecificData.chatgptPlanType || null, |
| }, |
| }); |
| } catch (error) { |
| console.log("Codex access token import error:", error); |
| return NextResponse.json({ error: error.message }, { status: 500 }); |
| } |
| } |
|
|