118 lines
3.6 KiB
Rust
118 lines
3.6 KiB
Rust
|
|
use base64::Engine;
|
||
|
|
use serde::Deserialize;
|
||
|
|
use serde::Serialize;
|
||
|
|
use thiserror::Error;
|
||
|
|
|
||
|
|
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Default)]
|
||
|
|
pub struct TokenData {
|
||
|
|
/// Flat info parsed from the JWT in auth.json.
|
||
|
|
#[serde(deserialize_with = "deserialize_id_token")]
|
||
|
|
pub id_token: IdTokenInfo,
|
||
|
|
|
||
|
|
/// This is a JWT.
|
||
|
|
pub access_token: String,
|
||
|
|
|
||
|
|
pub refresh_token: String,
|
||
|
|
|
||
|
|
pub account_id: Option<String>,
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Flat subset of useful claims in id_token from auth.json.
|
||
|
|
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
|
||
|
|
pub struct IdTokenInfo {
|
||
|
|
pub email: Option<String>,
|
||
|
|
/// The ChatGPT subscription plan type
|
||
|
|
/// (e.g., "free", "plus", "pro", "business", "enterprise", "edu").
|
||
|
|
/// (Note: ae has not verified that those are the exact values.)
|
||
|
|
pub chatgpt_plan_type: Option<String>,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Deserialize)]
|
||
|
|
struct IdClaims {
|
||
|
|
#[serde(default)]
|
||
|
|
email: Option<String>,
|
||
|
|
#[serde(rename = "https://api.openai.com/auth", default)]
|
||
|
|
auth: Option<AuthClaims>,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Deserialize)]
|
||
|
|
struct AuthClaims {
|
||
|
|
#[serde(default)]
|
||
|
|
chatgpt_plan_type: Option<String>,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Error)]
|
||
|
|
pub enum IdTokenInfoError {
|
||
|
|
#[error("invalid ID token format")]
|
||
|
|
InvalidFormat,
|
||
|
|
#[error(transparent)]
|
||
|
|
Base64(#[from] base64::DecodeError),
|
||
|
|
#[error(transparent)]
|
||
|
|
Json(#[from] serde_json::Error),
|
||
|
|
}
|
||
|
|
|
||
|
|
pub(crate) fn parse_id_token(id_token: &str) -> Result<IdTokenInfo, IdTokenInfoError> {
|
||
|
|
// JWT format: header.payload.signature
|
||
|
|
let mut parts = id_token.split('.');
|
||
|
|
let (_header_b64, payload_b64, _sig_b64) = match (parts.next(), parts.next(), parts.next()) {
|
||
|
|
(Some(h), Some(p), Some(s)) if !h.is_empty() && !p.is_empty() && !s.is_empty() => (h, p, s),
|
||
|
|
_ => return Err(IdTokenInfoError::InvalidFormat),
|
||
|
|
};
|
||
|
|
|
||
|
|
let payload_bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(payload_b64)?;
|
||
|
|
let claims: IdClaims = serde_json::from_slice(&payload_bytes)?;
|
||
|
|
|
||
|
|
Ok(IdTokenInfo {
|
||
|
|
email: claims.email,
|
||
|
|
chatgpt_plan_type: claims.auth.and_then(|a| a.chatgpt_plan_type),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
fn deserialize_id_token<'de, D>(deserializer: D) -> Result<IdTokenInfo, D::Error>
|
||
|
|
where
|
||
|
|
D: serde::Deserializer<'de>,
|
||
|
|
{
|
||
|
|
let s = String::deserialize(deserializer)?;
|
||
|
|
parse_id_token(&s).map_err(serde::de::Error::custom)
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use super::*;
|
||
|
|
use serde::Serialize;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
#[expect(clippy::expect_used, clippy::unwrap_used)]
|
||
|
|
fn id_token_info_parses_email_and_plan() {
|
||
|
|
// Build a fake JWT with a URL-safe base64 payload containing email and plan.
|
||
|
|
#[derive(Serialize)]
|
||
|
|
struct Header {
|
||
|
|
alg: &'static str,
|
||
|
|
typ: &'static str,
|
||
|
|
}
|
||
|
|
let header = Header {
|
||
|
|
alg: "none",
|
||
|
|
typ: "JWT",
|
||
|
|
};
|
||
|
|
let payload = serde_json::json!({
|
||
|
|
"email": "user@example.com",
|
||
|
|
"https://api.openai.com/auth": {
|
||
|
|
"chatgpt_plan_type": "pro"
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
fn b64url_no_pad(bytes: &[u8]) -> String {
|
||
|
|
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes)
|
||
|
|
}
|
||
|
|
|
||
|
|
let header_b64 = b64url_no_pad(&serde_json::to_vec(&header).unwrap());
|
||
|
|
let payload_b64 = b64url_no_pad(&serde_json::to_vec(&payload).unwrap());
|
||
|
|
let signature_b64 = b64url_no_pad(b"sig");
|
||
|
|
let fake_jwt = format!("{header_b64}.{payload_b64}.{signature_b64}");
|
||
|
|
|
||
|
|
let info = parse_id_token(&fake_jwt).expect("should parse");
|
||
|
|
assert_eq!(info.email.as_deref(), Some("user@example.com"));
|
||
|
|
assert_eq!(info.chatgpt_plan_type.as_deref(), Some("pro"));
|
||
|
|
}
|
||
|
|
}
|