pub struct SecurityScopes { /* private fields */ }Expand description
Required OAuth2 security scopes for a handler.
SecurityScopes aggregates the scopes required by the current handler’s
security dependency chain. It is typically injected alongside a bearer token
extractor so the handler can verify that the token grants the required
scopes.
In Python FastAPI, SecurityScopes is automatically populated from the
scopes parameter on Security(...) dependencies. In this Rust
implementation, scopes are set via request extensions (populated by
middleware or route configuration) and read by the FromRequest impl.
§Example
use fastapi_core::{SecurityScopes, BearerToken};
async fn get_admin(
token: BearerToken,
scopes: SecurityScopes,
) -> impl IntoResponse {
// scopes.scopes() returns ["admin", "users:read"]
// Verify the token grants all required scopes
for scope in scopes.scopes() {
// check token has scope...
}
}Implementations§
Source§impl SecurityScopes
impl SecurityScopes
Sourcepub fn from_scopes(scopes: impl IntoIterator<Item = impl Into<String>>) -> Self
pub fn from_scopes(scopes: impl IntoIterator<Item = impl Into<String>>) -> Self
Create from a list of scope strings.
Duplicates are removed while preserving order.
Sourcepub fn from_scope_str(scope_str: &str) -> Self
pub fn from_scope_str(scope_str: &str) -> Self
Create from a space-separated scope string.
The string is split on spaces, empty segments are ignored, and duplicates are removed while preserving order.
Sourcepub fn merge(&mut self, other: &SecurityScopes)
pub fn merge(&mut self, other: &SecurityScopes)
Merge another set of scopes into this one.
New scopes are appended, preserving order and deduplicating.
Sourcepub fn merged(&self, other: &SecurityScopes) -> Self
pub fn merged(&self, other: &SecurityScopes) -> Self
Create a new SecurityScopes by merging two sets.
Trait Implementations§
Source§impl Clone for SecurityScopes
impl Clone for SecurityScopes
Source§fn clone(&self) -> SecurityScopes
fn clone(&self) -> SecurityScopes
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more