solid-pod-rs-idp 0.4.0-alpha.12

Solid-OIDC identity provider (authorization-code + DPoP-bound tokens, JWKS, credentials, dynamic client registration) — Rust port of JavaScriptSolidServer/src/idp
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
//! Pluggable user-storage trait.
//!
//! Port of `JavaScriptSolidServer/src/idp/accounts.js` (the subset
//! the IdP itself reaches into: find-by-email + verify-password).
//! Real persistence is the consumer's responsibility; we ship an
//! in-memory store for tests and single-user dev.

use std::collections::HashMap;

use argon2::password_hash::SaltString;
use argon2::{Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
use async_trait::async_trait;
use parking_lot::RwLock;
use rand::rngs::OsRng;
use thiserror::Error;

use crate::credentials::MIN_PASSWORD_LENGTH;

/// Errors surfaced by [`UserStore`].
#[derive(Debug, Error)]
pub enum UserStoreError {
    /// Hashing / verification failure.
    #[error("password hash: {0}")]
    Hash(String),

    /// Store-specific back-end failure (DB down, etc).
    #[error("backend: {0}")]
    Backend(String),

    /// Password does not meet the minimum length requirement.
    /// JSS commit `1feead2` enforces >= 8 characters at registration.
    #[error("password must be at least {min_length} characters")]
    PasswordTooShort {
        /// The minimum length that was not met.
        min_length: usize,
    },

    /// The store does not implement this operation. Surfaced by the
    /// default [`UserStore::delete`] so that stores opting out of
    /// Sprint-11 `account delete` still compile.
    #[error("not implemented")]
    NotImplemented,
}

/// User record. `password_hash` is an Argon2id PHC string.
#[derive(Debug, Clone)]
pub struct User {
    /// Stable internal identifier.
    pub id: String,
    /// Primary email (case-normalised before storage).
    pub email: String,
    /// Solid WebID URL — what the access-token `webid` claim surfaces.
    pub webid: String,
    /// Display name (free-form).
    pub name: Option<String>,
    /// Argon2id PHC-encoded password hash.
    pub password_hash: String,
    /// Optional username (unique handle, case-insensitive).
    pub username: Option<String>,
    /// Optional Nostr public key (x-only, hex, 64 chars). Used by the
    /// Schnorr SSO typed-username fallback: when `did:nostr` resolution
    /// fails and a username is provided, the IdP looks up the account
    /// by username and verifies the Schnorr signature against this key
    /// (the profile's `verificationMethod`).
    pub nostr_pubkey: Option<String>,
}

/// Async user-store contract.
#[async_trait]
pub trait UserStore: Send + Sync + 'static {
    /// Look up a user by email. Returns `Ok(None)` on no-match
    /// (distinct from `Err(_)` which means the backend failed).
    async fn find_by_email(&self, email: &str) -> Result<Option<User>, UserStoreError>;

    /// Look up a user by internal id.
    async fn find_by_id(&self, id: &str) -> Result<Option<User>, UserStoreError>;

    /// Look up a user by username (case-insensitive). Returns `Ok(None)`
    /// on no-match. Used by the Schnorr SSO typed-username fallback
    /// when `did:nostr` resolution fails.
    ///
    /// Default impl returns `Ok(None)` so existing stores that lack
    /// username support compile unchanged.
    async fn find_by_username(&self, _username: &str) -> Result<Option<User>, UserStoreError> {
        Ok(None)
    }

    /// Verify `password` against the user's stored hash. This lives
    /// on the trait rather than free-function so stores that use
    /// external auth (LDAP, OAuth federation) can override the
    /// verification path.
    async fn verify_password(&self, user: &User, password: &str) -> Result<bool, UserStoreError> {
        let parsed = PasswordHash::new(&user.password_hash)
            .map_err(|e| UserStoreError::Hash(e.to_string()))?;
        let ok = Argon2::default()
            .verify_password(password.as_bytes(), &parsed)
            .is_ok();
        Ok(ok)
    }

    /// Delete a user and every record they own (pods, WebID profile,
    /// sessions). Mirrors JSS commit `d9e56d8` (#292).
    ///
    /// Default impl returns [`UserStoreError::NotImplemented`] so
    /// existing stores compile unchanged; operators wire this on the
    /// concrete store they ship. Returns `Ok(false)` when the `id` is
    /// unknown (already deleted / never existed), `Ok(true)` when a
    /// row was actually removed.
    async fn delete(&self, _id: &str) -> Result<bool, UserStoreError> {
        Err(UserStoreError::NotImplemented)
    }

    /// Update a user's password hash. The caller has already verified
    /// the current password and validated the new password length.
    /// Returns `Ok(true)` when the password was updated, `Ok(false)`
    /// when the user id was not found.
    ///
    /// Default impl returns [`UserStoreError::NotImplemented`].
    async fn update_password(
        &self,
        _id: &str,
        _new_password_hash: String,
    ) -> Result<bool, UserStoreError> {
        Err(UserStoreError::NotImplemented)
    }
}

/// Reference in-memory implementation.
#[derive(Default)]
pub struct InMemoryUserStore {
    /// Keyed by lowercase email.
    inner: RwLock<HashMap<String, User>>,
    /// Secondary index: lowercase username -> lowercase email.
    username_index: RwLock<HashMap<String, String>>,
}

impl InMemoryUserStore {
    /// Construct an empty store.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a user with an Argon2id hash of `password`. Returns
    /// the inserted record. Email is case-normalised (lowercased) on
    /// storage so `find_by_email` can match case-insensitively.
    ///
    /// Passwords shorter than [`MIN_PASSWORD_LENGTH`] (8 chars) are
    /// rejected with [`UserStoreError::PasswordTooShort`], matching
    /// JSS commit `1feead2`.
    pub fn insert_user(
        &self,
        id: impl Into<String>,
        email: impl Into<String>,
        webid: impl Into<String>,
        name: Option<String>,
        password: &str,
    ) -> Result<User, UserStoreError> {
        if password.len() < MIN_PASSWORD_LENGTH {
            return Err(UserStoreError::PasswordTooShort {
                min_length: MIN_PASSWORD_LENGTH,
            });
        }
        let salt = SaltString::generate(&mut OsRng);
        let hash = Argon2::default()
            .hash_password(password.as_bytes(), &salt)
            .map_err(|e| UserStoreError::Hash(e.to_string()))?
            .to_string();
        let user = User {
            id: id.into(),
            email: email.into().to_ascii_lowercase(),
            webid: webid.into(),
            name,
            password_hash: hash,
            username: None,
            nostr_pubkey: None,
        };
        self.inner.write().insert(user.email.clone(), user.clone());
        Ok(user)
    }

    /// Create a user with username and optional Nostr pubkey for
    /// Schnorr SSO typed-username fallback testing.
    pub fn insert_user_with_nostr(
        &self,
        id: impl Into<String>,
        email: impl Into<String>,
        webid: impl Into<String>,
        name: Option<String>,
        password: &str,
        username: Option<String>,
        nostr_pubkey: Option<String>,
    ) -> Result<User, UserStoreError> {
        if password.len() < MIN_PASSWORD_LENGTH {
            return Err(UserStoreError::PasswordTooShort {
                min_length: MIN_PASSWORD_LENGTH,
            });
        }
        let salt = SaltString::generate(&mut OsRng);
        let hash = Argon2::default()
            .hash_password(password.as_bytes(), &salt)
            .map_err(|e| UserStoreError::Hash(e.to_string()))?
            .to_string();
        let lower_email = email.into().to_ascii_lowercase();
        let user = User {
            id: id.into(),
            email: lower_email.clone(),
            webid: webid.into(),
            name,
            password_hash: hash,
            username: username.clone(),
            nostr_pubkey,
        };
        if let Some(ref uname) = username {
            self.username_index
                .write()
                .insert(uname.to_ascii_lowercase(), lower_email.clone());
        }
        self.inner.write().insert(lower_email, user.clone());
        Ok(user)
    }
}

#[async_trait]
impl UserStore for InMemoryUserStore {
    async fn find_by_email(&self, email: &str) -> Result<Option<User>, UserStoreError> {
        Ok(self.inner.read().get(&email.to_ascii_lowercase()).cloned())
    }

    async fn find_by_id(&self, id: &str) -> Result<Option<User>, UserStoreError> {
        Ok(self.inner.read().values().find(|u| u.id == id).cloned())
    }

    async fn find_by_username(&self, username: &str) -> Result<Option<User>, UserStoreError> {
        let lower = username.to_ascii_lowercase();
        let email = self.username_index.read().get(&lower).cloned();
        match email {
            Some(email_key) => Ok(self.inner.read().get(&email_key).cloned()),
            None => Ok(None),
        }
    }

    async fn delete(&self, id: &str) -> Result<bool, UserStoreError> {
        let mut guard = self.inner.write();
        // Find the keyed entry whose row matches this id and remove it.
        let email_key = guard
            .iter()
            .find(|(_, u)| u.id == id)
            .map(|(k, _)| k.clone());
        match email_key {
            Some(k) => {
                guard.remove(&k);
                Ok(true)
            }
            None => Ok(false),
        }
    }

    async fn update_password(
        &self,
        id: &str,
        new_password_hash: String,
    ) -> Result<bool, UserStoreError> {
        let mut guard = self.inner.write();
        let user = guard.values_mut().find(|u| u.id == id);
        match user {
            Some(u) => {
                u.password_hash = new_password_hash;
                Ok(true)
            }
            None => Ok(false),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn inmemory_stores_and_verifies() {
        let store = InMemoryUserStore::new();
        let user = store
            .insert_user(
                "u-1",
                "Ada@Example.COM",
                "https://ada.example/profile#me",
                Some("Ada".into()),
                "correct-horse-battery-staple",
            )
            .unwrap();
        assert_eq!(user.email, "ada@example.com");

        let found = store
            .find_by_email("ada@example.com")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(found.id, "u-1");

        // Case-insensitive email lookup.
        let found2 = store
            .find_by_email("ADA@example.COM")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(found2.id, "u-1");

        assert!(store
            .verify_password(&found, "correct-horse-battery-staple")
            .await
            .unwrap());
        assert!(!store
            .verify_password(&found, "wrong-password")
            .await
            .unwrap());
    }

    #[tokio::test]
    async fn inmemory_delete_removes_user() {
        let store = InMemoryUserStore::new();
        store
            .insert_user(
                "u-del",
                "del@example.com",
                "https://del.example/profile#me",
                None,
                "password",
            )
            .unwrap();
        assert!(store.find_by_id("u-del").await.unwrap().is_some());

        let removed = store.delete("u-del").await.unwrap();
        assert!(removed, "first delete should return true");
        assert!(store.find_by_id("u-del").await.unwrap().is_none());

        let removed_again = store.delete("u-del").await.unwrap();
        assert!(!removed_again, "second delete should return false");
    }

    #[tokio::test]
    async fn inmemory_find_by_id() {
        let store = InMemoryUserStore::new();
        store
            .insert_user(
                "u-2",
                "bob@example.com",
                "https://bob.example/profile#me",
                None,
                "password",
            )
            .unwrap();
        let found = store.find_by_id("u-2").await.unwrap().unwrap();
        assert_eq!(found.email, "bob@example.com");
        assert!(store.find_by_id("missing").await.unwrap().is_none());
    }

    #[tokio::test]
    async fn inmemory_update_password() {
        let store = InMemoryUserStore::new();
        store
            .insert_user(
                "u-pw",
                "pw@example.com",
                "https://pw.example/profile#me",
                None,
                "oldpassword123",
            )
            .unwrap();

        let user = store.find_by_id("u-pw").await.unwrap().unwrap();
        assert!(store
            .verify_password(&user, "oldpassword123")
            .await
            .unwrap());

        // Hash a new password and update.
        let salt = SaltString::generate(&mut OsRng);
        let new_hash = Argon2::default()
            .hash_password(b"newpassword456", &salt)
            .unwrap()
            .to_string();
        let updated = store.update_password("u-pw", new_hash).await.unwrap();
        assert!(updated);

        let user2 = store.find_by_id("u-pw").await.unwrap().unwrap();
        assert!(store
            .verify_password(&user2, "newpassword456")
            .await
            .unwrap());
        assert!(!store
            .verify_password(&user2, "oldpassword123")
            .await
            .unwrap());
    }

    #[tokio::test]
    async fn inmemory_update_password_unknown_user() {
        let store = InMemoryUserStore::new();
        let updated = store
            .update_password("nonexistent", "hash".into())
            .await
            .unwrap();
        assert!(!updated);
    }

    // ---- password-length validation at registration (JSS 1feead2) ----

    #[test]
    fn insert_user_rejects_7_char_password() {
        let store = InMemoryUserStore::new();
        let err = store
            .insert_user(
                "u-short",
                "short@example.com",
                "https://short.example/profile#me",
                None,
                "1234567",
            )
            .unwrap_err();
        match err {
            UserStoreError::PasswordTooShort { min_length } => {
                assert_eq!(min_length, 8);
            }
            other => panic!("expected PasswordTooShort, got {other:?}"),
        }
    }

    #[test]
    fn insert_user_accepts_8_char_password() {
        let store = InMemoryUserStore::new();
        let user = store
            .insert_user(
                "u-ok",
                "ok@example.com",
                "https://ok.example/profile#me",
                None,
                "12345678",
            )
            .unwrap();
        assert_eq!(user.id, "u-ok");
    }

    #[test]
    fn insert_user_rejects_empty_password() {
        let store = InMemoryUserStore::new();
        let err = store
            .insert_user(
                "u-empty",
                "empty@example.com",
                "https://empty.example/profile#me",
                None,
                "",
            )
            .unwrap_err();
        match err {
            UserStoreError::PasswordTooShort { min_length } => {
                assert_eq!(min_length, 8);
            }
            other => panic!("expected PasswordTooShort, got {other:?}"),
        }
    }
}