Yaws, c’est le serveur web écrit en erlang, célèbre pour ce graphe qui montre comment Yaws met sa pâté à Apache.
Voici le client minimal permettant de s’authentifier sur un serveur CAS.
A sauver dans un fichier cas.yaws à mettre dans le /var/yaws (document root par défaut).
N’oubliez pas d’adapter CASHOST et SERVICE à votre configuration.
<erl>
-define(CASHOST, "http://localhost:8080/cas/").
-define(SERVICE, "service=http%3A%2F%2Flocalhost%3A5224%2Fcas.yaws").
-include_lib("xmerl/include/xmerl.hrl").
out(A)->
H = A#arg.headers,
C = H#headers.cookie,
inets:start(),
case yaws_api:find_cookie_val("casuser", C) of
[]->
check_auth(A);
Cookie ->
{ok, Username} = yaws_api:cookieval_to_opaque(Cookie),
{html, "Authentified as "++Username}
end.
check_auth(A)->
case queryvar(A,"ticket") of
{ok, Ticket}->
case verify_ticket(Ticket) of
{ok, Username} ->
Cookie = yaws_api:new_cookie_session(Username),
CO = yaws_api:setcookie("casuser",Cookie,"/"),
[{html, "Authentified as "++Username}, CO];
{error, Reason} ->
[{status, 403},{html, "Unauthorized : "++Reason}]
end;
undefined ->
{redirect, ?CASHOST ++ "login?" ++ ?SERVICE }
end.
verify_ticket(Ticket) ->
inets:start(),
Url =?CASHOST++"proxyValidate?"++?SERVICE++"&ticket="++Ticket,
{ok, {_Status, _Headers, Body}} =
http:request(Url),
{ Xml, _Rest } = xmerl_scan:string(Body),
inets:stop(),
case xmerl_xpath:string("//cas:user/text()",Xml) of
[ #xmlText{value=Username} ] ->
{ok, Username};
[] ->
{error, "invalid ticket " ++ Url}
end.
</erl>
Tant qu’on est sur le sujet, il y a un article très intéressant sur le REST dans Yaws chez InfoQ.