Skip to content

Commit 4d863a2

Browse files
committed
fix: change parse_jwt unwrap logic
1 parent 7f96783 commit 4d863a2

File tree

3 files changed

+40
-43
lines changed

3 files changed

+40
-43
lines changed

Cargo.toml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "casdoor-sdk-rust"
3-
version = "1.1.0"
3+
version = "1.1.1"
44
edition = "2021"
55
license = "Apache-2.0"
66
description = "A Casdoor SDK (contain APIs) with more complete interfaces and better usability."
@@ -22,18 +22,14 @@ serde_urlencoded = "0.7"
2222
reqwest = { version = "0.12", features = ["json"] }
2323
jsonwebtoken = "9.3.0"
2424
oauth2 = "5.0.0"
25-
toml = "0.8"
25+
toml = "0.9"
2626
openssl = "0.10"
2727
anyhow = "1.0.95"
28-
rand = "0.8"
28+
rand = "0.9"
2929
chrono = "0.4.39"
30-
thiserror = "2.0.11"
30+
thiserror = "2.0.16"
3131
serde_with = { version = "3.12.0", features = ["chrono_0_4"] }
3232

3333
[dependencies.uuid]
3434
version = "1.12.1"
35-
features = [
36-
"v4",
37-
"fast-rng",
38-
"macro-diagnostics",
39-
]
35+
features = ["v4", "fast-rng", "macro-diagnostics"]

src/authn/mod.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ mod models;
22

33
use crate::{Method, QueryArgs, QueryResult, Sdk, SdkResult, NO_BODY};
44
use anyhow::{format_err, Result};
5-
use jsonwebtoken::{
6-
DecodingKey, TokenData, Validation,
7-
};
5+
use jsonwebtoken::{DecodingKey, TokenData, Validation};
86
pub use models::*;
9-
pub use oauth2::{basic::{BasicTokenIntrospectionResponse, BasicTokenType}, TokenIntrospectionResponse, TokenResponse};
7+
pub use oauth2::{
8+
basic::{BasicTokenIntrospectionResponse, BasicTokenType},
9+
TokenIntrospectionResponse, TokenResponse,
10+
};
1011
use oauth2::{url, AccessToken, AuthUrl, AuthorizationCode, ClientId, ClientSecret, IntrospectionUrl, RedirectUrl, RefreshToken, TokenUrl};
1112
use openssl::pkey::Id;
1213
use openssl::{
@@ -132,9 +133,14 @@ impl AuthSdk {
132133

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

135-
let td = get_tk(pb_key, validation, token).unwrap();
136-
137-
Ok(td.claims)
136+
match get_tk(pb_key, validation, token) {
137+
Ok(td) => {
138+
return Ok(td.claims);
139+
}
140+
Err(e) => {
141+
return Err(e.downcast().unwrap());
142+
}
143+
}
138144
}
139145

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

253259
Ok(token_data)
254-
},
260+
}
255261
Id::EC => {
256262
let ec_pb_key = pb_key.ec_key()?.public_key_to_pem()?;
257263
let decode_key = &DecodingKey::from_ec_pem(&ec_pb_key)?;
258264
let token_data: TokenData<ClaimsStandard> = jsonwebtoken::decode(token, decode_key, &validation)?;
259265

260266
Ok(token_data)
261-
},
267+
}
262268
Id::RSA_PSS => {
263269
let ec_pb_key = pb_key.rsa()?.public_key_to_pem()?;
264270
let decode_key = &DecodingKey::from_rsa_pem(&ec_pb_key)?;
265271
let token_data: TokenData<ClaimsStandard> = jsonwebtoken::decode(token, decode_key, &validation)?;
266272

267273
Ok(token_data)
268-
},
269-
_ => {
270-
Err(format_err!("not supported"))
271-
},
274+
}
275+
_ => Err(format_err!("not supported")),
272276
}
273277
}
274278

@@ -288,9 +292,9 @@ mod tests {
288292
"7d315de093a1b8268d0c7eb192bbe02f35a8877d".to_string(),
289293
cert,
290294
"built-in".to_string(),
291-
Some("app-built-in".to_owned())
295+
Some("app-built-in".to_owned()),
292296
)
293-
.into_sdk();
297+
.into_sdk();
294298

295299
let authnx = cfg.authn();
296300

@@ -308,9 +312,9 @@ mod tests {
308312
"secret".to_string(),
309313
cert,
310314
"Kubernetes".to_string(),
311-
Some("Cluster".to_owned())
315+
Some("Cluster".to_owned()),
312316
)
313-
.into_sdk();
317+
.into_sdk();
314318

315319
let authnx = cfg.authn();
316320

@@ -327,9 +331,9 @@ mod tests {
327331
"secret".to_string(),
328332
cert,
329333
"Kubernetes".to_string(),
330-
Some("Cluster".to_owned())
334+
Some("Cluster".to_owned()),
331335
)
332-
.into_sdk();
336+
.into_sdk();
333337

334338
let authnx = cfg.authn();
335339

@@ -348,7 +352,7 @@ mod tests {
348352
"secret".to_string(),
349353
cert,
350354
"org_name".to_string(),
351-
Some("app_name".to_owned())
355+
Some("app_name".to_owned()),
352356
)
353357
.into_sdk();
354358

@@ -369,7 +373,7 @@ mod tests {
369373
"secret".to_string(),
370374
cert,
371375
"org_name".to_string(),
372-
Some("app_name".to_owned())
376+
Some("app_name".to_owned()),
373377
)
374378
.into_sdk();
375379

@@ -390,7 +394,7 @@ mod tests {
390394
"secret".to_string(),
391395
cert,
392396
"org_name".to_string(),
393-
Some("app_name".to_owned())
397+
Some("app_name".to_owned()),
394398
)
395399
.into_sdk();
396400

@@ -412,7 +416,7 @@ mod tests {
412416
"secret".to_string(),
413417
cert,
414418
"org_name".to_string(),
415-
Some("app_name".to_owned())
419+
Some("app_name".to_owned()),
416420
)
417421
.into_sdk();
418422

@@ -432,7 +436,7 @@ mod tests {
432436
"secret".to_string(),
433437
cert,
434438
"org_name".to_string(),
435-
Some("app_name".to_owned())
439+
Some("app_name".to_owned()),
436440
)
437441
.into_sdk();
438442

src/config.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use std::{fs::File, io::Read};
22

3+
use openssl::{
4+
error::ErrorStack,
5+
pkey::{PKey, Public},
6+
x509::X509,
7+
};
38
use serde::{Deserialize, Serialize};
4-
use openssl::{error::ErrorStack, pkey::{PKey, Public}, x509::X509};
59

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

2327
impl Config {
2428
/// Create a new Config.
25-
pub fn new(
26-
endpoint: String,
27-
client_id: String,
28-
client_secret: String,
29-
certificate: String,
30-
org_name: String,
31-
app_name: Option<String>,
32-
) -> Self {
29+
pub fn new(endpoint: String, client_id: String, client_secret: String, certificate: String, org_name: String, app_name: Option<String>) -> Self {
3330
Config {
3431
endpoint,
3532
client_id,
3633
client_secret,
3734
certificate,
3835
org_name,
39-
app_name
36+
app_name,
4037
}
4138
}
4239

0 commit comments

Comments
 (0)