Skip to content

Commit

Permalink
chore: Improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
andeya committed Oct 23, 2024
1 parent 9feaa27 commit 3864345
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 29 deletions.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion workflows/release.yml → .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: cargo login ${{ secrets.CARGO_TOKEN }}

- name: Cargo Publish
run: ./publish.sh
run: cargo publish

- name: GitHub Release
id: create_release
Expand Down
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "casdoor-rs-sdk"
version = "2.0.0"
version = "2.0.1"
edition = "2021"
license = "Apache-2.0"
description = "A Casdoor SDK with more complete interfaces and better usability."
Expand All @@ -18,18 +18,18 @@ serde = { version = "1", features = ["derive"] }
reqwest = { version = "0.12", features = ["json"] }
tokio = { version = "1", features = ["full"] }
serde_json = "1.0"
jsonwebtoken = "9.3"
jsonwebtoken = "9.3.0"
http = "1.1"
urlencoding = "2.1.0"
urlencoding = "2.1"
oauth2 = "4.1"
toml = "0.8"
getset2 = "0.2"
anyhow = "1"
salvo = { version = "0.73", default-features = false, features = [
"oapi",
], optional = true }

jwt-claims = "1.0.1"

[features]
default = ["salvo"]
salvo = ["dep:salvo"]
default = []
salvo = ["dep:salvo", "jwt-claims/salvo"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A [Casdoor](https://github.com/casdoor/casdoor) SDK with more complete interfaces and better usability.

[![GitHub last commit](https://img.shields.io/github/last-commit/andeya/casdoor-rs-sdk)](https://github.com/andeya/casdoor-rs-sdk/commits/master)
[![GitHub last commit](https://img.shields.io/github/last-commit/andeya/casdoor-rs-sdk)](https://github.com/andeya/casdoor-rs-sdk/commits/main)
[![Crates.io](https://img.shields.io/crates/v/casdoor-rs-sdk.svg)](https://crates.io/crates/casdoor-rs-sdk)
[![Docs](https://docs.rs/casdoor-rs-sdk/badge.svg)](https://docs.rs/casdoor-rs-sdk)

Expand Down
17 changes: 5 additions & 12 deletions src/authn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,11 @@ impl AuthService {
}

pub fn parse_jwt_token(&self, token: &str) -> anyhow::Result<Claims> {
let v: TokenData<serde_json::Value> = jsonwebtoken::decode(
token,
&DecodingKey::from_rsa_pem(self.service.certificate().as_bytes())?,
&Validation::new(Algorithm::RS256),
)?;
println!("parse_jwt_token = {}", v.claims);
let res: TokenData<Claims> = jsonwebtoken::decode(
token,
&DecodingKey::from_rsa_pem(self.service.certificate().as_bytes())?,
&Validation::new(Algorithm::RS256),
)?;
Ok(res.claims)
let mut validation = Validation::new(Algorithm::RS256);
validation.set_audience(&[self.service.client_id()]);

let td: TokenData<Claims> = jsonwebtoken::decode(token, &DecodingKey::from_rsa_pem(self.service.certificate().as_bytes())?, &validation)?;
Ok(td.claims)
}

pub fn get_signin_url(&self, redirect_url: String) -> String {
Expand Down
11 changes: 8 additions & 3 deletions src/authn/types.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
use jwt_claims::RegisteredClaims;
pub use oauth2::TokenResponse;
use serde::{Deserialize, Serialize};

use crate::User;

#[cfg_attr(feature = "salvo", derive(salvo::prelude::ToSchema))]
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase", default)]
pub struct Claims {
#[serde(flatten)]
pub user: User,
pub access_token: String,
pub token_type: String,
pub refresh_token_type: String,
pub tag: String,
pub token_type: Option<String>,
pub nonce: Option<String>,
pub scope: Option<String>,
#[serde(flatten)]
pub reg_claims: RegisteredClaims,
}
19 changes: 13 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ 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: impl Into<String>,
client_id: impl Into<String>,
client_secret: impl Into<String>,
certificate: impl Into<String>,
org_name: impl Into<String>,
app_name: Option<String>,
) -> Self {
Config {
endpoint,
client_id,
client_secret,
certificate: Self::replace_cert_to_pub_key(certificate),
org_name,
endpoint: endpoint.into(),
client_id: client_id.into(),
client_secret: client_secret.into(),
certificate: Self::replace_cert_to_pub_key(certificate.into()),
org_name: org_name.into(),
app_name,
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,51 @@ pub use authn::*;
pub use config::*;
pub use service::*;
pub use user::*;

#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn example() {
let endpoint = "http://localhost:8000";
let client_id = "0e6ad201d317fb74fe9d";
let client_secret = "1fc847b0fdb3cb3f067c15ee383dee6213bd3fde";
let certificate = r###"
-----BEGIN CERTIFICATE-----
MIIE+TCCAuGgAwIBAgIDAeJAMA0GCSqGSIb3DQEBCwUAMDYxHTAbBgNVBAoTFENh
c2Rvb3IgT3JnYW5pemF0aW9uMRUwEwYDVQQDEwxDYXNkb29yIENlcnQwHhcNMjEx
MDE1MDgxMTUyWhcNNDExMDE1MDgxMTUyWjA2MR0wGwYDVQQKExRDYXNkb29yIE9y
Z2FuaXphdGlvbjEVMBMGA1UEAxMMQ2FzZG9vciBDZXJ0MIICIjANBgkqhkiG9w0B
AQEFAAOCAg8AMIICCgKCAgEAsInpb5E1/ym0f1RfSDSSE8IR7y+lw+RJjI74e5ej
rq4b8zMYk7HeHCyZr/hmNEwEVXnhXu1P0mBeQ5ypp/QGo8vgEmjAETNmzkI1NjOQ
CjCYwUrasO/f/MnI1C0j13vx6mV1kHZjSrKsMhYY1vaxTEP3+VB8Hjg3MHFWrb07
uvFMCJe5W8+0rKErZCKTR8+9VB3jan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E
HHnWKy8xHQKZF9sR7YBPGLs/Ac6tviv5Ua15OgJ/8dLRZ/veyFfGo2yZsI+hKVU5
nCCJHBcAyFnm1hdvdwEdH33jDBjNB6ciotJZrf/3VYaIWSalADosHAgMWfXuWP+h
8XKXmzlxuHbTMQYtZPDgspS5aK+S4Q9wb8RRAYo=
-----END CERTIFICATE-----
"###;
let org_name = "built-in";
let app_name = "myapp";

let service = Config::new(endpoint, client_id, client_secret, certificate, org_name, Some(app_name.to_owned())).into_service();
println!("{:?}", service.authn());
println!("{:?}", service.user());
}
}
9 changes: 9 additions & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ impl Deref for Service {
}
}

impl Config {
pub fn into_service(self) -> Service {
Service::new(self)
}
}

pub const NONE_BODY: Option<&()> = None::<&()>;

impl Service {
pub fn new(config: Config) -> Self {
Self { config: Arc::new(config) }
}
pub async fn request<Data, Data2>(
&self,
method: Method,
Expand Down
1 change: 1 addition & 0 deletions src/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Service {
}
}

#[derive(Debug, Clone)]
pub struct UserService {
service: Service,
}
Expand Down

0 comments on commit 3864345

Please sign in to comment.