Skip to main content

casdoor_rust_sdk/
logout.rs

1// Copyright 2026 The Casdoor Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::client::{Client, Response};
16use crate::error::{CasdoorError, Result};
17
18impl Client {
19    /// Log out the user that owns the `access_token` by calling the `/api/sso-logout` API.
20    /// It performs a single sign-out: all the sessions of the user across all the
21    /// applications of the organization are deleted and all the access tokens issued to
22    /// the user are expired.
23    ///
24    /// The `access_token` is the user's access token returned by
25    /// [`Client::get_oauth_token()`].
26    pub async fn logout(&self, access_token: &str) -> Result<()> {
27        self.sso_logout(access_token, true).await
28    }
29
30    /// Like [`Client::logout()`], but only ends the session that the `access_token`
31    /// belongs to, so the user stays signed in on their other devices and browsers.
32    pub async fn logout_current_session(&self, access_token: &str) -> Result<()> {
33        self.sso_logout(access_token, false).await
34    }
35
36    async fn sso_logout(&self, access_token: &str, logout_all: bool) -> Result<()> {
37        if access_token.is_empty() {
38            return Err(CasdoorError::Casdoor(
39                "logout() error: the accessToken should not be empty".to_string(),
40            ));
41        }
42
43        let url = self.get_url("sso-logout", &[("logoutAll", &logout_all.to_string())]);
44
45        // The "/api/sso-logout" API identifies the user by their own access token, so the
46        // Bearer token is used here instead of the application's Basic Auth.
47        let mut req = self.http().post(url).bearer_auth(access_token);
48        for (key, value) in &self.custom_headers {
49            req = req.header(key, value);
50        }
51
52        let response = req.send().await?;
53        let status = response.status();
54        let bytes = response.bytes().await?;
55
56        if status != reqwest::StatusCode::OK && status != reqwest::StatusCode::FORBIDDEN {
57            return Err(CasdoorError::HttpStatus {
58                status: status.as_u16(),
59                body: String::from_utf8_lossy(&bytes).into_owned(),
60            });
61        }
62
63        let response: Response = serde_json::from_slice(&bytes)?;
64        if response.status != "ok" {
65            return Err(CasdoorError::Casdoor(response.msg));
66        }
67
68        Ok(())
69    }
70}