this repo has no description
1use sqlx::PgPool;
2
3use super::super::{AuthorizedClientData, OAuthError};
4use super::helpers::{from_json, to_json};
5
6pub async fn upsert_authorized_client(
7 pool: &PgPool,
8 did: &str,
9 client_id: &str,
10 data: &AuthorizedClientData,
11) -> Result<(), OAuthError> {
12 let data_json = to_json(data)?;
13
14 sqlx::query!(
15 r#"
16 INSERT INTO oauth_authorized_client (did, client_id, created_at, updated_at, data)
17 VALUES ($1, $2, NOW(), NOW(), $3)
18 ON CONFLICT (did, client_id) DO UPDATE SET updated_at = NOW(), data = $3
19 "#,
20 did,
21 client_id,
22 data_json
23 )
24 .execute(pool)
25 .await?;
26
27 Ok(())
28}
29
30pub async fn get_authorized_client(
31 pool: &PgPool,
32 did: &str,
33 client_id: &str,
34) -> Result<Option<AuthorizedClientData>, OAuthError> {
35 let row = sqlx::query_scalar!(
36 r#"
37 SELECT data FROM oauth_authorized_client
38 WHERE did = $1 AND client_id = $2
39 "#,
40 did,
41 client_id
42 )
43 .fetch_optional(pool)
44 .await?;
45
46 match row {
47 Some(v) => Ok(Some(from_json(v)?)),
48 None => Ok(None),
49 }
50}