seamless-auth 0.2.1

Seamless Auth server adapter for Axum: cookie and bearer sessions in front of the Seamless Auth API
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
use std::collections::HashSet;
use std::fmt;
use std::net::SocketAddr;
use std::sync::Arc;

use axum::Router;
use axum::body::{Body, Bytes};
use axum::extract::{ConnectInfo, Request};
use axum::http::header::{CACHE_CONTROL, CONTENT_DISPOSITION, CONTENT_TYPE, SET_COOKIE};
use axum::http::{HeaderMap, HeaderValue, Method, StatusCode, request::Parts};
use axum::response::Response;
use axum::routing::any;
use http_body_util::{BodyExt, LengthLimitError, Limited};
use serde_json::{Value, json};

use crate::client_ip::{canonical, client_user_agent};
use crate::cookies::{CookieWrite, clear_cookie, set_cookie};
use crate::jwks::JwksCache;
use crate::manifest::ManifestSource;
use crate::options::{Builder, Config};
use crate::origin::check_origin;
use crate::refresh::Refresher;
use crate::service_token::ServiceTokens;

const TRANSPORT_HEADER: &str = "x-seamless-auth-transport";
pub(crate) const DELIVERY_MODE_HEADER: &str = "x-seamless-auth-delivery-mode";
pub(crate) const MAX_BODY_BYTES: usize = 1 << 20;

/// Serves the Seamless Auth routes and guards the application's own.
///
/// Cheap to clone: clones share the manifest, key set and refresh state.
#[derive(Clone)]
pub struct Adapter {
    pub(crate) inner: Arc<Inner>,
}

impl fmt::Debug for Adapter {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Adapter")
            .field("auth_server_url", &self.inner.config.auth_server_url)
            .finish_non_exhaustive()
    }
}

pub(crate) struct Inner {
    pub config: Config,
    pub jwks: JwksCache,
    pub manifest: ManifestSource,
    pub tokens: ServiceTokens,
    pub refreshes: Arc<Refresher>,
    pub console_client: std::sync::OnceLock<Option<reqwest::Client>>,
}

impl Adapter {
    /// Starts configuring an adapter for the auth API at `auth_server_url`.
    pub fn builder(auth_server_url: impl Into<String>) -> Builder {
        Builder::new(auth_server_url)
    }

    pub(crate) fn from_config(config: Config) -> Self {
        let jwks = JwksCache::new(&config.auth_server_url, config.http_client.clone());
        let manifest = ManifestSource::new(
            &config.auth_server_url,
            config.http_client.clone(),
            config.fetch_manifest,
        );
        let tokens = ServiceTokens::new(config.service_secret.clone(), config.jwks_kid.clone());
        Adapter {
            inner: Arc::new(Inner {
                config,
                jwks,
                manifest,
                tokens,
                refreshes: Arc::default(),
                console_client: std::sync::OnceLock::new(),
            }),
        }
    }

    /// The auth routes, relative to wherever they are nested. Nest them at
    /// `/auth`, which is where the client SDKs call:
    /// `Router::new().nest("/auth", auth.router())`.
    pub fn router<S>(&self) -> Router<S>
    where
        S: Clone + Send + Sync + 'static,
    {
        let inner = self.inner.clone();
        let handler = move |req: Request| {
            let inner = inner.clone();
            async move { inner.serve(req).await }
        };
        Router::new()
            .route("/", any(handler.clone()))
            .route("/{*path}", any(handler))
    }
}

/// What a route answers, applied to the response in one place.
pub(crate) struct Reply {
    pub status: StatusCode,
    pub body: Option<Value>,
    pub raw: Option<reqwest::Response>,
    pub set: Vec<CookieWrite>,
    pub clear: Vec<String>,
}

impl Reply {
    pub fn new(status: StatusCode, body: Option<Value>) -> Self {
        Reply {
            status,
            body,
            raw: None,
            set: Vec::new(),
            clear: Vec::new(),
        }
    }

    pub fn error(status: StatusCode, code: &str) -> Self {
        Reply::new(status, Some(json!({ "error": code })))
    }

    pub fn with_set(mut self, set: Vec<CookieWrite>) -> Self {
        self.set = set;
        self
    }

    pub fn with_clear(mut self, clear: Vec<String>) -> Self {
        self.clear = clear;
        self
    }
}

/// The end user's address and user agent, forwarded to the auth API.
#[derive(Clone, Default)]
pub(crate) struct Forwarded {
    pub ip: Option<String>,
    pub user_agent: Option<Vec<u8>>,
}

/// One request to the auth API.
pub(crate) struct UpstreamCall {
    pub method: Method,
    pub path: String,
    pub query: Option<String>,
    pub body: Option<Bytes>,
    pub authorization: Option<String>,
    pub service: Option<String>,
    pub external_delivery: bool,
}

impl UpstreamCall {
    pub fn new(method: Method, path: impl Into<String>) -> Self {
        UpstreamCall {
            method,
            path: path.into(),
            query: None,
            body: None,
            authorization: None,
            service: None,
            external_delivery: false,
        }
    }
}

pub(crate) fn json_response(status: StatusCode, body: &Value) -> Response {
    let mut res = Response::new(Body::from(body.to_string()));
    *res.status_mut() = status;
    res.headers_mut().insert(
        CONTENT_TYPE,
        HeaderValue::from_static("application/json; charset=utf-8"),
    );
    res
}

impl Inner {
    async fn serve(self: Arc<Self>, req: Request) -> Response {
        let (parts, body) = req.into_parts();

        if let Some(blocked) = check_origin(&self.config, &parts.method, &parts.headers) {
            return self.write(blocked, false);
        }

        let bearer = parts
            .headers
            .get(TRANSPORT_HEADER)
            .is_some_and(|v| v.as_bytes().eq_ignore_ascii_case(b"bearer"));
        let forwarded = self.forwarded(&parts);
        let path = parts.uri.path();
        // Matched on segments, as manifest routes are, so `/logout/` and `//logout`
        // are still logout and never fall through to a route that clears less.
        let special = path
            .split('/')
            .filter(|s| !s.is_empty())
            .collect::<Vec<_>>()
            .join("/")
            .to_ascii_lowercase();

        let reply = if parts.method == Method::POST && special == "refresh" {
            self.refresh_route(&parts.headers, &forwarded, bearer).await
        } else if parts.method == Method::DELETE && special == "logout" {
            self.logout_route(&parts.headers, &forwarded, bearer, "/logout")
                .await
        } else if parts.method == Method::DELETE && special == "logout/all" {
            self.logout_route(&parts.headers, &forwarded, bearer, "/logout/all")
                .await
        } else {
            let manifest = self.manifest.get().await;
            match manifest.find(parts.method.as_str(), path) {
                Some(found) => {
                    self.manifest_route(&parts, body, &forwarded, found, bearer)
                        .await
                }
                None => Reply::error(StatusCode::NOT_FOUND, "not_found"),
            }
        };

        self.write(reply, bearer)
    }

    fn forwarded(&self, parts: &Parts) -> Forwarded {
        let peer = parts
            .extensions
            .get::<ConnectInfo<SocketAddr>>()
            .map(|ConnectInfo(addr)| addr.ip());
        let ip = match &self.config.client_ip {
            Some(resolve) => resolve(&parts.headers, peer),
            None => peer,
        };
        Forwarded {
            ip: ip.map(canonical),
            user_agent: client_user_agent(&parts.headers),
        }
    }

    /// Applies a reply. Clears go before sets, because a reply that does both is
    /// replacing a session rather than ending one. Bearer transport writes no
    /// cookies at all.
    fn write(&self, reply: Reply, bearer: bool) -> Response {
        let mut cookies = HeaderMap::new();
        if !bearer {
            let mut seen = HashSet::new();
            for name in reply.clear.iter().filter(|n| seen.insert(n.as_str())) {
                if let Some(value) = clear_cookie(&self.config, name) {
                    cookies.append(SET_COOKIE, value);
                }
            }
            for cookie in &reply.set {
                match set_cookie(&self.config, cookie) {
                    Ok(value) => {
                        cookies.append(SET_COOKIE, value);
                    }
                    Err(err) => {
                        tracing::error!(
                            "[seamless-auth] Could not issue cookie {}: {err}",
                            cookie.name
                        );
                        return json_response(
                            StatusCode::INTERNAL_SERVER_ERROR,
                            &json!({ "error": "internal_error" }),
                        );
                    }
                }
            }
        }

        let mut res = if let Some(raw) = reply.raw {
            let mut headers = HeaderMap::new();
            for name in [CONTENT_TYPE, CONTENT_DISPOSITION, CACHE_CONTROL] {
                if let Some(value) = raw.headers().get(&name) {
                    headers.insert(name, value.clone());
                }
            }
            let mut res = Response::new(Body::new(reqwest::Body::from(raw)));
            *res.status_mut() = reply.status;
            res.headers_mut().extend(headers);
            res
        } else if let Some(body) = &reply.body {
            json_response(reply.status, body)
        } else {
            let mut res = Response::new(Body::empty());
            *res.status_mut() = reply.status;
            res
        };

        for value in cookies.get_all(SET_COOKIE) {
            res.headers_mut().append(SET_COOKIE, value.clone());
        }
        res
    }

    pub(crate) async fn call(
        &self,
        forwarded: &Forwarded,
        call: UpstreamCall,
    ) -> Result<reqwest::Response, reqwest::Error> {
        let mut url = format!("{}{}", self.config.auth_server_url, call.path);
        if let Some(query) = call.query.as_deref().filter(|q| !q.is_empty()) {
            url.push('?');
            url.push_str(query);
        }

        let mut req = self
            .config
            .http_client
            .request(call.method.clone(), url)
            .header("accept", "application/json");
        if let Some(body) = call.body.filter(|_| call.method != Method::GET) {
            req = req.header("content-type", "application/json").body(body);
        }
        if let Some(authorization) = call.authorization {
            req = req.header("authorization", authorization);
        }
        if let Some(service) = call.service {
            req = req.header("x-seamless-service-token", service);
        }
        if let Some(ip) = &forwarded.ip {
            req = req.header("x-seamless-client-ip", ip.as_str());
        }
        if let Some(ua) = forwarded
            .user_agent
            .as_ref()
            .and_then(|ua| HeaderValue::from_bytes(ua).ok())
        {
            req = req.header("x-seamless-client-user-agent", ua);
        }
        if call.external_delivery {
            req = req.header(DELIVERY_MODE_HEADER, "external");
        }
        req.send().await
    }
}

/// Reads at most `max` bytes of a response body, refusing a larger one.
pub(crate) async fn read_limited(
    mut res: reqwest::Response,
    max: usize,
) -> Result<Vec<u8>, String> {
    if res.content_length().is_some_and(|len| len > max as u64) {
        return Err("response body too large".into());
    }
    let mut out = Vec::new();
    while let Some(chunk) = res.chunk().await.map_err(|e| e.to_string())? {
        if out.len() + chunk.len() > max {
            return Err("response body too large".into());
        }
        out.extend_from_slice(&chunk);
    }
    Ok(out)
}

/// Reads a JSON response body. An empty body is `None`; a body that is not JSON
/// reports false.
pub(crate) async fn read_json(res: reqwest::Response) -> (Option<Value>, bool) {
    let Ok(data) = read_limited(res, MAX_BODY_BYTES).await else {
        return (None, false);
    };
    if data.trim_ascii().is_empty() {
        return (None, true);
    }
    match serde_json::from_slice::<Value>(&data) {
        Ok(Value::Null) => (None, true),
        Ok(value) => (Some(value), true),
        Err(_) => (None, false),
    }
}

/// Passes an upstream error through. Callers read fields off the auth API's own
/// error body, so an object goes out as-is; anything else becomes a code they can
/// still branch on.
pub(crate) fn failure(status: StatusCode, body: Option<Value>, is_json: bool) -> Reply {
    match body {
        Some(Value::Object(obj)) if is_json => Reply::new(status, Some(Value::Object(obj))),
        _ => Reply::error(status, "upstream_error"),
    }
}

pub(crate) enum BodyError {
    TooLarge,
    Unreadable,
}

pub(crate) async fn read_request_body(body: Body) -> Result<Option<Bytes>, BodyError> {
    match Limited::new(body, MAX_BODY_BYTES).collect().await {
        Ok(collected) => {
            let bytes = collected.to_bytes();
            Ok((!bytes.trim_ascii().is_empty()).then_some(bytes))
        }
        Err(err) if err.downcast_ref::<LengthLimitError>().is_some() => Err(BodyError::TooLarge),
        Err(_) => Err(BodyError::Unreadable),
    }
}

/// `application/json` or a `+json` type. A download such as
/// `application/x-ndjson` is not: parsing it would keep only its first line.
pub(crate) fn is_json_content_type(content_type: Option<&HeaderValue>) -> bool {
    let Some(value) = content_type else {
        return true;
    };
    let Ok(value) = value.to_str() else {
        return false;
    };
    if value.trim().is_empty() {
        return true;
    }
    let media_type = value
        .split(';')
        .next()
        .unwrap_or("")
        .trim()
        .to_ascii_lowercase();
    let Some((kind, subtype)) = media_type.split_once('/') else {
        return false;
    };
    let token = |s: &str| {
        !s.is_empty()
            && s.bytes()
                .all(|b| b.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&b))
    };
    if !token(kind) || !token(subtype) {
        return false;
    }
    media_type == "application/json" || subtype.ends_with("+json")
}

/// The token in an `Authorization: Bearer` header.
pub(crate) fn bearer_token(headers: &HeaderMap) -> Option<&str> {
    let header = headers.get("authorization")?.to_str().ok()?;
    if header.len() > 7 && header[..7].eq_ignore_ascii_case("bearer ") {
        let token = header[7..].trim();
        return (!token.is_empty()).then_some(token);
    }
    None
}