Commits

Anonymous committed 2aadaaf

+ Amall_http.get_basic_auth for HTTP basic auth

Comments (0)

Files changed (2)

src/amall_http.ml

 
 
 end;
+
+
+module It_pure = Iteratees.Make(Pure_IO);
+
+value get_basic_auth
+ : rq_headers -> option (string * string)  (* Some (user, password) *)
+ = fun h ->
+     match List.Assoc.get_opt ~keq:String.eq "Authorization" h.rq_all with
+     [ None -> None
+     | Some hval ->
+         match String.split is_whitespace hval with
+         [ [meth; uue] when String.eq_nocase_latin1 meth "basic" ->
+             let res_user_pass =
+               It_pure.(
+                 Pure_IO.catch
+                   (fun () ->
+                      Pure_IO.bind
+                      (fun it -> run it)
+                      (enum_string uue &
+                       joinI &
+                       base64_decode gather_to_string
+                      )
+                   )
+                   Pure_IO.error
+               ) in
+             match res_user_pass with
+             [ `Error _ -> None
+             | `Ok user_pass ->
+                 let (user, colon, pass) =
+                   String.split_by_first ( (=) ':' ) user_pass in
+                 if colon = ":"
+                 then Some (user, pass)
+                 else None
+             ]
+         | _ -> None
+         ]
+     ]
+;
+
     value ( >>> ) : enumerator 'a 'b -> enumerator 'a 'b -> enumerator 'a 'b;
     value enum_pure_1chunk : list 'a -> enumerator 'a 'b;
     value enum_pure_nchunk : list 'a -> int -> enumerator 'a 'b;
+    value enum_string : ?chunk_size:int -> string -> enumerator char 'a;
     value mprintf : format4 'a unit string (It_IO.m unit) -> 'a;
     value mres : It_IO.m 'a -> It_IO.m (It_Types.res 'a);
     value _munres : It_Types.res 'a -> It_IO.m 'a;
     value feedI :
       (stream 'a -> It_IO.m (iteratee 'a 'b * stream 'a)) ->
       stream 'a -> It_IO.m (iteratee 'a 'b);
+    value base64_decode : enumeratee char char 'a;
   end
 ;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.