@@ -19,33 +19,33 @@ use crate::Status;
1919///
2020/// let username = "nori";
2121/// let password = "secret_fish!!";
22- /// let authz = BasicAuth::new(username, Some( password) );
22+ /// let authz = BasicAuth::new(username, password);
2323///
2424/// let mut res = Response::new(200);
2525/// authz.apply(&mut res);
2626///
2727/// let authz = BasicAuth::from_headers(res)?.unwrap();
2828///
2929/// assert_eq!(authz.username(), username);
30- /// assert_eq!(authz.password(), Some( password) );
30+ /// assert_eq!(authz.password(), password);
3131/// #
3232/// # Ok(()) }
3333/// ```
3434#[ derive( Debug ) ]
3535pub struct BasicAuth {
3636 username : String ,
37- password : Option < String > ,
37+ password : String ,
3838}
3939
4040impl BasicAuth {
4141 /// Create a new instance of `BasicAuth`.
42- pub fn new < U , P > ( username : U , password : Option < P > ) -> Self
42+ pub fn new < U , P > ( username : U , password : P ) -> Self
4343 where
4444 U : AsRef < str > ,
4545 P : AsRef < str > ,
4646 {
4747 let username = username. as_ref ( ) . to_owned ( ) ;
48- let password = password. map ( |p| p . as_ref ( ) . to_owned ( ) ) ;
48+ let password = password. as_ref ( ) . to_owned ( ) ;
4949 Self { username, password }
5050 }
5151
@@ -71,8 +71,12 @@ impl BasicAuth {
7171 let password = iter. next ( ) ;
7272
7373 let ( username, password) = match ( username, password) {
74- ( Some ( username) , Some ( password) ) => ( username. to_string ( ) , Some ( password. to_string ( ) ) ) ,
75- ( Some ( username) , None ) => ( username. to_string ( ) , None ) ,
74+ ( Some ( username) , Some ( password) ) => ( username. to_string ( ) , password. to_string ( ) ) ,
75+ ( Some ( _) , None ) => {
76+ let mut err = format_err ! ( "Expected basic auth to a password" ) ;
77+ err. set_status ( 400 ) ;
78+ return Err ( err) ;
79+ }
7680 ( None , _) => {
7781 let mut err = format_err ! ( "Expected basic auth to contain a username" ) ;
7882 err. set_status ( 400 ) ;
@@ -96,10 +100,7 @@ impl BasicAuth {
96100 /// Get the `HeaderValue`.
97101 pub fn value ( & self ) -> HeaderValue {
98102 let scheme = AuthenticationScheme :: Basic ;
99- let credentials = match self . password . as_ref ( ) {
100- Some ( password) => base64:: encode ( format ! ( "{}:{}" , self . username, password) ) ,
101- None => base64:: encode ( self . username . clone ( ) ) ,
102- } ;
103+ let credentials = base64:: encode ( format ! ( "{}:{}" , self . username, self . password) ) ;
103104 let auth = Authorization :: new ( scheme, credentials) ;
104105 auth. value ( )
105106 }
@@ -110,8 +111,8 @@ impl BasicAuth {
110111 }
111112
112113 /// Get the password.
113- pub fn password ( & self ) -> Option < & str > {
114- self . password . as_deref ( )
114+ pub fn password ( & self ) -> & str {
115+ self . password . as_str ( )
115116 }
116117}
117118
@@ -124,15 +125,15 @@ mod test {
124125 fn smoke ( ) -> crate :: Result < ( ) > {
125126 let username = "nori" ;
126127 let password = "secret_fish!!" ;
127- let authz = BasicAuth :: new ( username, Some ( password) ) ;
128+ let authz = BasicAuth :: new ( username, password) ;
128129
129130 let mut headers = Headers :: new ( ) ;
130131 authz. apply ( & mut headers) ;
131132
132133 let authz = BasicAuth :: from_headers ( headers) ?. unwrap ( ) ;
133134
134135 assert_eq ! ( authz. username( ) , username) ;
135- assert_eq ! ( authz. password( ) , Some ( password) ) ;
136+ assert_eq ! ( authz. password( ) , password) ;
136137 Ok ( ( ) )
137138 }
138139
0 commit comments