File tree Expand file tree Collapse file tree 1 file changed +15
-3
lines changed
Expand file tree Collapse file tree 1 file changed +15
-3
lines changed Original file line number Diff line number Diff line change 1+ use once_cell:: sync:: Lazy ;
12use rand:: { distributions:: Alphanumeric , Rng } ;
23
4+ use regex:: Regex ;
35use rocket:: {
46 http:: Status ,
57 request:: { FromRequest , Outcome , Request } ,
@@ -27,9 +29,19 @@ impl<'r> FromRequest<'r> for PasswordHeader<'r> {
2729 async fn from_request ( request : & ' r Request < ' _ > ) -> Outcome < Self , Self :: Error > {
2830 let result = request. headers ( ) . get_one ( "Authorization" ) ;
2931
30- match result {
31- Some ( value) => Outcome :: Success ( PasswordHeader { value } ) ,
32- None => Outcome :: Error ( ( Status :: Unauthorized , "Missing Authorization header." ) ) ,
32+ let value = match result {
33+ Some ( value) => value,
34+ None => return Outcome :: Error ( ( Status :: Unauthorized , "Missing Authorization header." ) ) ,
35+ } ;
36+
37+ static PATTERN : Lazy < Regex > = Lazy :: new ( || Regex :: new ( r"(?i:password): .+" ) . unwrap ( ) ) ;
38+
39+ if !PATTERN . is_match ( value) {
40+ return Outcome :: Error ( ( Status :: Unauthorized , "Invalid Authorization header." ) ) ;
3341 }
42+
43+ Outcome :: Success ( PasswordHeader {
44+ value : value. split_once ( " " ) . unwrap ( ) . 1 ,
45+ } )
3446 }
3547}
You can’t perform that action at this time.
0 commit comments