1+ use actix_web:: dev:: ServiceRequest ;
12use actix_web:: { get, web, App , HttpResponse , HttpServer } ;
2- use futures:: Future ;
3+ use actix_web_httpauth:: extractors:: basic:: BasicAuth ;
4+ use actix_web_httpauth:: middleware:: HttpAuthentication ;
5+ use futures:: { future, Future } ;
36use rand:: distributions:: Alphanumeric ;
47use rand:: { thread_rng, Rng } ;
58use serde:: Deserialize ;
@@ -13,9 +16,14 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
1316 let config = web:: Data :: new ( config) ;
1417
1518 HttpServer :: new ( move || {
19+ let basic_auth = HttpAuthentication :: basic ( authenticate) ;
1620 App :: new ( )
1721 . register_data ( config. clone ( ) )
18- . service ( web:: resource ( "/" ) . route ( web:: post ( ) . to_async ( new_paste) ) )
22+ . service (
23+ web:: resource ( "/" )
24+ . route ( web:: post ( ) . to_async ( new_paste) )
25+ . wrap ( basic_auth) ,
26+ )
1927 . service ( send_paste)
2028 } )
2129 . bind ( "127.0.0.1:8080" )
@@ -29,7 +37,8 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
2937pub struct Config {
3038 pub paste_dir : String ,
3139 pub url_base : String ,
32- // TODO: Fields for HTTP auth (user/pass)
40+ pub username : String ,
41+ pub password : String ,
3342}
3443
3544impl Config {
@@ -38,9 +47,13 @@ impl Config {
3847 // TODO: Parse command line arguments
3948 let paste_dir = String :: from ( "./pastes" ) ;
4049 let url_base = String :: from ( "https://localhost" ) ;
50+ let username = String :: from ( "tansly" ) ;
51+ let password = String :: from ( "hebele" ) ;
4152 Ok ( Config {
4253 paste_dir,
4354 url_base,
55+ username,
56+ password,
4457 } )
4558 }
4659}
@@ -100,6 +113,13 @@ fn send_paste(
100113 } )
101114}
102115
116+ fn authenticate (
117+ req : ServiceRequest ,
118+ _credentials : BasicAuth ,
119+ ) -> impl Future < Item = ServiceRequest , Error = actix_web:: Error > {
120+ future:: ok ( req)
121+ }
122+
103123#[ cfg( test) ]
104124mod tests {
105125 use super :: * ;
@@ -113,6 +133,8 @@ mod tests {
113133 Config {
114134 paste_dir : String :: from ( paste_dir) ,
115135 url_base : String :: from ( "https://testurl" ) ,
136+ username : String :: from ( "tansly" ) ,
137+ password : String :: from ( "hebele" ) ,
116138 }
117139 }
118140
0 commit comments