alibi-plugins 0.8.0

Built-in authentication plugins and API metadata for Alibi
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use crate::oauth::providers::OAuthClientAssertionContext;
use crate::oauth::providers::OAuthProvider;
use crate::oauth::providers::OAuthTokenEndpointAuth;
use crate::oauth::providers::OAuthTokenGrant;
use crate::oauth::providers::OAuthTokenSet;
use crate::oauth::providers::OAuthUserInfoRequest;
use crate::oauth::providers::OAuthUserInfoResponse;
use alibi_core::AuthError;
use alibi_core::AuthResult;
use base64::Engine;
use chrono::Utc;
pub(in crate::oauth) async fn refresh_tokens_via_provider(
    provider: &OAuthProvider,
    refresh_token: &str,
    context: Option<crate::oauth::providers::OAuthRefreshContext<'_>>,
) -> AuthResult<OAuthTokenSet> {
    if let Some(handler) = &provider.refresh_access_token {
        return handler
            .refresh_access_token_with_context(refresh_token, context)
            .await
            .map_err(AuthError::internal)
            .and_then(|tokens| with_default_access_expiry(provider, tokens));
    }

    // Resolve once per real grant, without mutating the shared provider config.
    let mut resolved_provider;
    let provider = if let Some(resolver) = provider
        .authorization
        .as_ref()
        .and_then(|policy| policy.refresh_token_params_resolver.as_ref())
    {
        let params = resolver
            .0
            .resolve(context)
            .await
            .map_err(AuthError::internal)?;
        resolved_provider = provider.clone();
        if let Some(policy) = &mut resolved_provider.authorization {
            policy.refresh_token_params = params.unwrap_or_default();
        }
        &resolved_provider
    } else {
        provider
    };

    let mut form = vec![
        ("grant_type", "refresh_token"),
        ("refresh_token", refresh_token),
    ];
    if let Some(scope) = provider
        .authorization
        .as_ref()
        .and_then(|policy| policy.refresh_scope.as_deref())
    {
        form.push(("scope", scope));
    }
    let request = provider_token_request(provider, &form, OAuthTokenGrant::RefreshToken).await?;
    let token_resp = request
        .send()
        .await
        .map_err(|e| AuthError::internal(format!("Token refresh failed: {e}")))?;

    if !token_resp.status().is_success() {
        let error_body = token_resp
            .text()
            .await
            .unwrap_or_else(|_| "Unknown error".to_owned());
        return Err(AuthError::internal(format!(
            "Token refresh returned error: {error_body}"
        )));
    }

    let token_data: serde_json::Value = token_resp
        .json()
        .await
        .map_err(|e| AuthError::internal(format!("Failed to parse refresh response: {e}")))?;

    with_default_access_expiry(
        provider,
        parse_token_response(
            token_data,
            provider
                .authorization
                .as_ref()
                .is_some_and(|policy| policy.allow_missing_access_token),
        )?,
    )
}

pub(in crate::oauth::handlers) async fn provider_token_request(
    provider: &OAuthProvider,
    fields: &[(&str, &str)],
    grant_type: OAuthTokenGrant,
) -> AuthResult<reqwest::RequestBuilder> {
    let mut form: Vec<_> = fields
        .iter()
        .map(|(key, value)| ((*key).to_owned(), (*value).to_owned()))
        .collect();
    if let Some(policy) = &provider.authorization {
        let additions = if grant_type == OAuthTokenGrant::RefreshToken {
            &policy.refresh_token_params
        } else {
            &policy.authorization_code_params
        };
        for (key, value) in additions {
            if grant_type == OAuthTokenGrant::RefreshToken {
                if matches!(
                    key.as_str(),
                    "grant_type" | "refresh_token" | "__proto__" | "constructor" | "prototype"
                ) {
                    continue;
                }
                form.retain(|(existing, _)| existing != key);
            } else if form.iter().any(|(existing, _)| existing == key) {
                continue;
            }
            form.push((key.clone(), value.clone()));
        }
    }
    let request = reqwest::Client::builder()
        .redirect(reqwest::redirect::Policy::none())
        .build()
        .map_err(|error| AuthError::internal(format!("Token HTTP client failed: {error}")))?
        .post(&provider.token_url)
        .header("Accept", "application/json");
    let mut request = request;
    if grant_type == OAuthTokenGrant::AuthorizationCode
        && let Some(policy) = &provider.authorization
    {
        let mut headers = reqwest::header::HeaderMap::new();
        for (name, value) in &policy.authorization_code_headers {
            let name =
                reqwest::header::HeaderName::from_bytes(name.as_bytes()).map_err(|error| {
                    AuthError::config(format!("Invalid code grant header: {error}"))
                })?;
            let value = reqwest::header::HeaderValue::from_str(value).map_err(|error| {
                AuthError::config(format!("Invalid code grant header: {error}"))
            })?;
            _ = headers.insert(name, value);
        }
        request = request.headers(headers);
    }
    let authentication = provider.authorization.as_ref().and_then(|policy| {
        if grant_type == OAuthTokenGrant::RefreshToken {
            policy
                .refresh_token_endpoint_auth
                .or(policy.token_endpoint_auth)
        } else {
            policy.token_endpoint_auth
        }
    });
    let has_field = |name: &str| form.iter().any(|(key, _)| key == name);
    if has_field("client_assertion") != has_field("client_assertion_type") {
        return Err(AuthError::config(
            "client_assertion and client_assertion_type must both be provided",
        ));
    }
    if has_field("client_assertion") {
        if authentication.is_some() {
            return Err(AuthError::config(
                "client_assertion body parameters cannot be combined with tokenEndpointAuth",
            ));
        }
        if !provider.client_secret.is_empty() || has_field("client_secret") {
            return Err(AuthError::config(
                "private_key_jwt token endpoint authentication cannot be combined with clientSecret",
            ));
        }
        if !provider.client_id.is_empty() {
            form.retain(|(key, _)| key != "client_id");
            form.push(("client_id".into(), provider.client_id.clone()));
        }
        return Ok(request.form(&form));
    }
    // Generic parameter policies use the published automatic authentication
    // selection. Retain the legacy transport for providers with no additions.
    let authentication = authentication.or_else(|| {
        provider.authorization.as_ref().and_then(|policy| {
            let additions = if grant_type == OAuthTokenGrant::RefreshToken {
                &policy.refresh_token_params
            } else {
                &policy.authorization_code_params
            };
            (!additions.is_empty()).then_some(if provider.client_secret.is_empty() {
                OAuthTokenEndpointAuth::None
            } else {
                OAuthTokenEndpointAuth::ClientSecretPost
            })
        })
    });
    let request = match authentication {
        None => {
            form.retain(|(key, _)| key != "client_id" && key != "client_secret");
            form.extend([
                ("client_id".into(), provider.client_id.clone()),
                ("client_secret".into(), provider.client_secret.clone()),
            ]);
            request
        }
        Some(OAuthTokenEndpointAuth::None) => {
            if provider.client_id.is_empty()
                || !provider.client_secret.is_empty()
                || has_field("client_secret")
            {
                return Err(AuthError::config(
                    "Public token authentication requires client ID and no secret",
                ));
            }
            form.retain(|(key, _)| key != "client_id");
            form.push(("client_id".into(), provider.client_id.clone()));
            request
        }
        Some(OAuthTokenEndpointAuth::PrivateKeyJwt) => {
            if provider.client_id.is_empty()
                || provider.token_url.is_empty()
                || !provider.client_secret.is_empty()
                || has_field("client_secret")
            {
                return Err(AuthError::config(
                    "Client assertion requires client ID and no secret",
                ));
            }
            let assertion = provider
                .authorization
                .as_ref()
                .and_then(|policy| policy.client_assertion.as_ref())
                .ok_or_else(|| AuthError::config("Client assertion callback is required"))?
                .0
                .get_client_assertion(OAuthClientAssertionContext {
                    client_id: provider.client_id.clone(),
                    token_endpoint: provider.token_url.clone(),
                    grant_type,
                })
                .await
                .map_err(AuthError::internal)?;
            form.retain(|(key, _)| key != "client_id");
            form.extend([
                ("client_id".into(), provider.client_id.clone()),
                ("client_assertion".into(), assertion),
                (
                    "client_assertion_type".into(),
                    "urn:ietf:params:oauth:client-assertion-type:jwt-bearer".into(),
                ),
            ]);
            request
        }
        Some(OAuthTokenEndpointAuth::ClientKeyPost) => {
            form.retain(|(key, _)| key != "client_key" && key != "client_secret");
            form.extend([
                ("client_key".into(), provider.client_id.clone()),
                ("client_secret".into(), provider.client_secret.clone()),
            ]);
            request
        }
        Some(method) => {
            if provider.client_id.is_empty() || provider.client_secret.is_empty() {
                return Err(AuthError::config(
                    "Client ID and client secret are required",
                ));
            }
            if method == OAuthTokenEndpointAuth::ClientSecretBasic {
                if has_field("client_secret") {
                    return Err(AuthError::config(
                        "client_secret_basic token endpoint authentication cannot be combined with client_secret body parameters",
                    ));
                }
                let encode = |value: &str| {
                    url::form_urlencoded::Serializer::new(String::new())
                        .append_key_only(value)
                        .finish()
                };
                request.basic_auth(
                    encode(&provider.client_id),
                    Some(encode(&provider.client_secret)),
                )
            } else {
                form.retain(|(key, _)| key != "client_id" && key != "client_secret");
                form.extend([
                    ("client_id".into(), provider.client_id.clone()),
                    ("client_secret".into(), provider.client_secret.clone()),
                ]);
                request
            }
        }
    };
    Ok(request.form(&form))
}

pub(in crate::oauth::handlers) fn with_default_access_expiry(
    provider: &OAuthProvider,
    mut tokens: OAuthTokenSet,
) -> AuthResult<OAuthTokenSet> {
    if tokens.access_token_expires_at.is_none()
        && let Some(seconds) = provider
            .authorization
            .as_ref()
            .and_then(|policy| policy.default_access_token_expires_in)
        && seconds != 0.0
        && !seconds.is_nan()
    {
        tokens.access_token_expires_at = crate::oauth::providers::remaining_profile::grant_expiry(
            &serde_json::json!(seconds),
            false,
        );
        if tokens.access_token_expires_at.is_none() {
            return Err(AuthError::internal("Invalid provider token expiry"));
        }
    }
    Ok(tokens)
}

pub(in crate::oauth::handlers) fn parse_token_response(
    token_data: serde_json::Value,
    allow_missing_access_token: bool,
) -> AuthResult<OAuthTokenSet> {
    if token_data.is_null() {
        return Err(AuthError::internal("Missing token response"));
    }
    let access_token = token_data
        .get("access_token")
        .and_then(|v| v.as_str())
        .map(str::to_owned);
    if access_token.is_none() && !allow_missing_access_token {
        return Err(AuthError::internal(
            "Missing access_token in token response",
        ));
    }
    let refresh_token = token_data
        .get("refresh_token")
        .and_then(|v| v.as_str())
        .map(String::from);
    let id_token = token_data
        .get("id_token")
        .and_then(|v| v.as_str())
        .map(String::from);
    let expiry = |field: &str| -> Option<chrono::DateTime<Utc>> {
        crate::oauth::providers::remaining_profile::grant_expiry(token_data.get(field)?, true)
    };
    let access_token_expires_at = expiry("expires_in");
    let refresh_token_expires_at = expiry("refresh_token_expires_in");
    let scopes = match token_data.get("scope") {
        Some(serde_json::Value::String(scope)) => scope
            .split(crate::oauth::providers::remaining_profile::js_whitespace)
            .filter(|value| !value.is_empty())
            .map(String::from)
            .collect(),
        Some(serde_json::Value::Array(scopes)) => scopes
            .iter()
            .filter_map(serde_json::Value::as_str)
            .map(|value| {
                value.trim_matches(crate::oauth::providers::remaining_profile::js_whitespace)
            })
            .filter(|value| !value.is_empty())
            .map(String::from)
            .collect(),
        _ => Vec::new(),
    };

    Ok(OAuthTokenSet {
        token_type: token_data
            .get("token_type")
            .and_then(|v| v.as_str())
            .map(String::from),
        access_token,
        refresh_token,
        access_token_expires_at,
        refresh_token_expires_at,
        scopes,
        id_token,
        raw: Some(token_data),
    })
}

pub(crate) async fn validate_authorization_code_via_provider(
    provider: &OAuthProvider,
    code: &str,
    redirect_uri: &str,
    code_verifier: Option<&str>,
    device_id: Option<&str>,
) -> AuthResult<OAuthTokenSet> {
    let redirect_uri = provider
        .authorization
        .as_ref()
        .and_then(|policy| policy.redirect_uri.as_deref())
        .filter(|uri| !uri.is_empty())
        .unwrap_or(redirect_uri);
    if let Some(handler) = provider
        .authorization
        .as_ref()
        .and_then(|policy| policy.authorization_code.as_ref())
    {
        return handler
            .0
            .validate_authorization_code(crate::oauth::providers::OAuthAuthorizationCodeContext {
                code: code.into(),
                redirect_uri: redirect_uri.into(),
                code_verifier: code_verifier.map(str::to_owned),
                device_id: device_id.map(str::to_owned),
            })
            .await
            .map_err(AuthError::internal)
            .and_then(|tokens| with_default_access_expiry(provider, tokens));
    }
    let mut form: Vec<(&str, &str)> = vec![
        ("grant_type", "authorization_code"),
        ("code", code),
        ("redirect_uri", redirect_uri),
    ];
    if let Some(client_key) = provider
        .authorization
        .as_ref()
        .and_then(|policy| policy.authorization_code_client_key.as_deref())
        .filter(|value| !value.is_empty())
    {
        form.push(("client_key", client_key));
    }
    if let Some(code_verifier) = code_verifier {
        form.push(("code_verifier", code_verifier));
    }
    if let Some(device_id) = device_id {
        form.push(("device_id", device_id));
    }

    let request =
        provider_token_request(provider, &form, OAuthTokenGrant::AuthorizationCode).await?;
    let token_resp = request
        .send()
        .await
        .map_err(|e| AuthError::internal(format!("Token exchange failed: {e}")))?;

    if !token_resp.status().is_success() {
        let error_body = token_resp
            .text()
            .await
            .unwrap_or_else(|_| "Unknown error".to_owned());
        return Err(AuthError::internal(format!(
            "Token exchange returned error: {error_body}"
        )));
    }

    let token_data: serde_json::Value = token_resp
        .json()
        .await
        .map_err(|e| AuthError::internal(format!("Failed to parse token response: {e}")))?;
    with_default_access_expiry(
        provider,
        parse_token_response(
            token_data,
            provider
                .authorization
                .as_ref()
                .is_some_and(|policy| policy.allow_missing_access_token),
        )?,
    )
}

pub(crate) async fn fetch_user_info_from_provider(
    provider: &OAuthProvider,
    request: OAuthUserInfoRequest,
) -> AuthResult<OAuthUserInfoResponse> {
    if let Some(handler) = &provider.get_user_info {
        let response = handler.get_user_info(request).await.map_err(|error| {
            if provider
                .authorization
                .as_ref()
                .is_some_and(|policy| policy.source_profile_exceptions)
                && (handler.errors_are_exceptions()
                    || error.starts_with(
                        crate::oauth::providers::remaining_profile::PROFILE_EXCEPTION_PREFIX,
                    ))
            {
                AuthError::Api {
                    status: 500,
                    code: Some("OAUTH_PROFILE_EXCEPTION".into()),
                    message: "Provider profile callback failed".into(),
                }
            } else {
                AuthError::internal(error)
            }
        })?;
        return Ok(response);
    }

    if let Some(token) = request
        .id_token
        .as_deref()
        .filter(|_| provider.id_token.is_some())
    {
        // Direct sign-in verifies this immutable token before requesting its profile;
        // the code flow obtains it from the trusted provider token exchange.
        let payload = token
            .split('.')
            .nth(1)
            .ok_or_else(|| AuthError::internal("Missing ID-token payload"))?;
        let bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD
            .decode(payload)
            .map_err(|error| AuthError::internal(error.to_string()))?;
        let profile: serde_json::Value = serde_json::from_slice(&bytes)
            .map_err(|error| AuthError::internal(error.to_string()))?;
        if !crate::oauth::id_token::hosted_domain_allowed(
            provider,
            profile.get("hd").and_then(serde_json::Value::as_str),
        ) {
            return Err(AuthError::internal("Hosted domain mismatch"));
        }
        let mapper = provider
            .map_user_info
            .ok_or_else(|| AuthError::internal("Missing user-info mapper"))?;
        let user = mapper(profile.clone()).map_err(AuthError::internal)?;
        let response = OAuthUserInfoResponse {
            user_output: None,
            user,
            data: profile,
        };
        return Ok(response);
    }

    let user_info_url = provider
        .user_info_url
        .as_deref()
        .ok_or_else(|| AuthError::internal("Missing user_info_url for provider"))?;
    let access_token = request
        .access_token
        .as_deref()
        .ok_or_else(|| AuthError::internal("Missing access token for user-info lookup"))?;
    let mapper = provider
        .map_user_info
        .ok_or_else(|| AuthError::internal("Missing user-info mapper for provider"))?;

    let client = reqwest::Client::new();
    let user_info_resp = client
        .get(user_info_url)
        .bearer_auth(access_token)
        .header("Accept", "application/json")
        .send()
        .await
        .map_err(|e| AuthError::internal(format!("Failed to fetch user info: {e}")))?;

    if !user_info_resp.status().is_success() {
        let error_body = user_info_resp
            .text()
            .await
            .unwrap_or_else(|_| "Unknown error".to_owned());
        return Err(AuthError::internal(format!(
            "User info request failed: {error_body}"
        )));
    }

    let user_info_json: serde_json::Value = user_info_resp
        .json()
        .await
        .map_err(|e| AuthError::internal(format!("Failed to parse user info: {e}")))?;

    let user = mapper(user_info_json.clone())
        .map_err(|e| AuthError::internal(format!("Failed to map user info: {e}")))?;

    let response = OAuthUserInfoResponse {
        user_output: None,
        user,
        data: user_info_json,
    };
    Ok(response)
}