Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update dependencies #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions Cargo.toml
Expand Up @@ -14,14 +14,14 @@ A simple JWT validator for Microsoft Azure Id tokens.
"""

[dependencies]
jsonwebtoken = { version = "7.2.0", default-features = false }
reqwest = {version = "0.11.1", default-features = false, features = ["blocking", "json", "rustls-tls"]}
serde = { version = "1.0.124", features = ["derive"] }
chrono = "0.4.19"
jsonwebtoken = { version = "9", default-features = false }
reqwest = {version = "0.11", default-features = false, features = ["blocking", "json", "rustls-tls"]}
serde = { version = "1", features = ["derive"] }
chrono = { version = "0.4", default-features = false, features = ["clock"] }

[dev-dependencies]
criterion = "0.3.4"
base64 = "0.13.0"
base64 = "0.22.0"

[[bench]]
name = "validation"
Expand Down
4 changes: 2 additions & 2 deletions benches/validation.rs
Expand Up @@ -90,8 +90,8 @@ fn generate_test_token() -> String {
.join(".");

// we create the signature using our private key
let signature =
jwt::crypto::sign(&test_token, &private_key, jwt::Algorithm::RS256).expect("Singed.");
let signature = jwt::crypto::sign(&test_token.as_bytes(), &private_key, jwt::Algorithm::RS256)
.expect("Singed.");

// we construct a complete token which looks like: header.claims.signature
let complete_token = format!("{}.{}", test_token, signature);
Expand Down
26 changes: 17 additions & 9 deletions src/lib.rs
Expand Up @@ -319,7 +319,7 @@ impl AzureAuth {
}
};

let key = DecodingKey::from_rsa_components(auth_key.modulus(), auth_key.exponent());
let key = DecodingKey::from_rsa_components(auth_key.modulus(), auth_key.exponent())?;
let valid: Token<T> = jwt::decode(token, &key, &validator)?;

Ok(valid)
Expand Down Expand Up @@ -632,42 +632,50 @@ xMd+OWT6JsInVM1ASh1mcn+Q0/Z3WqxxetCQLqaMs+FATn059dGf";
// We create a test token from parts here. We use the v2 token used as example
// in https://docs.microsoft.com/en-us/azure/active-directory/develop/id-tokens
fn generate_test_token() -> String {
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};

let private_key = jwt::EncodingKey::from_base64_secret(PRIVATE_KEY_TEST).unwrap();

// we need to construct the calims in a function since we need to set
// the expiration relative to current time
let test_token_playload = test_token_claims();
let test_token_payload = test_token_claims();
let test_token_header = test_token_header();

// we base64 (url-safe-base64) the header and claims and arrange
// as a jwt payload -> header_as_base64.claims_as_base64
let test_token = [
base64::encode_config(&test_token_header, base64::URL_SAFE),
base64::encode_config(&test_token_playload, base64::URL_SAFE),
URL_SAFE_NO_PAD.encode(&test_token_header),
URL_SAFE_NO_PAD.encode(&test_token_payload),
]
.join(".");

// we create the signature using our private key
let signature =
jwt::crypto::sign(&test_token, &private_key, jwt::Algorithm::RS256).expect("Signed");
jwt::crypto::sign(&test_token.as_bytes(), &private_key, jwt::Algorithm::RS256)
.expect("Signed");

let public_key = Jwk {
kid: "".to_string(),
n: PUBLIC_KEY_N.to_string(),
e: PUBLIC_KEY_E.to_string(),
};

let public_key = DecodingKey::from_rsa_components(&public_key.n, &public_key.e);
let public_key = DecodingKey::from_rsa_components(&public_key.n, &public_key.e)
.expect("Decoding key could not be created from rsa component");

// we construct a complete token which looks like: header.claims.signature
let complete_token = format!("{}.{}", test_token, signature);

// we verify the signature here as well to catch errors in our testing
// code early

let verified =
jwt::crypto::verify(&signature, &test_token, &public_key, jwt::Algorithm::RS256)
.expect("verified");
let verified = jwt::crypto::verify(
&signature,
&test_token.as_bytes(),
&public_key,
jwt::Algorithm::RS256,
)
.expect("verified");
assert!(verified);

complete_token
Expand Down
11 changes: 6 additions & 5 deletions tests/integration.rs
@@ -1,4 +1,5 @@
use azure_jwt::*;
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use jsonwebtoken as jwt;

const PUBLIC_KEY_N: &str = "AOx0GOQcSt5AZu02nlGWUuXXppxeV9Cu_9LcgpVBg_WQb-5DBHZpqs8AMek5u5iI4hkHCcOyMbQrBsDIVa9xxZxR2kq_8GtERsnd6NClQimspxT1WVgX5_WCAd5rk__Iv0GocP2c_1CcdT8is2OZHeWQySyQNSgyJYg6Up7kFtYabiCyU5q9tTIHQPXiwY53IGsNvSkqbk-OsdWPT3E4dqp3vNraMqXhuSZ-52kLCHqwPgAsbztfFJxSAEBcp-TS3uNuHeSJwNWjvDKTPy2oMacNpbsKb2gZgzubR6hTjvupRjaQ9SHhXyL9lmSZOpCzz2XJSVRopKUUtB-VGA0qVlk";
Expand Down Expand Up @@ -76,20 +77,20 @@ fn generate_test_token() -> String {

// we need to construct the calims in a function since we need to set
// the expiration relative to current time
let test_token_playload = test_token_claims();
let test_token_payload = test_token_claims();
let test_token_header = test_token_header();

// we base64 (url-safe-base64) the header and claims and arrange
// as a jwt payload -> header_as_base64.claims_as_base64
let test_token = [
base64::encode_config(&test_token_header, base64::URL_SAFE),
base64::encode_config(&test_token_playload, base64::URL_SAFE),
URL_SAFE_NO_PAD.encode(&test_token_header),
URL_SAFE_NO_PAD.encode(&test_token_payload),
]
.join(".");

// we create the signature using our private key
let signature =
jwt::crypto::sign(&test_token, &private_key, jwt::Algorithm::RS256).expect("Singed.");
let signature = jwt::crypto::sign(&test_token.as_bytes(), &private_key, jwt::Algorithm::RS256)
.expect("Singed.");

// we construct a complete token which looks like: header.claims.signature
let complete_token = format!("{}.{}", test_token, signature);
Expand Down