-
Notifications
You must be signed in to change notification settings - Fork 10.8k
chore: use access token expiration for proactive auth refresh #15545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| use base64::Engine; | ||
| use chrono::DateTime; | ||
| use chrono::Utc; | ||
| use serde::Deserialize; | ||
| use serde::Serialize; | ||
| use serde::de::DeserializeOwned; | ||
| use thiserror::Error; | ||
|
|
||
| #[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Default)] | ||
|
|
@@ -117,6 +120,12 @@ struct AuthClaims { | |
| chatgpt_account_id: Option<String>, | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct StandardJwtClaims { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why a separate struct? can we put this onto AuthClaims?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or IdClaims ?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this function is supposed to be generic so it can parse any jwt, not just id / access tokens I think |
||
| #[serde(default)] | ||
| exp: Option<i64>, | ||
| } | ||
|
|
||
| #[derive(Debug, Error)] | ||
| pub enum IdTokenInfoError { | ||
| #[error("invalid ID token format")] | ||
|
|
@@ -127,7 +136,7 @@ pub enum IdTokenInfoError { | |
| Json(#[from] serde_json::Error), | ||
| } | ||
|
|
||
| pub fn parse_chatgpt_jwt_claims(jwt: &str) -> Result<IdTokenInfo, IdTokenInfoError> { | ||
| fn decode_jwt_payload<T: DeserializeOwned>(jwt: &str) -> Result<T, IdTokenInfoError> { | ||
| // JWT format: header.payload.signature | ||
| let mut parts = jwt.split('.'); | ||
| let (_header_b64, payload_b64, _sig_b64) = match (parts.next(), parts.next(), parts.next()) { | ||
|
|
@@ -136,7 +145,19 @@ pub fn parse_chatgpt_jwt_claims(jwt: &str) -> Result<IdTokenInfo, IdTokenInfoErr | |
| }; | ||
|
|
||
| let payload_bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(payload_b64)?; | ||
| let claims: IdClaims = serde_json::from_slice(&payload_bytes)?; | ||
| let claims = serde_json::from_slice(&payload_bytes)?; | ||
| Ok(claims) | ||
| } | ||
|
|
||
| pub fn parse_jwt_expiration(jwt: &str) -> Result<Option<DateTime<Utc>>, IdTokenInfoError> { | ||
| let claims: StandardJwtClaims = decode_jwt_payload(jwt)?; | ||
| Ok(claims | ||
| .exp | ||
| .and_then(|exp| DateTime::<Utc>::from_timestamp(exp, 0))) | ||
| } | ||
|
|
||
| pub fn parse_chatgpt_jwt_claims(jwt: &str) -> Result<IdTokenInfo, IdTokenInfoError> { | ||
| let claims: IdClaims = decode_jwt_payload(jwt)?; | ||
| let email = claims | ||
| .or_else(|| claims.profile.and_then(|profile| profile.email)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we want a buffer? TOKEN_REFRESH_INTERVAL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need a buffer?
expired_atis a timestamp? we still have the fallback code path that usesToken_refresh_intervalafter this in caseexpfield doesn't exist in token?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need that codepath, can "exp" not be there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically, jwt token doesn't necessarily have the 'exp' field. In reality, the auth token returned should always have this field. We can also throw an error here if exp doesn't exist, but I think having this silent fallback is safer