Skip to main content

android_sms_gateway/types/
auth.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4/// A JWT scope value.
5///
6/// This is a transparent newtype over `String` with predefined constants
7/// for all available scopes.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct JwtScope(pub String);
11
12impl JwtScope {
13    pub const DEVICES_LIST: &'static str = "devices:list";
14    pub const DEVICES_DELETE: &'static str = "devices:delete";
15    pub const INBOX_LIST: &'static str = "inbox:list";
16    pub const INBOX_REFRESH: &'static str = "inbox:refresh";
17    pub const LOGS_READ: &'static str = "logs:read";
18    pub const MESSAGES_CANCEL: &'static str = "messages:cancel";
19    pub const MESSAGES_SEND: &'static str = "messages:send";
20    pub const MESSAGES_READ: &'static str = "messages:read";
21    pub const MESSAGES_LIST: &'static str = "messages:list";
22    pub const MESSAGES_EXPORT: &'static str = "messages:export";
23    pub const SETTINGS_READ: &'static str = "settings:read";
24    pub const SETTINGS_WRITE: &'static str = "settings:write";
25    pub const TOKENS_MANAGE: &'static str = "tokens:manage";
26    pub const WEBHOOKS_LIST: &'static str = "webhooks:list";
27    pub const WEBHOOKS_WRITE: &'static str = "webhooks:write";
28    pub const WEBHOOKS_DELETE: &'static str = "webhooks:delete";
29
30    /// Creates a new JWT scope value.
31    pub fn new(s: impl Into<String>) -> Self {
32        Self(s.into())
33    }
34
35    /// Returns the scope as a string slice.
36    pub fn as_str(&self) -> &str {
37        &self.0
38    }
39}
40
41/// Request to generate a new JWT token.
42#[derive(Debug, Clone, Serialize, Deserialize)]
43#[serde(rename_all = "camelCase")]
44pub struct TokenRequest {
45    /// Time-to-live in seconds for the token.
46    #[serde(default, skip_serializing_if = "Option::is_none")]
47    pub ttl: Option<u64>,
48    /// List of scopes to grant.
49    #[serde(default)]
50    pub scopes: Vec<JwtScope>,
51}
52
53/// Response containing a generated JWT token.
54#[derive(Debug, Clone, Serialize, Deserialize)]
55#[serde(rename_all = "camelCase")]
56pub struct TokenResponse {
57    /// Token ID (JTI).
58    pub id: String,
59    /// Token type (e.g., "bearer").
60    pub token_type: String,
61    /// The access token string.
62    pub access_token: String,
63    /// Optional refresh token for renewing the access token.
64    #[serde(default, skip_serializing_if = "Option::is_none")]
65    pub refresh_token: Option<String>,
66    /// Token expiration time.
67    pub expires_at: DateTime<Utc>,
68}