Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
include:
- build: pinned
os: ubuntu-24.04
rust: 1.85.0
rust: 1.86.0
- build: stable
os: ubuntu-latest
rust: stable
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub trait JwtVerifier: Verifier<Vec<u8>> {
/// If you just want to encode a JWT, use `encode` instead.
pub fn sign(message: &[u8], key: &EncodingKey, algorithm: Algorithm) -> Result<String> {
let provider = (CryptoProvider::get_default().signer_factory)(&algorithm, key)?;
Ok(b64_encode(provider.sign(message)))
Ok(b64_encode(provider.try_sign(message)?))
}

/// Compares the signature given with a re-computed signature for HMAC or using the public key
Expand Down
2 changes: 1 addition & 1 deletion src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn encode<T: Serialize>(header: &Header, claims: &T, key: &EncodingKey) -> R
let encoded_claims = b64_encode_part(claims)?;
let message = [encoded_header, encoded_claims].join(".");

let signature = b64_encode(signing_provider.sign(message.as_bytes()));
let signature = b64_encode(signing_provider.try_sign(message.as_bytes())?);

Ok([message, signature].join("."))
}
10 changes: 10 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub enum ErrorKind {
InvalidRsaKey(String),
/// We could not sign with the given key
RsaFailedSigning,
/// Signing failed
Signing(String),
/// When the algorithm from string doesn't match the one passed to `from_str`
InvalidAlgorithmName,
/// When a key is provided with an invalid format
Expand Down Expand Up @@ -91,6 +93,7 @@ impl StdError for Error {
ErrorKind::InvalidEcdsaKey => None,
ErrorKind::InvalidEddsaKey => None,
ErrorKind::RsaFailedSigning => None,
ErrorKind::Signing(_) => None,
ErrorKind::InvalidRsaKey(_) => None,
ErrorKind::ExpiredSignature => None,
ErrorKind::MissingAlgorithm => None,
Expand Down Expand Up @@ -129,6 +132,7 @@ impl fmt::Display for Error {
| ErrorKind::InvalidAlgorithmName => write!(f, "{:?}", self.0),
ErrorKind::MissingRequiredClaim(c) => write!(f, "Missing required claim: {}", c),
ErrorKind::InvalidRsaKey(msg) => write!(f, "RSA key invalid: {}", msg),
ErrorKind::Signing(msg) => write!(f, "Signing failed: {}", msg),
ErrorKind::Json(err) => write!(f, "JSON error: {}", err),
ErrorKind::Utf8(err) => write!(f, "UTF-8 error: {}", err),
ErrorKind::Base64(err) => write!(f, "Base64 error: {}", err),
Expand Down Expand Up @@ -170,6 +174,12 @@ impl From<ErrorKind> for Error {
}
}

impl From<signature::Error> for Error {
fn from(err: signature::Error) -> Error {
new_error(ErrorKind::Signing(err.to_string()))
}
}

#[cfg(test)]
mod tests {
use wasm_bindgen_test::wasm_bindgen_test;
Expand Down
Loading