1 ! Copyright (c) 2007 Chris Double.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel splitting base64 namespaces make strings
4 http http.server.responses furnace.auth ;
7 TUPLE: basic-auth-realm < realm ;
9 : <basic-auth-realm> ( responder name -- realm )
10 basic-auth-realm new-realm ;
12 : parse-basic-auth ( header -- username/f password/f )
14 " " split1 swap "Basic" = [
15 base64> >string ":" split1
19 : <401> ( realm -- response )
20 401 "Invalid username or password" <trivial-response>
21 [ "Basic realm=\"" % swap % "\"" % ] "" make "WWW-Authenticate" set-header ;
23 M: basic-auth-realm login-required*
26 M: basic-auth-realm logged-in-username
28 request get "authorization" header parse-basic-auth
29 dup [ over check-login swap and ] [ 2drop f ] if ;
31 M: basic-auth-realm init-realm drop ;