use serde::{Deserialize, Serialize};
use crate::shared::jwt::JwtToken;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OAuthToken {
pub access_token: String,
pub token_type: String,
pub expires_in: Option<u64>,
pub refresh_token: Option<String>,
pub scope: Option<String>,
pub id_token: Option<String>,
}
impl OAuthToken {
#[must_use]
#[allow(clippy::cast_possible_wrap)]
pub fn into_jwt_token(self) -> Option<JwtToken> {
let expires_at = self.expires_in.map_or_else(
|| chrono::Utc::now().timestamp() + 3600,
|exp| chrono::Utc::now().timestamp() + exp as i64,
);
JwtToken::from_parts(
self.access_token,
self.refresh_token,
expires_at,
self.scope,
None,
None,
)
.into()
}
#[must_use]
pub fn is_expired(&self, buffer_seconds: u64) -> bool {
self.expires_in.is_some_and(|exp| exp <= buffer_seconds)
}
}