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
use super::{
    CompletedSession, IssuedSession, IssuedSessionRecord, SessionIssueError, SessionOverrides,
};
use alibi_core::entity::AuthUser;
use alibi_core::{AuthContext, AuthError, AuthRequest, AuthResponse, AuthResult, UpdateUser};
use chrono::Utc;

/// Source ordinary middleware admits the authenticated cache snapshot.
/// Only the nested read is caught; subsequent storage and callback errors keep
/// their own endpoint contract.
pub(crate) async fn ordinary_session<S: alibi_core::AuthSchema>(
    request: &impl alibi_core::session::SessionRequest,
    ctx: &AuthContext<S>,
) -> AuthResult<(alibi_core::AuthenticatedUser<S>, alibi_core::SessionView)> {
    ctx.require_cached_session(request)
        .await
        .map_err(super::unauthorized_if_unauthenticated)
}

/// Convert an `expiresIn` value (**seconds** from now) into an RFC 3339
/// `expires_at` timestamp string.
///
/// Returns `None` when `expires_in_secs` is `None`.
///
/// # Errors
///
/// Returns an error if the duration or resulting timestamp is outside the supported range.
pub fn expires_in_to_at(expires_in_secs: Option<i64>) -> AuthResult<Option<String>> {
    expires_in_secs
        .map(|secs| {
            chrono::Duration::try_seconds(secs)
                .and_then(|duration| Utc::now().checked_add_signed(duration))
                .map(|expires_at| expires_at.to_rfc3339())
                .ok_or_else(|| AuthError::bad_request("expiresIn is out of range"))
        })
        .transpose()
}

/// Resolve the session selected by a completed response's signed session cookie.
///
/// Cookie clearing is not a new session. The record is read from storage so
/// hooks never trust user or session fields from a response body.
///
/// # Errors
///
/// Returns an error if the session cannot be serialized.
pub async fn response_session<S: alibi_core::AuthSchema>(
    ctx: &AuthContext<S>,
    response: &AuthResponse,
) -> AuthResult<Option<IssuedSession<S>>> {
    let token = response
        .headers
        .get_all("set-cookie")
        .filter_map(|header| {
            let cookie = cookie::Cookie::parse(header.clone()).ok()?;
            if cookie.name() != ctx.config.session.cookie_name || cookie.value().is_empty() {
                return None;
            }
            alibi_core::utils::cookie_utils::verify_cookie_value(
                cookie.value(),
                ctx.config.current_secret(),
            )
        })
        .last();
    let Some(token) = token else {
        return Ok(None);
    };
    let Some(session) = ctx.database.get_session(&token).await? else {
        return Ok(None);
    };
    let Some(user) = ctx.session_user(&session).await? else {
        return Ok(None);
    };
    Ok(Some(IssuedSession { user, session }))
}

pub(crate) fn record_completed_session<S: alibi_core::AuthSchema>(
    user: &S::User,
    session: &S::Session,
) {
    if let Some(request) = alibi_core::hooks::current_request_hook_context() {
        request.extensions.insert(CompletedSession::<S> {
            user: user.clone(),
            session: session.clone(),
            user_view: None,
            user_record: None,
            session_record: None,
        });
    }
}

pub(crate) fn record_completed_session_record<S: alibi_core::AuthSchema>(
    user: &alibi_core::AdapterRecord<S::User>,
    session: &alibi_core::AdapterRecord<S::Session>,
) {
    if let Some(request) = alibi_core::hooks::current_request_hook_context() {
        request.extensions.insert(CompletedSession::<S> {
            user: user.stored().clone(),
            session: session.stored().clone(),
            user_view: None,
            user_record: Some(user.clone()),
            session_record: Some(session.clone()),
        });
    }
}

/// Retain a Source-defined callback projection after genuine session issuance.
/// This cannot create a completion or replace its raw models/owner/token.
pub(crate) fn record_completed_session_user_view<S: alibi_core::AuthSchema>(
    original_user: &impl AuthUser,
    session: &impl alibi_core::AuthSession,
    view: alibi_core::wire::UserView,
) {
    use alibi_core::AuthSession;
    if let Some(request) = alibi_core::hooks::current_request_hook_context()
        && let Some(completed) = request.extensions.get::<CompletedSession<S>>()
        && completed.user.id() == original_user.id()
        && view.id == original_user.id().as_ref()
        && completed.session.token() == session.token()
    {
        request.extensions.insert(CompletedSession::<S> {
            user: completed.user.clone(),
            session: completed.session.clone(),
            user_view: Some(view),
            user_record: completed.user_record.clone(),
            session_record: completed.session_record.clone(),
        });
    }
}

pub(crate) fn response_has_session_cookie<S: alibi_core::AuthSchema>(
    ctx: &AuthContext<S>,
    response: &AuthResponse,
) -> bool {
    response.headers.get_all("set-cookie").any(|header| {
        cookie::Cookie::parse(header.clone()).is_ok_and(|cookie| {
            cookie.name() == ctx.config.session.cookie_name
                && cookie
                    .value()
                    .split('.')
                    .next()
                    .is_some_and(|value| !value.is_empty())
        })
    })
}

pub(crate) fn completed_response_session<S: alibi_core::AuthSchema>(
    req: &AuthRequest,
    ctx: &AuthContext<S>,
    response: &AuthResponse,
) -> Option<std::sync::Arc<CompletedSession<S>>> {
    // A clearing cookie is not a completed login. The snapshot comes from the
    // trusted issuer, never the response body or a freshly mutated database row.
    if response_has_session_cookie(ctx, response) {
        req.extensions().get::<CompletedSession<S>>()
    } else {
        None
    }
}

/// Whether the admin plugin is active for this auth instance.
#[must_use]
pub fn admin_plugin_enabled(ctx: &AuthContext<impl alibi_core::AuthSchema>) -> bool {
    ctx.get_metadata("admin.enabled")
        .and_then(serde_json::Value::as_bool)
        .unwrap_or(false)
}

/// Resolve the configured message shown when a banned user attempts to create
/// a session.
pub fn admin_banned_user_message(ctx: &AuthContext<impl alibi_core::AuthSchema>) -> Option<String> {
    ctx.get_metadata("admin.banned_user_message")
        .and_then(|value| value.as_str())
        .map(ToOwned::to_owned)
}

/// Resolve an awaited application message from the actual stored user entity.
pub(crate) async fn resolve_admin_banned_user_message<S: alibi_core::AuthSchema>(
    ctx: &AuthContext<S>,
    user: &impl AuthUser,
) -> AuthResult<String> {
    if let Some(policy) = ctx
        .extensions
        .get::<crate::admin::BannedUserMessagePolicy>()
    {
        return policy.message(user).await;
    }
    Ok(admin_banned_user_message(ctx).unwrap_or_else(|| {
        "You have been banned from this application. Please contact support if you believe this is an error.".to_owned()
    }))
}

/// Issue a session for the given user, applying admin-plugin ban semantics
/// when the admin plugin is enabled.
///
/// # Errors
///
/// Returns an error if session hooks reject issuance, the user is banned, or storage fails.
pub async fn issue_user_session<S: alibi_core::AuthSchema>(
    ctx: &AuthContext<S>,
    user_id: &str,
    ip_address: Option<String>,
    user_agent: Option<String>,
) -> Result<IssuedSession<S>, SessionIssueError> {
    issue_user_session_record(ctx, user_id, ip_address, user_agent)
        .await
        .map(IssuedSessionRecord::into_stored)
}

/// Issue and publish genuine retained adapter results.
///
/// # Errors
/// Propagates validation, storage and configured callback errors.
pub async fn issue_user_session_record<S: alibi_core::AuthSchema>(
    ctx: &AuthContext<S>,
    user_id: &str,
    ip_address: Option<String>,
    user_agent: Option<String>,
) -> Result<IssuedSessionRecord<S>, SessionIssueError> {
    issue_user_session_inner(ctx, user_id, ip_address, user_agent, None, true, None).await
}

/// Issue a session with trusted fields applied during its initial insert.
///
/// Configured field policies and creation hooks receive the supplied fields;
/// issuance publishes the resulting row through the normal cache and completion
/// path. No follow-up session update is performed. Admin ban policy still applies.
///
/// # Errors
/// Returns an error if hooks reject issuance, the user is banned, or storage fails.
pub async fn issue_user_session_with_fields<S: alibi_core::AuthSchema>(
    ctx: &AuthContext<S>,
    user_id: &str,
    ip_address: Option<String>,
    user_agent: Option<String>,
    fields: SessionOverrides,
) -> Result<IssuedSession<S>, SessionIssueError> {
    issue_user_session_with_fields_record(ctx, user_id, ip_address, user_agent, fields)
        .await
        .map(IssuedSessionRecord::into_stored)
}

/// Issue a session with trusted initial fields and retain its adapter output.
///
/// This is the retained-record counterpart to [`issue_user_session_with_fields`].
///
/// # Errors
/// Propagates validation, storage, ban policy, and configured callback errors.
pub async fn issue_user_session_with_fields_record<S: alibi_core::AuthSchema>(
    ctx: &AuthContext<S>,
    user_id: &str,
    ip_address: Option<String>,
    user_agent: Option<String>,
    fields: SessionOverrides,
) -> Result<IssuedSessionRecord<S>, SessionIssueError> {
    // Additional fields use schema bindings, so reject names that could replace
    // issuance-owned columns rather than trusting the caller's map shape.
    if let Some(name) = fields.additional_fields.keys().find(|name| {
        matches!(
            name.as_str(),
            "id" | "token"
                | "userId"
                | "user_id"
                | "expiresAt"
                | "expires_at"
                | "createdAt"
                | "created_at"
                | "updatedAt"
                | "updated_at"
        )
    }) {
        return Err(
            AuthError::bad_request(format!("Session issuance owns the {name} field")).into(),
        );
    }
    issue_user_session_inner(
        ctx,
        user_id,
        ip_address,
        user_agent,
        Some(fields),
        true,
        None,
    )
    .await
}

/// Create a genuine session for an endpoint that publishes it after later
/// persistence and application callbacks have completed.
pub(crate) async fn create_user_session_record<S: alibi_core::AuthSchema>(
    ctx: &AuthContext<S>,
    user_id: &str,
    ip_address: Option<String>,
    user_agent: Option<String>,
) -> Result<IssuedSessionRecord<S>, SessionIssueError> {
    issue_user_session_inner(ctx, user_id, ip_address, user_agent, None, false, None).await
}

/// Issue a replacement session while preserving trusted session extension fields.
///
/// # Errors
///
/// Returns an error if session hooks reject issuance, the user is banned, or storage fails.
pub async fn issue_user_session_with_overrides<S: alibi_core::AuthSchema>(
    ctx: &AuthContext<S>,
    user_id: &str,
    ip_address: Option<String>,
    user_agent: Option<String>,
    current_session: &impl alibi_core::AuthSession,
) -> Result<IssuedSession<S>, SessionIssueError> {
    issue_user_session_with_overrides_record(ctx, user_id, ip_address, user_agent, current_session)
        .await
        .map(IssuedSessionRecord::into_stored)
}

/// Issue replacement records with actual retained output.
///
/// # Errors
/// Propagates persistence or configured callback errors.
pub async fn issue_user_session_with_overrides_record<S: alibi_core::AuthSchema>(
    ctx: &AuthContext<S>,
    user_id: &str,
    ip_address: Option<String>,
    user_agent: Option<String>,
    current_session: &impl alibi_core::AuthSession,
) -> Result<IssuedSessionRecord<S>, SessionIssueError> {
    let overrides = SessionOverrides {
        additional_fields: current_session
            .additional_fields()
            .into_iter()
            .map(|(name, value)| (name, alibi_core::utils::json::JsValue::from(value)))
            .collect(),
        impersonated_by: current_session.impersonated_by().map(str::to_owned),
        active_organization_id: current_session.active_organization_id().map(str::to_owned),
        active_team_id: current_session.active_team_id().map(str::to_owned),
    };
    issue_user_session_inner(
        ctx,
        user_id,
        ip_address,
        user_agent,
        Some(overrides),
        true,
        None,
    )
    .await
}

/// Issue from the actual lookup already used to authenticate this user. This
/// retains its callback snapshot without repeating adapter output callbacks.
pub(crate) async fn issue_selected_user_session_record<S: alibi_core::AuthSchema>(
    ctx: &AuthContext<S>,
    user: alibi_core::AdapterRecord<S::User>,
    ip_address: Option<String>,
    user_agent: Option<String>,
) -> Result<IssuedSessionRecord<S>, SessionIssueError> {
    let id = user.id().into_owned();
    issue_user_session_inner(ctx, &id, ip_address, user_agent, None, true, Some(user)).await
}

pub(in crate::helpers) async fn issue_user_session_inner<S: alibi_core::AuthSchema>(
    ctx: &AuthContext<S>,
    user_id: &str,
    ip_address: Option<String>,
    user_agent: Option<String>,
    overrides: Option<SessionOverrides>,
    publish: bool,
    selected: Option<alibi_core::AdapterRecord<S::User>>,
) -> Result<IssuedSessionRecord<S>, SessionIssueError> {
    let user = match selected {
        Some(user) => user,
        None => ctx
            .database
            .get_user_by_id_record(user_id)
            .await?
            .ok_or(AuthError::UserNotFound)?,
    };

    if admin_plugin_enabled(ctx) && user.banned() {
        if user
            .ban_expires()
            .is_some_and(|expires| expires < Utc::now())
        {
            _ = ctx
                .database
                .update_user_record(
                    user_id,
                    UpdateUser {
                        banned: Some(false),
                        ban_reason: None,
                        ban_expires: None,
                        ..Default::default()
                    },
                )
                .await?;
        } else {
            return Err(SessionIssueError::Banned {
                message: resolve_admin_banned_user_message(ctx, &user).await?,
            });
        }
    }

    let session = match overrides {
        None => {
            ctx.session_manager()
                .create_session_record(&user, ip_address, user_agent)
                .await?
        }
        Some(overrides) => {
            ctx.database
                .create_session_record(alibi_core::CreateSession {
                    additional_fields: overrides.additional_fields,
                    token: None,
                    user_id: user.id().to_string(),
                    expires_at: Utc::now() + ctx.config.session.expires_in,
                    ip_address,
                    user_agent,
                    impersonated_by: overrides.impersonated_by,
                    active_organization_id: overrides.active_organization_id,
                    active_team_id: overrides.active_team_id,
                })
                .await?
        }
    };

    if publish {
        alibi_core::session::cookie_cache::runtime::emit_issuance(ctx, &user, &session).await?;
        record_completed_session_record::<S>(&user, &session);
    }
    Ok(IssuedSessionRecord { user, session })
}

/// Parse a cookie value from the request's `Cookie` header.
#[must_use]
pub fn get_cookie(req: &impl alibi_core::session::SessionRequest, name: &str) -> Option<String> {
    let header = req.session_headers().get("cookie")?;
    header.split(';').find_map(|cookie| {
        let trimmed = cookie.trim();
        let (cookie_name, cookie_value) = trimmed.split_once('=')?;
        (cookie_name == name).then_some(cookie_value.to_owned())
    })
}

/// TS-style cookie clearing used by `deleteSessionCookie`.
pub fn delete_session_cookie_headers(config: &alibi_core::AuthConfig) -> AuthResult<Vec<String>> {
    alibi_core::utils::cookie_utils::delete_session_cookie_headers(config)
}