2019-11-03 10:46:08 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
2017-01-09 00:50:51 -05:00
|
|
|
|
2019-11-03 11:13:22 -05:00
|
|
|
use crate::algorithms::Algorithm;
|
|
|
|
use crate::errors::Result;
|
2019-11-11 14:16:34 -05:00
|
|
|
use crate::serialization::b64_decode;
|
2019-11-08 14:00:19 -05:00
|
|
|
|
2017-01-09 00:50:51 -05:00
|
|
|
/// A basic JWT header, the alg defaults to HS256 and typ is automatically
|
2017-04-12 21:08:07 -04:00
|
|
|
/// set to `JWT`. All the other fields are optional.
|
2020-06-16 06:04:22 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Hash, Serialize, Deserialize)]
|
2017-01-09 00:50:51 -05:00
|
|
|
pub struct Header {
|
2017-04-12 21:08:07 -04:00
|
|
|
/// The type of JWS: it can only be "JWT" here
|
|
|
|
///
|
|
|
|
/// Defined in [RFC7515#4.1.9](https://tools.ietf.org/html/rfc7515#section-4.1.9).
|
2017-08-25 20:54:20 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2017-09-07 03:46:40 -04:00
|
|
|
pub typ: Option<String>,
|
2017-04-12 21:08:07 -04:00
|
|
|
/// The algorithm used
|
|
|
|
///
|
|
|
|
/// Defined in [RFC7515#4.1.1](https://tools.ietf.org/html/rfc7515#section-4.1.1).
|
2017-01-09 00:50:51 -05:00
|
|
|
pub alg: Algorithm,
|
2017-04-12 21:08:07 -04:00
|
|
|
/// Content type
|
|
|
|
///
|
|
|
|
/// Defined in [RFC7519#5.2](https://tools.ietf.org/html/rfc7519#section-5.2).
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub cty: Option<String>,
|
|
|
|
/// JSON Key URL
|
|
|
|
///
|
|
|
|
/// Defined in [RFC7515#4.1.2](https://tools.ietf.org/html/rfc7515#section-4.1.2).
|
2017-04-10 23:40:01 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2017-01-09 00:50:51 -05:00
|
|
|
pub jku: Option<String>,
|
2017-04-12 21:08:07 -04:00
|
|
|
/// Key ID
|
|
|
|
///
|
|
|
|
/// Defined in [RFC7515#4.1.4](https://tools.ietf.org/html/rfc7515#section-4.1.4).
|
2017-04-10 23:40:01 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2017-01-09 00:50:51 -05:00
|
|
|
pub kid: Option<String>,
|
2017-04-12 21:08:07 -04:00
|
|
|
/// X.509 URL
|
|
|
|
///
|
|
|
|
/// Defined in [RFC7515#4.1.5](https://tools.ietf.org/html/rfc7515#section-4.1.5).
|
2017-04-10 23:40:01 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2017-01-09 00:50:51 -05:00
|
|
|
pub x5u: Option<String>,
|
2017-04-12 21:08:07 -04:00
|
|
|
/// X.509 certificate thumbprint
|
|
|
|
///
|
|
|
|
/// Defined in [RFC7515#4.1.7](https://tools.ietf.org/html/rfc7515#section-4.1.7).
|
2017-04-10 23:40:01 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub x5t: Option<String>,
|
2017-01-09 00:50:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Header {
|
2017-04-12 21:08:07 -04:00
|
|
|
/// Returns a JWT header with the algorithm given
|
2019-11-03 11:13:22 -05:00
|
|
|
pub fn new(algorithm: Algorithm) -> Self {
|
2017-01-09 00:50:51 -05:00
|
|
|
Header {
|
2017-08-25 04:51:44 -04:00
|
|
|
typ: Some("JWT".to_string()),
|
2017-01-09 00:50:51 -05:00
|
|
|
alg: algorithm,
|
2017-04-12 21:08:07 -04:00
|
|
|
cty: None,
|
2017-01-09 00:50:51 -05:00
|
|
|
jku: None,
|
|
|
|
kid: None,
|
|
|
|
x5u: None,
|
2017-04-12 21:08:07 -04:00
|
|
|
x5t: None,
|
2017-01-09 00:50:51 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-03 11:13:22 -05:00
|
|
|
|
|
|
|
/// Converts an encoded part into the Header struct if possible
|
|
|
|
pub(crate) fn from_encoded(encoded_part: &str) -> Result<Self> {
|
2019-11-11 14:16:34 -05:00
|
|
|
let decoded = b64_decode(encoded_part)?;
|
2019-11-03 11:13:22 -05:00
|
|
|
let s = String::from_utf8(decoded)?;
|
|
|
|
|
|
|
|
Ok(serde_json::from_str(&s)?)
|
|
|
|
}
|
2017-01-09 00:50:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Header {
|
2017-06-13 04:25:21 -04:00
|
|
|
/// Returns a JWT header using the default Algorithm, HS256
|
|
|
|
fn default() -> Self {
|
|
|
|
Header::new(Algorithm::default())
|
2017-01-09 00:50:51 -05:00
|
|
|
}
|
|
|
|
}
|