Spaces:
Runtime error
Runtime error
File size: 2,890 Bytes
cd8bd0a | 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 | /**
* Grok Build OAuth Provider — Import Token Flow with Refresh Support
*
* User pastes the entire auth.json from ~/.grok/auth.json
* or just the JWT access token string.
* Supports automatic token refresh using the refresh_token.
*/
import { GROK_CLI_CONFIG } from "../constants/oauth";
interface GrokCliAuthInfo {
user_id: string;
email: string;
team_id: string;
tier: number;
principal_type: string;
}
function parseJwtPayload(token: string): {
email: string | null;
authInfo: GrokCliAuthInfo | null;
} {
try {
const parts = token.split(".");
if (parts.length !== 3) return { email: null, authInfo: null };
let base64 = parts[1];
switch (base64.length % 4) {
case 2:
base64 += "==";
break;
case 3:
base64 += "=";
break;
}
base64 = base64.replace(/-/g, "+").replace(/_/g, "/");
const payload = JSON.parse(Buffer.from(base64, "base64").toString("utf-8"));
return {
email: payload.email || null,
authInfo: {
user_id: payload.sub || "",
email: payload.email || "",
team_id: payload.team_id || "",
tier: payload.tier || 1,
principal_type: payload.principal_type || "User",
},
};
} catch {
return { email: null, authInfo: null };
}
}
/**
* Extract the JWT access token and refresh_token from user input.
* Accepts either:
* - Raw JWT string (no refresh_token available)
* - The entire auth.json object: { "https://auth.x.ai::...": { "key": "eyJ...", "refresh_token": "..." } }
*/
function extractTokenAndRefresh(input: any): { accessToken: string; refreshToken: string | null } {
if (typeof input === "string") return { accessToken: input, refreshToken: null };
if (input && typeof input === "object") {
// auth.json format: first entry's "key" and "refresh_token" fields
const keys = Object.keys(input);
if (keys.length > 0 && input[keys[0]]?.key) {
return {
accessToken: input[keys[0]].key,
refreshToken: input[keys[0]].refresh_token || null,
};
}
// Already has accessToken
if (input.accessToken) {
return {
accessToken: input.accessToken,
refreshToken: input.refreshToken || null,
};
}
}
return { accessToken: "", refreshToken: null };
}
export const grokCli = {
config: GROK_CLI_CONFIG,
flowType: "import_token",
mapTokens: (token: any) => {
const { accessToken, refreshToken } = extractTokenAndRefresh(token);
const { email, authInfo } = parseJwtPayload(accessToken);
return {
accessToken,
refreshToken,
expiresIn: 21600,
email,
providerSpecificData: {
userId: authInfo?.user_id || null,
teamId: authInfo?.team_id || null,
tier: authInfo?.tier || 1,
principalType: authInfo?.principal_type || "User",
},
};
},
};
|