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
|
use crate::database::{DbAccessToken, DbAuditLog, DbAuthCode, DbOAuthClient};
use crate::domain::models::*;
use anyhow::Result;
/// Trait for converting from database models to domain models
pub trait FromDb<T> {
fn from_db(db_model: T) -> Result<Self>
where
Self: Sized;
}
/// Trait for converting from domain models to database models
pub trait ToDb<T> {
fn to_db(&self) -> Result<T>;
}
// OAuth Client conversions
impl FromDb<DbOAuthClient> for OAuthClient {
fn from_db(db_client: DbOAuthClient) -> Result<Self> {
let redirect_uris: Vec<String> = serde_json::from_str(&db_client.redirect_uris)?;
let scopes: Vec<String> = db_client
.scopes
.split_whitespace()
.map(|s| s.to_string())
.collect();
let grant_types: Vec<String> = db_client
.grant_types
.split_whitespace()
.map(|s| s.to_string())
.collect();
let response_types: Vec<String> = db_client
.response_types
.split_whitespace()
.map(|s| s.to_string())
.collect();
Ok(OAuthClient {
client_id: db_client.client_id,
client_name: db_client.client_name,
redirect_uris,
scopes,
grant_types,
response_types,
is_active: db_client.is_active,
created_at: db_client.created_at,
updated_at: db_client.updated_at,
})
}
}
impl ToDb<DbOAuthClient> for OAuthClient {
fn to_db(&self) -> Result<DbOAuthClient> {
Ok(DbOAuthClient {
id: 0, // Will be set by database
client_id: self.client_id.clone(),
client_secret_hash: String::new(), // Will be set separately
client_name: self.client_name.clone(),
redirect_uris: serde_json::to_string(&self.redirect_uris)?,
scopes: self.scopes.join(" "),
grant_types: self.grant_types.join(" "),
response_types: self.response_types.join(" "),
created_at: self.created_at,
updated_at: self.updated_at,
is_active: self.is_active,
})
}
}
// Authorization Code conversions
impl FromDb<DbAuthCode> for AuthorizationCode {
fn from_db(db_code: DbAuthCode) -> Result<Self> {
let scopes = db_code
.scope
.map(|s| {
s.split_whitespace()
.map(|scope| scope.to_string())
.collect()
})
.unwrap_or_default();
Ok(AuthorizationCode {
code: db_code.code,
client_id: db_code.client_id,
user_id: db_code.user_id,
redirect_uri: db_code.redirect_uri,
scopes,
expires_at: db_code.expires_at,
created_at: db_code.created_at,
is_used: db_code.is_used,
code_challenge: db_code.code_challenge,
code_challenge_method: db_code.code_challenge_method,
})
}
}
impl ToDb<DbAuthCode> for AuthorizationCode {
fn to_db(&self) -> Result<DbAuthCode> {
let scope = if self.scopes.is_empty() {
None
} else {
Some(self.scopes.join(" "))
};
Ok(DbAuthCode {
id: 0, // Will be set by database
code: self.code.clone(),
client_id: self.client_id.clone(),
user_id: self.user_id.clone(),
redirect_uri: self.redirect_uri.clone(),
scope,
expires_at: self.expires_at,
created_at: self.created_at,
is_used: self.is_used,
code_challenge: self.code_challenge.clone(),
code_challenge_method: self.code_challenge_method.clone(),
})
}
}
// Access Token conversions
impl FromDb<DbAccessToken> for AccessToken {
fn from_db(db_token: DbAccessToken) -> Result<Self> {
let scopes = db_token
.scope
.map(|s| {
s.split_whitespace()
.map(|scope| scope.to_string())
.collect()
})
.unwrap_or_default();
Ok(AccessToken {
token_id: db_token.token_id,
client_id: db_token.client_id,
user_id: db_token.user_id,
scopes,
expires_at: db_token.expires_at,
created_at: db_token.created_at,
is_revoked: db_token.is_revoked,
})
}
}
impl ToDb<DbAccessToken> for AccessToken {
fn to_db(&self) -> Result<DbAccessToken> {
let scope = if self.scopes.is_empty() {
None
} else {
Some(self.scopes.join(" "))
};
Ok(DbAccessToken {
id: 0, // Will be set by database
token_id: self.token_id.clone(),
client_id: self.client_id.clone(),
user_id: self.user_id.clone(),
scope,
expires_at: self.expires_at,
created_at: self.created_at,
is_revoked: self.is_revoked,
token_hash: String::new(), // Will be set by service layer
})
}
}
// Audit Event conversions
impl FromDb<DbAuditLog> for AuditEvent {
fn from_db(db_log: DbAuditLog) -> Result<Self> {
Ok(AuditEvent {
event_type: db_log.event_type,
client_id: db_log.client_id,
user_id: db_log.user_id,
ip_address: db_log.ip_address,
user_agent: db_log.user_agent,
details: db_log.details,
success: db_log.success,
timestamp: db_log.created_at,
})
}
}
impl ToDb<DbAuditLog> for AuditEvent {
fn to_db(&self) -> Result<DbAuditLog> {
Ok(DbAuditLog {
id: 0, // Will be set by database
event_type: self.event_type.clone(),
client_id: self.client_id.clone(),
user_id: self.user_id.clone(),
ip_address: self.ip_address.clone(),
user_agent: self.user_agent.clone(),
details: self.details.clone(),
created_at: self.timestamp,
success: self.success,
})
}
}
|