| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
use std::collections::BTreeSet; |
| 10 |
use std::fmt; |
| 11 |
use std::str::FromStr; |
| 12 |
|
| 13 |
|
| 14 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 15 |
pub enum OAuthScope { |
| 16 |
|
| 17 |
ProfileRead, |
| 18 |
|
| 19 |
PerksRead, |
| 20 |
|
| 21 |
Offline, |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
Sync, |
| 26 |
} |
| 27 |
|
| 28 |
impl OAuthScope { |
| 29 |
pub fn as_str(self) -> &'static str { |
| 30 |
match self { |
| 31 |
OAuthScope::ProfileRead => "profile:read", |
| 32 |
OAuthScope::PerksRead => "perks:read", |
| 33 |
OAuthScope::Offline => "offline_access", |
| 34 |
OAuthScope::Sync => "sync", |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
impl FromStr for OAuthScope { |
| 40 |
type Err = (); |
| 41 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 42 |
match s { |
| 43 |
"profile:read" => Ok(OAuthScope::ProfileRead), |
| 44 |
"perks:read" => Ok(OAuthScope::PerksRead), |
| 45 |
"offline_access" => Ok(OAuthScope::Offline), |
| 46 |
"sync" => Ok(OAuthScope::Sync), |
| 47 |
_ => Err(()), |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
#[derive(Debug, Clone, PartialEq, Eq, Default)] |
| 54 |
pub struct GrantedScopes(BTreeSet<OAuthScope>); |
| 55 |
|
| 56 |
impl GrantedScopes { |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
pub fn parse(raw: &str) -> Self { |
| 62 |
GrantedScopes( |
| 63 |
raw.split_whitespace() |
| 64 |
.filter_map(|s| OAuthScope::from_str(s).ok()) |
| 65 |
.collect(), |
| 66 |
) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
pub fn default_userinfo() -> Self { |
| 73 |
let mut set = BTreeSet::new(); |
| 74 |
set.insert(OAuthScope::ProfileRead); |
| 75 |
set.insert(OAuthScope::PerksRead); |
| 76 |
GrantedScopes(set) |
| 77 |
} |
| 78 |
|
| 79 |
pub fn contains(&self, scope: OAuthScope) -> bool { |
| 80 |
self.0.contains(&scope) |
| 81 |
} |
| 82 |
|
| 83 |
pub fn is_empty(&self) -> bool { |
| 84 |
self.0.is_empty() |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
pub fn is_sync_request(&self) -> bool { |
| 98 |
self.contains(OAuthScope::Sync) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
pub fn subset_of(&self, other: &GrantedScopes) -> bool { |
| 106 |
self.0.is_subset(&other.0) |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
pub fn union_with(&mut self, other: &GrantedScopes) { |
| 112 |
self.0.extend(other.0.iter().copied()); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
impl fmt::Display for GrantedScopes { |
| 117 |
|
| 118 |
|
| 119 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 120 |
let mut first = true; |
| 121 |
for scope in &self.0 { |
| 122 |
if !first { |
| 123 |
f.write_str(" ")?; |
| 124 |
} |
| 125 |
f.write_str(scope.as_str())?; |
| 126 |
first = false; |
| 127 |
} |
| 128 |
Ok(()) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
#[cfg(test)] |
| 133 |
mod tests { |
| 134 |
use super::*; |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn parse_round_trips_canonical() { |
| 138 |
let s = GrantedScopes::parse("perks:read profile:read"); |
| 139 |
|
| 140 |
assert_eq!(s.to_string(), "profile:read perks:read"); |
| 141 |
} |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn parse_drops_unknown_scopes() { |
| 145 |
let s = GrantedScopes::parse("profile:read admin:everything perks:read"); |
| 146 |
assert!(s.contains(OAuthScope::ProfileRead)); |
| 147 |
assert!(s.contains(OAuthScope::PerksRead)); |
| 148 |
assert_eq!(s.to_string(), "profile:read perks:read"); |
| 149 |
} |
| 150 |
|
| 151 |
#[test] |
| 152 |
fn default_has_no_offline() { |
| 153 |
let s = GrantedScopes::default_userinfo(); |
| 154 |
assert!(s.contains(OAuthScope::ProfileRead)); |
| 155 |
assert!(s.contains(OAuthScope::PerksRead)); |
| 156 |
assert!(!s.contains(OAuthScope::Offline)); |
| 157 |
} |
| 158 |
|
| 159 |
#[test] |
| 160 |
fn subset_of_enforces_downgrade_only() { |
| 161 |
let granted = GrantedScopes::parse("profile:read perks:read offline_access"); |
| 162 |
let narrower = GrantedScopes::parse("perks:read"); |
| 163 |
let same = GrantedScopes::parse("profile:read perks:read offline_access"); |
| 164 |
let wider = GrantedScopes::parse("profile:read perks:read offline_access"); |
| 165 |
assert!(narrower.subset_of(&granted)); |
| 166 |
assert!(same.subset_of(&granted)); |
| 167 |
|
| 168 |
let escalated = GrantedScopes::parse("perks:read"); |
| 169 |
assert!(!granted.subset_of(&escalated)); |
| 170 |
assert!(wider.subset_of(&granted)); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn empty_string_parses_empty() { |
| 175 |
assert!(GrantedScopes::parse("").is_empty()); |
| 176 |
assert!(GrantedScopes::parse(" ").is_empty()); |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
fn union_with_merges_and_dedups() { |
| 181 |
let mut a = GrantedScopes::parse("profile:read"); |
| 182 |
a.union_with(&GrantedScopes::parse("perks:read profile:read")); |
| 183 |
assert_eq!(a.to_string(), "profile:read perks:read"); |
| 184 |
|
| 185 |
assert!(GrantedScopes::parse("perks:read").subset_of(&a)); |
| 186 |
assert!(!GrantedScopes::parse("offline_access").subset_of(&a)); |
| 187 |
} |
| 188 |
|
| 189 |
#[test] |
| 190 |
fn sync_request_detection() { |
| 191 |
|
| 192 |
assert!(GrantedScopes::parse("sync").is_sync_request()); |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
assert!(!GrantedScopes::parse("").is_sync_request()); |
| 197 |
assert!(!GrantedScopes::parse(" ").is_sync_request()); |
| 198 |
|
| 199 |
assert!(!GrantedScopes::parse("admin:everything").is_sync_request()); |
| 200 |
|
| 201 |
assert!(!GrantedScopes::parse("profile:read").is_sync_request()); |
| 202 |
assert!(!GrantedScopes::parse("profile:read perks:read").is_sync_request()); |
| 203 |
|
| 204 |
assert_eq!(GrantedScopes::parse("sync").to_string(), "sync"); |
| 205 |
} |
| 206 |
} |
| 207 |
|