@@ -309,9 +309,15 @@ impl Write for Stream {
309309 }
310310}
311311
312+ #[ derive( Copy , Clone ) ]
313+ pub enum AuthType {
314+ Basic ,
315+ Bearer ,
316+ }
317+
312318fn publish_http (
313319 base_url : & str ,
314- basic_auth : Option < & str > ,
320+ auth : Option < ( AuthType , & str ) > ,
315321 item : & serde_json:: Value ,
316322) -> Result < ( ) , Box < dyn Error > > {
317323 let parsed_url = match parse_url ( base_url) {
@@ -340,12 +346,17 @@ fn publish_http(
340346 body. len( )
341347 ) ;
342348
343- if let Some ( s ) = basic_auth {
349+ if let Some ( ( auth_type , value ) ) = auth {
344350 if parsed_url. scheme != "https" {
345351 return Err ( "Authentication requires https" . into ( ) ) ;
346352 }
347353
348- req. push_str ( & format ! ( "Authorization: Basic {}\r \n " , base64:: encode( s) ) ) ;
354+ let header_value = match auth_type {
355+ AuthType :: Basic => format ! ( "Basic {}" , base64:: encode( value) ) ,
356+ AuthType :: Bearer => format ! ( "Bearer {value}" ) ,
357+ } ;
358+
359+ req. push_str ( & format ! ( "Authorization: {header_value}\r \n " ) ) ;
349360 }
350361
351362 req += "\r \n " ;
@@ -391,7 +402,7 @@ fn publish_http(
391402
392403 let pos = match rest. find ( ' ' ) {
393404 Some ( pos) => pos,
394- None => return Err ( io :: Error :: from ( io :: ErrorKind :: InvalidData ) . into ( ) ) ,
405+ None => rest . len ( ) ,
395406 } ;
396407
397408 let code = & rest[ ..pos] ;
@@ -451,7 +462,7 @@ pub enum Action {
451462
452463pub struct Config {
453464 pub spec : String ,
454- pub basic_auth : Option < String > ,
465+ pub auth : Option < ( AuthType , String ) > ,
455466 pub channel : String ,
456467 pub id : String ,
457468 pub prev_id : String ,
@@ -609,9 +620,9 @@ pub fn run(config: &Config) -> Result<(), Box<dyn Error>> {
609620 if config. spec . starts_with ( "https:" ) || config. spec . starts_with ( "http:" ) {
610621 let item = tnet_to_json ( & item) ?;
611622
612- let basic_auth = config. basic_auth . as_deref ( ) ;
623+ let auth = config. auth . as_ref ( ) . map ( | ( t , v ) | ( * t , v . as_str ( ) ) ) ;
613624
614- publish_http ( & config. spec , basic_auth , & item) ?;
625+ publish_http ( & config. spec , auth , & item) ?;
615626 } else {
616627 publish_zmq ( & config. spec , & item) ?;
617628 }
0 commit comments