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
|
use super::*;
use crate::database::{Database, DbAccessToken, DbAuditLog, DbAuthCode, DbOAuthClient};
use anyhow::Result;
use std::sync::{Arc, Mutex};
/// SQLite implementation of ClientRepository
pub struct SqliteClientRepository {
database: Arc<Mutex<Database>>,
}
impl SqliteClientRepository {
pub fn new(database: Arc<Mutex<Database>>) -> Self {
Self { database }
}
}
impl ClientRepository for SqliteClientRepository {
fn get_client(&self, client_id: &str) -> Result<Option<DbOAuthClient>> {
let db = self.database.lock().unwrap();
db.get_oauth_client(client_id)
}
fn create_client(&self, client: &DbOAuthClient) -> Result<()> {
let db = self.database.lock().unwrap();
db.create_oauth_client(client).map(|_| ())
}
fn update_client(&self, client: &DbOAuthClient) -> Result<()> {
let db = self.database.lock().unwrap();
db.update_oauth_client(client)
}
fn delete_client(&self, client_id: &str) -> Result<()> {
let db = self.database.lock().unwrap();
db.delete_oauth_client(client_id)
}
fn list_clients(&self) -> Result<Vec<DbOAuthClient>> {
let db = self.database.lock().unwrap();
db.list_oauth_clients()
}
}
/// SQLite implementation of AuthCodeRepository
pub struct SqliteAuthCodeRepository {
database: Arc<Mutex<Database>>,
}
impl SqliteAuthCodeRepository {
pub fn new(database: Arc<Mutex<Database>>) -> Self {
Self { database }
}
}
impl AuthCodeRepository for SqliteAuthCodeRepository {
fn create_auth_code(&self, code: &DbAuthCode) -> Result<()> {
let db = self.database.lock().unwrap();
db.create_auth_code(code).map(|_| ())
}
fn get_auth_code(&self, code: &str) -> Result<Option<DbAuthCode>> {
let db = self.database.lock().unwrap();
db.get_auth_code(code)
}
fn mark_auth_code_used(&self, code: &str) -> Result<()> {
let db = self.database.lock().unwrap();
db.mark_auth_code_used(code)
}
fn cleanup_expired_codes(&self) -> Result<()> {
let db = self.database.lock().unwrap();
db.cleanup_expired_codes().map(|_| ())
}
}
/// SQLite implementation of TokenRepository
pub struct SqliteTokenRepository {
database: Arc<Mutex<Database>>,
}
impl SqliteTokenRepository {
pub fn new(database: Arc<Mutex<Database>>) -> Self {
Self { database }
}
}
impl TokenRepository for SqliteTokenRepository {
fn create_access_token(&self, token: &DbAccessToken) -> Result<()> {
let db = self.database.lock().unwrap();
db.create_access_token(token).map(|_| ())
}
fn get_access_token(&self, token_hash: &str) -> Result<Option<DbAccessToken>> {
let db = self.database.lock().unwrap();
db.get_access_token(token_hash)
}
fn revoke_access_token(&self, token_hash: &str) -> Result<()> {
let db = self.database.lock().unwrap();
db.revoke_access_token(token_hash)
}
fn cleanup_expired_tokens(&self) -> Result<()> {
let db = self.database.lock().unwrap();
db.cleanup_expired_tokens().map(|_| ())
}
}
/// SQLite implementation of AuditRepository
pub struct SqliteAuditRepository {
database: Arc<Mutex<Database>>,
}
impl SqliteAuditRepository {
pub fn new(database: Arc<Mutex<Database>>) -> Self {
Self { database }
}
}
impl AuditRepository for SqliteAuditRepository {
fn create_audit_log(&self, log: &DbAuditLog) -> Result<()> {
let db = self.database.lock().unwrap();
db.create_audit_log(log).map(|_| ())
}
fn get_audit_logs(&self, limit: Option<i32>) -> Result<Vec<DbAuditLog>> {
let db = self.database.lock().unwrap();
db.get_audit_logs(limit.unwrap_or(100))
}
fn cleanup_old_audit_logs(&self, days: i32) -> Result<()> {
let db = self.database.lock().unwrap();
db.cleanup_old_audit_logs(days).map(|_| ())
}
}
/// SQLite implementation of RateRepository
pub struct SqliteRateRepository {
database: Arc<Mutex<Database>>,
}
impl SqliteRateRepository {
pub fn new(database: Arc<Mutex<Database>>) -> Self {
Self { database }
}
}
impl RateRepository for SqliteRateRepository {
fn increment_rate_limit(
&self,
identifier: &str,
endpoint: &str,
window_size: i32,
) -> Result<i32> {
let db = self.database.lock().unwrap();
db.increment_rate_limit(identifier, endpoint, window_size)
}
fn cleanup_old_rate_limits(&self) -> Result<()> {
let db = self.database.lock().unwrap();
db.cleanup_old_rate_limits()
}
}
|