Skip to main content

casdoor_rust_sdk/
url.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;
16
17impl Client {
18    /// Return the URL of the Casdoor sign-up page. `redirect_uri` can be empty when
19    /// `enable_password` is true, since only the password based sign-up page is needed
20    /// then.
21    pub fn get_signup_url(&self, enable_password: bool, redirect_uri: &str) -> String {
22        if enable_password {
23            return format!(
24                "{}/signup/{}",
25                self.config.endpoint, self.config.application_name
26            );
27        }
28
29        self.get_signin_url(redirect_uri)
30            .replace("/login/oauth/authorize", "/signup/oauth/authorize")
31    }
32
33    /// Return the URL of the Casdoor sign-in page, the user is redirected to
34    /// `redirect_uri` with an authorization code after signing in.
35    pub fn get_signin_url(&self, redirect_uri: &str) -> String {
36        let scope = "read";
37        let state = &self.config.application_name;
38
39        format!(
40            "{}/login/oauth/authorize?client_id={}&response_type=code&redirect_uri={}&scope={}&state={}",
41            self.config.endpoint,
42            self.config.client_id,
43            urlencoding::encode(redirect_uri),
44            scope,
45            state
46        )
47    }
48
49    /// Return the URL of a user's profile page.
50    pub fn get_user_profile_url(&self, user_name: &str, access_token: Option<&str>) -> String {
51        format!(
52            "{}/users/{}/{}{}",
53            self.config.endpoint,
54            self.config.organization_name,
55            user_name,
56            access_token_param(access_token)
57        )
58    }
59
60    /// Return the URL of the account page of the user who owns the access token.
61    pub fn get_my_profile_url(&self, access_token: Option<&str>) -> String {
62        format!(
63            "{}/account{}",
64            self.config.endpoint,
65            access_token_param(access_token)
66        )
67    }
68}
69
70fn access_token_param(access_token: Option<&str>) -> String {
71    match access_token {
72        Some(token) if !token.is_empty() => format!("?access_token={token}"),
73        _ => String::new(),
74    }
75}