Skip to main content

ruma_identifiers_validation/
oauth_scope.rs

1use crate::Error;
2
3/// Validate the format of an OAuth scope, as defined in [RFC 6749].
4///
5/// OAuth scopes may only contain:
6/// - \x21 (`!`)
7/// - Characters in the range \x23 (`#`) to \x5B (`[`)
8/// - Characters in the range \x5D (`]`) to \x7E (`~`)
9///
10/// [RFC 6749]: https://datatracker.ietf.org/doc/html/rfc6749#section-3.3
11pub fn validate(scope: &str) -> Result<(), Error> {
12    if scope.is_empty() {
13        return Err(Error::Empty);
14    }
15
16    if !scope.chars().all(|char| matches!(char, '\x21' | '\x23'..='\x5B' | '\x5D'..='\x7E')) {
17        return Err(Error::InvalidCharacters);
18    }
19
20    Ok(())
21}