use crate::credentials::TokenExpiry;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserToken {
pub(crate) refresh_token: String,
pub(crate) access_token: String,
pub(crate) expiry: u64,
}
impl UserToken {
pub fn access_token(&self) -> String {
self.access_token.to_string()
}
pub fn as_header(&self) -> String {
format!("Bearer {}", self.access_token)
}
pub fn from_access_token(access_token: impl Into<String>) -> Self {
Self {
access_token: access_token.into(),
refresh_token: String::new(),
expiry: u64::MAX,
}
}
#[cfg(any(test, feature = "test-utils"))]
pub fn new_from_raw(
refresh_token: impl Into<String>,
access_token: impl Into<String>,
expiry: u64,
) -> Self {
Self {
refresh_token: refresh_token.into(),
access_token: access_token.into(),
expiry,
}
}
}
impl TokenExpiry<'_> for UserToken {
fn expires_at_secs(&self) -> u64 {
self.expiry
}
}