Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "casdoor-sdk-rust"
version = "1.1.0"
version = "1.1.1"
edition = "2021"
license = "Apache-2.0"
description = "A Casdoor SDK (contain APIs) with more complete interfaces and better usability."
Expand All @@ -22,18 +22,14 @@ serde_urlencoded = "0.7"
reqwest = { version = "0.12", features = ["json"] }
jsonwebtoken = "9.3.0"
oauth2 = "5.0.0"
toml = "0.8"
toml = "0.9"
openssl = "0.10"
anyhow = "1.0.95"
rand = "0.8"
rand = "0.9"
chrono = "0.4.39"
thiserror = "2.0.11"
thiserror = "2.0.16"
serde_with = { version = "3.12.0", features = ["chrono_0_4"] }

[dependencies.uuid]
version = "1.12.1"
features = [
"v4",
"fast-rng",
"macro-diagnostics",
]
features = ["v4", "fast-rng", "macro-diagnostics"]
52 changes: 28 additions & 24 deletions src/authn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ mod models;

use crate::{Method, QueryArgs, QueryResult, Sdk, SdkResult, NO_BODY};
use anyhow::{format_err, Result};
use jsonwebtoken::{
DecodingKey, TokenData, Validation,
};
use jsonwebtoken::{DecodingKey, TokenData, Validation};
pub use models::*;
pub use oauth2::{basic::{BasicTokenIntrospectionResponse, BasicTokenType}, TokenIntrospectionResponse, TokenResponse};
pub use oauth2::{
basic::{BasicTokenIntrospectionResponse, BasicTokenType},
TokenIntrospectionResponse, TokenResponse,
};
use oauth2::{url, AccessToken, AuthUrl, AuthorizationCode, ClientId, ClientSecret, IntrospectionUrl, RedirectUrl, RefreshToken, TokenUrl};
use openssl::pkey::Id;
use openssl::{
Expand Down Expand Up @@ -132,9 +133,14 @@ impl AuthSdk {

let pb_key = self.sdk.replace_cert_to_pub_key().unwrap();

let td = get_tk(pb_key, validation, token).unwrap();

Ok(td.claims)
match get_tk(pb_key, validation, token) {
Ok(td) => {
return Ok(td.claims);
}
Err(e) => {
return Err(e.downcast().unwrap());
}
}
}

pub fn get_signing_url(&self, redirect_url: String) -> String {
Expand Down Expand Up @@ -251,24 +257,22 @@ fn get_tk(pb_key: PKey<Public>, validation: Validation, token: &str) -> Result<T
let token_data: TokenData<ClaimsStandard> = jsonwebtoken::decode(token, decode_key, &validation)?;

Ok(token_data)
},
}
Id::EC => {
let ec_pb_key = pb_key.ec_key()?.public_key_to_pem()?;
let decode_key = &DecodingKey::from_ec_pem(&ec_pb_key)?;
let token_data: TokenData<ClaimsStandard> = jsonwebtoken::decode(token, decode_key, &validation)?;

Ok(token_data)
},
}
Id::RSA_PSS => {
let ec_pb_key = pb_key.rsa()?.public_key_to_pem()?;
let decode_key = &DecodingKey::from_rsa_pem(&ec_pb_key)?;
let token_data: TokenData<ClaimsStandard> = jsonwebtoken::decode(token, decode_key, &validation)?;

Ok(token_data)
},
_ => {
Err(format_err!("not supported"))
},
}
_ => Err(format_err!("not supported")),
}
}

Expand All @@ -288,9 +292,9 @@ mod tests {
"7d315de093a1b8268d0c7eb192bbe02f35a8877d".to_string(),
cert,
"built-in".to_string(),
Some("app-built-in".to_owned())
Some("app-built-in".to_owned()),
)
.into_sdk();
.into_sdk();

let authnx = cfg.authn();

Expand All @@ -308,9 +312,9 @@ mod tests {
"secret".to_string(),
cert,
"Kubernetes".to_string(),
Some("Cluster".to_owned())
Some("Cluster".to_owned()),
)
.into_sdk();
.into_sdk();

let authnx = cfg.authn();

Expand All @@ -327,9 +331,9 @@ mod tests {
"secret".to_string(),
cert,
"Kubernetes".to_string(),
Some("Cluster".to_owned())
Some("Cluster".to_owned()),
)
.into_sdk();
.into_sdk();

let authnx = cfg.authn();

Expand All @@ -348,7 +352,7 @@ mod tests {
"secret".to_string(),
cert,
"org_name".to_string(),
Some("app_name".to_owned())
Some("app_name".to_owned()),
)
.into_sdk();

Expand All @@ -369,7 +373,7 @@ mod tests {
"secret".to_string(),
cert,
"org_name".to_string(),
Some("app_name".to_owned())
Some("app_name".to_owned()),
)
.into_sdk();

Expand All @@ -390,7 +394,7 @@ mod tests {
"secret".to_string(),
cert,
"org_name".to_string(),
Some("app_name".to_owned())
Some("app_name".to_owned()),
)
.into_sdk();

Expand All @@ -412,7 +416,7 @@ mod tests {
"secret".to_string(),
cert,
"org_name".to_string(),
Some("app_name".to_owned())
Some("app_name".to_owned()),
)
.into_sdk();

Expand All @@ -432,7 +436,7 @@ mod tests {
"secret".to_string(),
cert,
"org_name".to_string(),
Some("app_name".to_owned())
Some("app_name".to_owned()),
)
.into_sdk();

Expand Down
17 changes: 7 additions & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::{fs::File, io::Read};

use openssl::{
error::ErrorStack,
pkey::{PKey, Public},
x509::X509,
};
use serde::{Deserialize, Serialize};
use openssl::{error::ErrorStack, pkey::{PKey, Public}, x509::X509};

/// Config is the core configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -22,21 +26,14 @@ pub struct Config {

impl Config {
/// Create a new Config.
pub fn new(
endpoint: String,
client_id: String,
client_secret: String,
certificate: String,
org_name: String,
app_name: Option<String>,
) -> Self {
pub fn new(endpoint: String, client_id: String, client_secret: String, certificate: String, org_name: String, app_name: Option<String>) -> Self {
Config {
endpoint,
client_id,
client_secret,
certificate,
org_name,
app_name
app_name,
}
}

Expand Down
Loading