ezidam/crates/openid/src/scopes.rs

53 lines
1.5 KiB
Rust

use itertools::Itertools;
use openidconnect::Scope;
use std::collections::HashSet;
pub struct SupportedScopes(pub Vec<Scope>);
impl SupportedScopes {
pub fn url_format() -> String {
Self::default().0.iter().map(|s| s.as_str()).join(" ")
}
pub fn check_supported_scopes(scopes: &str) -> bool {
if scopes.is_empty() {
return false;
}
let list = Self::default();
let scope_set: &HashSet<_> = &list.0.iter().map(|s| s.as_str()).collect();
let requested_scopes: HashSet<_> = scopes.split_whitespace().collect();
requested_scopes.is_subset(scope_set)
}
}
impl Default for SupportedScopes {
fn default() -> Self {
Self(vec![
Scope::new("openid".to_string()),
Scope::new("profile".to_string()),
Scope::new("email".to_string()),
])
}
}
#[cfg(test)]
mod tests {
use super::SupportedScopes;
#[test]
fn check_valid() {
assert!(SupportedScopes::check_supported_scopes("openid"));
assert!(SupportedScopes::check_supported_scopes("profile email"));
assert!(SupportedScopes::check_supported_scopes("email openid"));
}
#[test]
fn check_invalid() {
assert!(!SupportedScopes::check_supported_scopes(""));
assert!(!SupportedScopes::check_supported_scopes("openid abc"));
assert!(!SupportedScopes::check_supported_scopes("test"));
assert!(!SupportedScopes::check_supported_scopes(
"email testing wrong profile"
));
}
}