1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-26 18:37:41 +02:00

actix-web beta 15 updates (#216)

This commit is contained in:
Rob Ede
2021-12-18 03:37:23 +00:00
committed by GitHub
parent c047cd5653
commit 6676a50944
26 changed files with 101 additions and 82 deletions

View File

@ -1,7 +1,7 @@
use std::fmt;
use actix_web::error::ParseError;
use actix_web::http::header::{Header, HeaderName, HeaderValue, IntoHeaderValue, AUTHORIZATION};
use actix_web::http::header::{Header, HeaderName, HeaderValue, TryIntoHeaderValue, AUTHORIZATION};
use actix_web::HttpMessage;
use crate::headers::authorization::scheme::Scheme;
@ -86,8 +86,8 @@ impl<S: Scheme> Header for Authorization<S> {
}
}
impl<S: Scheme> IntoHeaderValue for Authorization<S> {
type Error = <S as IntoHeaderValue>::Error;
impl<S: Scheme> TryIntoHeaderValue for Authorization<S> {
type Error = <S as TryIntoHeaderValue>::Error;
fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
self.0.try_into_value()

View File

@ -1,12 +1,11 @@
use std::borrow::Cow;
use std::fmt;
use std::str;
use std::{borrow::Cow, fmt, str};
use actix_web::http::header::{HeaderValue, IntoHeaderValue, InvalidHeaderValue};
use actix_web::web::{BufMut, BytesMut};
use actix_web::{
http::header::{HeaderValue, InvalidHeaderValue, TryIntoHeaderValue},
web::{BufMut, BytesMut},
};
use crate::headers::authorization::errors::ParseError;
use crate::headers::authorization::Scheme;
use crate::headers::authorization::{errors::ParseError, Scheme};
/// Credentials for `Basic` authentication scheme, defined in [RFC 7617](https://tools.ietf.org/html/rfc7617)
#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)]
@ -94,10 +93,10 @@ impl fmt::Display for Basic {
}
}
impl IntoHeaderValue for Basic {
impl TryIntoHeaderValue for Basic {
type Error = InvalidHeaderValue;
fn try_into_value(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
let mut credentials = BytesMut::with_capacity(
self.user_id.len()
+ 1 // ':'
@ -123,8 +122,7 @@ impl IntoHeaderValue for Basic {
#[cfg(test)]
mod tests {
use super::{Basic, Scheme};
use actix_web::http::header::{HeaderValue, IntoHeaderValue};
use super::*;
#[test]
fn test_parse_header() {

View File

@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::fmt;
use actix_web::http::header::{HeaderValue, IntoHeaderValue, InvalidHeaderValue};
use actix_web::http::header::{HeaderValue, InvalidHeaderValue, TryIntoHeaderValue};
use actix_web::web::{BufMut, BytesMut};
use crate::headers::authorization::errors::ParseError;
@ -73,10 +73,10 @@ impl fmt::Display for Bearer {
}
}
impl IntoHeaderValue for Bearer {
impl TryIntoHeaderValue for Bearer {
type Error = InvalidHeaderValue;
fn try_into_value(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
let mut buffer = BytesMut::with_capacity(7 + self.token.len());
buffer.put(&b"Bearer "[..]);
buffer.extend_from_slice(self.token.as_bytes());
@ -87,8 +87,7 @@ impl IntoHeaderValue for Bearer {
#[cfg(test)]
mod tests {
use super::{Bearer, Scheme};
use actix_web::http::header::{HeaderValue, IntoHeaderValue};
use super::*;
#[test]
fn test_parse_header() {

View File

@ -1,6 +1,6 @@
use std::fmt::{Debug, Display};
use actix_web::http::header::{HeaderValue, IntoHeaderValue};
use actix_web::http::header::{HeaderValue, TryIntoHeaderValue};
pub mod basic;
pub mod bearer;
@ -9,7 +9,7 @@ use crate::headers::authorization::errors::ParseError;
/// Authentication scheme for [`Authorization`](./struct.Authorization.html)
/// header.
pub trait Scheme: IntoHeaderValue + Debug + Display + Clone + Send + Sync {
pub trait Scheme: TryIntoHeaderValue + Debug + Display + Clone + Send + Sync {
/// Try to parse the authentication scheme from the `Authorization` header.
fn parse(header: &HeaderValue) -> Result<Self, ParseError>;
}

View File

@ -5,7 +5,7 @@ use std::default::Default;
use std::fmt;
use std::str;
use actix_web::http::header::{HeaderValue, IntoHeaderValue, InvalidHeaderValue};
use actix_web::http::header::{HeaderValue, InvalidHeaderValue, TryIntoHeaderValue};
use actix_web::web::{BufMut, Bytes, BytesMut};
use super::Challenge;
@ -103,18 +103,17 @@ impl fmt::Display for Basic {
}
}
impl IntoHeaderValue for Basic {
impl TryIntoHeaderValue for Basic {
type Error = InvalidHeaderValue;
fn try_into_value(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
HeaderValue::from_maybe_shared(self.to_bytes())
}
}
#[cfg(test)]
mod tests {
use super::Basic;
use actix_web::http::header::IntoHeaderValue;
use super::*;
#[test]
fn test_plain_into_header_value() {

View File

@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::fmt;
use std::str;
use actix_web::http::header::{HeaderValue, IntoHeaderValue, InvalidHeaderValue};
use actix_web::http::header::{HeaderValue, InvalidHeaderValue, TryIntoHeaderValue};
use actix_web::web::{BufMut, Bytes, BytesMut};
use super::super::Challenge;
@ -130,10 +130,10 @@ impl fmt::Display for Bearer {
}
}
impl IntoHeaderValue for Bearer {
impl TryIntoHeaderValue for Bearer {
type Error = InvalidHeaderValue;
fn try_into_value(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
HeaderValue::from_maybe_shared(self.to_bytes())
}
}

View File

@ -1,13 +1,12 @@
use std::fmt::{Debug, Display};
use actix_web::http::header::IntoHeaderValue;
use actix_web::web::Bytes;
use actix_web::{http::header::TryIntoHeaderValue, web::Bytes};
pub mod basic;
pub mod bearer;
/// Authentication challenge for `WWW-Authenticate` header.
pub trait Challenge: IntoHeaderValue + Debug + Display + Clone + Send + Sync {
pub trait Challenge: TryIntoHeaderValue + Debug + Display + Clone + Send + Sync {
/// Converts the challenge into a bytes suitable for HTTP transmission.
fn to_bytes(&self) -> Bytes;
}

View File

@ -1,6 +1,8 @@
use actix_web::error::ParseError;
use actix_web::http::header::{Header, HeaderName, HeaderValue, IntoHeaderValue, WWW_AUTHENTICATE};
use actix_web::HttpMessage;
use actix_web::{
error::ParseError,
http::header::{Header, HeaderName, HeaderValue, TryIntoHeaderValue, WWW_AUTHENTICATE},
HttpMessage,
};
use super::Challenge;
@ -22,10 +24,10 @@ impl<C: Challenge> Header for WwwAuthenticate<C> {
}
}
impl<C: Challenge> IntoHeaderValue for WwwAuthenticate<C> {
type Error = <C as IntoHeaderValue>::Error;
impl<C: Challenge> TryIntoHeaderValue for WwwAuthenticate<C> {
type Error = <C as TryIntoHeaderValue>::Error;
fn try_into_value(self) -> Result<HeaderValue, <Self as IntoHeaderValue>::Error> {
fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
self.0.try_into_value()
}
}

View File

@ -1,7 +1,6 @@
//! HTTP Authentication middleware.
use std::{
error::Error as StdError,
future::Future,
marker::PhantomData,
pin::Pin,
@ -125,7 +124,7 @@ where
O: Future<Output = Result<ServiceRequest, Error>> + 'static,
T: AuthExtractor + 'static,
B: MessageBody + 'static,
B::Error: StdError,
B::Error: Into<Error>,
{
type Response = ServiceResponse<EitherBody<B>>;
type Error = Error;
@ -160,7 +159,7 @@ where
O: Future<Output = Result<ServiceRequest, Error>> + 'static,
T: AuthExtractor + 'static,
B: MessageBody + 'static,
B::Error: StdError,
B::Error: Into<Error>,
{
type Response = ServiceResponse<EitherBody<B>>;
type Error = S::Error;