Skip to content

Commit

Permalink
Add support for Axum framework (#32)
Browse files Browse the repository at this point in the history
* add clerk authorizer

* refactor actix validator to use clerk authorizer

* add axum validator

* add axum example

* bump version
  • Loading branch information
brunoribeiro127 authored May 14, 2024
1 parent f0f73c4 commit 11d1b3c
Show file tree
Hide file tree
Showing 10 changed files with 678 additions and 184 deletions.
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "clerk-rs"
version = "0.2.3"
version = "0.3.0"
authors = ["DarrenBaldwin07"]
description = "An unofficial Rust SDK for the Clerk API"
repository = "https://github.com/Cincinnati-Ventures/clerk-rs"
Expand All @@ -21,6 +21,10 @@ path = "examples/api.rs"
name = "actix"
path = "examples/actix.rs"

[[example]]
name = "axum"
path = "examples/axum.rs"

[lib]
doctest = false

Expand All @@ -35,14 +39,19 @@ jsonwebtoken = "8.3.0"
actix-web = "4.3.1"
futures-util = "0.3.28"
actix-rt = "2.9.0"
axum = "0.7.5"
axum-extra = { version = "0.9.3", features = ["cookie"] }
tower = "0.4.13"

[dependencies.reqwest]
version = "^0.11"
default-features = false
features = ["json", "multipart"]

[dev-dependencies]
base64 = "0.22.1"
clerk-rs = { path = "../clerk-rs" }
openssl = "0.10.64"
tokio = { version = "1.27.0", features = ["full"] }

[features]
Expand Down
35 changes: 27 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,48 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

```rust
use actix_web::{web, App, HttpServer, Responder};
use clerk_rs::{ClerkConfiguration, validators::actix::ClerkMiddleware};
use clerk_rs::{validators::actix::ClerkMiddleware, ClerkConfiguration};

async fn index() -> impl Responder {
"Hello world!"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {

HttpServer::new(|| {
let config = ClerkConfiguration::new(None, None, Some("sk_test_key".to_string()), None);
App::new().service(
web::scope("/app")
.wrap(ClerkMiddleware::new(config, None, false))
.route("/index", web::get().to(index)),
)
let config = ClerkConfiguration::new(None, None, Some("your_secret_key".to_string()), None);
App::new()
.wrap(ClerkMiddleware::new(config, None, true))
.route("/index", web::get().to(index))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
```

### Protecting a axum endpoint with Clerk.dev:

```rust
use axum::{routing::get, Router};
use clerk_rs::validators::axum::ClerkLayer;
use clerk_rs::ClerkConfiguration;
use std::net::SocketAddr;

async fn index() -> &'static str {
"Hello world!"
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
let config: ClerkConfiguration = ClerkConfiguration::new(None, None, Some("your_secret_key".to_string()), None);
let app = Router::new().route("/index", get(index)).layer(ClerkLayer::new(config, None, true));
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
let listener = tokio::net::TcpListener::bind(addr).await?;
axum::serve(listener, app).await
}
```

## Roadmap

- [ ] Support other http clients along with the default reqwest client (like hyper)
Expand Down
2 changes: 1 addition & 1 deletion examples/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
let config = ClerkConfiguration::new(None, None, Some("your_secret_key".to_string()), None);
App::new()
.wrap(ClerkMiddleware::new(config, None, false))
.wrap(ClerkMiddleware::new(config, None, true))
.route("/index", web::get().to(index))
})
.bind(("127.0.0.1", 8080))?
Expand Down
17 changes: 17 additions & 0 deletions examples/axum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use axum::{routing::get, Router};
use clerk_rs::validators::axum::ClerkLayer;
use clerk_rs::ClerkConfiguration;
use std::net::SocketAddr;

async fn index() -> &'static str {
"Hello world!"
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
let config: ClerkConfiguration = ClerkConfiguration::new(None, None, Some("your_secret_key".to_string()), None);
let app = Router::new().route("/index", get(index)).layer(ClerkLayer::new(config, None, true));
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
let listener = tokio::net::TcpListener::bind(addr).await?;
axum::serve(listener, app).await
}
1 change: 1 addition & 0 deletions src/clerk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub const USER_AGENT: &str = concat!("Clerk/v1 RustBindings/", env!("CARGO_PKG_V
*
* NOTE: This SDK is based on the official clerk openAPI spec found here: https://clerk.com/docs/reference/backend-api
*/
#[derive(Clone)]
pub struct Clerk {
pub config: configuration::ClerkConfiguration,
}
Expand Down
4 changes: 2 additions & 2 deletions src/models/email_address_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub enum Strategy {
#[serde(rename = "admin")]
Admin,
#[serde(other)]
Other
Other,
}

impl Default for Strategy {
Expand Down Expand Up @@ -115,4 +115,4 @@ mod tests {
// Assert that the deserialized object matches the expected object
assert_eq!(deserialized, expected);
}
}
}
Loading

0 comments on commit 11d1b3c

Please sign in to comment.