-
Notifications
You must be signed in to change notification settings - Fork 3
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
1cf1547
commit 0ac2308
Showing
4 changed files
with
169 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
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,103 @@ | ||
use axum::{ | ||
extract::FromRef, | ||
response::IntoResponse, | ||
routing::{get, post}, | ||
Json, Router, | ||
}; | ||
|
||
use axum_jwt_auth::{Claims, Decoder, JwtDecoderState, LocalDecoder}; | ||
use chrono::{Duration, Utc}; | ||
use jsonwebtoken::{encode, Algorithm, DecodingKey, EncodingKey, Header, Validation}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, FromRef)] | ||
struct AppState { | ||
decoder: JwtDecoderState, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct MyClaims { | ||
iat: u64, | ||
aud: String, | ||
exp: u64, | ||
} | ||
|
||
#[tokio::test] | ||
async fn token_is_valid() { | ||
// Load the keys | ||
let encoding_key = EncodingKey::from_rsa_pem(include_bytes!("jwt.key")).unwrap(); | ||
let decoding_key = DecodingKey::from_rsa_pem(include_bytes!("jwt.key.pub")).unwrap(); | ||
|
||
let mut validation = Validation::new(Algorithm::RS256); | ||
validation.set_audience(&["https://example.com"]); | ||
let decoder: Decoder = LocalDecoder::new(vec![decoding_key.to_owned()], validation).into(); | ||
let state = AppState { | ||
decoder: JwtDecoderState { decoder }, | ||
}; | ||
|
||
let app = Router::new() | ||
.route("/", get(|| async { "Hello, World!" })) | ||
.route( | ||
"/user_info", | ||
get(|Claims(claims): Claims<MyClaims>| async { Json(claims) }), | ||
) | ||
.route( | ||
"/login", | ||
post(|| async move { | ||
let mut header = Header::new(Algorithm::RS256); | ||
header.kid = Some("test".to_string()); | ||
|
||
let exp = Utc::now() + Duration::hours(1); | ||
let claims = MyClaims { | ||
iat: 1234567890, | ||
aud: "https://example.com".to_string(), | ||
exp: exp.timestamp() as u64, | ||
}; | ||
|
||
let token = encode::<MyClaims>(&header, &claims, &encoding_key).unwrap(); | ||
|
||
token.into_response() | ||
}), | ||
) | ||
.with_state(state); | ||
|
||
// run it on localhost:3000 | ||
tokio::spawn(async move { | ||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); | ||
axum::serve(listener, app).await.unwrap(); | ||
}); | ||
|
||
// Test the server | ||
let client = reqwest::Client::new(); | ||
|
||
// Unauthorized | ||
let res = client | ||
.get("http://localhost:3000/user_info") | ||
.send() | ||
.await | ||
.unwrap(); | ||
assert_eq!(res.status(), 401); | ||
|
||
// Login | ||
let res = client | ||
.post("http://localhost:3000/login") | ||
.send() | ||
.await | ||
.unwrap(); | ||
let token = res.text().await.unwrap(); | ||
|
||
// Authorized with token | ||
let res = client | ||
.get("http://localhost:3000/user_info") | ||
.header("Authorization", format!("Bearer {}", token)) | ||
.send() | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(res.status(), 200); | ||
let body = res.text().await.unwrap(); | ||
let claims: MyClaims = serde_json::from_str(&body).unwrap(); | ||
assert_eq!(claims.aud, "https://example.com"); | ||
assert_eq!(claims.iat, 1234567890); | ||
assert!(claims.exp > Utc::now().timestamp() as u64); | ||
} |
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,51 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIJKAIBAAKCAgEAsPWeaqsN+2KZu9rlto59XASssMoaVjIxMYXtLyifky1sXS4E | ||
vYFnvr37X63B+lMwuZ3xACc7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MPBQBdwjAvuo3RueXukNYJNaI2zjpDDaMP3gIFmO+mqyE= | ||
-----END RSA PRIVATE KEY----- |
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 @@ | ||
-----BEGIN PUBLIC KEY----- | ||
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAsPWeaqsN+2KZu9rlto59 | ||
XASssMoaVjIxMYXtLyifky1sXS4EvYFnvr37X63B+lMwuZ3xACc7xsUPK+GXPe6X | ||
qZGJdj+Wgf7a3J6FieSNpnrDK4x6CMr0iAPgIhoEYp7BUyPKzPv21vMl6A5kJvlA | ||
AdxfPm3jhk5NDWHSfiFnWiC7UESARgyFl0TlJ+f9H3qaArkzp3Cb+m+wlHpleewO | ||
Sr9maTPLdIS+ZzZ1ZC4lDIQnetJJ0kue+o1wAL4VmdBMY8IVxEutPAaZO+9G8eYJ | ||
ywZiDDkcrrqWymDvSUarcB/AOzEQjxN6nSSNuW6UbalfnDlGmR0kFK8fopraA4nw | ||
U4tG6fAuKTPpOmahC910IRAkedOp6IrRU+2LmcBQ0oyzukHjXd9o9/5MES2wTDFg | ||
ZBalVRZCo55vdQt5CtQDQWVUbQ1y95dm/0EmmgZzWBgiguSKcO2QuqwYIiq5t9ui | ||
kFleeVQDVnd+V6yZ5wWfnA6H0+dPw4VTEUkxaTN8jQImQtB9gvj8iknsGX08LGF5 | ||
WjWh1ewJI0L74Ey5T/ytsXME6Xpn1qfXB2sr5tPol3KeV8pjuGrAymvaLJZz4ZqN | ||
Y3f4wULfCsyVasUOdknMm8UmTgPR+vnDlF+1ItsmN+Jl+RJ1dFkXRDcelCIJS44s | ||
MSchnxv47OwnqvBHCPbiUI8CAwEAAQ== | ||
-----END PUBLIC KEY----- |