allowthem-core 0.0.3

Core types, database, and auth logic for allowthem
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
use base64ct::{Base64UrlUnpadded, Encoding};
use chrono::{DateTime, Utc};
use rand::TryRngCore;
use rand::rngs::OsRng;
use sha2::{Digest, Sha256};

use crate::db::Db;
use crate::error::AuthError;
use crate::types::{Email, InvitationId, UserId};

/// A single-use invitation token record.
///
/// Returned by `Db::create_invitation` and `Db::validate_invitation`.
/// The `token_hash` is never exposed — only the raw token (returned once
/// at creation) can be used to validate.
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct Invitation {
    pub id: InvitationId,
    pub email: Option<Email>,
    pub metadata: Option<String>,
    pub invited_by: Option<UserId>,
    pub expires_at: DateTime<Utc>,
    pub consumed_at: Option<DateTime<Utc>>,
    pub created_at: DateTime<Utc>,
}

/// Generate a cryptographically random invitation token.
///
/// Fills 32 bytes from the OS random source and encodes as base64url without
/// padding, producing a 43-character string suitable for inclusion in a URL.
fn generate_invitation_token() -> String {
    let mut bytes = [0u8; 32];
    OsRng
        .try_fill_bytes(&mut bytes)
        .expect("OS RNG unavailable");
    Base64UrlUnpadded::encode_string(&bytes)
}

/// Hash a raw invitation token with SHA-256.
///
/// Returns the hex-encoded digest. This is what is stored in the database.
/// The raw token is only ever shown once, at creation time.
fn hash_invitation_token(token: &str) -> String {
    let digest = Sha256::digest(token.as_bytes());
    format!("{digest:x}")
}

impl Db {
    /// Create an invitation. Returns the raw token (shown once) and the
    /// `Invitation` record.
    ///
    /// If `email` is `Some`, the invitation is targeted at that address.
    /// If `email` is `None`, it is an open invitation usable by anyone.
    pub async fn create_invitation(
        &self,
        email: Option<&Email>,
        metadata: Option<&str>,
        invited_by: Option<UserId>,
        expires_at: DateTime<Utc>,
    ) -> Result<(String, Invitation), AuthError> {
        let id = InvitationId::new();
        let raw_token = generate_invitation_token();
        let token_hash = hash_invitation_token(&raw_token);
        let now = Utc::now();
        let now_str = now.format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string();
        let expires_str = expires_at.format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string();

        sqlx::query(
            "INSERT INTO allowthem_invitations \
             (id, email, token_hash, metadata, invited_by, expires_at, created_at) \
             VALUES (?, ?, ?, ?, ?, ?, ?)",
        )
        .bind(id)
        .bind(email)
        .bind(&token_hash)
        .bind(metadata)
        .bind(invited_by)
        .bind(&expires_str)
        .bind(&now_str)
        .execute(self.pool())
        .await
        .map_err(AuthError::Database)?;

        let inv = Invitation {
            id,
            email: email.cloned(),
            metadata: metadata.map(String::from),
            invited_by,
            expires_at,
            consumed_at: None,
            created_at: now,
        };

        Ok((raw_token, inv))
    }

    /// Mark an invitation as consumed.
    ///
    /// Uses `consumed_at IS NULL` as a concurrency guard. Returns `Ok(())`
    /// on success. Returns `Err(AuthError::NotFound)` if the ID does not
    /// exist. Returns `Err(AuthError::Gone)` if already consumed — the
    /// caller should treat this as a race loss.
    pub async fn consume_invitation(&self, id: InvitationId) -> Result<(), AuthError> {
        let now = Utc::now().format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string();

        let result = sqlx::query(
            "UPDATE allowthem_invitations SET consumed_at = ? \
             WHERE id = ? AND consumed_at IS NULL",
        )
        .bind(&now)
        .bind(id)
        .execute(self.pool())
        .await
        .map_err(AuthError::Database)?;

        if result.rows_affected() == 0 {
            // Distinguish "does not exist" from "already consumed".
            let exists: bool = sqlx::query_scalar(
                "SELECT EXISTS(SELECT 1 FROM allowthem_invitations WHERE id = ?)",
            )
            .bind(id)
            .fetch_one(self.pool())
            .await
            .map_err(AuthError::Database)?;

            return Err(if exists {
                AuthError::Gone
            } else {
                AuthError::NotFound
            });
        }

        Ok(())
    }

    /// List unconsumed, non-expired invitations, newest first.
    pub async fn list_pending_invitations(&self) -> Result<Vec<Invitation>, AuthError> {
        let now = Utc::now().format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string();

        sqlx::query_as::<_, Invitation>(
            "SELECT id, email, metadata, invited_by, expires_at, consumed_at, created_at \
             FROM allowthem_invitations \
             WHERE consumed_at IS NULL AND expires_at > ? \
             ORDER BY created_at DESC",
        )
        .bind(&now)
        .fetch_all(self.pool())
        .await
        .map_err(AuthError::Database)
    }

    /// Delete an invitation outright, whether pending or consumed.
    pub async fn delete_invitation(&self, id: InvitationId) -> Result<(), AuthError> {
        let result = sqlx::query("DELETE FROM allowthem_invitations WHERE id = ?")
            .bind(id)
            .execute(self.pool())
            .await
            .map_err(AuthError::Database)?;

        if result.rows_affected() == 0 {
            return Err(AuthError::NotFound);
        }

        Ok(())
    }

    /// Validate a raw invitation token.
    ///
    /// Returns `Some(Invitation)` if the token exists, is not expired, and has
    /// not been consumed. Returns `None` otherwise. The caller is responsible
    /// for checking email match on targeted invitations.
    pub async fn validate_invitation(
        &self,
        raw_token: &str,
    ) -> Result<Option<Invitation>, AuthError> {
        let token_hash = hash_invitation_token(raw_token);
        let now = Utc::now().format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string();

        sqlx::query_as::<_, Invitation>(
            "SELECT id, email, metadata, invited_by, expires_at, consumed_at, created_at \
             FROM allowthem_invitations \
             WHERE token_hash = ? AND expires_at > ? AND consumed_at IS NULL",
        )
        .bind(&token_hash)
        .bind(&now)
        .fetch_optional(self.pool())
        .await
        .map_err(AuthError::Database)
    }
}

#[cfg(test)]
mod tests {
    use chrono::{Duration, Utc};

    use crate::db::Db;
    use crate::error::AuthError;
    use crate::types::Email;

    async fn test_db() -> Db {
        Db::connect("sqlite::memory:").await.expect("in-memory db")
    }

    #[tokio::test]
    async fn create_invitation_returns_raw_token_and_invitation() {
        let db = test_db().await;
        let email = Email::new("invite@example.com".to_string()).unwrap();
        let expires = Utc::now() + Duration::hours(24);

        let (raw_token, inv) = db
            .create_invitation(Some(&email), Some(r#"{"role":"editor"}"#), None, expires)
            .await
            .expect("create_invitation");

        assert!(!raw_token.is_empty());
        assert_eq!(inv.email.as_ref().unwrap().as_str(), "invite@example.com");
        assert_eq!(inv.metadata.as_deref(), Some(r#"{"role":"editor"}"#));
        assert!(inv.invited_by.is_none());
        assert!(inv.consumed_at.is_none());
    }

    #[tokio::test]
    async fn create_open_invitation_has_no_email() {
        let db = test_db().await;
        let expires = Utc::now() + Duration::hours(24);

        let (_raw, inv) = db
            .create_invitation(None, None, None, expires)
            .await
            .expect("create open invitation");

        assert!(inv.email.is_none());
        assert!(inv.metadata.is_none());
    }

    #[tokio::test]
    async fn validate_returns_invitation_for_valid_token() {
        let db = test_db().await;
        let email = Email::new("v@example.com".to_string()).unwrap();
        let expires = Utc::now() + Duration::hours(24);
        let (raw, _) = db
            .create_invitation(Some(&email), Some("{}"), None, expires)
            .await
            .unwrap();

        let inv = db.validate_invitation(&raw).await.expect("validate");
        assert!(inv.is_some());
        let inv = inv.unwrap();
        assert_eq!(inv.email.as_ref().unwrap().as_str(), "v@example.com");
    }

    #[tokio::test]
    async fn validate_returns_none_for_garbage_token() {
        let db = test_db().await;
        let result = db
            .validate_invitation("not-a-real-token")
            .await
            .expect("validate");
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn validate_returns_none_for_expired_token() {
        let db = test_db().await;
        let expires = Utc::now() - Duration::hours(1);
        let (raw, _) = db
            .create_invitation(None, None, None, expires)
            .await
            .unwrap();

        let result = db.validate_invitation(&raw).await.expect("validate");
        assert!(result.is_none(), "expired invitation must return None");
    }

    #[tokio::test]
    async fn consume_marks_invitation_consumed() {
        let db = test_db().await;
        let expires = Utc::now() + Duration::hours(24);
        let (raw, inv) = db
            .create_invitation(None, None, None, expires)
            .await
            .unwrap();

        db.consume_invitation(inv.id).await.expect("consume");

        // Validation must now return None.
        let result = db.validate_invitation(&raw).await.expect("validate");
        assert!(result.is_none(), "consumed invitation must not validate");
    }

    #[tokio::test]
    async fn consume_twice_returns_gone() {
        let db = test_db().await;
        let expires = Utc::now() + Duration::hours(24);
        let (_, inv) = db
            .create_invitation(None, None, None, expires)
            .await
            .unwrap();

        db.consume_invitation(inv.id).await.expect("first consume");

        let err = db
            .consume_invitation(inv.id)
            .await
            .expect_err("second consume should fail");
        assert!(
            matches!(err, AuthError::Gone),
            "expected AuthError::Gone, got {err:?}"
        );
    }

    #[tokio::test]
    async fn list_pending_excludes_expired_and_consumed() {
        let db = test_db().await;
        let future = Utc::now() + Duration::hours(24);
        let past = Utc::now() - Duration::hours(1);

        // Pending (should appear)
        let (_, pending) = db
            .create_invitation(None, Some("pending"), None, future)
            .await
            .unwrap();

        // Expired (should not appear)
        let _ = db
            .create_invitation(None, Some("expired"), None, past)
            .await
            .unwrap();

        // Consumed (should not appear)
        let (_, consumed) = db
            .create_invitation(None, Some("consumed"), None, future)
            .await
            .unwrap();
        db.consume_invitation(consumed.id).await.unwrap();

        let list = db.list_pending_invitations().await.expect("list");
        assert_eq!(list.len(), 1);
        assert_eq!(list[0].id, pending.id);
    }

    #[tokio::test]
    async fn create_invitation_with_invited_by_stores_user_id() {
        let db = test_db().await;
        let email = Email::new("creator@example.com".to_string()).unwrap();
        let user = db
            .create_user(email, "password123", None, None)
            .await
            .expect("create user");
        let expires = Utc::now() + Duration::hours(24);

        let (_, inv) = db
            .create_invitation(None, None, Some(user.id), expires)
            .await
            .expect("create with invited_by");

        assert_eq!(inv.invited_by, Some(user.id));
    }

    #[tokio::test]
    async fn delete_invitation_removes_it() {
        let db = test_db().await;
        let expires = Utc::now() + Duration::hours(24);
        let (raw, inv) = db
            .create_invitation(None, None, None, expires)
            .await
            .unwrap();

        db.delete_invitation(inv.id).await.expect("delete");

        let result = db.validate_invitation(&raw).await.expect("validate");
        assert!(result.is_none(), "deleted invitation must not validate");

        // List should be empty.
        let list = db.list_pending_invitations().await.expect("list");
        assert!(list.is_empty());
    }

    #[tokio::test]
    async fn consume_nonexistent_returns_not_found() {
        let db = test_db().await;
        let fake_id = crate::types::InvitationId::new();
        let err = db
            .consume_invitation(fake_id)
            .await
            .expect_err("consume nonexistent should fail");
        assert!(
            matches!(err, AuthError::NotFound),
            "expected AuthError::NotFound, got {err:?}"
        );
    }

    #[tokio::test]
    async fn delete_nonexistent_returns_not_found() {
        let db = test_db().await;
        let fake_id = crate::types::InvitationId::new();
        let err = db
            .delete_invitation(fake_id)
            .await
            .expect_err("delete nonexistent should fail");
        assert!(
            matches!(err, AuthError::NotFound),
            "expected AuthError::NotFound, got {err:?}"
        );
    }
}