|
| 1 | +use crate::auth::{constants::GRPC_AUTH_HEADER, AuthError}; |
| 2 | + |
| 3 | +use anyhow::{bail, Context as _, Result}; |
| 4 | +use axum::http::HeaderValue; |
| 5 | +use tonic::metadata::MetadataMap; |
| 6 | + |
| 7 | +pub fn parse_http_basic_auth_arg(arg: &str) -> Result<Option<String>> { |
| 8 | + if arg == "always" { |
| 9 | + return Ok(None); |
| 10 | + } |
| 11 | + |
| 12 | + let Some((scheme, param)) = arg.split_once(':') else { |
| 13 | + bail!("invalid HTTP auth config: {arg}") |
| 14 | + }; |
| 15 | + |
| 16 | + if scheme == "basic" { |
| 17 | + Ok(Some(param.into())) |
| 18 | + } else { |
| 19 | + bail!("unsupported HTTP auth scheme: {scheme:?}") |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +pub fn parse_jwt_key(data: &str) -> Result<jsonwebtoken::DecodingKey> { |
| 24 | + if data.starts_with("-----BEGIN PUBLIC KEY-----") { |
| 25 | + jsonwebtoken::DecodingKey::from_ed_pem(data.as_bytes()) |
| 26 | + .context("Could not decode Ed25519 public key from PEM") |
| 27 | + } else if data.starts_with("-----BEGIN PRIVATE KEY-----") { |
| 28 | + bail!("Received a private key, but a public key is expected") |
| 29 | + } else if data.starts_with("-----BEGIN") { |
| 30 | + bail!("Key is in unsupported PEM format") |
| 31 | + } else { |
| 32 | + jsonwebtoken::DecodingKey::from_ed_components(data) |
| 33 | + .context("Could not decode Ed25519 public key from base64") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +pub(crate) fn parse_grpc_auth_header(metadata: &MetadataMap) -> Option<HeaderValue> { |
| 38 | + metadata |
| 39 | + .get(GRPC_AUTH_HEADER) |
| 40 | + .map(|v| v.to_bytes().expect("Auth should always be ASCII")) |
| 41 | + .map(|v| HeaderValue::from_maybe_shared(v).expect("Should already be valid header")) |
| 42 | +} |
| 43 | + |
| 44 | +pub fn parse_http_auth_header<'a>( |
| 45 | + expected_scheme: &str, |
| 46 | + auth_header: &'a Option<HeaderValue>, |
| 47 | +) -> Result<&'a str, AuthError> { |
| 48 | + let Some(header) = auth_header else { |
| 49 | + return Err(AuthError::HttpAuthHeaderMissing); |
| 50 | + }; |
| 51 | + |
| 52 | + let Ok(header) = header.to_str() else { |
| 53 | + return Err(AuthError::HttpAuthHeaderInvalid); |
| 54 | + }; |
| 55 | + |
| 56 | + let Some((scheme, param)) = header.split_once(' ') else { |
| 57 | + return Err(AuthError::HttpAuthHeaderInvalid); |
| 58 | + }; |
| 59 | + |
| 60 | + if !scheme.eq_ignore_ascii_case(expected_scheme) { |
| 61 | + return Err(AuthError::HttpAuthHeaderUnsupportedScheme); |
| 62 | + } |
| 63 | + |
| 64 | + Ok(param) |
| 65 | +} |
| 66 | + |
| 67 | +#[cfg(test)] |
| 68 | +mod tests { |
| 69 | + use axum::http::HeaderValue; |
| 70 | + use hyper::header::AUTHORIZATION; |
| 71 | + |
| 72 | + use crate::auth::{parse_http_auth_header, AuthError}; |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn parse_http_auth_header_returns_auth_header_param_when_valid() { |
| 76 | + assert_eq!( |
| 77 | + parse_http_auth_header("basic", &HeaderValue::from_str("Basic abc").ok()).unwrap(), |
| 78 | + "abc" |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn parse_http_auth_header_errors_when_auth_header_missing() { |
| 84 | + assert_eq!( |
| 85 | + parse_http_auth_header("basic", &None).unwrap_err(), |
| 86 | + AuthError::HttpAuthHeaderMissing |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn parse_http_auth_header_errors_when_auth_header_cannot_be_converted_to_str() { |
| 92 | + assert_eq!( |
| 93 | + parse_http_auth_header("basic", &Some(HeaderValue::from_name(AUTHORIZATION))) |
| 94 | + .unwrap_err(), |
| 95 | + AuthError::HttpAuthHeaderInvalid |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn parse_http_auth_header_errors_when_auth_header_invalid_format() { |
| 101 | + assert_eq!( |
| 102 | + parse_http_auth_header("basic", &HeaderValue::from_str("invalid").ok()).unwrap_err(), |
| 103 | + AuthError::HttpAuthHeaderInvalid |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn parse_http_auth_header_errors_when_auth_header_is_unsupported_scheme() { |
| 109 | + assert_eq!( |
| 110 | + parse_http_auth_header("basic", &HeaderValue::from_str("Bearer abc").ok()).unwrap_err(), |
| 111 | + AuthError::HttpAuthHeaderUnsupportedScheme |
| 112 | + ) |
| 113 | + } |
| 114 | +} |
0 commit comments