Skip to main content

raps_kernel/auth/
types.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2024-2025 Dmytro Yemelianov
3
4//! Authentication types for APS OAuth 2.0
5
6use serde::{Deserialize, Serialize};
7use std::time::{Duration, Instant};
8
9use crate::types::StoredToken;
10
11/// User profile information from /userinfo endpoint
12#[derive(Debug, Clone, Deserialize)]
13// API response structs may contain fields we don't use - this is expected for external API contracts
14#[allow(dead_code)]
15pub struct UserInfo {
16    /// The unique APS ID of the user
17    pub sub: String,
18    /// Full name
19    pub name: Option<String>,
20    /// First name
21    pub given_name: Option<String>,
22    /// Last name
23    pub family_name: Option<String>,
24    /// Preferred username
25    pub preferred_username: Option<String>,
26    /// Email address
27    pub email: Option<String>,
28    /// Whether email is verified
29    pub email_verified: Option<bool>,
30    /// Profile URL
31    pub profile: Option<String>,
32    /// Profile picture URL
33    pub picture: Option<String>,
34}
35
36/// OAuth 2.0 token response from APS
37#[derive(Debug, Clone, Deserialize, Serialize)]
38pub struct TokenResponse {
39    pub access_token: String,
40    pub token_type: String,
41    pub expires_in: u64,
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub refresh_token: Option<String>,
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub scope: Option<String>,
46}
47
48/// Cached token with expiry tracking (for 2-legged)
49#[derive(Debug, Clone)]
50pub(crate) struct CachedToken {
51    pub(crate) access_token: String,
52    pub(crate) expires_at: Instant,
53}
54
55impl CachedToken {
56    pub(crate) fn is_valid(&self) -> bool {
57        self.expires_at > Instant::now() + Duration::from_secs(60)
58    }
59}
60
61/// Cached 3-legged token with refresh coordination
62#[derive(Debug, Clone)]
63pub(crate) struct TokenCache {
64    pub(crate) token: Option<StoredToken>,
65    pub(crate) refreshing: bool,
66}