16 unstable releases (3 breaking)

0.4.0 Oct 19, 2024
0.3.1 Jul 7, 2024
0.3.0 May 16, 2024
0.2.3 Jan 16, 2024
0.1.7 Jun 23, 2023

#122 in Web programming

Download history 153/week @ 2024-08-19 452/week @ 2024-08-26 308/week @ 2024-09-02 172/week @ 2024-09-09 157/week @ 2024-09-16 322/week @ 2024-09-23 180/week @ 2024-09-30 176/week @ 2024-10-07 391/week @ 2024-10-14 111/week @ 2024-10-21 64/week @ 2024-10-28 359/week @ 2024-11-04 518/week @ 2024-11-11 299/week @ 2024-11-18 253/week @ 2024-11-25 273/week @ 2024-12-02

1,351 downloads per month
Used in 2 crates

MIT license

495KB
10K SLoC

crates.io Downloads docs.rs

A official community-maintained Clerk SDK for Rust

For more detailed documentation, please reference the below links:

This SDK is updated frequently to keep up with any changes to the actual Clerk API. If you see anything that needs updated or is not inline with the official Clerk api, please open an issue!

Examples

Checkout examples in the /examples directory

Using a traditional http request to a valid clerk endpoint:

use tokio;
use clerk_rs::{clerk::Clerk, ClerkConfiguration, endpoints::ClerkGetEndpoint};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = ClerkConfiguration::new(None, None, Some("sk_test_key".to_string()), None);
    let client = Clerk::new(config);

    let res = client.get(ClerkGetEndpoint::GetUserList).await?;

    Ok(())
}

Using a clerk-rs method:

use tokio;
use clerk_rs::{clerk::Clerk, ClerkConfiguration, apis::emails_api::Email};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = ClerkConfiguration::new(None, None, Some("sk_test_key".to_string()), None);
    let client = Clerk::new(config);

    Email::create(&client, Some(your_clerk_email));

    Ok(())
}

Protecting a actix-web endpoint with Clerk.dev:

With the actix feature enabled:

use actix_web::{web, App, HttpServer, Responder};
use clerk_rs::{
    clerk::Clerk,
    validators::{actix::ClerkMiddleware, jwks::MemoryCacheJwksProvider},
    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("your_secret_key".to_string()), None);
        let clerk = Clerk::new(config);

        App::new()
            .wrap(ClerkMiddleware::new(MemoryCacheJwksProvider::new(clerk), None, true))
            .route("/index", web::get().to(index))
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

Protecting a axum endpoint with Clerk.dev:

With the axum feature enabled:

use axum::{routing::get, Router};
use clerk_rs::{
    clerk::Clerk,
    validators::{axum::ClerkLayer, jwks::MemoryCacheJwksProvider},
    ClerkConfiguration,
};

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

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let config = ClerkConfiguration::new(None, None, Some("your_secret_key".to_string()), None);
    let clerk = Clerk::new(config);

    let app = Router::new()
        .route("/index", get(index))
        .layer(ClerkLayer::new(MemoryCacheJwksProvider::new(clerk), None, true));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await?;
    axum::serve(listener, app).await
}

Protecting a rocket endpoint with Clerk.dev:

With the rocket feature enabled:

use clerk_rs::{
	clerk::Clerk,
	validators::{
		jwks::MemoryCacheJwksProvider,
		rocket::{ClerkGuard, ClerkGuardConfig},
	},
	ClerkConfiguration,
};
use rocket::{
	get, launch, routes,
	serde::{Deserialize, Serialize},
};

#[derive(Serialize, Deserialize)]
struct Message {
	content: String,
}

#[get("/")]
fn index(jwt: ClerkGuard<MemoryCacheJwksProvider>) -> &'static str {
	"Hello world!"
}

#[launch]
fn rocket() -> _ {
	let config = ClerkConfiguration::new(None, None, Some("sk_test_F9HM5l3WMTDMdBB0ygcMMAiL37QA6BvXYV1v18Noit".to_string()), None);
	let clerk = Clerk::new(config);
	let clerk_config = ClerkGuardConfig::new(
		MemoryCacheJwksProvider::new(clerk),
		None,
		true, // validate_session_cookie
	);

	rocket::build().mount("/", routes![index]).manage(clerk_config)
}

Roadmap

  • Support other http clients along with the default reqwest client (like hyper)
  • Tokio and async-std async runtimes for hyper clients
  • Optional reqwest blocking client
  • Support authorization via __session cookie on same-origin
  • Add validator support for axum, rocket, warp

Production users


Dependencies

~8–43MB
~716K SLoC