Skip to content

Commit 380c8cd

Browse files
authored
Fixed actix tutorial (#438)
* Removed rust version in tutorials * Split tutorials from ci build + included actix_web * Code cleanup & dependency upgrade
1 parent 628599a commit 380c8cd

File tree

9 files changed

+68
-60
lines changed

9 files changed

+68
-60
lines changed

.github/workflows/ci.yml

+36-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
on: [push, pull_request]
1+
on: [ push, pull_request ]
22

33
name: CI
44

@@ -29,7 +29,7 @@ jobs:
2929
strategy:
3030
fail-fast: false
3131
matrix:
32-
rust_version: [stable, 1.70.0]
32+
rust_version: [ stable, 1.70.0 ]
3333
os:
3434
- ubuntu-latest
3535
- windows-latest
@@ -58,13 +58,13 @@ jobs:
5858
token: ${{ github.token }}
5959
# Transitive dependency of wasm-bindgen; works around https://github.com/rustwasm/wasm-bindgen/issues/3918
6060
- run: cargo update -p bumpalo --precise 3.14.0
61-
- run: cargo build --workspace --exclude webauthn-authenticator-rs
61+
- run: cargo build --workspace --exclude webauthn-authenticator-rs --exclude actix_web --exclude web_authn --exclude tide-server
6262

6363
# Don't run clippy on Windows, we only need to run it on Linux
6464
- if: runner.os != 'windows'
65-
run: cargo clippy --no-deps --workspace --exclude webauthn-authenticator-rs --all-targets
65+
run: cargo clippy --no-deps --workspace --exclude webauthn-authenticator-rs --exclude actix_web --exclude web_authn --exclude tide-server --all-targets
6666

67-
- run: cargo test --workspace --exclude webauthn-authenticator-rs
67+
- run: cargo test --workspace --exclude webauthn-authenticator-rs --exclude actix_web --exclude web_authn --exclude tide-server
6868

6969
# Some clap errors manifest as panics at runtime. Running tools with
7070
# --help should be enough to find an issue.
@@ -85,7 +85,7 @@ jobs:
8585
strategy:
8686
fail-fast: false
8787
matrix:
88-
rust_version: [stable, 1.70.0]
88+
rust_version: [ stable, 1.70.0 ]
8989
features:
9090
- bluetooth
9191
- cable
@@ -186,7 +186,7 @@ jobs:
186186
strategy:
187187
fail-fast: false
188188
matrix:
189-
rust_version: [stable, nightly]
189+
rust_version: [ stable, nightly ]
190190
runs-on: ubuntu-latest
191191
steps:
192192
- uses: actions/checkout@v3
@@ -233,3 +233,32 @@ jobs:
233233
if-no-files-found: error
234234
retention-days: 14
235235
- run: ./.github/check_built_docs.sh
236+
237+
tutorial:
238+
name: Tutorial builds
239+
strategy:
240+
fail-fast: false
241+
matrix:
242+
rust_version: [ stable ]
243+
os:
244+
- ubuntu-latest
245+
- windows-latest
246+
247+
runs-on: ${{ matrix.os }}
248+
steps:
249+
- uses: actions/checkout@v3
250+
- uses: dtolnay/rust-toolchain@master
251+
with:
252+
toolchain: ${{ matrix.rust_version }}
253+
components: clippy
254+
- uses: mozilla-actions/[email protected]
255+
with:
256+
version: v0.4.2
257+
- if: runner.os == 'windows'
258+
uses: johnwason/vcpkg-action@v4
259+
with:
260+
pkgs: openssl
261+
triplet: x64-windows-static-md
262+
token: ${{ github.token }}
263+
264+
- run: cargo build -p actix_web -p web_authn -p tide-server

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ members = [
2222
# Tutorial / Example sites.
2323
"tutorial/server/tide",
2424
"tutorial/server/axum",
25-
# "tutorial/server/actix_web",
25+
"tutorial/server/actix_web",
2626
"tutorial/wasm",
2727
# Attestatation struct format
2828
"attestation-ca",

tutorial/server/actix_web/Cargo.toml

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@
22
name = "actix_web"
33
version = "0.1.0"
44
edition = "2021"
5-
rust-version = "1.70.0"
65
authors = ["Niklas Pfister <[email protected]>"]
76

87
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
98

109
[dependencies]
1110
# Webframework
12-
actix-web = { version = "~4.5.1" }
11+
actix-web = { version = ">=4.5.1" }
1312
# Session framework for actix-web
14-
actix-session = { version = "~0.7", features = ["cookie-session"] }
13+
actix-session = { version = "~0.9", features = ["cookie-session"] }
1514
# Async trait, anyhow, chrono, once_cell and rand are required for the implementation of a
1615
# server-side memory-backed session store.
1716
# Normally, you want to use a database / redis backend as session store, but for the simplicity of this
1817
# tutorial, we implement our own.
1918
async-trait = { version = "~0.1" }
20-
anyhow = { version = "~1.0" }
19+
anyhow = { version = "~1" }
2120
chrono = { version = "~0.4" }
22-
once_cell = { version = "~1.18" }
23-
rand.workspace = true
21+
once_cell = { version = ">=1.18" }
22+
rand = { workspace = true }
23+
24+
# Nicer error management
25+
thiserror = { version = "~1" }
2426

2527
# Serve static file. Used to serve wasm
2628
actix-files = { version = "~0.6" }

tutorial/server/actix_web/src/handler/mod.rs

+12-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use actix_session::{SessionGetError, SessionInsertError};
2-
use std::fmt::{Display, Formatter};
32

43
use actix_web::http::StatusCode;
4+
use thiserror::Error;
55
use webauthn_rs::prelude::WebauthnError;
66

77
pub(crate) mod auth;
@@ -16,42 +16,24 @@ type WebResult<T> = Result<T, Error>;
1616
/**
1717
Unified errors for simpler Responses
1818
*/
19-
#[derive(Debug)]
19+
#[derive(Debug, Error)]
2020
pub(crate) enum Error {
21+
#[error("Unknown webauthn error")]
2122
Unknown(WebauthnError),
22-
SessionGet(SessionGetError),
23-
SessionInsert(SessionInsertError),
23+
#[error("Corrupt session")]
24+
SessionGet(#[from] SessionGetError),
25+
#[error("Corrupt session")]
26+
SessionInsert(#[from] SessionInsertError),
27+
#[error("Corrupt session")]
2428
CorruptSession,
25-
BadRequest(WebauthnError),
29+
#[error("Bad request")]
30+
BadRequest(#[from] WebauthnError),
31+
#[error("User not found")]
2632
UserNotFound,
33+
#[error("User has no credentials")]
2734
UserHasNoCredentials,
2835
}
2936

30-
impl From<SessionGetError> for Error {
31-
fn from(value: SessionGetError) -> Self {
32-
Self::SessionGet(value)
33-
}
34-
}
35-
36-
impl From<SessionInsertError> for Error {
37-
fn from(value: SessionInsertError) -> Self {
38-
Self::SessionInsert(value)
39-
}
40-
}
41-
42-
impl Display for Error {
43-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
44-
match self {
45-
Error::Unknown(_) => write!(f, "Unknown webauthn error"),
46-
Error::SessionGet(_) | Error::SessionInsert(_) => write!(f, "Corrupt session"),
47-
Error::BadRequest(_) => write!(f, "Bad request"),
48-
Error::UserNotFound => write!(f, "User not found"),
49-
Error::UserHasNoCredentials => write!(f, "User has no credentials"),
50-
Error::CorruptSession => write!(f, "Corrupt session"),
51-
}
52-
}
53-
}
54-
5537
impl actix_web::ResponseError for Error {
5638
fn status_code(&self) -> StatusCode {
5739
StatusCode::INTERNAL_SERVER_ERROR
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::path::PathBuf;
1+
use std::path::Path;
22

33
use actix_files::NamedFile;
44
use actix_web::HttpRequest;
55

66
pub const WASM_DIR: &str = "../../wasm/pkg";
77

88
pub(crate) async fn serve_wasm(req: HttpRequest) -> actix_web::Result<NamedFile> {
9-
let fp: PathBuf = req.match_info().query("filename").parse().unwrap();
10-
let path = PathBuf::new().join(WASM_DIR).join(fp);
9+
let fp = req.match_info().query("filename");
10+
let path = Path::new(WASM_DIR).join(fp);
1111
Ok(NamedFile::open(path)?)
1212
}

tutorial/server/actix_web/src/main.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use core::panic;
2-
use std::path::PathBuf;
1+
use std::path::Path;
32

43
use actix_session::SessionMiddleware;
54
use actix_web::cookie::Key;
@@ -23,7 +22,7 @@ mod startup;
2322

2423
#[tokio::main]
2524
async fn main() {
26-
if std::env::var("RUST_LOG").is_err() {
25+
if std::env::var_os("RUST_LOG").is_none() {
2726
std::env::set_var("RUST_LOG", "info");
2827
}
2928

@@ -36,7 +35,7 @@ async fn main() {
3635

3736
let (webauthn, webauthn_users) = startup();
3837

39-
if !PathBuf::from(WASM_DIR).exists() {
38+
if !Path::new(WASM_DIR).exists() {
4039
panic!("{} does not exist, can't serve WASM files.", WASM_DIR);
4140
} else {
4241
info!("Found WASM files OK");

tutorial/server/actix_web/src/session.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::sync::Mutex;
55
use actix_session::storage::{LoadError, SaveError, SessionKey, SessionStore, UpdateError};
66
use actix_web::cookie::time::Duration;
77
use anyhow::anyhow;
8-
use async_trait::async_trait;
98
use chrono::Utc;
109
use once_cell::sync::Lazy;
1110
use rand::distributions::{Alphanumeric, DistString};
@@ -27,7 +26,6 @@ Implementation of the [SessionStore] trait of [actix_session].
2726
#[derive(Default)]
2827
pub(crate) struct MemorySession;
2928

30-
#[async_trait(?Send)]
3129
impl SessionStore for MemorySession {
3230
async fn load(
3331
&self,
@@ -74,8 +72,8 @@ impl SessionStore for MemorySession {
7472
},
7573
);
7674

77-
Ok(SessionKey::try_from(session_key)
78-
.map_err(|_| SaveError::Serialization(anyhow!("Invalid Session Key Error")))?)
75+
SessionKey::try_from(session_key)
76+
.map_err(|_| SaveError::Serialization(anyhow!("Invalid Session Key Error")))
7977
}
8078

8179
async fn update(

tutorial/server/axum/Cargo.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
name = "web_authn"
33
version = "0.1.0"
44
edition = "2021"
5-
rust-version = "1.70.0"
65
authors = ["William Brown <[email protected]>, Ben Wishovich <[email protected]>"]
76
license = "MPL-2.0"
87

@@ -13,13 +12,13 @@ tracing.workspace = true
1312
tracing-subscriber.workspace = true
1413
serde.workspace = true
1514
webauthn-rs = { workspace = true, features = ["danger-allow-state-serialisation"] }
16-
axum = {version = "0.6.1", features = ["http2"]}
15+
axum = { version = "0.6.1", features = ["http2"] }
1716
tokio = { workspace = true, features = ["full"] }
18-
uuid = { workspace = true, features=["v4"] }
17+
uuid = { workspace = true, features = ["v4"] }
1918
url.workspace = true
2019
thiserror.workspace = true
2120
tower = "0.4.13"
22-
tower-http = {version="0.4.4", features=["fs"]}
21+
tower-http = { version = "0.4.4", features = ["fs"] }
2322
tower-sessions = "0.6"
2423

2524
[features]

tutorial/server/tide/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
name = "tide-server"
33
version = "0.1.0"
44
edition = "2021"
5-
rust-version = "1.70.0"
65
authors = ["William Brown <[email protected]>"]
76
license = "MPL-2.0"
87

0 commit comments

Comments
 (0)