Skip to content

Commit

Permalink
Add GET /api/claims
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubelotix committed Sep 2, 2024
1 parent 430d25e commit a226445
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
jsonwebtoken = "9"
rocket = "0.5.0"
serde = {version = "1.0", features = ["derive"] }
serde_json = "1.0"
urlencoding = "2.1.3"
isahc = "1.7"
string-tools = "0.1.0"
Expand Down
20 changes: 20 additions & 0 deletions src/get_claims.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::*;

pub struct GetClaimsResponse {
claims: Claims
}

impl <'r, 'o: 'r> Responder<'r, 'o> for GetClaimsResponse {
fn respond_to(self, _: &'r rocket::Request<'_>) -> rocket::response::Result<'o> {
let mut response = Response::build();
let response = response.status(Status::Ok);
let claims = serde_json::to_string(&self.claims).unwrap_or_default();
let response = response.sized_body(claims.len(), Cursor::new(claims));
Ok(response.finalize())
}
}

#[get("/api/claims")]
pub fn get_claims(keys: &State<(EncodingKey, DecodingKey)>, cookies: &CookieJar<'_>) -> Result<GetClaimsResponse, VerificationError> {
verify(keys, cookies).map(|claims| GetClaimsResponse { claims: claims.0 })
}
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub use validate::*;
mod provider;
pub use provider::*;

#[path = "get_claims.rs"]
mod get_claims_mod;
pub use get_claims_mod::*;

#[path = "verify.rs"]
mod verify_mod;
pub use verify_mod::*;
Expand Down Expand Up @@ -71,5 +75,5 @@ fn rocket() -> _ {
let decoding_key: DecodingKey = DecodingKey::from_ec_pem(&public_key).expect("Invalid public key");
rocket::build()
.manage((encoding_key, decoding_key))
.mount("/", routes![root, login_callback, verify, login, logout, provider_login, provider_validate])
.mount("/", routes![root, login_callback, verify, login, logout, provider_login, provider_validate, get_claims])
}

0 comments on commit a226445

Please sign in to comment.