use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Token {
pub token: String,
pub token_type: String,
pub expires_in: i64,
}
impl Token {
pub fn new(token: String, expires_in: i64) -> Self {
Self {
token,
token_type: "Bearer".to_string(),
expires_in,
}
}
pub fn with_type(token: String, token_type: String, expires_in: i64) -> Self {
Self {
token,
token_type,
expires_in,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TokenPair {
pub access_token: String,
pub refresh_token: String,
pub token_type: String,
pub expires_in: i64,
pub refresh_expires_in: i64,
}
impl TokenPair {
pub fn new(
access_token: String,
refresh_token: String,
expires_in: i64,
refresh_expires_in: i64,
) -> Self {
Self {
access_token,
refresh_token,
token_type: "Bearer".to_string(),
expires_in,
refresh_expires_in,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_token_creation() {
let token = Token::new("abc123".to_string(), 3600);
assert_eq!(token.token, "abc123");
assert_eq!(token.token_type, "Bearer");
assert_eq!(token.expires_in, 3600);
}
#[test]
fn test_token_pair() {
let pair = TokenPair::new(
"access123".to_string(),
"refresh456".to_string(),
3600,
604800,
);
assert_eq!(pair.access_token, "access123");
assert_eq!(pair.refresh_token, "refresh456");
assert_eq!(pair.expires_in, 3600);
assert_eq!(pair.refresh_expires_in, 604800);
}
}