summaryrefslogtreecommitdiff
path: root/src/domain/models.rs
blob: 26e6df3db94771840114df0f9445a40e78aa1e88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Core domain model for OAuth2 clients
#[derive(Debug, Clone, PartialEq)]
pub struct OAuthClient {
    pub client_id: String,
    pub client_name: String,
    pub redirect_uris: Vec<String>,
    pub scopes: Vec<String>,
    pub grant_types: Vec<String>,
    pub response_types: Vec<String>,
    pub is_active: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

/// Core domain model for authorization codes
#[derive(Debug, Clone, PartialEq)]
pub struct AuthorizationCode {
    pub code: String,
    pub client_id: String,
    pub user_id: String,
    pub redirect_uri: String,
    pub scopes: Vec<String>,
    pub expires_at: DateTime<Utc>,
    pub created_at: DateTime<Utc>,
    pub is_used: bool,
    // PKCE fields
    pub code_challenge: Option<String>,
    pub code_challenge_method: Option<String>,
}

/// Core domain model for access tokens
#[derive(Debug, Clone, PartialEq)]
pub struct AccessToken {
    pub token_id: String,
    pub client_id: String,
    pub user_id: String,
    pub scopes: Vec<String>,
    pub expires_at: DateTime<Utc>,
    pub created_at: DateTime<Utc>,
    pub is_revoked: bool,
}

/// Core domain model for refresh tokens
#[derive(Debug, Clone, PartialEq)]
pub struct RefreshToken {
    pub token_id: String,
    pub access_token_id: String,
    pub client_id: String,
    pub user_id: String,
    pub scopes: Vec<String>,
    pub expires_at: DateTime<Utc>,
    pub created_at: DateTime<Utc>,
    pub is_revoked: bool,
}

/// Domain model for audit events
#[derive(Debug, Clone, PartialEq)]
pub struct AuditEvent {
    pub event_type: String,
    pub client_id: Option<String>,
    pub user_id: Option<String>,
    pub ip_address: Option<String>,
    pub user_agent: Option<String>,
    pub details: Option<String>,
    pub success: bool,
    pub timestamp: DateTime<Utc>,
}

/// Domain model for rate limiting
#[derive(Debug, Clone, PartialEq)]
pub struct RateLimit {
    pub identifier: String,
    pub endpoint: String,
    pub count: u32,
    pub window_start: DateTime<Utc>,
    pub window_duration_minutes: u32,
}

/// JWT token claims for domain use
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TokenClaims {
    pub sub: String,           // Subject (user ID)
    pub iss: String,           // Issuer
    pub aud: String,           // Audience (client ID)
    pub exp: u64,              // Expiration time
    pub iat: u64,              // Issued at
    pub jti: String,           // JWT ID (token ID)
    pub scope: Option<String>, // Granted scopes
}

/// OAuth2 authorization request
#[derive(Debug, Clone, PartialEq)]
pub struct AuthorizationRequest {
    pub client_id: String,
    pub redirect_uri: String,
    pub response_type: String,
    pub scope: Option<String>,
    pub state: Option<String>,
    // PKCE
    pub code_challenge: Option<String>,
    pub code_challenge_method: Option<String>,
}

/// OAuth2 token request
#[derive(Debug, Clone, PartialEq)]
pub struct TokenRequest {
    pub grant_type: String,
    pub code: Option<String>,          // For authorization_code grant
    pub refresh_token: Option<String>, // For refresh_token grant
    pub redirect_uri: Option<String>,  // For authorization_code grant
    pub client_id: String,
    pub client_secret: Option<String>,
    // PKCE
    pub code_verifier: Option<String>,
}

/// Result of successful authorization
#[derive(Debug, Clone, PartialEq)]
pub struct AuthorizationResult {
    pub redirect_url: String,
}

/// Result of successful token generation
#[derive(Debug, Clone, PartialEq)]
pub struct TokenResult {
    pub access_token: String,
    pub token_type: String,
    pub expires_in: u64,
    pub refresh_token: Option<String>,
    pub scope: Option<String>,
}

/// OAuth2 error with domain context
#[derive(Debug, Clone, PartialEq)]
pub struct OAuthError {
    pub error_code: String,
    pub description: Option<String>,
    pub uri: Option<String>,
}

impl OAuthError {
    pub fn invalid_request(description: &str) -> Self {
        Self {
            error_code: "invalid_request".to_string(),
            description: Some(description.to_string()),
            uri: None,
        }
    }

    pub fn invalid_client(description: &str) -> Self {
        Self {
            error_code: "invalid_client".to_string(),
            description: Some(description.to_string()),
            uri: None,
        }
    }

    pub fn invalid_grant(description: &str) -> Self {
        Self {
            error_code: "invalid_grant".to_string(),
            description: Some(description.to_string()),
            uri: None,
        }
    }

    pub fn unsupported_grant_type(description: &str) -> Self {
        Self {
            error_code: "unsupported_grant_type".to_string(),
            description: Some(description.to_string()),
            uri: None,
        }
    }

    pub fn server_error(description: &str) -> Self {
        Self {
            error_code: "server_error".to_string(),
            description: Some(description.to_string()),
            uri: None,
        }
    }
}

/// User representation for OAuth2 flows
#[derive(Debug, Clone, PartialEq)]
pub struct User {
    pub id: String,
    pub username: Option<String>,
    pub email: Option<String>,
    pub is_active: bool,
}

/// Scope representation
#[derive(Debug, Clone, PartialEq)]
pub struct Scope {
    pub name: String,
    pub description: Option<String>,
}

impl Scope {
    pub fn openid() -> Self {
        Self {
            name: "openid".to_string(),
            description: Some("OpenID Connect scope".to_string()),
        }
    }

    pub fn profile() -> Self {
        Self {
            name: "profile".to_string(),
            description: Some("Access to user profile information".to_string()),
        }
    }

    pub fn email() -> Self {
        Self {
            name: "email".to_string(),
            description: Some("Access to user email address".to_string()),
        }
    }
}