@@ -11,10 +11,10 @@ use crate::headers::{Header, HeaderName, HeaderValue, Headers, AUTHORIZATION};
1111/// # Examples
1212///
1313/// ```
14- /// # fn main() -> http_types ::Result<()> {
14+ /// # fn main() -> http_types_rs ::Result<()> {
1515/// #
16- /// use http_types ::Response;
17- /// use http_types ::auth::{AuthenticationScheme, Authorization};
16+ /// use http_types_rs ::Response;
17+ /// use http_types_rs ::auth::{AuthenticationScheme, Authorization};
1818///
1919/// let scheme = AuthenticationScheme::Basic;
2020/// let credentials = "0xdeadbeef202020";
@@ -32,107 +32,99 @@ use crate::headers::{Header, HeaderName, HeaderValue, Headers, AUTHORIZATION};
3232/// ```
3333#[ derive( Debug ) ]
3434pub struct Authorization {
35- scheme : AuthenticationScheme ,
36- credentials : String ,
35+ scheme : AuthenticationScheme ,
36+ credentials : String ,
3737}
3838
3939impl Authorization {
40- /// Create a new instance of `Authorization`.
41- pub fn new ( scheme : AuthenticationScheme , credentials : String ) -> Self {
42- Self {
43- scheme,
44- credentials,
45- }
46- }
47-
48- /// Create a new instance from headers.
49- pub fn from_headers ( headers : impl AsRef < Headers > ) -> crate :: Result < Option < Self > > {
50- let headers = match headers. as_ref ( ) . get ( AUTHORIZATION ) {
51- Some ( headers) => headers,
52- None => return Ok ( None ) ,
53- } ;
54-
55- // If we successfully parsed the header then there's always at least one
56- // entry. We want the last entry.
57- let value = headers. iter ( ) . last ( ) . unwrap ( ) ;
58-
59- let mut iter = value. as_str ( ) . splitn ( 2 , ' ' ) ;
60- let scheme = iter. next ( ) ;
61- let credential = iter. next ( ) ;
62- let ( scheme, credentials) = match ( scheme, credential) {
63- ( None , _) => bail ! ( 400 , "Could not find scheme" ) ,
64- ( Some ( _) , None ) => bail ! ( 400 , "Could not find credentials" ) ,
65- ( Some ( scheme) , Some ( credentials) ) => ( scheme. parse ( ) ?, credentials. to_owned ( ) ) ,
66- } ;
67-
68- Ok ( Some ( Self {
69- scheme,
70- credentials,
71- } ) )
72- }
73-
74- /// Get the authorization scheme.
75- pub fn scheme ( & self ) -> AuthenticationScheme {
76- self . scheme
77- }
78-
79- /// Set the authorization scheme.
80- pub fn set_scheme ( & mut self , scheme : AuthenticationScheme ) {
81- self . scheme = scheme;
82- }
83-
84- /// Get the authorization credentials.
85- pub fn credentials ( & self ) -> & str {
86- self . credentials . as_str ( )
87- }
88-
89- /// Set the authorization credentials.
90- pub fn set_credentials ( & mut self , credentials : String ) {
91- self . credentials = credentials;
92- }
40+ /// Create a new instance of `Authorization`.
41+ pub fn new ( scheme : AuthenticationScheme , credentials : String ) -> Self {
42+ Self { scheme, credentials }
43+ }
44+
45+ /// Create a new instance from headers.
46+ pub fn from_headers ( headers : impl AsRef < Headers > ) -> crate :: Result < Option < Self > > {
47+ let headers = match headers. as_ref ( ) . get ( AUTHORIZATION ) {
48+ Some ( headers) => headers,
49+ None => return Ok ( None ) ,
50+ } ;
51+
52+ // If we successfully parsed the header then there's always at least one
53+ // entry. We want the last entry.
54+ let value = headers. iter ( ) . last ( ) . unwrap ( ) ;
55+
56+ let mut iter = value. as_str ( ) . splitn ( 2 , ' ' ) ;
57+ let scheme = iter. next ( ) ;
58+ let credential = iter. next ( ) ;
59+ let ( scheme, credentials) = match ( scheme, credential) {
60+ ( None , _) => bail ! ( 400 , "Could not find scheme" ) ,
61+ ( Some ( _) , None ) => bail ! ( 400 , "Could not find credentials" ) ,
62+ ( Some ( scheme) , Some ( credentials) ) => ( scheme. parse ( ) ?, credentials. to_owned ( ) ) ,
63+ } ;
64+
65+ Ok ( Some ( Self { scheme, credentials } ) )
66+ }
67+
68+ /// Get the authorization scheme.
69+ pub fn scheme ( & self ) -> AuthenticationScheme {
70+ self . scheme
71+ }
72+
73+ /// Set the authorization scheme.
74+ pub fn set_scheme ( & mut self , scheme : AuthenticationScheme ) {
75+ self . scheme = scheme;
76+ }
77+
78+ /// Get the authorization credentials.
79+ pub fn credentials ( & self ) -> & str {
80+ self . credentials . as_str ( )
81+ }
82+
83+ /// Set the authorization credentials.
84+ pub fn set_credentials ( & mut self , credentials : String ) {
85+ self . credentials = credentials;
86+ }
9387}
9488
9589impl Header for Authorization {
96- fn header_name ( & self ) -> HeaderName {
97- AUTHORIZATION
98- }
90+ fn header_name ( & self ) -> HeaderName {
91+ AUTHORIZATION
92+ }
9993
100- fn header_value ( & self ) -> HeaderValue {
101- let output = format ! ( "{} {}" , self . scheme, self . credentials) ;
94+ fn header_value ( & self ) -> HeaderValue {
95+ let output = format ! ( "{} {}" , self . scheme, self . credentials) ;
10296
103- // SAFETY: the internal string is validated to be ASCII.
104- unsafe { HeaderValue :: from_bytes_unchecked ( output. into ( ) ) }
105- }
97+ // SAFETY: the internal string is validated to be ASCII.
98+ unsafe { HeaderValue :: from_bytes_unchecked ( output. into ( ) ) }
99+ }
106100}
107101
108102#[ cfg( test) ]
109103mod test {
110- use super :: * ;
111- use crate :: headers:: Headers ;
112-
113- #[ test]
114- fn smoke ( ) -> crate :: Result < ( ) > {
115- let scheme = AuthenticationScheme :: Basic ;
116- let credentials = "0xdeadbeef202020" ;
117- let authz = Authorization :: new ( scheme, credentials. into ( ) ) ;
118-
119- let mut headers = Headers :: new ( ) ;
120- authz. apply_header ( & mut headers) ;
121-
122- let authz = Authorization :: from_headers ( headers) ?. unwrap ( ) ;
123-
124- assert_eq ! ( authz. scheme( ) , AuthenticationScheme :: Basic ) ;
125- assert_eq ! ( authz. credentials( ) , credentials) ;
126- Ok ( ( ) )
127- }
128-
129- #[ test]
130- fn bad_request_on_parse_error ( ) {
131- let mut headers = Headers :: new ( ) ;
132- headers
133- . insert ( AUTHORIZATION , "<nori ate the tag. yum.>" )
134- . unwrap ( ) ;
135- let err = Authorization :: from_headers ( headers) . unwrap_err ( ) ;
136- assert_eq ! ( err. status( ) , 400 ) ;
137- }
104+ use super :: * ;
105+ use crate :: headers:: Headers ;
106+
107+ #[ test]
108+ fn smoke ( ) -> crate :: Result < ( ) > {
109+ let scheme = AuthenticationScheme :: Basic ;
110+ let credentials = "0xdeadbeef202020" ;
111+ let authz = Authorization :: new ( scheme, credentials. into ( ) ) ;
112+
113+ let mut headers = Headers :: new ( ) ;
114+ authz. apply_header ( & mut headers) ;
115+
116+ let authz = Authorization :: from_headers ( headers) ?. unwrap ( ) ;
117+
118+ assert_eq ! ( authz. scheme( ) , AuthenticationScheme :: Basic ) ;
119+ assert_eq ! ( authz. credentials( ) , credentials) ;
120+ Ok ( ( ) )
121+ }
122+
123+ #[ test]
124+ fn bad_request_on_parse_error ( ) {
125+ let mut headers = Headers :: new ( ) ;
126+ headers. insert ( AUTHORIZATION , "<nori ate the tag. yum.>" ) . unwrap ( ) ;
127+ let err = Authorization :: from_headers ( headers) . unwrap_err ( ) ;
128+ assert_eq ! ( err. status( ) , 400 ) ;
129+ }
138130}
0 commit comments