From 2a033efc7a7897b2009e6b9c3e9d6e423d24d7db Mon Sep 17 00:00:00 2001 From: EthanYuan Date: Wed, 30 Oct 2024 16:32:32 +0800 Subject: [PATCH] update: parse JWT from JSON token field in auth response --- mutiny-core/src/authclient.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mutiny-core/src/authclient.rs b/mutiny-core/src/authclient.rs index d85f4d2e9..85502176e 100644 --- a/mutiny-core/src/authclient.rs +++ b/mutiny-core/src/authclient.rs @@ -132,11 +132,17 @@ impl MutinyAuthClient { })?; if response.status().is_success() { - let jwt = response + let response_text = response .text() .await .map_err(|_| MutinyError::JwtAuthFailure)?; + let jwt: String = serde_json::from_str::(&response_text) + .map_err(|_| MutinyError::JwtAuthFailure)? + .get("token") + .and_then(|token| token.as_str().map(String::from)) + .ok_or(MutinyError::JwtAuthFailure)?; + // basic validation to make sure it is a valid string let _ = UntrustedToken::new(&jwt).map_err(|e| { log_error!(self.logger, "Could not validate JWT {jwt}: {e}"); @@ -205,8 +211,11 @@ mod tests { "application/json", )) .map(|| { + let token_response = json!({ + "token": "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.tyh-VfuzIxCyGYDlkBA7DfyjrqmSHu6pQ2hoZuFqUSLPNY2N0mpHb3nk5K17HWP_3cYHBw7AhHale5wky6-sVA" + }); warp::reply::with_status( - "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.tyh-VfuzIxCyGYDlkBA7DfyjrqmSHu6pQ2hoZuFqUSLPNY2N0mpHb3nk5K17HWP_3cYHBw7AhHale5wky6-sVA", + warp::reply::json(&token_response), warp::http::StatusCode::OK, ) });