comindly-engine 0.5.0-beta.8

comindly's engine: the index behind /api/v1 and /mcp, on this machine or as a team server, with the web app, indexing jobs, members and roles.
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
//! Sign-in through an OpenID Connect provider: the authorization code flow with PKCE.

use std::collections::HashMap;
use std::sync::Mutex;
use std::time::{Duration, Instant};

use anyhow::{bail, Context, Result};
use openidconnect::core::{CoreAuthenticationFlow, CoreClient, CoreProviderMetadata};
use openidconnect::{
    AuthorizationCode, ClientId, ClientSecret, CsrfToken, EndpointMaybeSet, EndpointNotSet,
    EndpointSet, IssuerUrl, Nonce, PkceCodeChallenge, PkceCodeVerifier, RedirectUrl, Scope,
    TokenResponse,
};

use crate::server::OidcConfig;

type Client = CoreClient<
    EndpointSet,
    EndpointNotSet,
    EndpointNotSet,
    EndpointNotSet,
    EndpointMaybeSet,
    EndpointMaybeSet,
>;

/// How long someone may take at the provider before the sign-in has to start again.
const PENDING_FOR: Duration = Duration::from_secs(600);
/// Signing keys and endpoints are fetched again after this long, or when a token fails to verify.
const DISCOVERY_FOR: Duration = Duration::from_secs(3600);
const MAX_PENDING: usize = 1000;

/// Where the provider sends people back.
pub const CALLBACK: &str = "/auth/oidc/callback";

struct Pending {
    nonce: Nonce,
    verifier: PkceCodeVerifier,
    started: Instant,
}

pub struct Oidc {
    pub config: OidcConfig,
    redirect: RedirectUrl,
    http: reqwest::Client,
    client: tokio::sync::Mutex<Option<(Client, Instant)>>,
    pending: Mutex<HashMap<String, Pending>>,
}

#[derive(Debug)]
struct HttpError(String);

impl std::fmt::Display for HttpError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for HttpError {}

async fn send(
    http: reqwest::Client,
    req: openidconnect::HttpRequest,
) -> Result<openidconnect::HttpResponse, HttpError> {
    let err = |e: &dyn std::fmt::Display| HttpError(e.to_string());
    let req = reqwest::Request::try_from(req).map_err(|e| err(&e))?;
    let res = http.execute(req).await.map_err(|e| err(&e))?;
    let mut out = openidconnect::http::Response::builder().status(res.status());
    for (k, v) in res.headers() {
        out = out.header(k, v);
    }
    let body = res.bytes().await.map_err(|e| err(&e))?;
    out.body(body.to_vec()).map_err(|e| err(&e))
}

impl Oidc {
    pub fn new(config: OidcConfig, public_url: &str) -> Result<Oidc> {
        IssuerUrl::new(config.issuer.clone()).context("COMINDLY_OIDC_ISSUER is not a URL")?;
        let redirect = RedirectUrl::new(format!("{public_url}{CALLBACK}"))
            .context("COMINDLY_PUBLIC_URL is not a URL")?;
        let http = reqwest::Client::builder()
            .redirect(reqwest::redirect::Policy::none())
            .timeout(Duration::from_secs(15))
            .build()?;
        Ok(Oidc {
            config,
            redirect,
            http,
            client: Default::default(),
            pending: Default::default(),
        })
    }

    /// The provider's endpoints and signing keys, fetched again when old or when `fresh`.
    async fn client(&self, fresh: bool) -> Result<Client> {
        let mut cached = self.client.lock().await;
        if let Some((c, at)) = cached.as_ref() {
            if !fresh && at.elapsed() < DISCOVERY_FOR {
                return Ok(c.clone());
            }
        }
        let http = self.http.clone();
        let call = move |r| send(http.clone(), r);
        let issuer = IssuerUrl::new(self.config.issuer.clone())?;
        let meta = CoreProviderMetadata::discover_async(issuer, &call)
            .await
            .with_context(|| {
                format!(
                    "cannot reach the sign-in provider at {}",
                    self.config.issuer
                )
            })?;
        let client = CoreClient::from_provider_metadata(
            meta,
            ClientId::new(self.config.client_id.clone()),
            Some(ClientSecret::new(self.config.client_secret.clone())),
        )
        .set_redirect_uri(self.redirect.clone());
        *cached = Some((client.clone(), Instant::now()));
        Ok(client)
    }

    /// Where to send the browser, and the state it must bring back.
    pub async fn start(&self) -> Result<(String, String)> {
        let client = self.client(false).await?;
        let (challenge, verifier) = PkceCodeChallenge::new_random_sha256();
        let (url, state, nonce) = client
            .authorize_url(
                CoreAuthenticationFlow::AuthorizationCode,
                CsrfToken::new_random,
                Nonce::new_random,
            )
            .add_scope(Scope::new("email".into()))
            .add_scope(Scope::new("profile".into()))
            .set_pkce_challenge(challenge)
            .url();
        let mut pending = self.pending.lock().unwrap();
        pending.retain(|_, p| p.started.elapsed() < PENDING_FOR);
        anyhow::ensure!(
            pending.len() < MAX_PENDING,
            "Too many sign-ins in progress; try again in a few minutes"
        );
        pending.insert(
            state.secret().clone(),
            Pending {
                nonce,
                verifier,
                started: Instant::now(),
            },
        );
        Ok((url.to_string(), state.secret().clone()))
    }

    /// The verified email address of someone coming back from the provider.
    pub async fn finish(&self, state: &str, code: &str) -> Result<String> {
        let pending = self
            .pending
            .lock()
            .unwrap()
            .remove(state)
            .filter(|p| p.started.elapsed() < PENDING_FOR)
            .context("This sign-in expired or was already used; start again")?;
        let client = self.client(false).await?;
        let http = self.http.clone();
        let call = move |r| send(http.clone(), r);
        let tokens = client
            .exchange_code(AuthorizationCode::new(code.to_string()))?
            .set_pkce_verifier(pending.verifier)
            .request_async(&call)
            .await
            .map_err(|e| {
                crate::ui::warn(&format!("sign-in provider refused the code: {e:?}"));
                anyhow::anyhow!("The sign-in provider did not accept this sign-in; start again")
            })?;
        let id_token = tokens
            .id_token()
            .context("The sign-in provider sent no ID token")?;
        let claims = match id_token.claims(&client.id_token_verifier(), &pending.nonce) {
            Ok(c) => c.clone(),
            // The provider may have rotated its signing keys since they were fetched.
            Err(_) => {
                let client = self.client(true).await?;
                let verified = id_token
                    .claims(&client.id_token_verifier(), &pending.nonce)
                    .cloned();
                verified.map_err(|e| {
                    crate::ui::warn(&format!("ID token rejected: {e}"));
                    anyhow::anyhow!("The sign-in could not be verified; start again")
                })?
            }
        };
        if claims.email_verified() == Some(false) {
            bail!("The sign-in provider has not verified this email address");
        }
        let email = claims
            .email()
            .map(|e| e.as_str().to_string())
            .or_else(|| {
                claims
                    .preferred_username()
                    .map(|u| u.as_str().to_string())
                    .filter(|u| u.contains('@'))
            })
            .context("The sign-in provider sent no email address; ask its admin to include the email claim")?;
        Ok(crate::server::normalize(&email))
    }

    /// `microsoft` or `google` when the issuer is theirs.
    pub fn brand(&self) -> Option<&'static str> {
        let host = self.config.issuer.split("://").nth(1)?.split('/').next()?;
        match host {
            "login.microsoftonline.com" | "sts.windows.net" => Some("microsoft"),
            "accounts.google.com" => Some("google"),
            _ => None,
        }
    }

    /// Whether someone at `email` may join without an invitation.
    pub fn may_join(&self, email: &str) -> bool {
        email
            .rsplit_once('@')
            .is_some_and(|(_, d)| self.config.allowed_domains.iter().any(|a| a == d))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use openidconnect::core::{
        CoreIdToken, CoreIdTokenClaims, CoreJsonWebKeySet, CoreJwsSigningAlgorithm,
        CoreRsaPrivateSigningKey,
    };
    use openidconnect::{
        Audience, EmptyAdditionalClaims, EndUserEmail, PrivateSigningKey, StandardClaims,
        SubjectIdentifier,
    };
    use std::sync::Arc;

    /// What the provider puts in the next ID token.
    #[derive(Clone)]
    struct Next {
        email: Option<String>,
        verified: Option<bool>,
        audience: String,
        nonce: Option<String>,
        expires_in: i64,
    }

    struct Provider {
        issuer: String,
        key: CoreRsaPrivateSigningKey,
        next: Mutex<Next>,
        /// The PKCE verifier each token request carried.
        verifiers: Mutex<Vec<String>>,
    }

    fn signing_key() -> CoreRsaPrivateSigningKey {
        use rsa::pkcs1::EncodeRsaPrivateKey;
        let key = rsa::RsaPrivateKey::new(&mut rsa::rand_core::OsRng, 2048).unwrap();
        let pem = key.to_pkcs1_pem(rsa::pkcs8::LineEnding::LF).unwrap();
        CoreRsaPrivateSigningKey::from_pem(
            &pem,
            Some(openidconnect::JsonWebKeyId::new("k1".into())),
        )
        .unwrap()
    }

    async fn provider() -> Arc<Provider> {
        use axum::routing::{get, post};
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let issuer = format!("http://{}", listener.local_addr().unwrap());
        let p = Arc::new(Provider {
            issuer: issuer.clone(),
            key: signing_key(),
            next: Mutex::new(Next {
                email: Some("Dev@Example.com".into()),
                verified: None,
                audience: "comindly".into(),
                nonce: None,
                expires_in: 300,
            }),
            verifiers: Default::default(),
        });
        let (meta, jwks, token) = (p.clone(), p.clone(), p.clone());
        let app = axum::Router::new()
            .route(
                "/.well-known/openid-configuration",
                get(move || async move {
                    let i = &meta.issuer;
                    axum::Json(serde_json::json!({
                        "issuer": i,
                        "authorization_endpoint": format!("{i}/authorize"),
                        "token_endpoint": format!("{i}/token"),
                        "jwks_uri": format!("{i}/jwks"),
                        "response_types_supported": ["code"],
                        "subject_types_supported": ["public"],
                        "id_token_signing_alg_values_supported": ["RS256"],
                    }))
                }),
            )
            .route(
                "/jwks",
                get(move || async move {
                    axum::Json(CoreJsonWebKeySet::new(vec![jwks.key.as_verification_key()]))
                }),
            )
            .route(
                "/token",
                post(move |body: String| async move {
                    let form: HashMap<String, String> =
                        openidconnect::url::form_urlencoded::parse(body.as_bytes())
                            .into_owned()
                            .collect();
                    token
                        .verifiers
                        .lock()
                        .unwrap()
                        .push(form.get("code_verifier").cloned().unwrap_or_default());
                    let n = token.next.lock().unwrap().clone();
                    let now = chrono::Utc::now();
                    let mut standard = StandardClaims::new(SubjectIdentifier::new("u1".into()));
                    standard = standard
                        .set_email(n.email.map(EndUserEmail::new))
                        .set_email_verified(n.verified);
                    let claims = CoreIdTokenClaims::new(
                        IssuerUrl::new(token.issuer.clone()).unwrap(),
                        vec![Audience::new(n.audience)],
                        now + chrono::Duration::seconds(n.expires_in),
                        now - chrono::Duration::seconds(60),
                        standard,
                        EmptyAdditionalClaims {},
                    )
                    .set_nonce(n.nonce.map(Nonce::new));
                    let id = CoreIdToken::new(
                        claims,
                        &token.key,
                        CoreJwsSigningAlgorithm::RsaSsaPkcs1V15Sha256,
                        None,
                        None,
                    )
                    .unwrap();
                    axum::Json(serde_json::json!({
                        "access_token": "at",
                        "token_type": "Bearer",
                        "expires_in": 3600,
                        "id_token": id,
                    }))
                }),
            );
        tokio::spawn(async move { axum::serve(listener, app).await });
        p
    }

    fn oidc(issuer: &str) -> Oidc {
        Oidc::new(
            OidcConfig {
                issuer: issuer.into(),
                client_id: "comindly".into(),
                client_secret: "client secret".into(),
                label: "Example".into(),
                allowed_domains: vec!["example.com".into()],
            },
            "https://comindly.example.com",
        )
        .unwrap()
    }

    /// Start a sign-in; the provider will answer with this sign-in's nonce.
    async fn begin(p: &Provider, o: &Oidc) -> (String, String) {
        let (url, state) = o.start().await.unwrap();
        let url = openidconnect::url::Url::parse(&url).unwrap();
        let q: HashMap<String, String> = url.query_pairs().into_owned().collect();
        assert_eq!(q["state"], state);
        assert_eq!(q["code_challenge_method"], "S256");
        assert_eq!(
            q["redirect_uri"],
            "https://comindly.example.com/auth/oidc/callback"
        );
        assert!(q["scope"].contains("openid") && q["scope"].contains("email"));
        p.next.lock().unwrap().nonce = Some(q["nonce"].clone());
        (state, q["code_challenge"].clone())
    }

    #[tokio::test]
    async fn a_signed_and_matching_id_token_gives_the_verified_email_once() {
        let p = provider().await;
        let o = oidc(&p.issuer);
        let (state, challenge) = begin(&p, &o).await;
        assert_eq!(o.finish(&state, "code-1").await.unwrap(), "dev@example.com");
        let verifier = p.verifiers.lock().unwrap()[0].clone();
        let expected =
            PkceCodeChallenge::from_code_verifier_sha256(&PkceCodeVerifier::new(verifier));
        assert_eq!(
            expected.as_str(),
            challenge,
            "the code is redeemed with this sign-in's PKCE secret"
        );
        assert!(
            o.finish(&state, "code-1").await.is_err(),
            "a state works once"
        );
        assert!(o.finish("made-up", "code-1").await.is_err());
        assert!(o.may_join("dev@example.com"));
        assert!(!o.may_join("dev@example.com.evil.org"));
        assert!(!o.may_join("dev@other.org"));
        assert_eq!(o.brand(), None);
        assert_eq!(
            oidc("https://login.microsoftonline.com/tenant/v2.0").brand(),
            Some("microsoft")
        );
        assert_eq!(oidc("https://accounts.google.com").brand(), Some("google"));
    }

    #[tokio::test]
    async fn tokens_for_someone_else_stale_or_replayed_are_refused() {
        let p = provider().await;
        let o = oidc(&p.issuer);
        type Change = fn(&mut Next);
        let cases: [(&str, Change); 5] = [
            ("another client's token", |n| {
                n.audience = "other-app".into()
            }),
            ("an expired token", |n| n.expires_in = -120),
            ("another sign-in's nonce", |n| n.nonce = Some("old".into())),
            ("an unverified email", |n| n.verified = Some(false)),
            ("no email", |n| n.email = None),
        ];
        for (what, change) in cases {
            let (state, _) = begin(&p, &o).await;
            change(&mut p.next.lock().unwrap());
            assert!(
                o.finish(&state, "code").await.is_err(),
                "{what} was accepted"
            );
            *p.next.lock().unwrap() = Next {
                email: Some("dev@example.com".into()),
                verified: None,
                audience: "comindly".into(),
                nonce: None,
                expires_in: 300,
            };
        }
    }

    #[tokio::test]
    async fn an_unreachable_provider_is_an_error_not_a_panic() {
        let o = oidc("http://127.0.0.1:9");
        let e = o.start().await.unwrap_err();
        assert!(format!("{e:#}").contains("cannot reach the sign-in provider"));
    }
}