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
//! This crate implements middleware to authenticate requests to [axum]. Overall
//! the aim is to provide simple, passwordless authentication for secure network
//! communication. A session key is stored in a cookie and signed with a secret
//! (using crypto implementations in [the `tower-cookies`
//! crate](https://crates.io/crates/tower-cookies)). Due to the signature, the
//! session key cannot be modified. Aside from storing the secret, the system is
//! stateless, requiring no storage on the server.
//!
//! In the normal case, a token is provided out-of-band to the user. For
//! example, the user will start the server from an SSH session and copy the
//! token to their browser. Alternatively, if the connection is defined as
//! trusted (e.g. if it is a loopback connection), authentication occurs without
//! any check.
//!
//! This is useful in cases where a user launches a server process and wants to
//! achieve network-based control of the server without the server exposing this
//! functionality to unauthenticated network connections. In this scenario, if
//! the user provides the correct token in the URL upon initial connection, the
//! server sets a cookie in the user's browser and subsequent requests are
//! automatically validated with no further token in the URL.
//!
//! The user does not need an account, Passkey, OpenID Connect (OIDC), OAuth,
//! OAuth2, FIDO U2F, FIDO2, WebAuthn, SAML, LDAP, Kerberos, RADIUS, or SSO
//! credentials. The developer also does not need to configure these services.
//! Rather, the user uses a URL with the correct token in the query parameters
//! when initially connecting to the server.
//!
//! # Typical flow
//!
//! 1. A user starts or connects to a server and the user is shown an initial
//!    authentication token (e.g. the server prints a URL including the token in
//!    the console).
//! 2. The user connects via a browser to the server. In the first HTTP request
//!    from the user, the token is included in the query parameter in the URL.
//! 3. A new [SessionKey] is included as a new cookie in the HTTP response to
//!    the user. The request is further processed by the next service with
//!    session key information being made available.
//! 4. Subsequent requests from the user browser include the newly set cookie
//!    (and no longer include the token in the URL) and the middleware makes the
//!    session key information available to the next service.
//!
//! # Trusted connection flow
//!
//! In case of a trusted connection, no token is required for initial
//! authentication. The session key is still issued as above.
//!
//! # Cookie expiration
//!
//! The cookies stored on the clients' browser can be persisted (the response
//! sets a cookie with an `Expires` field) or they can be "session cookies". If
//! the `expires` field in [AuthConfig] is set to `None`, a successful auth will
//! set a "session cookie", meaning the cookie does not contain an `Expires` or
//! `Max-age` key. Otherwise, when the `expires` field is set, browsers will
//! store the cookie persistently. (Note that this expiry information cannot be
//! used for security purposes as it is entirely controlled by the clients'
//! browser.)
//!
//! # Session Key expiration
//!
//! The signature on the cookies containing session keys is valid until the
//! persistent secret is changed. (If you need to invalidate keys, switch the
//! persistent secret or use a more full-featured authentication middleware.)
//!
//! # For more extensive needs
//!
//! If this crate does not meet your needs, check
//! [`axum-login`](https://crates.io/crates/axum-login).
#![forbid(unsafe_code)]
#![deny(missing_docs)]

use axum::{
    async_trait,
    extract::{FromRequestParts, Request},
    http::{request::Parts, StatusCode},
    response::Response,
    BoxError,
};

use cookie::{
    time::{Duration, OffsetDateTime},
    Key,
};
use futures_util::future::BoxFuture;
use std::task::{Context, Poll};
use tower_layer::Layer;
use tower_service::Service;

/// One or more validation errors
#[derive(thiserror::Error, Debug)]
#[error("one or more validation errors")]
pub struct ValidationErrors(Vec<String>);

impl ValidationErrors {
    /// Return an iterator over the validation errors that ocurred
    pub fn errors(&self) -> impl Iterator<Item = &str> {
        self.0.iter().map(String::as_str)
    }
}

/// Identifier for each session (one per client browser).
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct SessionKey(pub uuid::Uuid);

impl Default for SessionKey {
    fn default() -> Self {
        SessionKey(uuid::Uuid::new_v4())
    }
}

/// Configuration for URI query parameters to implement token-based
/// authentication.
#[derive(Clone, Debug)]
pub struct TokenConfig {
    /// The key of the token in the URI query parameters.
    pub name: String,
    /// The value of the access-granting token in the URI query parameters.
    ///
    /// This is a secret shared with users (which should therefore be
    /// short-lived).
    pub value: String,
}

impl TokenConfig {
    /// Generate a new token value.
    pub fn new_token(name: &str) -> Self {
        Self {
            name: name.into(),
            value: format!("{}", uuid::Uuid::new_v4()),
        }
    }
    /// Parse token from URL query
    fn parse_token_from_uri_query(&self, req: &Request) -> bool {
        use std::borrow::Cow;

        let query = req.uri().query();
        let query_pairs = url::form_urlencoded::parse(query.unwrap_or("").as_bytes());
        for (key, value) in query_pairs {
            if key == Cow::Borrowed(&self.name) && self.value.as_str() == value {
                return true;
            }
        }
        false
    }
}

/// Configuration for [AuthLayer] and [AuthMiddleware].
#[derive(Clone, Debug)]
pub struct AuthConfig<'a> {
    /// The cookie name
    ///
    /// This is the name of the cookie stored in the clients' browsers.
    pub cookie_name: &'a str,
    /// A long lived secret used to sign cookies set to the users.
    ///
    /// The secret is not shared with users.
    ///
    /// All issued session keys are valid as long as the persistent secret is
    /// unchanged. There is no mechanism to invalidate individual sessions.
    pub persistent_secret: Key,
    /// The authentication token value and its configuration.
    ///
    /// Set to `None` if the entire connection is trusted (e.g. it is on a
    /// loopback interface). In this case, token checking is disabled but still
    /// [SessionKey] is still provided by [AuthMiddleware].
    pub token_config: Option<TokenConfig>,
    /// If set, the newly set cookie has an Expires field which corresponds the
    /// value set in `expires`. If not set, the cookie does not have an
    /// "Expires" (or "Max-Age") field and consequently is typically stored as a
    /// "session cookie" and thus saved only until the browser quits.
    ///
    /// Note that this does *not* limit the validity duration of the session
    /// key. All issued session keys are valid as long as the persistent secret
    /// is unchanged. There is no mechanism to invalidate individual sessions.
    pub cookie_expires: Option<std::time::Duration>,
}

impl<'a> Default for AuthConfig<'a> {
    fn default() -> Self {
        Self {
            cookie_name: env!["CARGO_PKG_NAME"],
            persistent_secret: Key::generate(),
            token_config: None,
            cookie_expires: None,
        }
    }
}

impl<'a> AuthConfig<'a> {
    /// Convert [Self] to an [AuthLayer].
    pub fn into_layer(self) -> AuthLayer {
        let access_info = AccessInfo::new(self);
        AuthLayer { access_info }
    }
}

#[derive(Clone)]
struct AccessInfo {
    cookie_name: String,
    token_config: Option<TokenConfig>,
    cookie_expires: Option<std::time::Duration>,
    key: tower_cookies::Key,
}

impl AccessInfo {
    /// Generate a random token if needed and return access control information.
    fn new(cfg: AuthConfig<'_>) -> Self {
        let AuthConfig {
            cookie_name,
            persistent_secret,
            token_config,
            cookie_expires,
        } = cfg;

        let key = persistent_secret;

        Self {
            cookie_name: cookie_name.into(),
            token_config,
            key,
            cookie_expires,
        }
    }

    fn check_token_and_cookie(
        &self,
        req: &Request,
        valid_session_key: Option<SessionKey>,
    ) -> Result<(bool, SessionKey), ValidationErrors> {
        let mut errors = Vec::new();

        // First check for token in URI.
        let has_valid_token = self
            .token_config
            .as_ref()
            .map(|i| i.parse_token_from_uri_query(req))
            .unwrap_or(true);

        match (has_valid_token, valid_session_key) {
            (false, None) => {
                errors.push("No (valid) token in uri and no (valid) session.".into());
                Err(ValidationErrors(errors))
            }
            (true, None) => Ok((true, SessionKey::default())),
            (_has_valid_token, Some(session_key)) => Ok((false, session_key)),
        }
    }
}

#[async_trait]
impl<S> FromRequestParts<S> for SessionKey
where
    S: Send + Sync,
{
    type Rejection = (StatusCode, &'static str);

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        if let Some(session_key) = parts.extensions.remove::<SessionKey>() {
            Ok(session_key.clone())
        } else {
            Err((StatusCode::UNAUTHORIZED, "(valid) session key is missing"))
        }
    }
}

/// Implements [Layer] for [AuthMiddleware]
///
/// See the crate-level documentation for an overview.
#[derive(Clone)]
pub struct AuthLayer {
    access_info: AccessInfo,
}

impl<S> Layer<S> for AuthLayer {
    type Service = tower_cookies::CookieManager<AuthMiddleware<S>>;

    fn layer(&self, inner: S) -> Self::Service {
        let auth_middleware = AuthMiddleware {
            inner,
            access_info: self.access_info.clone(),
        };
        tower_cookies::CookieManager::new(auth_middleware)
    }
}

/// Middleware which checks if request is authenticated and, if so, extends the
/// request to include [SessionKey] information.
#[derive(Clone)]
pub struct AuthMiddleware<S> {
    inner: S,
    access_info: AccessInfo,
}

impl<S> Service<Request> for AuthMiddleware<S>
where
    S: Service<Request, Response = Response> + Send + 'static,
    S::Error: Into<BoxError>,
    S::Future: Send + 'static,
{
    type Response = S::Response;
    type Error = BoxError;
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        match self.inner.poll_ready(cx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(r) => Poll::Ready(r.map_err(Into::into)),
        }
    }

    fn call(&mut self, mut request: Request) -> Self::Future {
        let Some(cookies) = request
            .extensions()
            .get::<tower_cookies::Cookies>()
            .cloned()
        else {
            // In practice this should never happen because we wrap `CookieManager`
            // directly.
            tracing::error!("missing cookies request extension");
            return Box::pin(std::future::ready(Err(Box::new(ValidationErrors(vec![
                "missing cookies request extension".into(),
            ])) as BoxError)));
        };
        let signed = cookies.signed(&self.access_info.key);

        let err_info = {
            let opt_session_key: Option<SessionKey> = signed
                .get(&self.access_info.cookie_name)
                .map(|received_cookie| {
                    SessionKey(uuid::Uuid::parse_str(received_cookie.value()).unwrap())
                });

            // check if authenticated
            match self
                .access_info
                .check_token_and_cookie(&request, opt_session_key)
            {
                Ok((new_cookie_value, session_key)) => {
                    let expires = self.access_info.cookie_expires.as_ref().map(|exp| {
                        OffsetDateTime::now_utc()
                            .checked_add(Duration::try_from(*exp).unwrap())
                            .unwrap()
                    });

                    request.extensions_mut().insert(session_key.clone());
                    if new_cookie_value {
                        let value = format!("{}", session_key.0.as_hyphenated());
                        let mut set_cookie =
                            tower_cookies::Cookie::new(self.access_info.cookie_name.clone(), value);

                        if let Some(expires) = expires {
                            set_cookie.set_expires(expires);
                        }

                        signed.add(set_cookie);
                    }
                    None
                }
                Err(val_err) => Some(val_err),
            }
        };

        // Build future which generates response.
        let fut = match err_info {
            None => self.inner.call(request),
            Some(val_err) => {
                return Box::pin(std::future::ready(Err(val_err.into())));
            }
        };

        // Await future.
        Box::pin(async move {
            let response: Response = fut.await.map_err(|e| e.into())?;
            Ok(response)
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Result;
    use axum::body::Body;
    use cookie::Cookie;
    use http::{Request, StatusCode};

    use std::convert::Infallible;
    use tower::{ServiceBuilder, ServiceExt};

    async fn handler(_: Request<Body>) -> std::result::Result<Response<Body>, Infallible> {
        Ok(Response::new(Body::empty()))
    }

    fn get_cfg() -> AuthConfig<'static> {
        let token_config = Some(TokenConfig {
            name: "token".into(),
            value: "token_value".into(),
        });
        AuthConfig {
            cookie_name: "auth",
            persistent_secret: Key::generate(),
            token_config,
            cookie_expires: None,
        }
    }

    #[tokio::test]
    async fn fail_without_token_or_cookie() -> Result<()> {
        let auth_layer = get_cfg().into_layer();
        let svc = ServiceBuilder::new().layer(auth_layer).service_fn(handler);

        let req = Request::builder().body(Body::empty())?;
        let res = svc.oneshot(req).await;
        assert!(!res
            .err()
            .unwrap()
            .downcast::<ValidationErrors>()
            .unwrap()
            .errors()
            .collect::<Vec<_>>()
            .is_empty());
        Ok(())
    }

    async fn get_second_response(
        cfg: AuthConfig<'_>,
        req: Request<Body>,
    ) -> Result<Response<Body>> {
        let auth_layer = cfg.into_layer();
        let svc = ServiceBuilder::new().layer(auth_layer).service_fn(handler);

        // Make a request to get the cookie.
        let res = svc.clone().oneshot(req).await.unwrap();

        // Extract the cookie
        let cookie = {
            let set_cookie: Vec<_> = res
                .headers()
                .get_all(http::header::SET_COOKIE)
                .iter()
                .collect();
            assert_eq!(set_cookie.len(), 1);
            Cookie::parse(set_cookie[0].to_str()?.to_string())?
        };

        // Now make a new request with the cookie.
        let req2 = Request::builder()
            .header(http::header::COOKIE, cookie.stripped().to_string())
            .body(Body::empty())
            .unwrap();
        let res2 = svc.oneshot(req2).await.unwrap();
        Ok(res2)
    }

    #[tokio::test]
    async fn set_cookie_with_trusted_socket() -> Result<()> {
        let mut cfg = get_cfg();
        cfg.token_config = None;
        let uri = "http://example.com/path";
        let req = Request::builder().uri(uri).body(Body::empty()).unwrap();

        let res2 = get_second_response(cfg, req).await?;
        assert_eq!(res2.status(), StatusCode::OK);
        Ok(())
    }

    #[tokio::test]
    async fn set_cookie_with_valid_token() -> Result<()> {
        let cfg = get_cfg();
        let uri = {
            let x = cfg.token_config.as_ref().unwrap();
            format!("http://example.com/path?{}={}", x.name, x.value)
        };
        let req = Request::builder().uri(uri).body(Body::empty()).unwrap();

        let res2 = get_second_response(cfg, req).await?;

        assert_eq!(res2.status(), StatusCode::OK);
        Ok(())
    }
}