Skip to content

Commit

Permalink
Return clearer errors when splitting components and ensure there are …
Browse files Browse the repository at this point in the history
…exactly three
  • Loading branch information
mikkyang committed Jun 28, 2020
1 parent c9050c9 commit 0a66d00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub enum Error {
AlgorithmMismatch(AlgorithmType, AlgorithmType),
NoKeyId,
NoKeyWithKeyId(String),
NoHeaderComponent,
NoClaimsComponent,
NoSignatureComponent,
TooManyComponents,
Format,
Base64(DecodeError),
Json(JsonError),
Expand All @@ -29,6 +33,10 @@ impl fmt::Display for Error {
}
NoKeyId => write!(f, "No key id found"),
NoKeyWithKeyId(ref kid) => write!(f, "Key with key id {} not found", kid),
NoHeaderComponent => write!(f, "No header component found in token string"),
NoClaimsComponent => write!(f, "No claims component found in token string"),
NoSignatureComponent => write!(f, "No signature component found in token string"),
TooManyComponents => write!(f, "Too many components found in token string"),
Format => write!(f, "Format"),
Base64(ref x) => write!(f, "{}", x),
Json(ref x) => write!(f, "{}", x),
Expand Down
10 changes: 7 additions & 3 deletions src/token/verified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ impl<'a, H: FromBase64, C: FromBase64> Token<H, C, Unverified<'a>> {

pub(crate) fn split_components(token: &str) -> Result<[&str; 3], Error> {
let mut components = token.split(SEPARATOR);
let header = components.next().ok_or(Error::Format)?;
let claims = components.next().ok_or(Error::Format)?;
let signature = components.next().ok_or(Error::Format)?;
let header = components.next().ok_or(Error::NoHeaderComponent)?;
let claims = components.next().ok_or(Error::NoClaimsComponent)?;
let signature = components.next().ok_or(Error::NoSignatureComponent)?;

if components.next().is_some() {
return Err(Error::TooManyComponents);
}

Ok([header, claims, signature])
}
Expand Down

0 comments on commit 0a66d00

Please sign in to comment.