pubky-homeserver 0.12.0

A Pubky homeserver implementation.
Documentation
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Repository for Grant entities (grant auth).

use pubky_common::{
    auth::jws::{ClientId, GrantId},
    capabilities::Capabilities,
    crypto::PublicKey,
};
use sea_query::{Expr, Iden, PostgresQueryBuilder, Query, SimpleExpr};
use sea_query_binder::SqlxBinder;
use sqlx::{postgres::PgRow, FromRow, Row};

use crate::persistence::sql::{
    entities::user::{UserIden, USER_TABLE},
    migrations::m20260325_create_grant_sessions::{GrantIden, GRANTS_TABLE},
    UnifiedExecutor,
};

/// Repository for grant CRUD operations.
pub struct GrantRepository;

impl GrantRepository {
    /// Insert a grant. Ignores if a grant with the same id already exists (idempotent).
    pub async fn create<'a>(
        grant: &NewGrant,
        executor: &mut UnifiedExecutor<'a>,
    ) -> Result<(), sqlx::Error> {
        let statement = Query::insert()
            .into_table(GRANTS_TABLE)
            .columns([
                GrantIden::Id,
                GrantIden::User,
                GrantIden::ClientId,
                GrantIden::ClientCnfKey,
                GrantIden::Capabilities,
                GrantIden::IssuedAt,
                GrantIden::ExpiresAt,
            ])
            .values(vec![
                SimpleExpr::Value(grant.id.to_string().into()),
                SimpleExpr::Value(grant.user_id.into()),
                SimpleExpr::Value(grant.client_id.to_string().into()),
                SimpleExpr::Value(grant.client_cnf_key.clone().into()),
                SimpleExpr::Value(grant.capabilities.to_string().into()),
                SimpleExpr::Value((grant.issued_at as i64).into()),
                SimpleExpr::Value((grant.expires_at as i64).into()),
            ])
            .expect("invariant: values count matches columns count")
            .on_conflict(
                sea_query::OnConflict::column(GrantIden::Id)
                    .do_nothing()
                    .to_owned(),
            )
            .to_owned();

        let (query, values) = statement.build_sqlx(PostgresQueryBuilder);
        let con = executor.get_con().await?;
        sqlx::query_with(&query, values).execute(con).await?;
        Ok(())
    }

    /// Get a grant by its id.
    pub async fn get_by_id<'a>(
        grant_id: &GrantId,
        executor: &mut UnifiedExecutor<'a>,
    ) -> Result<GrantEntity, sqlx::Error> {
        let statement = Query::select()
            .from(GRANTS_TABLE)
            .columns([
                (GRANTS_TABLE, GrantIden::Id),
                (GRANTS_TABLE, GrantIden::User),
                (GRANTS_TABLE, GrantIden::ClientId),
                (GRANTS_TABLE, GrantIden::ClientCnfKey),
                (GRANTS_TABLE, GrantIden::Capabilities),
                (GRANTS_TABLE, GrantIden::IssuedAt),
                (GRANTS_TABLE, GrantIden::ExpiresAt),
                (GRANTS_TABLE, GrantIden::RevokedAt),
                (GRANTS_TABLE, GrantIden::CreatedAt),
            ])
            .column((USER_TABLE, UserIden::PublicKey))
            .left_join(
                USER_TABLE,
                Expr::col((GRANTS_TABLE, GrantIden::User))
                    .eq(Expr::col((USER_TABLE, UserIden::Id))),
            )
            .and_where(Expr::col((GRANTS_TABLE, GrantIden::Id)).eq(grant_id.to_string()))
            .to_owned();

        let (query, values) = statement.build_sqlx(PostgresQueryBuilder);
        let con = executor.get_con().await?;
        sqlx::query_as_with(&query, values).fetch_one(con).await
    }

    /// Revoke a grant by setting revoked_at to the current unix timestamp.
    pub async fn revoke<'a>(
        grant_id: &GrantId,
        executor: &mut UnifiedExecutor<'a>,
    ) -> Result<(), sqlx::Error> {
        let now = chrono::Utc::now().timestamp();
        let statement = Query::update()
            .table(GRANTS_TABLE)
            .value(GrantIden::RevokedAt, SimpleExpr::Value(now.into()))
            .and_where(Expr::col(GrantIden::Id).eq(grant_id.to_string()))
            .to_owned();

        let (query, values) = statement.build_sqlx(PostgresQueryBuilder);
        let con = executor.get_con().await?;
        sqlx::query_with(&query, values).execute(con).await?;
        Ok(())
    }

    /// Check if a grant has been revoked.
    pub async fn is_revoked<'a>(
        grant_id: &GrantId,
        executor: &mut UnifiedExecutor<'a>,
    ) -> Result<bool, sqlx::Error> {
        let statement = Query::select()
            .from(GRANTS_TABLE)
            .column(GrantIden::RevokedAt)
            .and_where(Expr::col(GrantIden::Id).eq(grant_id.to_string()))
            .to_owned();

        let (query, values) = statement.build_sqlx(PostgresQueryBuilder);
        let con = executor.get_con().await?;
        let row: PgRow = sqlx::query_with(&query, values).fetch_one(con).await?;
        let revoked_at: Option<i64> = row.try_get(GrantIden::RevokedAt.to_string().as_str())?;
        Ok(revoked_at.is_some())
    }

    /// List all active (non-revoked, non-expired) grants for a user.
    pub async fn list_active_for_user<'a>(
        user_id: i32,
        executor: &mut UnifiedExecutor<'a>,
    ) -> Result<Vec<GrantEntity>, sqlx::Error> {
        let now = chrono::Utc::now().timestamp();
        let statement = Query::select()
            .from(GRANTS_TABLE)
            .columns([
                (GRANTS_TABLE, GrantIden::Id),
                (GRANTS_TABLE, GrantIden::User),
                (GRANTS_TABLE, GrantIden::ClientId),
                (GRANTS_TABLE, GrantIden::ClientCnfKey),
                (GRANTS_TABLE, GrantIden::Capabilities),
                (GRANTS_TABLE, GrantIden::IssuedAt),
                (GRANTS_TABLE, GrantIden::ExpiresAt),
                (GRANTS_TABLE, GrantIden::RevokedAt),
                (GRANTS_TABLE, GrantIden::CreatedAt),
            ])
            .column((USER_TABLE, UserIden::PublicKey))
            .left_join(
                USER_TABLE,
                Expr::col((GRANTS_TABLE, GrantIden::User))
                    .eq(Expr::col((USER_TABLE, UserIden::Id))),
            )
            .and_where(Expr::col((GRANTS_TABLE, GrantIden::User)).eq(user_id))
            .and_where(Expr::col((GRANTS_TABLE, GrantIden::RevokedAt)).is_null())
            .and_where(Expr::col((GRANTS_TABLE, GrantIden::ExpiresAt)).gt(now))
            .to_owned();

        let (query, values) = statement.build_sqlx(PostgresQueryBuilder);
        let con = executor.get_con().await?;
        sqlx::query_as_with(&query, values).fetch_all(con).await
    }
}

/// Data needed to create a new grant.
pub struct NewGrant {
    pub id: GrantId,
    pub user_id: i32,
    pub client_id: ClientId,
    pub client_cnf_key: String,
    pub capabilities: Capabilities,
    pub issued_at: u64,
    pub expires_at: u64,
}

/// A grant entity as stored in the database.
#[derive(Debug, Clone)]
pub struct GrantEntity {
    pub id: GrantId,
    pub user_id: i32,
    pub user_pubkey: PublicKey,
    pub client_id: ClientId,
    #[allow(dead_code)]
    pub client_cnf_key: String,
    pub capabilities: Capabilities,
    pub issued_at: i64,
    pub expires_at: i64,
    pub revoked_at: Option<i64>,
    pub created_at: sqlx::types::chrono::NaiveDateTime,
}

/// Why a grant is no longer active.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GrantStatus {
    Revoked,
    Expired,
}

impl GrantEntity {
    /// Check that this grant is active (not revoked, not expired).
    pub fn require_active(&self, now_unix: i64) -> Result<(), GrantStatus> {
        if self.revoked_at.is_some() {
            return Err(GrantStatus::Revoked);
        }
        if self.expires_at <= now_unix {
            return Err(GrantStatus::Expired);
        }
        Ok(())
    }
}

impl FromRow<'_, PgRow> for GrantEntity {
    fn from_row(row: &PgRow) -> Result<Self, sqlx::Error> {
        let (id, user_id, user_pubkey, client_id, client_cnf_key) = parse_identity_fields(row)?;
        let (capabilities, issued_at, expires_at, revoked_at, created_at) =
            parse_grant_metadata(row)?;
        Ok(GrantEntity {
            id,
            user_id,
            user_pubkey,
            client_id,
            client_cnf_key,
            capabilities,
            issued_at,
            expires_at,
            revoked_at,
            created_at,
        })
    }
}

fn parse_identity_fields(
    row: &PgRow,
) -> Result<(GrantId, i32, PublicKey, ClientId, String), sqlx::Error> {
    let id: String = row.try_get(GrantIden::Id.to_string().as_str())?;
    let id = GrantId::parse(&id).map_err(|e| sqlx::Error::Decode(e.into()))?;
    let user_id: i32 = row.try_get(GrantIden::User.to_string().as_str())?;
    let user_pubkey: String = row.try_get(UserIden::PublicKey.to_string().as_str())?;
    let user_pubkey: PublicKey = user_pubkey
        .try_into()
        .map_err(|e: pkarr::errors::PublicKeyError| sqlx::Error::Decode(e.into()))?;
    let client_id: String = row.try_get(GrantIden::ClientId.to_string().as_str())?;
    let client_id = ClientId::new(&client_id).map_err(|e| sqlx::Error::Decode(e.into()))?;
    let client_cnf_key: String = row.try_get(GrantIden::ClientCnfKey.to_string().as_str())?;
    Ok((id, user_id, user_pubkey, client_id, client_cnf_key))
}

fn parse_grant_metadata(
    row: &PgRow,
) -> Result<
    (
        Capabilities,
        i64,
        i64,
        Option<i64>,
        sqlx::types::chrono::NaiveDateTime,
    ),
    sqlx::Error,
> {
    let capabilities: String = row.try_get(GrantIden::Capabilities.to_string().as_str())?;
    let capabilities: Capabilities =
        capabilities
            .parse()
            .map_err(|e: pubky_common::capabilities::CapabilitiesParseError| {
                sqlx::Error::Decode(e.into())
            })?;
    let issued_at: i64 = row.try_get(GrantIden::IssuedAt.to_string().as_str())?;
    let expires_at: i64 = row.try_get(GrantIden::ExpiresAt.to_string().as_str())?;
    let revoked_at: Option<i64> = row.try_get(GrantIden::RevokedAt.to_string().as_str())?;
    let created_at = row.try_get(GrantIden::CreatedAt.to_string().as_str())?;
    Ok((capabilities, issued_at, expires_at, revoked_at, created_at))
}

#[cfg(test)]
mod tests {
    use super::*;
    use pubky_common::{
        auth::jws::{ClientId, GrantId},
        capabilities::{Capabilities, Capability},
        crypto::Keypair,
    };

    use crate::persistence::sql::SqlDb;
    use crate::services::user_service::UserService;

    fn make_new_grant(user_id: i32) -> NewGrant {
        let now = chrono::Utc::now().timestamp() as u64;
        NewGrant {
            id: GrantId::generate(),
            user_id,
            client_id: ClientId::new("test.app").unwrap(),
            client_cnf_key: Keypair::random().public_key().z32(),
            capabilities: Capabilities::builder().cap(Capability::root()).finish(),
            issued_at: now,
            expires_at: now + 3600,
        }
    }

    #[tokio::test]
    #[pubky_test_utils::test]
    async fn test_create_and_get_grant() {
        let db = SqlDb::test().await;
        let keypair = Keypair::random();
        let user = UserService::new(db.clone())
            .create(&keypair.public_key())
            .await
            .unwrap();

        let new_grant = make_new_grant(user.id);
        let grant_id = new_grant.id.clone();
        let client_id = new_grant.client_id.clone();
        let caps = new_grant.capabilities.clone();
        let issued_at = new_grant.issued_at;
        let expires_at = new_grant.expires_at;

        GrantRepository::create(&new_grant, &mut db.pool().into())
            .await
            .unwrap();

        let entity = GrantRepository::get_by_id(&grant_id, &mut db.pool().into())
            .await
            .unwrap();

        assert_eq!(entity.id, grant_id);
        assert_eq!(entity.user_id, user.id);
        assert_eq!(entity.user_pubkey, keypair.public_key());
        assert_eq!(entity.client_id, client_id);
        assert_eq!(entity.capabilities, caps);
        assert_eq!(entity.issued_at, issued_at as i64);
        assert_eq!(entity.expires_at, expires_at as i64);
        assert!(entity.revoked_at.is_none());
    }

    #[tokio::test]
    #[pubky_test_utils::test]
    async fn test_create_grant_is_idempotent() {
        let db = SqlDb::test().await;
        let user = UserService::new(db.clone())
            .create(&Keypair::random().public_key())
            .await
            .unwrap();

        let new_grant = make_new_grant(user.id);
        GrantRepository::create(&new_grant, &mut db.pool().into())
            .await
            .unwrap();
        // Second insert with same grant_id should succeed (ON CONFLICT DO NOTHING)
        GrantRepository::create(&new_grant, &mut db.pool().into())
            .await
            .unwrap();
    }

    #[tokio::test]
    #[pubky_test_utils::test]
    async fn test_revoke_and_is_revoked() {
        let db = SqlDb::test().await;
        let user = UserService::new(db.clone())
            .create(&Keypair::random().public_key())
            .await
            .unwrap();

        let new_grant = make_new_grant(user.id);
        let grant_id = new_grant.id.clone();
        GrantRepository::create(&new_grant, &mut db.pool().into())
            .await
            .unwrap();

        assert!(
            !GrantRepository::is_revoked(&grant_id, &mut db.pool().into())
                .await
                .unwrap()
        );

        GrantRepository::revoke(&grant_id, &mut db.pool().into())
            .await
            .unwrap();

        assert!(
            GrantRepository::is_revoked(&grant_id, &mut db.pool().into())
                .await
                .unwrap()
        );

        let entity = GrantRepository::get_by_id(&grant_id, &mut db.pool().into())
            .await
            .unwrap();
        assert!(entity.revoked_at.is_some());
    }

    #[tokio::test]
    #[pubky_test_utils::test]
    async fn test_list_active_for_user() {
        let db = SqlDb::test().await;
        let user = UserService::new(db.clone())
            .create(&Keypair::random().public_key())
            .await
            .unwrap();

        // Active grant
        let active = make_new_grant(user.id);
        let active_id = active.id.clone();
        GrantRepository::create(&active, &mut db.pool().into())
            .await
            .unwrap();

        // Revoked grant
        let revoked = make_new_grant(user.id);
        let revoked_id = revoked.id.clone();
        GrantRepository::create(&revoked, &mut db.pool().into())
            .await
            .unwrap();
        GrantRepository::revoke(&revoked_id, &mut db.pool().into())
            .await
            .unwrap();

        // Expired grant
        let now = chrono::Utc::now().timestamp() as u64;
        let mut expired = make_new_grant(user.id);
        expired.issued_at = now.saturating_sub(7200);
        expired.expires_at = now.saturating_sub(3600);
        GrantRepository::create(&expired, &mut db.pool().into())
            .await
            .unwrap();

        let list = GrantRepository::list_active_for_user(user.id, &mut db.pool().into())
            .await
            .unwrap();
        assert_eq!(list.len(), 1);
        assert_eq!(list[0].id, active_id);
    }

    #[tokio::test]
    #[pubky_test_utils::test]
    async fn test_list_active_for_user_empty() {
        let db = SqlDb::test().await;
        let user = UserService::new(db.clone())
            .create(&Keypair::random().public_key())
            .await
            .unwrap();

        let list = GrantRepository::list_active_for_user(user.id, &mut db.pool().into())
            .await
            .unwrap();
        assert!(list.is_empty());
    }

    fn make_grant_entity(expires_at: i64, revoked_at: Option<i64>) -> GrantEntity {
        GrantEntity {
            id: GrantId::generate(),
            user_id: 1,
            user_pubkey: Keypair::random().public_key(),
            client_id: ClientId::new("test.app").unwrap(),
            client_cnf_key: Keypair::random().public_key().z32(),
            capabilities: Capabilities::builder().cap(Capability::root()).finish(),
            issued_at: 1000,
            expires_at,
            revoked_at,
            created_at: chrono::Utc::now().naive_utc(),
        }
    }

    #[test]
    fn require_active_passes_for_active_grant() {
        let grant = make_grant_entity(2000, None);
        assert!(grant.require_active(1500).is_ok());
    }

    #[test]
    fn require_active_returns_revoked() {
        let grant = make_grant_entity(2000, Some(1500));
        assert_eq!(
            grant.require_active(1500).unwrap_err(),
            GrantStatus::Revoked
        );
    }

    #[test]
    fn require_active_returns_expired() {
        let grant = make_grant_entity(1000, None);
        assert_eq!(
            grant.require_active(1500).unwrap_err(),
            GrantStatus::Expired
        );
    }

    #[test]
    fn require_active_boundary_expires_at_equals_now() {
        let grant = make_grant_entity(1000, None);
        assert_eq!(
            grant.require_active(1000).unwrap_err(),
            GrantStatus::Expired
        );
    }

    #[test]
    fn require_active_revoked_takes_precedence_over_expired() {
        let grant = make_grant_entity(1000, Some(900));
        assert_eq!(
            grant.require_active(1500).unwrap_err(),
            GrantStatus::Revoked
        );
    }
}