diff --git a/Cargo.lock b/Cargo.lock index a5f2834a..4ad71265 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2201,9 +2201,9 @@ dependencies = [ [[package]] name = "jsonwebtoken" -version = "10.4.0" +version = "11.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eba32bfb4ffdeaca3e34431072faf01745c9b26d25504aa7a6cf5684334fc4fc" +checksum = "e75fe14a82d81e5f5af639997db37d8b96045938a7ac6ab18cdbe1c7467e05e1" dependencies = [ "base64 0.22.1", "ed25519-dalek", @@ -2340,9 +2340,9 @@ checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" [[package]] name = "mediatype" -version = "0.21.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "120fa187be19d9962f0926633453784691731018a2bf936ddb4e29101b79c4a7" +checksum = "0695a693e38d35838dd34bc3ab19911395f6837ccf55c7271189cfb12cb8b57f" [[package]] name = "memchr" diff --git a/Cargo.toml b/Cargo.toml index 908902c0..cc0fd4f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,9 +65,9 @@ humantime-serde = "1.1.1" indicatif = "0.18.4" infer = { version = "0.22.0", default-features = false } insta = "1.48.0" -jsonwebtoken = { version = "10.4.0", features = ["rust_crypto"] } +jsonwebtoken = { version = "11.1.0", features = ["rust_crypto"] } lru = "0.19.0" -mediatype = "0.21.0" +mediatype = "0.23.0" metrics = "0.24.6" metrics-exporter-dogstatsd = "0.9.8" multer = "3.1.0" diff --git a/clients/rust/src/auth.rs b/clients/rust/src/auth.rs index 624a6609..84f74149 100644 --- a/clients/rust/src/auth.rs +++ b/clients/rust/src/auth.rs @@ -15,6 +15,14 @@ const DEFAULT_PERMISSIONS: [Permission; 3] = [ Permission::ObjectDelete, ]; +/// An error creating or signing an auth token, such as an invalid key. +/// +/// The message describes the cause. The underlying signing library is an implementation detail +/// and not part of the public API. +#[derive(Debug, thiserror::Error)] +#[error(transparent)] +pub struct TokenError(jsonwebtoken::errors::Error); + /// Key configuration that will be used to sign tokens in Objectstore requests. pub struct SecretKey { /// A key ID that Objectstore must use to load the corresponding public key. @@ -132,7 +140,8 @@ struct JwtClaims { impl TokenGenerator { /// Create a new [`TokenGenerator`] for a given key configuration. pub fn new(secret_key: SecretKey) -> crate::Result { - let encoding_key = EncodingKey::from_ed_pem(secret_key.secret_key.as_bytes())?; + let encoding_key = + EncodingKey::from_ed_pem(secret_key.secret_key.as_bytes()).map_err(TokenError)?; Ok(TokenGenerator { kid: secret_key.kid, encoding_key, @@ -256,6 +265,7 @@ impl TokenRequest<'_> { let mut header = Header::new(Algorithm::EdDSA); header.kid = Some(self.generator.kid.clone()); - Ok(encode(&header, &claims, &self.generator.encoding_key)?) + let token = encode(&header, &claims, &self.generator.encoding_key).map_err(TokenError)?; + Ok(token) } } diff --git a/clients/rust/src/error.rs b/clients/rust/src/error.rs index e1985144..a99ead26 100644 --- a/clients/rust/src/error.rs +++ b/clients/rust/src/error.rs @@ -20,8 +20,8 @@ pub enum Error { #[error("invalid scope: {0}")] InvalidScope(#[from] objectstore_types::scope::InvalidScopeError), /// Error when creating auth tokens, such as invalid keys. - #[error(transparent)] - TokenError(#[from] jsonwebtoken::errors::Error), + #[error("failed to create auth token: {0}")] + TokenError(#[from] crate::TokenError), /// Error when the permissions requested for a token exceed those granted to the /// [`TokenGenerator`](crate::TokenGenerator). #[error("requested permissions not granted to this token generator: {escalated:?}")] diff --git a/objectstore-types/src/metadata.rs b/objectstore-types/src/metadata.rs index 6f9e1a5e..8286f474 100644 --- a/objectstore-types/src/metadata.rs +++ b/objectstore-types/src/metadata.rs @@ -129,6 +129,15 @@ pub enum ExpiryAnchor { Now, } +/// A content type is not a valid [IANA Media +/// Type](https://www.iana.org/assignments/media-types/media-types.xhtml). +/// +/// The message describes which part is malformed. The underlying parser is an implementation +/// detail and not part of the public API. +#[derive(Debug, thiserror::Error)] +#[error(transparent)] +pub struct InvalidContentType(mediatype::MediaTypeError); + /// Errors that can happen dealing with metadata #[derive(Debug, thiserror::Error)] pub enum Error { @@ -143,7 +152,7 @@ pub enum Error { Compression, /// The content type is invalid. #[error("invalid content type")] - ContentType(#[from] mediatype::MediaTypeError), + ContentType(#[from] InvalidContentType), /// The creation time is invalid. #[error("invalid creation time")] CreationTime(#[from] humantime::TimestampError), @@ -598,7 +607,7 @@ impl Metadata { /// Validates that `content_type` is a valid [IANA Media /// Type](https://www.iana.org/assignments/media-types/media-types.xhtml). fn validate_content_type(content_type: &str) -> Result<(), Error> { - mediatype::MediaType::parse(content_type)?; + mediatype::MediaType::parse(content_type).map_err(InvalidContentType)?; Ok(()) }