@@ -32,99 +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 { 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
- }
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
+ }
87
87
}
88
88
89
89
impl Header for Authorization {
90
- fn header_name ( & self ) -> HeaderName {
91
- AUTHORIZATION
92
- }
90
+ fn header_name ( & self ) -> HeaderName {
91
+ AUTHORIZATION
92
+ }
93
93
94
- fn header_value ( & self ) -> HeaderValue {
95
- let output = format ! ( "{} {}" , self . scheme, self . credentials) ;
94
+ fn header_value ( & self ) -> HeaderValue {
95
+ let output = format ! ( "{} {}" , self . scheme, self . credentials) ;
96
96
97
- // SAFETY: the internal string is validated to be ASCII.
98
- unsafe { HeaderValue :: from_bytes_unchecked ( output. into ( ) ) }
99
- }
97
+ // SAFETY: the internal string is validated to be ASCII.
98
+ unsafe { HeaderValue :: from_bytes_unchecked ( output. into ( ) ) }
99
+ }
100
100
}
101
101
102
102
#[ cfg( test) ]
103
103
mod test {
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
- }
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
+ }
130
130
}
0 commit comments