htmx logout tidy up (#2884)

This commit is contained in:
Firstyear 2024-07-15 17:11:00 +10:00 committed by GitHub
parent d0e57442d2
commit d7a5097527
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 4 deletions

View file

@ -59,6 +59,7 @@ const EXPECT_VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(Debug)] #[derive(Debug)]
pub enum ClientError { pub enum ClientError {
Unauthorized, Unauthorized,
SessionExpired,
Http(reqwest::StatusCode, Option<OperationError>, String), Http(reqwest::StatusCode, Option<OperationError>, String),
Transport(reqwest::Error), Transport(reqwest::Error),
AuthenticationFailed, AuthenticationFailed,
@ -633,10 +634,16 @@ impl KanidmClient {
} }
pub async fn logout(&self) -> Result<(), ClientError> { pub async fn logout(&self) -> Result<(), ClientError> {
self.perform_get_request("/v1/logout").await?; match self.perform_get_request("/v1/logout").await {
let mut tguard = self.bearer_token.write().await; Err(ClientError::Unauthorized)
*tguard = None; | Err(ClientError::Http(reqwest::StatusCode::UNAUTHORIZED, _, _))
Ok(()) | Ok(()) => {
let mut tguard = self.bearer_token.write().await;
*tguard = None;
Ok(())
}
e => e,
}
} }
pub fn get_token_cache_path(&self) -> String { pub fn get_token_cache_path(&self) -> String {

View file

@ -1662,6 +1662,13 @@ async fn test_server_user_auth_token_lifecycle(rsclient: KanidmClient) {
.await .await
.expect("Failed to destroy user auth token"); .expect("Failed to destroy user auth token");
// The session is revoked server side, but we can still call logout locally
// and on the 401 it will just clear the token.
rsclient
.logout()
.await
.expect("Failed to remove local token");
// Since the session is revoked, check with the admin. // Since the session is revoked, check with the admin.
let res = rsclient let res = rsclient
.auth_simple_password(ADMIN_TEST_USER, ADMIN_TEST_PASSWORD) .auth_simple_password(ADMIN_TEST_USER, ADMIN_TEST_PASSWORD)