-
Notifications
You must be signed in to change notification settings - Fork 8
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
Public Metadata Issuance #25
Open
cbranch
wants to merge
4
commits into
raphaelrobert:main
Choose a base branch
from
cbranch:cbranch/public-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dc41732
Parse extensions provided in the HTTP Authorization header
cbranch 52b4970
Implement the public metadata issuance protocol
cbranch 22f2c00
Update to the newest revision and allow for some quirks
fisherdarling 3a34d2d
Merge pull request #1 from fisherdarling/fisher/newest-revision-updates
cbranch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
//! This module contains the authorization logic for redemption phase of the | ||
//! protocol. | ||
|
||
use base64::engine::general_purpose::URL_SAFE_NO_PAD; | ||
use base64::{engine::general_purpose::URL_SAFE, Engine as _}; | ||
use generic_array::{ArrayLength, GenericArray}; | ||
use http::{header::HeaderName, HeaderValue}; | ||
use nom::branch::alt; | ||
use nom::sequence::delimited; | ||
use nom::{ | ||
bytes::complete::{tag, tag_no_case}, | ||
multi::{many1, separated_list1}, | ||
|
@@ -148,6 +151,30 @@ pub fn build_authorization_header<Nk: ArrayLength<u8>>( | |
Ok((header_name, header_value)) | ||
} | ||
|
||
/// Builds a `Authorize` header according to the following scheme: | ||
/// | ||
/// `PrivateToken token=... extensions=...` | ||
/// | ||
/// # Errors | ||
/// Returns an error if the token is not valid. | ||
pub fn build_authorization_header_ext<Nk: ArrayLength<u8>>( | ||
token: &Token<Nk>, | ||
extensions: &[u8], | ||
) -> Result<(HeaderName, HeaderValue), BuildError> { | ||
let value = format!( | ||
"PrivateToken token={} extensions={}", | ||
URL_SAFE.encode( | ||
token | ||
.tls_serialize_detached() | ||
.map_err(|_| BuildError::InvalidToken)? | ||
), | ||
URL_SAFE.encode(extensions), | ||
); | ||
let header_name = http::header::AUTHORIZATION; | ||
let header_value = HeaderValue::from_str(&value).map_err(|_| BuildError::InvalidToken)?; | ||
Ok((header_name, header_value)) | ||
} | ||
|
||
/// Building error for the `Authorization` header values | ||
#[derive(Error, Debug)] | ||
pub enum BuildError { | ||
|
@@ -167,10 +194,24 @@ pub fn parse_authorization_header<Nk: ArrayLength<u8>>( | |
) -> Result<Token<Nk>, ParseError> { | ||
let s = value.to_str().map_err(|_| ParseError::InvalidInput)?; | ||
let tokens = parse_header_value(s)?; | ||
let token = tokens[0].clone(); | ||
let token = tokens[0].0.clone(); | ||
Ok(token) | ||
} | ||
|
||
/// Parses an `Authorization` header according to the following scheme: | ||
/// | ||
/// `PrivateToken token=... [extensions=...]` | ||
/// | ||
/// # Errors | ||
/// Returns an error if the header value is not valid. | ||
pub fn parse_authorization_header_ext<Nk: ArrayLength<u8>>( | ||
value: &HeaderValue, | ||
) -> Result<(Token<Nk>, Option<Vec<u8>>), ParseError> { | ||
let s = value.to_str().map_err(|_| ParseError::InvalidInput)?; | ||
let mut tokens = parse_header_value(s)?; | ||
Ok(tokens.pop().unwrap()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should return an error instead |
||
} | ||
|
||
/// Parsing error for the `WWW-Authenticate` header values | ||
#[derive(Error, Debug)] | ||
pub enum ParseError { | ||
|
@@ -189,7 +230,10 @@ fn parse_key_value(input: &str) -> IResult<&str, (&str, &str)> { | |
let (input, _) = tag("=")(input)?; | ||
let (input, _) = opt_spaces(input)?; | ||
let (input, value) = match key.to_lowercase().as_str() { | ||
"token" => base64_char(input)?, | ||
"token" | "extensions" => { | ||
// Values may or may not be delimited with quotes. | ||
alt((delimited(tag("\""), base64_char, tag("\"")), base64_char))(input)? | ||
} | ||
_ => { | ||
return Err(nom::Err::Failure(nom::error::make_error( | ||
input, | ||
|
@@ -200,13 +244,14 @@ fn parse_key_value(input: &str) -> IResult<&str, (&str, &str)> { | |
Ok((input, (key, value))) | ||
} | ||
|
||
fn parse_private_token(input: &str) -> IResult<&str, &str> { | ||
fn parse_private_token(input: &str) -> IResult<&str, (&str, Option<&str>)> { | ||
let (input, _) = opt_spaces(input)?; | ||
let (input, _) = tag_no_case("PrivateToken")(input)?; | ||
let (input, _) = many1(space)(input)?; | ||
let (input, key_values) = separated_list1(tag(","), parse_key_value)(input)?; | ||
let (input, key_values) = separated_list1(tag(" "), parse_key_value)(input)?; | ||
|
||
let mut token = None; | ||
let mut extensions = None; | ||
let err = nom::Err::Failure(nom::error::make_error(input, nom::error::ErrorKind::Tag)); | ||
|
||
for (key, value) in key_values { | ||
|
@@ -217,33 +262,50 @@ fn parse_private_token(input: &str) -> IResult<&str, &str> { | |
} | ||
token = Some(value) | ||
} | ||
"extensions" => { | ||
if extensions.is_some() { | ||
return Err(err); | ||
} | ||
extensions = Some(value) | ||
} | ||
_ => return Err(err), | ||
} | ||
} | ||
let token = token.ok_or(err)?; | ||
|
||
Ok((input, token)) | ||
Ok((input, (token, extensions))) | ||
} | ||
|
||
fn parse_private_tokens(input: &str) -> IResult<&str, Vec<&str>> { | ||
fn parse_private_tokens(input: &str) -> IResult<&str, Vec<(&str, Option<&str>)>> { | ||
separated_list1(tag(","), parse_private_token)(input) | ||
} | ||
|
||
fn parse_header_value<Nk: ArrayLength<u8>>(input: &str) -> Result<Vec<Token<Nk>>, ParseError> { | ||
fn parse_header_value<Nk: ArrayLength<u8>>( | ||
input: &str, | ||
) -> Result<Vec<(Token<Nk>, Option<Vec<u8>>)>, ParseError> { | ||
let (output, tokens) = parse_private_tokens(input).map_err(|_| ParseError::InvalidInput)?; | ||
if !output.is_empty() { | ||
return Err(ParseError::InvalidInput); | ||
} | ||
let tokens = tokens | ||
.into_iter() | ||
.map(|token_value| { | ||
Token::tls_deserialize( | ||
&mut URL_SAFE | ||
.decode(token_value) | ||
.map_err(|_| ParseError::InvalidToken)? | ||
.as_slice(), | ||
) | ||
.map_err(|_| ParseError::InvalidToken) | ||
.map(|(token_value, extensions_value)| { | ||
let ext = extensions_value.and_then(|x| { | ||
URL_SAFE_NO_PAD | ||
.decode(x) | ||
.or_else(|_| URL_SAFE.decode(x)) | ||
.ok() | ||
}); | ||
Ok(( | ||
Token::tls_deserialize( | ||
&mut URL_SAFE | ||
.decode(token_value) | ||
.map_err(|_| ParseError::InvalidToken)? | ||
.as_slice(), | ||
) | ||
.map_err(|_| ParseError::InvalidToken)?, | ||
ext, | ||
)) | ||
}) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
Ok(tokens) | ||
|
@@ -275,3 +337,32 @@ fn builder_parser_test() { | |
assert_eq!(token.token_key_id(), &token_key_id); | ||
assert_eq!(token.authenticator(), &authenticator); | ||
} | ||
|
||
#[test] | ||
fn builder_parser_extensions_test() { | ||
use generic_array::typenum::U32; | ||
|
||
let nonce = [1u8; 32]; | ||
let challenge_digest = [2u8; 32]; | ||
let token_key_id = [3u8; 32]; | ||
let authenticator = [4u8; 32]; | ||
let token = Token::<U32>::new( | ||
TokenType::Private, | ||
nonce, | ||
challenge_digest, | ||
token_key_id, | ||
GenericArray::clone_from_slice(&authenticator), | ||
); | ||
let extensions = b"hello world"; | ||
let (header_name, header_value) = build_authorization_header_ext(&token, extensions).unwrap(); | ||
|
||
assert_eq!(header_name, http::header::AUTHORIZATION); | ||
|
||
let (token, maybe_extensions) = parse_authorization_header_ext::<U32>(&header_value).unwrap(); | ||
assert_eq!(token.token_type(), TokenType::Private); | ||
assert_eq!(token.nonce(), nonce); | ||
assert_eq!(token.challenge_digest(), &challenge_digest); | ||
assert_eq!(token.token_key_id(), &token_key_id); | ||
assert_eq!(token.authenticator(), &authenticator); | ||
assert_eq!(maybe_extensions, Some(extensions.to_vec())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.