pub struct WebSocketUser(/* private fields */);Expand description
Wrapper for extracting an authenticated user during WebSocket upgrade.
This is a convenience wrapper that extracts the User from the HttpRequest
extensions during the WebSocket handshake phase.
§Spring Security Equivalent
@AuthenticationPrincipal in a WebSocket handler
§Example
use actix_web::{get, web, HttpRequest, HttpResponse};
use actix_security::http::security::websocket::WebSocketUser;
#[get("/ws")]
async fn ws_handler(
req: HttpRequest,
stream: web::Payload,
) -> Result<HttpResponse, actix_web::Error> {
// Extract user - returns error if not authenticated
let user = WebSocketUser::extract(&req)?;
// Pass user to WebSocket actor
let resp = actix_ws::start(MyActor::new(user.into_inner()), &req, stream)?;
Ok(resp)
}Implementations§
Source§impl WebSocketUser
impl WebSocketUser
Sourcepub fn extract(req: &HttpRequest) -> Result<Self, WebSocketSecurityError>
pub fn extract(req: &HttpRequest) -> Result<Self, WebSocketSecurityError>
Extracts the authenticated user from the request.
§Errors
Returns WebSocketSecurityError::Unauthorized if no user is present.
Sourcepub fn try_extract(req: &HttpRequest) -> Option<Self>
pub fn try_extract(req: &HttpRequest) -> Option<Self>
Extracts the user if present, otherwise returns None.
Sourcepub fn into_inner(self) -> User
pub fn into_inner(self) -> User
Returns the inner User.
Sourcepub fn has_any_role(&self, roles: &[&str]) -> bool
pub fn has_any_role(&self, roles: &[&str]) -> bool
Checks if the user has any of the specified roles.
Checks if the user has the specified authority.
Checks if the user has any of the specified authorities.
Sourcepub fn get_username(&self) -> &str
pub fn get_username(&self) -> &str
Returns the username.
Sourcepub fn require_role(self, role: &str) -> Result<Self, WebSocketSecurityError>
pub fn require_role(self, role: &str) -> Result<Self, WebSocketSecurityError>
Requires the user to have the specified role.
§Errors
Returns WebSocketSecurityError::MissingRole if the user doesn’t have the role.
Sourcepub fn require_any_role(
self,
roles: &[&str],
) -> Result<Self, WebSocketSecurityError>
pub fn require_any_role( self, roles: &[&str], ) -> Result<Self, WebSocketSecurityError>
Requires the user to have any of the specified roles.
§Errors
Returns WebSocketSecurityError::MissingRole if the user doesn’t have any of the roles.
Requires the user to have the specified authority.
§Errors
Returns WebSocketSecurityError::MissingAuthority if the user doesn’t have the authority.
Requires the user to have any of the specified authorities.
§Errors
Returns WebSocketSecurityError::MissingAuthority if the user doesn’t have any of the authorities.
Methods from Deref<Target = User>§
Sourcepub fn get_username(&self) -> &str
pub fn get_username(&self) -> &str
Returns the username.
Sourcepub fn get_password(&self) -> &str
pub fn get_password(&self) -> &str
Returns the password (for authentication checks).
Returns the user’s authorities.
Sourcepub fn has_any_role(&self, roles: &[&str]) -> bool
pub fn has_any_role(&self, roles: &[&str]) -> bool
Checks if the user has ANY of the specified roles (OR logic).
Sourcepub fn has_all_roles(&self, roles: &[&str]) -> bool
pub fn has_all_roles(&self, roles: &[&str]) -> bool
Checks if the user has ALL of the specified roles (AND logic).
Checks if the user has a specific authority.
Checks if the user has ANY of the specified authorities (OR logic).
Checks if the user has ALL of the specified authorities (AND logic).
Trait Implementations§
Source§impl Clone for WebSocketUser
impl Clone for WebSocketUser
Source§fn clone(&self) -> WebSocketUser
fn clone(&self) -> WebSocketUser
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for WebSocketUser
impl Debug for WebSocketUser
Source§impl Deref for WebSocketUser
impl Deref for WebSocketUser
Source§impl From<WebSocketUser> for User
impl From<WebSocketUser> for User
Source§fn from(ws_user: WebSocketUser) -> Self
fn from(ws_user: WebSocketUser) -> Self
Auto Trait Implementations§
impl Freeze for WebSocketUser
impl RefUnwindSafe for WebSocketUser
impl Send for WebSocketUser
impl Sync for WebSocketUser
impl Unpin for WebSocketUser
impl UnwindSafe for WebSocketUser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more