-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bd6e46
commit 2757034
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "helloworld" | ||
version = "0.1.1" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
axum = "0.7.1" | ||
axum_jwt_ware = { version = "0.1.4", path = "../.." } | ||
chrono = "0.4.31" | ||
tokio = "1.34.0" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
mod route; | ||
use route::create_router; | ||
mod service; | ||
pub use axum_jwt_ware; | ||
|
||
|
||
#[tokio::main] | ||
async fn main() { | ||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); | ||
axum::serve(listener, create_router().into_make_service()) | ||
.await | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::service::{hello, MyUserData}; | ||
use axum::{ | ||
middleware, | ||
routing::{get, post}, | ||
Json, Router, | ||
}; | ||
use axum_jwt_ware::{self, refresh_token, Claims, DecodingContext, EncodingContext, RefreshBody}; | ||
use chrono::{Duration, Utc}; | ||
|
||
pub fn create_router() -> Router { | ||
let user_data = MyUserData; | ||
let jwt_secret = "secret"; | ||
let refresh_secret = "refresh_secret"; | ||
|
||
let app = Router::new() | ||
.route( | ||
"/hello", | ||
get(hello).layer(middleware::from_fn(move |req, next| { | ||
let key = axum_jwt_ware::DecodingKey::from_secret(jwt_secret.as_ref()); | ||
let validation = axum_jwt_ware::Validation::default(); | ||
async move { axum_jwt_ware::verify_user(req, &key, validation, next).await } | ||
})), | ||
) | ||
.route( | ||
"/login", | ||
post(move |body: Json<axum_jwt_ware::RequestBody>| { | ||
let expiry_timestamp = Utc::now() + Duration::hours(48); | ||
|
||
axum_jwt_ware::login( | ||
body, | ||
user_data.clone(), | ||
jwt_secret, | ||
refresh_secret, | ||
expiry_timestamp.timestamp(), | ||
) | ||
}), | ||
) | ||
.route( | ||
"/refresh", | ||
post(move |body: Json<RefreshBody>| { | ||
let encoding_context = EncodingContext { | ||
header: axum_jwt_ware::Header::default(), | ||
validation: axum_jwt_ware::Validation::default(), | ||
key: axum_jwt_ware::EncodingKey::from_secret("refresh_secret".as_ref()), | ||
}; | ||
let decoding_context = DecodingContext { | ||
header: axum_jwt_ware::Header::default(), | ||
validation: axum_jwt_ware::Validation::default(), | ||
key: axum_jwt_ware::DecodingKey::from_secret("refresh_secret".as_ref()), | ||
}; | ||
let claims = Claims { | ||
sub: "jkfajfafghjjfn".to_string(), | ||
username: "ezesunday".to_string(), | ||
exp: (Utc::now() + Duration::hours(48)).timestamp(), | ||
}; | ||
refresh_token(body, encoding_context, decoding_context, claims) | ||
}), | ||
); | ||
app | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use axum::{Extension, Json}; | ||
use crate::axum_jwt_ware::{UserData, CurrentUser, Claims}; | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct MyUserData; | ||
|
||
impl UserData for MyUserData { | ||
fn get_user_by_email(&self, _email: &str) -> Option<CurrentUser> { | ||
// Implement the logic to fetch user by email from your database | ||
// This is just a placeholder; replace it with the actual implementation | ||
Some(CurrentUser { | ||
password: "password".to_string(), | ||
name: "Eze Sunday".to_string(), | ||
email: "[email protected]".to_string(), | ||
username: "ezesunday".to_string(), | ||
id: "jkfajfafghjjfn".to_string(), | ||
}) | ||
} | ||
} | ||
|
||
pub async fn hello( | ||
Extension(claims): Extension<Claims>, | ||
) -> Json<Claims> { | ||
Json(claims) | ||
} |