@@ -11,10 +11,10 @@ use crate::headers::{Header, HeaderName, HeaderValue, Headers, AUTHORIZATION};
11
11
/// # Examples
12
12
///
13
13
/// ```
14
- /// # fn main() -> http_types ::Result<()> {
14
+ /// # fn main() -> http_types_rs ::Result<()> {
15
15
/// #
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};
18
18
///
19
19
/// let scheme = AuthenticationScheme::Basic;
20
20
/// let credentials = "0xdeadbeef202020";
@@ -32,107 +32,99 @@ use crate::headers::{Header, HeaderName, HeaderValue, Headers, AUTHORIZATION};
32
32
/// ```
33
33
#[ derive( Debug ) ]
34
34
pub struct Authorization {
35
- scheme : AuthenticationScheme ,
36
- credentials : String ,
35
+ scheme : AuthenticationScheme ,
36
+ credentials : String ,
37
37
}
38
38
39
39
impl 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
+ }
93
87
}
94
88
95
89
impl Header for Authorization {
96
- fn header_name ( & self ) -> HeaderName {
97
- AUTHORIZATION
98
- }
90
+ fn header_name ( & self ) -> HeaderName {
91
+ AUTHORIZATION
92
+ }
99
93
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) ;
102
96
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
+ }
106
100
}
107
101
108
102
#[ cfg( test) ]
109
103
mod 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
+ }
138
130
}
0 commit comments