Skip to content

Commit

Permalink
added clerk auth middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
suhaybu committed Dec 3, 2024
1 parent f678e3a commit 89d1b6a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ aws-config = "1.5"
lambda_http = "0.13"
lambda_runtime = "0.13"

clerk-rs = { version = "0.4", features = ["axum"] }

tokio = { version = "1", features = ["macros"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", default-features = false, features = [
Expand Down
1 change: 1 addition & 0 deletions src/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ tracing.workspace = true
chrono.workspace = true
sqlx.workspace = true
thiserror.workspace = true
clerk-rs.workspace = true
11 changes: 11 additions & 0 deletions src/common/src/services/clerk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::error::{Error, Result};
use clerk_rs::{clerk::Clerk, ClerkConfiguration};

pub fn init_clerk() -> Result<Clerk> {
let secret_key = std::env::var("CLERK_SECRET_KEY")
.map_err(|_| Error::validation("CLERK_SECRET_KEY must be set"))?;

let config = ClerkConfiguration::new(None, None, Some(secret_key), None);

Ok(Clerk::new(config))
}
1 change: 1 addition & 0 deletions src/common/src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod clerk;
pub mod cors;
pub mod mw_auth;
1 change: 1 addition & 0 deletions src/usertasks-lambda/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ chrono.workspace = true
validator.workspace = true
uuid.workspace = true
sqlx.workspace = true
clerk-rs.workspace = true
13 changes: 11 additions & 2 deletions src/usertasks-lambda/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use axum::{
routing::{delete, get, patch, post},
Router,
};
use clerk_rs::validators::{axum::ClerkLayer, jwks::MemoryCacheJwksProvider};
use lambda_http::{run, Error};
use std::env::set_var;
use tracing_subscriber::fmt::layer;

use common::services::{cors::cors_middleware, mw_auth::auth};
use common::services::{self, cors::cors_middleware, mw_auth::auth};
use handlers::schedule::get_user_schedule;
use handlers::tasks::{create_task, delete_task, get_all_tasks, get_tasks_batch, update_task};

Expand All @@ -24,6 +26,8 @@ async fn main() -> Result<(), Error> {

set_var("AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH", "true");

let clerk_client = services::clerk::init_clerk()?;

let app = Router::new()
.route("/v1/user/:user_id/schedule", get(get_user_schedule))
.route("/v1/tasks", get(get_all_tasks))
Expand All @@ -32,7 +36,12 @@ async fn main() -> Result<(), Error> {
.route("/v1/user/:user_id/tasks/:task_id", patch(update_task))
.route("/v1/user/:user_id/tasks/:task_id", delete(delete_task))
.layer(middleware::from_fn(cors_middleware))
.layer(middleware::from_fn(auth));
// .layer(middleware::from_fn(auth))
.layer(ClerkLayer::new(
MemoryCacheJwksProvider::new(clerk_client),
None,
true,
));

run(app).await
}

0 comments on commit 89d1b6a

Please sign in to comment.