| 1 |
|
| 2 |
|
| 3 |
use chrono::{DateTime, Utc}; |
| 4 |
use sqlx::PgPool; |
| 5 |
use uuid::Uuid; |
| 6 |
|
| 7 |
use super::models::{DbOAuthCode, DbOAuthRefreshToken}; |
| 8 |
use super::{SyncAppId, UserId}; |
| 9 |
use crate::error::Result; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#[allow(clippy::too_many_arguments)] |
| 18 |
#[tracing::instrument(skip_all)] |
| 19 |
pub(crate) async fn create_oauth_code( |
| 20 |
pool: &PgPool, |
| 21 |
code_hash: &str, |
| 22 |
app_id: SyncAppId, |
| 23 |
user_id: UserId, |
| 24 |
code_challenge: &str, |
| 25 |
code_challenge_method: &str, |
| 26 |
redirect_uri: &str, |
| 27 |
scope: &str, |
| 28 |
expires_at: DateTime<Utc>, |
| 29 |
) -> Result<DbOAuthCode> { |
| 30 |
let row = sqlx::query_as::<_, DbOAuthCode>( |
| 31 |
r" |
| 32 |
INSERT INTO oauth_authorization_codes |
| 33 |
(code, app_id, user_id, code_challenge, code_challenge_method, redirect_uri, scope, expires_at) |
| 34 |
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) |
| 35 |
RETURNING * |
| 36 |
", |
| 37 |
) |
| 38 |
.bind(code_hash) |
| 39 |
.bind(app_id) |
| 40 |
.bind(user_id) |
| 41 |
.bind(code_challenge) |
| 42 |
.bind(code_challenge_method) |
| 43 |
.bind(redirect_uri) |
| 44 |
.bind(scope) |
| 45 |
.bind(expires_at) |
| 46 |
.fetch_one(pool) |
| 47 |
.await?; |
| 48 |
|
| 49 |
Ok(row) |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
pub(crate) enum RefreshRotateOutcome { |
| 54 |
|
| 55 |
|
| 56 |
Valid(Box<DbOAuthRefreshToken>), |
| 57 |
|
| 58 |
|
| 59 |
Reused { chain_id: Uuid }, |
| 60 |
|
| 61 |
Invalid, |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#[allow(clippy::too_many_arguments)] |
| 67 |
#[tracing::instrument(skip_all)] |
| 68 |
pub(crate) async fn create_refresh_token( |
| 69 |
pool: &PgPool, |
| 70 |
token_hash: &str, |
| 71 |
app_id: SyncAppId, |
| 72 |
user_id: UserId, |
| 73 |
key: &str, |
| 74 |
scope: &str, |
| 75 |
chain_id: Uuid, |
| 76 |
expires_at: DateTime<Utc>, |
| 77 |
) -> Result<DbOAuthRefreshToken> { |
| 78 |
let row = sqlx::query_as::<_, DbOAuthRefreshToken>( |
| 79 |
r" |
| 80 |
INSERT INTO oauth_refresh_tokens |
| 81 |
(token_hash, app_id, user_id, key, scope, chain_id, expires_at) |
| 82 |
VALUES ($1, $2, $3, $4, $5, $6, $7) |
| 83 |
RETURNING * |
| 84 |
", |
| 85 |
) |
| 86 |
.bind(token_hash) |
| 87 |
.bind(app_id) |
| 88 |
.bind(user_id) |
| 89 |
.bind(key) |
| 90 |
.bind(scope) |
| 91 |
.bind(chain_id) |
| 92 |
.bind(expires_at) |
| 93 |
.fetch_one(pool) |
| 94 |
.await?; |
| 95 |
|
| 96 |
Ok(row) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
#[tracing::instrument(skip_all)] |
| 106 |
pub(crate) async fn rotate_refresh_token( |
| 107 |
pool: &PgPool, |
| 108 |
token_hash: &str, |
| 109 |
) -> Result<RefreshRotateOutcome> { |
| 110 |
let consumed = sqlx::query_as::<_, DbOAuthRefreshToken>( |
| 111 |
r" |
| 112 |
UPDATE oauth_refresh_tokens |
| 113 |
SET used_at = NOW() |
| 114 |
WHERE token_hash = $1 |
| 115 |
AND used_at IS NULL |
| 116 |
AND revoked_at IS NULL |
| 117 |
AND expires_at > NOW() |
| 118 |
RETURNING * |
| 119 |
", |
| 120 |
) |
| 121 |
.bind(token_hash) |
| 122 |
.fetch_optional(pool) |
| 123 |
.await?; |
| 124 |
|
| 125 |
if let Some(row) = consumed { |
| 126 |
return Ok(RefreshRotateOutcome::Valid(Box::new(row))); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
let existing: Option<(Uuid, Option<DateTime<Utc>>)> = |
| 131 |
sqlx::query_as("SELECT chain_id, used_at FROM oauth_refresh_tokens WHERE token_hash = $1") |
| 132 |
.bind(token_hash) |
| 133 |
.fetch_optional(pool) |
| 134 |
.await?; |
| 135 |
|
| 136 |
match existing { |
| 137 |
Some((chain_id, Some(_used))) => Ok(RefreshRotateOutcome::Reused { chain_id }), |
| 138 |
_ => Ok(RefreshRotateOutcome::Invalid), |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
#[tracing::instrument(skip_all)] |
| 144 |
pub(crate) async fn revoke_refresh_chain(pool: &PgPool, chain_id: Uuid) -> Result<()> { |
| 145 |
sqlx::query( |
| 146 |
"UPDATE oauth_refresh_tokens SET revoked_at = NOW() WHERE chain_id = $1 AND revoked_at IS NULL", |
| 147 |
) |
| 148 |
.bind(chain_id) |
| 149 |
.execute(pool) |
| 150 |
.await?; |
| 151 |
Ok(()) |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
#[tracing::instrument(skip_all)] |
| 157 |
pub(crate) async fn cleanup_expired_refresh_tokens(pool: &PgPool) -> Result<u64> { |
| 158 |
let result = sqlx::query( |
| 159 |
"DELETE FROM oauth_refresh_tokens |
| 160 |
WHERE expires_at < NOW() |
| 161 |
OR (used_at IS NOT NULL AND used_at < NOW() - INTERVAL '1 day') |
| 162 |
OR (revoked_at IS NOT NULL AND revoked_at < NOW() - INTERVAL '1 day')", |
| 163 |
) |
| 164 |
.execute(pool) |
| 165 |
.await?; |
| 166 |
|
| 167 |
Ok(result.rows_affected()) |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
#[tracing::instrument(skip_all)] |
| 181 |
pub(crate) async fn peek_oauth_code(pool: &PgPool, code_hash: &str) -> Result<Option<DbOAuthCode>> { |
| 182 |
let row = sqlx::query_as::<_, DbOAuthCode>( |
| 183 |
r" |
| 184 |
SELECT * FROM oauth_authorization_codes |
| 185 |
WHERE code = $1 |
| 186 |
AND used_at IS NULL |
| 187 |
AND expires_at > NOW() |
| 188 |
", |
| 189 |
) |
| 190 |
.bind(code_hash) |
| 191 |
.fetch_optional(pool) |
| 192 |
.await?; |
| 193 |
|
| 194 |
Ok(row) |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
#[tracing::instrument(skip_all)] |
| 204 |
pub(crate) async fn consume_oauth_code( |
| 205 |
pool: &PgPool, |
| 206 |
code_hash: &str, |
| 207 |
) -> Result<Option<DbOAuthCode>> { |
| 208 |
let row = sqlx::query_as::<_, DbOAuthCode>( |
| 209 |
r" |
| 210 |
UPDATE oauth_authorization_codes |
| 211 |
SET used_at = NOW() |
| 212 |
WHERE code = $1 |
| 213 |
AND used_at IS NULL |
| 214 |
AND expires_at > NOW() |
| 215 |
RETURNING * |
| 216 |
", |
| 217 |
) |
| 218 |
.bind(code_hash) |
| 219 |
.fetch_optional(pool) |
| 220 |
.await?; |
| 221 |
|
| 222 |
Ok(row) |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
#[tracing::instrument(skip_all)] |
| 228 |
pub(crate) async fn cleanup_expired_oauth_codes(pool: &PgPool) -> Result<u64> { |
| 229 |
let result = sqlx::query( |
| 230 |
"DELETE FROM oauth_authorization_codes WHERE expires_at < NOW() - INTERVAL '1 hour' OR (used_at IS NOT NULL AND used_at < NOW() - INTERVAL '1 hour')", |
| 231 |
) |
| 232 |
.execute(pool) |
| 233 |
.await?; |
| 234 |
|
| 235 |
Ok(result.rows_affected()) |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
#[tracing::instrument(skip_all)] |
| 246 |
pub(crate) async fn is_registered_redirect_uri( |
| 247 |
pool: &PgPool, |
| 248 |
app_id: SyncAppId, |
| 249 |
uri: &str, |
| 250 |
) -> Result<bool> { |
| 251 |
let row: Option<(bool,)> = sqlx::query_as( |
| 252 |
"SELECT $2 = ANY(redirect_uris) FROM sync_apps WHERE id = $1 AND is_active = true", |
| 253 |
) |
| 254 |
.bind(app_id) |
| 255 |
.bind(uri) |
| 256 |
.fetch_optional(pool) |
| 257 |
.await?; |
| 258 |
|
| 259 |
Ok(row.is_some_and(|r| r.0)) |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
#[tracing::instrument(skip_all)] |
| 266 |
pub(crate) async fn get_granted_scopes( |
| 267 |
pool: &PgPool, |
| 268 |
user_id: UserId, |
| 269 |
app_id: SyncAppId, |
| 270 |
) -> Result<crate::oauth_scope::GrantedScopes> { |
| 271 |
let row = sqlx::query_scalar!( |
| 272 |
"SELECT scopes FROM oauth_granted_scopes WHERE user_id = $1 AND app_id = $2", |
| 273 |
user_id as UserId, |
| 274 |
app_id as SyncAppId, |
| 275 |
) |
| 276 |
.fetch_optional(pool) |
| 277 |
.await?; |
| 278 |
Ok(row |
| 279 |
.map(|s| crate::oauth_scope::GrantedScopes::parse(&s)) |
| 280 |
.unwrap_or_default()) |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
#[tracing::instrument(skip_all)] |
| 287 |
pub(crate) async fn record_granted_scopes( |
| 288 |
pool: &PgPool, |
| 289 |
user_id: UserId, |
| 290 |
app_id: SyncAppId, |
| 291 |
scope: &crate::oauth_scope::GrantedScopes, |
| 292 |
) -> Result<()> { |
| 293 |
let mut tx = pool.begin().await?; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
sqlx::query("SELECT pg_advisory_xact_lock(hashtextextended($1::text || ':' || $2::text, 0))") |
| 301 |
.bind(user_id) |
| 302 |
.bind(app_id) |
| 303 |
.execute(&mut *tx) |
| 304 |
.await?; |
| 305 |
|
| 306 |
|
| 307 |
let existing: Option<String> = sqlx::query_scalar( |
| 308 |
"SELECT scopes FROM oauth_granted_scopes WHERE user_id = $1 AND app_id = $2", |
| 309 |
) |
| 310 |
.bind(user_id) |
| 311 |
.bind(app_id) |
| 312 |
.fetch_optional(&mut *tx) |
| 313 |
.await?; |
| 314 |
let mut merged = existing |
| 315 |
.map(|s| crate::oauth_scope::GrantedScopes::parse(&s)) |
| 316 |
.unwrap_or_default(); |
| 317 |
merged.union_with(scope); |
| 318 |
let scopes = merged.to_string(); |
| 319 |
|
| 320 |
sqlx::query!( |
| 321 |
r#" |
| 322 |
INSERT INTO oauth_granted_scopes (user_id, app_id, scopes) |
| 323 |
VALUES ($1, $2, $3) |
| 324 |
ON CONFLICT (user_id, app_id) |
| 325 |
DO UPDATE SET scopes = EXCLUDED.scopes, updated_at = NOW() |
| 326 |
"#, |
| 327 |
user_id as UserId, |
| 328 |
app_id as SyncAppId, |
| 329 |
scopes, |
| 330 |
) |
| 331 |
.execute(&mut *tx) |
| 332 |
.await?; |
| 333 |
tx.commit().await?; |
| 334 |
Ok(()) |
| 335 |
} |
| 336 |
|