casdoor_rs_sdk/
lib.rs

1//! A [Casdoor](https://github.com/casdoor/casdoor) SDK (contain APIs) with more complete interfaces and better usability.
2
3// -------- rust coding guidelines: https://rust-coding-guidelines.github.io/rust-coding-guidelines-zh/ --------
4// -------- rustc lint doc: https://doc.rust-lang.org/rustc/lints/listing/index.html --------
5// -------- rust-clippy doc: https://rust-lang.github.io/rust-clippy/master/index.html --------
6
7// [REQUIRED] G.VAR.02 Do not use non-ASCII characters in identifiers
8#![deny(non_ascii_idents)]
9// [REQUIRED]
10#![allow(clippy::disallowed_names)]
11// [REQUIRED]
12#![allow(clippy::blanket_clippy_restriction_lints)]
13// [REQUIRED] G.CMT.02 Add Panic documentation in the docs of public APIs that may panic under certain circumstances
14#![warn(clippy::missing_panics_doc)]
15// [RECOMMENDED] G.CNS.05 Use const fn for functions or methods wherever applicable
16#![warn(clippy::missing_const_for_fn)]
17// [REQUIRED] G.TYP.01 Prefer safe conversion functions over `as` for type casting
18#![warn(
19    clippy::as_conversions,
20    clippy::cast_lossless,
21    clippy::cast_possible_truncation,
22    clippy::cast_possible_wrap,
23    clippy::ptr_as_ptr
24)]
25// [RECOMMENDED] G.VAR.01 Avoid using too many meaningless variable names when destructuring tuples with more than four
26// variables
27#![warn(clippy::many_single_char_names)]
28// [RECOMMENDED] G.TYP.02 Explicitly specify the type for numeric literals
29#![warn(clippy::default_numeric_fallback)]
30// [RECOMMENDED] G.TYP.03 Use `try_from` methods instead of relying on numeric boundaries for safe conversion
31#![warn(clippy::checked_conversions)]
32// [RECOMMENDED] G.TYP.BOL.02 Use `if` expressions instead of `match` for boolean conditions
33#![warn(clippy::match_bool)]
34// [RECOMMENDED] G.TYP.BOL.05 Use logical operators (&&/||) instead of bitwise operators (&/|) for boolean operations
35// when not necessary
36#![warn(clippy::needless_bitwise_bool)]
37// [REQUIRED] G.TYP.INT.02 Avoid `as` casting between signed and unsigned integers; use safe conversion functions
38#![deny(clippy::cast_sign_loss)]
39// [REQUIRED] G.TYP.INT.03 Avoid using `%` for modulo operations on negative numbers
40#![warn(clippy::modulo_arithmetic)]
41// [REQUIRED] G.TYP.FLT.02 Avoid precision loss when casting from any numeric type to floating-point; use safe
42// conversion functions
43#![warn(clippy::cast_precision_loss)]
44// [REQUIRED] G.TYP.FLT.03 Be cautious of precision loss in floating-point arithmetic and comparisons
45#![warn(clippy::float_arithmetic, clippy::float_cmp, clippy::float_cmp_const)]
46// [REQUIRED] G.TYP.FLT.04 Use Rust's built-in methods for floating-point calculations
47#![warn(clippy::imprecise_flops, clippy::suboptimal_flops)]
48// [OPTIONAL] G.TYP.ARR.01 Use static variables instead of constants for large global arrays
49#![warn(clippy::large_stack_arrays)]
50// [RECOMMENDED] G.FUD.03 Consider using a custom struct or enum instead of many boolean parameters in function
51// signatures
52#![warn(clippy::fn_params_excessive_bools)]
53// [RECOMMENDED] G.TYP.ENM.04 Avoid using glob imports for enum variants in `use` statements
54#![warn(clippy::enum_glob_use)]
55// [RECOMMENDED] G.CTF.02 Ensure `else` branches are present whenever `else if` is used
56#![warn(clippy::else_if_without_else)]
57// [RECOMMENDED] G.STR.03 Convert string literals containing only ASCII characters to byte sequences using `b"str"`
58// syntax instead of `as_bytes()`
59#![warn(clippy::string_lit_as_bytes)]
60// [RECOMMENDED] G.STR.05 Take care to avoid disrupting UTF-8 encoding when slicing strings at specific positions
61#![warn(clippy::string_slice)]
62// [RECOMMENDED] G.FUD.02 Prefer passing large values by reference if function parameters implement `Copy`
63#![warn(clippy::large_types_passed_by_value)]
64// [RECOMMENDED] G.FUD.04 Pass small `Copy` type values by value instead of by reference
65#![warn(clippy::trivially_copy_pass_by_ref)]
66// [REQUIRED] G.GEN.02 Be cautious to avoid using generic default implementations of some methods from Rust's standard
67// library; prefer specific type implementations
68#![warn(clippy::inefficient_to_string)]
69// [REQUIRED] G.TRA.BLN.02 Do not implement the `Copy` trait for iterators
70#![warn(clippy::copy_iterator)]
71// [RECOMMENDED] G.TRA.BLN.07 Use `copied` method instead of `cloned` for iterable `Copy` types
72#![warn(clippy::cloned_instead_of_copied)]
73// [RECOMMENDED] G.ERR.01 Avoid using `unwrap` indiscriminately when handling `Option<T>` and `Result<T, E>`
74#![warn(clippy::unwrap_used)]
75// [RECOMMENDED] G.MOD.03 Avoid using wildcard imports in module declarations
76#![warn(clippy::wildcard_imports)]
77// [REQUIRED] G.MOD.04 Avoid using different module layout styles within the same project
78#![warn(clippy::self_named_module_files)]
79// [RECOMMENDED] G.CAR.02 Ensure that necessary metadata is included in the `Cargo.toml` of the crate
80#![warn(clippy::cargo_common_metadata)]
81// [RECOMMENDED] G.CAR.03 Avoid negative or redundant prefixes and suffixes in feature names
82#![warn(clippy::negative_feature_names, clippy::redundant_feature_names)]
83// [REQUIRED] G.CAR.04 Avoid using wildcard dependencies in `Cargo.toml`
84#![warn(clippy::wildcard_dependencies)]
85// [RECOMMENDED] G.MAC.01 Only use the `dbg!()` macro for debugging code
86#![warn(clippy::dbg_macro)]
87// [REQUIRED] Ensure that locks are released before `await` is called in asynchronous code
88#![warn(clippy::await_holding_lock)]
89// [REQUIRED] Handle `RefCell` references across `await` points
90#![warn(clippy::await_holding_refcell_ref)]
91// [RECOMMENDED] G.ASY.04 Avoid defining unnecessary async functions
92#![warn(clippy::unused_async)]
93// [REQUIRED] G.UNS.SAS.02 Use `assert!` instead of `debug_assert!` to verify boundary conditions in unsafe functions
94#![warn(clippy::debug_assert_with_mut_call)]
95
96mod application;
97mod authn;
98mod authz;
99mod cert;
100mod config;
101mod organization;
102mod provider;
103mod sdk;
104mod user;
105pub mod utils;
106
107pub use application::*;
108pub use authn::*;
109pub use authz::*;
110#[cfg(feature = "api")]
111pub use casdoor_api::{apis, models as api_models};
112pub use cert::*;
113pub use config::*;
114pub use organization::*;
115pub use provider::*;
116pub use reqwest::{Method, StatusCode, Url};
117pub use sdk::*;
118pub use user::*;
119
120pub type SdkResult<T> = std::result::Result<T, SdkError>;
121
122#[cfg(test)]
123mod tests {
124    use crate::*;
125    #[test]
126    fn example() {
127        let endpoint = "http://localhost:8000";
128        let client_id = "0e6ad201d317fb74fe9d";
129        let client_secret = "1fc847b0fdb3cb3f067c15ee383dee6213bd3fde";
130        let certificate = r###"
131-----BEGIN CERTIFICATE-----
132MIIE+TCCAuGgAwIBAgIDAeJAMA0GCSqGSIb3DQEBCwUAMDYxHTAbBgNVBAoTFENh
133c2Rvb3IgT3JnYW5pemF0aW9uMRUwEwYDVQQDEwxDYXNkb29yIENlcnQwHhcNMjEx
134MDE1MDgxMTUyWhcNNDExMDE1MDgxMTUyWjA2MR0wGwYDVQQKExRDYXNkb29yIE9y
135Z2FuaXphdGlvbjEVMBMGA1UEAxMMQ2FzZG9vciBDZXJ0MIICIjANBgkqhkiG9w0B
136AQEFAAOCAg8AMIICCgKCAgEAsInpb5E1/ym0f1RfSDSSE8IR7y+lw+RJjI74e5ej
137rq4b8zMYk7HeHCyZr/hmNEwEVXnhXu1P0mBeQ5ypp/QGo8vgEmjAETNmzkI1NjOQ
138CjCYwUrasO/f/MnI1C0j13vx6mV1kHZjSrKsMhYY1vaxTEP3+VB8Hjg3MHFWrb07
139uvFMCJe5W8+0rKErZCKTR8+9VB3janeBz//zQePFVh79bFZate/hLirPK0Go9P1g
140OvwIoC1A3sarHTP4Qm/LQRt0rHqZFybdySpyWAQvhNaDFE7mTstRSBb/wUjNCUBD
141PTSLVjC04WllSf6Nkfx0Z7KvmbPstSj+btvcqsvRAGtvdsB9h62Kptjs1Yn7GAuo
142I3qt/4zoKbiURYxkQJXIvwCQsEftUuk5ew5zuPSlDRLoLByQTLbx0JqLAFNfW3g/
143pzSDjgd/60d6HTmvbZni4SmjdyFhXCDb1Kn7N+xTojnfaNkwep2REV+RMc0fx4Gu
144hRsnLsmkmUDeyIZ9aBL9oj11YEQfM2JZEq+RVtUx+wB4y8K/tD1bcY+IfnG5rBpw
145IDpS262boq4SRSvb3Z7bB0w4ZxvOfJ/1VLoRftjPbLIf0bhfr/AeZMHpIKOXvfz4
146yE+hqzi68wdF0VR9xYc/RbSAf7323OsjYnjjEgInUtRohnRgCpjIk/Mt2Kt84Kb0
147wn8CAwEAAaMQMA4wDAYDVR0TAQH/BAIwADANBgkqhkiG9w0BAQsFAAOCAgEAn2lf
148DKkLX+F1vKRO/5gJ+Plr8P5NKuQkmwH97b8CS2gS1phDyNgIc4/LSdzuf4Awe6ve
149C06lVdWSIis8UPUPdjmT2uMPSNjwLxG3QsrimMURNwFlLTfRem/heJe0Zgur9J1M
1508haawdSdJjH2RgmFoDeE2r8NVRfhbR8KnCO1ddTJKuS1N0/irHz21W4jt4rxzCvl
1512nR42Fybap3O/g2JXMhNNROwZmNjgpsF7XVENCSuFO1jTywLaqjuXCg54IL7XVLG
152omKNNNcc8h1FCeKj/nnbGMhodnFWKDTsJcbNmcOPNHo6ixzqMy/Hqc+mWYv7maAG
153Jtevs3qgMZ8F9Qzr3HpUc6R3ZYYWDY/xxPisuKftOPZgtH979XC4mdf0WPnOBLqL
1542DJ1zaBmjiGJolvb7XNVKcUfDXYw85ZTZQ5b9clI4e+6bmyWqQItlwt+Ati/uFEV
155XzCj70B4lALX6xau1kLEpV9O1GERizYRz5P9NJNA7KoO5AVMp9w0DQTkt+LbXnZE
156HHnWKy8xHQKZF9sR7YBPGLs/Ac6tviv5Ua15OgJ/8dLRZ/veyFfGo2yZsI+hKVU5
157nCCJHBcAyFnm1hdvdwEdH33jDBjNB6ciotJZrf/3VYaIWSalADosHAgMWfXuWP+h
1588XKXmzlxuHbTMQYtZPDgspS5aK+S4Q9wb8RRAYo=
159-----END CERTIFICATE-----
160"###;
161        let org_name = "built-in";
162        let app_name = "myapp";
163
164        let sdk = Config::new(
165            endpoint,
166            client_id,
167            client_secret,
168            certificate,
169            org_name,
170            Some(app_name.to_owned()),
171        )
172        .into_sdk();
173        println!("{:?}", sdk);
174        println!("{:?}", sdk.authn());
175    }
176}