factor/basis/furnace/auth/basic/basic.factor

30 lines
1015 B
Factor
Raw Normal View History

! Copyright (c) 2007 Chris Double.
! See http://factorcode.org/license.txt for BSD license.
2008-09-10 23:11:40 -04:00
USING: accessors kernel splitting base64 namespaces make strings
2008-06-16 04:34:17 -04:00
http http.server.responses furnace.auth ;
IN: furnace.auth.basic
2008-06-16 04:34:17 -04:00
TUPLE: basic-auth-realm < realm ;
: <basic-auth-realm> ( responder name -- realm )
basic-auth-realm new-realm ;
2008-06-16 04:34:17 -04:00
: parse-basic-auth ( header -- username/f password/f )
dup [
" " split1 swap "Basic" = [
base64> >string ":" split1
2008-06-16 04:34:17 -04:00
] [ drop f f ] if
] [ drop f f ] if ;
: <401> ( realm -- response )
2008-06-16 04:34:17 -04:00
401 "Invalid username or password" <trivial-response>
[ "Basic realm=\"" % swap % "\"" % ] "" make "WWW-Authenticate" set-header ;
2008-07-10 00:41:45 -04:00
M: basic-auth-realm login-required* ( description capabilities realm -- response )
2nip name>> <401> ;
2008-06-16 04:34:17 -04:00
M: basic-auth-realm logged-in-username ( realm -- uid )
drop
2008-06-16 04:34:17 -04:00
request get "authorization" header parse-basic-auth
dup [ over check-login swap and ] [ 2drop f ] if ;