Skip to main content

micron/api/
mod.rs

1use std::time::Duration;
2
3use serde::{Deserialize, Serialize};
4
5// TODO make into set of enum variants for more granular control
6/// Defines the scope of access for resulting access token.
7#[derive(Clone, Debug, Default, Serialize, Deserialize)]
8pub enum AuthScope {
9    /// Restricted to publicly visible information
10    Public,
11    #[default]
12    Complete,
13}
14
15/// Defines the length-of-life of resulting access token.
16#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
17pub enum AuthDuration {
18    /// 1 hour
19    Short,
20    /// 1 day
21    Medium,
22    /// 30 days
23    #[default]
24    Long,
25}
26
27// conversion method for making `AuthDuration` into an actual `Duration`
28impl Into<Duration> for AuthDuration {
29    fn into(self) -> Duration {
30        match self {
31            Self::Short => Duration::from_secs(60 * 60),
32            Self::Medium => Duration::from_secs(24 * 60 * 60),
33            Self::Long => Duration::from_secs(30 * 24 * 60 * 60),
34        }
35    }
36}
37
38/// Auth request to be sent to `api/auth` endpoint.
39///
40/// If credentials match a new access token will be generated and sent back
41/// to the caller. The token will be generated using information provided in
42/// the request.
43#[derive(Clone, Debug, Serialize, Deserialize)]
44pub struct AuthRequest {
45    pub email: String,
46    pub password: String,
47    /// Scope of information that shall be available when using the resulting
48    /// token.
49    pub scope: AuthScope,
50    /// General duration for which the resulting token shall be valid.
51    pub term: AuthDuration,
52    /// Context in which the resulting token is being requested, e.g.
53    /// application name or other additional information.
54    pub context: String,
55}
56
57/// Auth response to be sent back from `api/auth` endpoint.
58///
59/// Contains a newly generated access token that can be used with subsequent
60/// API requests (as bearer token).
61#[derive(Clone, Debug, Serialize, Deserialize)]
62pub struct AuthResponse {
63    pub token: String,
64}
65
66#[derive(Clone, Debug, Serialize, Deserialize)]
67pub struct MeResponse {
68    pub email: String,
69    pub name: String,
70    pub plan: String,
71}
72
73#[derive(Clone, Debug, Serialize, Deserialize)]
74pub struct CreditsAddRequest {
75    pub amount: u32,
76}
77#[derive(Clone, Debug, Serialize, Deserialize)]
78pub struct CreditsAddResponse {
79    pub resulting_balance: f32,
80}
81
82#[derive(Clone, Debug, Serialize, Deserialize)]
83pub struct DeployQuery {
84    pub name: String,
85    pub address: String,
86}