Skip to content

Updating the typ field to allow any string #84

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 17 additions & 13 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub trait JoseHeader {
None
}

fn type_(&self) -> Option<HeaderType> {
fn type_(&self) -> Option<&str> {
None
}

Expand All @@ -37,7 +37,7 @@ pub struct Header {
pub key_id: Option<String>,

#[serde(rename = "typ", skip_serializing_if = "Option::is_none")]
pub type_: Option<HeaderType>,
pub type_: Option<String>,

#[serde(rename = "cty", skip_serializing_if = "Option::is_none")]
pub content_type: Option<HeaderContentType>,
Expand All @@ -52,22 +52,15 @@ impl JoseHeader for Header {
self.key_id.as_deref()
}

fn type_(&self) -> Option<HeaderType> {
self.type_
fn type_(&self) -> Option<&str> {
self.type_.as_deref()
}

fn content_type(&self) -> Option<HeaderContentType> {
self.content_type
}
}

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum HeaderType {
#[serde(rename = "JWT")]
JsonWebToken,
}

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum HeaderContentType {
#[serde(rename = "JWT")]
Expand Down Expand Up @@ -132,15 +125,15 @@ impl<'a> JoseHeader for BorrowedKeyHeader<'a> {
mod tests {
use crate::algorithm::AlgorithmType;
use crate::error::Error;
use crate::header::{Header, HeaderType, PrecomputedAlgorithmOnlyHeader};
use crate::header::{Header, PrecomputedAlgorithmOnlyHeader};
use crate::{FromBase64, ToBase64};

#[test]
fn from_base64() -> Result<(), Error> {
let enc = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
let header = Header::from_base64(enc)?;

assert_eq!(header.type_.unwrap(), HeaderType::JsonWebToken);
assert_eq!(header.type_.unwrap(), "JWT".to_string());
assert_eq!(header.algorithm, AlgorithmType::Hs256);

let enc = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjFLU0YzZyJ9";
Expand Down Expand Up @@ -188,4 +181,15 @@ mod tests {

Ok(())
}

#[test]
fn typ_field_can_contain_any_value() -> Result<(), Error> {
// A `typ` value other than "JWT"
let enc = "eyJhbGciOiJSUzI1NiIsImtpZCI6IkVDN0RFNzc5OTI5RjUwNzQ4RDYwNDgwN0UwODgzMDA0IiwidHlwIjoiYXQrand0In0";
let header = Header::from_base64(enc)?;

assert_eq!(header.type_.unwrap(), "at+jwt".to_string());

Ok(())
}
}