Skip to main content

parse_algorithm

Function parse_algorithm 

Source
pub fn parse_algorithm(name: &str) -> Result<Algorithm, AlgorithmError>
Expand description

Parse one crate::OAuthConfig::algorithms entry, refusing everything that must never be accepted with the reason spelled out (the error lands in a startup failure). crate::OAuthConfig::resolve calls it for every entry; it is public for applications that validate an algorithm list themselves.

Names are JWS names, matched case-sensitively after trimming.

§Errors

AlgorithmError::Unsigned for none (in any case), AlgorithmError::Hmac for HS256/HS384/HS512, and AlgorithmError::Unsupported for anything else that is not a JWS algorithm this crate can verify (including ES512, which the underlying ring cannot).

§Examples

use oauth_resource_server::{Algorithm, AlgorithmError, parse_algorithm};

assert_eq!(parse_algorithm("ES256"), Ok(Algorithm::ES256));
let err = parse_algorithm("HS256").unwrap_err();
assert!(matches!(err, AlgorithmError::Hmac { .. }));
assert!(err.to_string().contains("key-confusion"));
assert!(matches!(parse_algorithm("none"), Err(AlgorithmError::Unsigned { .. })));