Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Fix tokens module when not all features are enabled #23

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 9 additions & 7 deletions src/tokens/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::errors::{GenericError, RsaKeyErrors};
use crate::errors::GenericError;
#[cfg(feature = "v1")]
use crate::errors::RsaKeyErrors;

#[cfg(all(not(feature = "v2"), feature = "v1"))]
use crate::v1::local_paseto as V1Local;
Expand Down Expand Up @@ -84,14 +86,14 @@ impl PasetoBuilder {
let strd_msg = to_string(&self.extra_claims)?;

if let Some(mut enc_key) = self.encryption_key {
return V1Local(strd_msg, self.footer, &mut self.enc_key);
return V1Local(&strd_msg, self.footer.as_deref(), &mut enc_key);
} else if let Some(the_rsa_key) = self.rsa_key {
let key_pair = RsaKeyPair::from_der(&the_rsa_key);
if key_pair.is_err() {
return Err(RsaKeyErrors::InvalidKey {})?;
}
let key_pair = Arc::new(key_pair.unwrap());
return V1Public(strd_msg, self.footer, &mut signing_state);
let mut key_pair = key_pair.unwrap();
return V1Public(&strd_msg, self.footer.as_deref(), &mut key_pair);
} else {
return Err(GenericError::NoKeyProvided {})?;
}
Expand All @@ -111,13 +113,13 @@ impl PasetoBuilder {
}

/// Builds a token.
pub fn build(self) -> Result<String> {
pub fn build(self) -> Result<String, Error> {
let strd_msg = to_string(&self.extra_claims)?;

if let Some(mut enc_key) = self.encryption_key {
return V2Local(&strd_msg, self.footer, &mut enc_key);
return V2Local(&strd_msg, self.footer.as_deref(), &mut enc_key);
} else if let Some(ed_key_pair) = self.ed_key {
return V2Public(&strd_msg, self.footer, &ed_key_pair);
return V2Public(&strd_msg, self.footer.as_deref(), &ed_key_pair);
} else {
return Err(GenericError::NoKeyProvided {})?;
}
Expand Down
23 changes: 11 additions & 12 deletions src/tokens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use crate::v2::{decrypt_paseto as V2Decrypt, verify_paseto as V2Verify};
use chrono::prelude::*;
use failure::Error;
#[cfg(feature = "v2")]
use ring::signature::Ed25519KeyPair;
use ring::signature::KeyPair;
use ring::signature::{Ed25519KeyPair, KeyPair};
use serde_json::{from_str as ParseJson, Value as JsonValue};

pub mod builder;
Expand Down Expand Up @@ -140,9 +139,9 @@ pub fn validate_local_token(token: &str, footer: Option<&str>, key: Vec<u8>) ->
/// Because we validate these fields the resulting type must be a json object. If it's not
/// please use the protocol impls directly.
#[cfg(all(feature = "v1", not(feature = "v2")))]
pub fn validate_local_token(token: &str, footer: Option<&str>, key: &Vec<u8>) -> Result<Jsonvalue, Error> {
pub fn validate_local_token(token: &str, footer: Option<&str>, key: &Vec<u8>) -> Result<JsonValue, Error> {
let token = V1Decrypt(token, footer, key)?;
return validate_potential_json_blob(token);
return validate_potential_json_blob(&token);
}

/// Validate a local token for V2.
Expand All @@ -159,9 +158,9 @@ pub fn validate_local_token(token: &str, footer: Option<&str>, key: &Vec<u8>) ->
/// Because we validate these fields the resulting type must be a json object. If it's not
/// please use the protocol impls directly.
#[cfg(all(feature = "v2", not(feature = "v1")))]
pub fn validate_local_token(token: &str, footer: Option<&str>, key: &Vec<u8>) -> Result<Jsonvalue, Error> {
pub fn validate_local_token(token: &str, footer: Option<&str>, key: &Vec<u8>) -> Result<JsonValue, Error> {
let token = V2Decrypt(token, footer, key)?;
return validate_potential_json_blob(token);
return validate_potential_json_blob(&token);
}

/// Validate a public token for V1, or V2.
Expand All @@ -177,6 +176,7 @@ pub fn validate_local_token(token: &str, footer: Option<&str>, key: &Vec<u8>) ->
/// * subject
/// Because we validate these fields the resulting type must be a json object. If it's not
/// please use the protocol impls directly.
#[cfg(all(feature = "v2", feature = "v1"))]
pub fn validate_public_token(token: &str, footer: Option<&str>, key: &PasetoPublicKey) -> Result<JsonValue, Error> {
if token.starts_with("v2.public.") {
return match key {
Expand Down Expand Up @@ -217,13 +217,12 @@ pub fn validate_public_token(token: &str, footer: Option<&str>, key: &PasetoPubl
/// Because we validate these fields the resulting type must be a json object. If it's not
/// please use the protocol impls directly.
#[cfg(all(feature = "v1", not(feature = "v2")))]
pub fn validate_public_token(token: &str, footer: Option<&str>, key: &PasetoPublicKey) -> Result<Jsonvalue, Error> {
pub fn validate_public_token(token: &str, footer: Option<&str>, key: &PasetoPublicKey) -> Result<JsonValue, Error> {
return match key {
PasetoPublicKey::RSAPublicKey(key_content) => {
let internal_msg = V1Verify(token, footer, &key_content)?;
validate_potential_json_blob(internal_msg)
validate_potential_json_blob(&internal_msg)
}
_ => Err(GenericError::NoKeyProvided {})?,
};
}

Expand All @@ -241,11 +240,11 @@ pub fn validate_public_token(token: &str, footer: Option<&str>, key: &PasetoPubl
/// Because we validate these fields the resulting type must be a json object. If it's not
/// please use the protocol impls directly.
#[cfg(all(feature = "v2", not(feature = "v1")))]
pub fn validate_public_token(token: String, footer: Option<&str>, key: PasetoPublicKey) -> Result<Jsonvalue, Error> {
pub fn validate_public_token(token: String, footer: Option<&str>, key: PasetoPublicKey) -> Result<JsonValue, Error> {
return match key {
PasetoPublicKey::ED25519KeyPair(key_pair) => {
let internal_msg = V2Verify(token, footer, &key_pair)?;
validate_potential_json_blob(internal_msg)
let internal_msg = V2Verify(&token, footer, key_pair.public_key().as_ref())?;
validate_potential_json_blob(&internal_msg)
}
_ => Err(GenericError::NoKeyProvided {})?,
};
Expand Down