Chris@0: /* This file is part of ClioPatria.
Chris@0:
Chris@0: Author:
Chris@0: HTTP: http://e-culture.multimedian.nl/
Chris@0: GITWEB: http://gollem.science.uva.nl/git/ClioPatria.git
Chris@0: GIT: git://gollem.science.uva.nl/home/git/ClioPatria.git
Chris@0: GIT: http://gollem.science.uva.nl/home/git/ClioPatria.git
Chris@0: Copyright: 2007, E-Culture/MultimediaN
Chris@0:
Chris@0: ClioPatria is free software: you can redistribute it and/or modify
Chris@0: it under the terms of the GNU General Public License as published by
Chris@0: the Free Software Foundation, either version 2 of the License, or
Chris@0: (at your option) any later version.
Chris@0:
Chris@0: ClioPatria is distributed in the hope that it will be useful,
Chris@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0: GNU General Public License for more details.
Chris@0:
Chris@0: You should have received a copy of the GNU General Public License
Chris@0: along with ClioPatria. If not, see .
Chris@0: */
Chris@0:
Chris@0: :- asserta(file_search_path(library, '..')).
Chris@0:
Chris@0: :- use_module(http_openid).
Chris@0: :- use_module(library('http/http_dispatch')).
Chris@0: :- use_module(library('http/http_error')).
Chris@0: :- use_module(library('http/thread_httpd')).
Chris@0: :- use_module(library('http/html_write')).
Chris@0: :- use_module(library(plunit)).
Chris@0:
Chris@0: :- debug(openid(_)).
Chris@0: %:- debug(http(_)).
Chris@0:
Chris@0: consumer(Port) :-
Chris@0: http_server(http_dispatch,
Chris@0: [ port(Port)
Chris@0: ]).
Chris@0:
Chris@0:
Chris@0: assoc :-
Chris@0: openid_associate('http://localhost:8001/openid/server', Handle, Assoc),
Chris@0: writeln(Handle-Assoc).
Chris@0:
Chris@0: %% secret(+Request) is det.
Chris@0: %
Chris@0: % Example of a handler that requires an OpenID login. If the user
Chris@0: % is not logged it, it will be redirected to the login page, from
Chris@0: % there to the OpenID server and back here. All this is completely
Chris@0: % transparent to us.
Chris@0:
Chris@0: :- http_handler('/secret', secret, []).
Chris@0:
Chris@0: secret(Request) :-
Chris@0: openid_user(Request, User, []),
Chris@0: reply_html_page(title('Secret'),
Chris@0: [ 'You\'ve reached the secret page as user ',
Chris@0: a(href(User), User)
Chris@0: ]).
Chris@0:
Chris@0: %% root(+Request).
Chris@0: %% allow(+Request).
Chris@0: %
Chris@0: % Shows an indirect login.
Chris@0:
Chris@0: :- http_handler(/, root, []).
Chris@0: :- http_handler('/test/verify', openid_verify([return_to(allow)]), []).
Chris@0: :- http_handler('/test/allow', allow, []).
Chris@0:
Chris@0: root(_Request) :-
Chris@0: reply_html_page(title('Demo OpenID consumer'),
Chris@0: [ h1('OpenID consumer'),
Chris@0: form([ name(login),
Chris@0: action('/test/verify'),
Chris@0: method('GET')
Chris@0: ],
Chris@0: [ div([ 'OpenID: ',
Chris@0: input([ name(openid_url),
Chris@0: size(40),
Chris@0: value('http://localhost:8000/bob') % test
Chris@0: ]),
Chris@0: input([type(submit), value('Verify!')])
Chris@0: ])
Chris@0: ]),
Chris@0: p([ 'Or go directly to the ', a(href=secret, 'secret page') ])
Chris@0: ]).
Chris@0:
Chris@0:
Chris@0: allow(Request) :-
Chris@0: openid_authenticate(Request, Server, Identity, ReturnTo),
Chris@0: reply_html_page(title('Success'),
Chris@0: [ h1('OpenID login succeeded'),
Chris@0: p([ 'The OpenID server ',
Chris@0: a(href(Server),Server),
Chris@0: ' verified you as ',
Chris@0: a(href(Identity), Identity)
Chris@0: ]),
Chris@0: p(['Redirect to ', a(href(ReturnTo), ReturnTo)])
Chris@0: ]).
Chris@0:
Chris@0:
Chris@0: /*******************************
Chris@0: * OpenID SERVER *
Chris@0: *******************************/
Chris@0:
Chris@0: :- http_handler(prefix('/user/'), user_page, []).
Chris@0: :- http_handler('/openid/server', openid_server([]), []).
Chris@0:
Chris@0: %% user_page(+Request) is det.
Chris@0: %
Chris@0: % Generate a page for user below /user/.
Chris@0:
Chris@0: user_page(Request) :-
Chris@0: http_openid:current_root_url(Request, Root),
Chris@0: atom_concat(Root, 'openid/server', Me),
Chris@0: memberchk(path(Path), Request),
Chris@0: atom_concat('/user/', User, Path),
Chris@0: reply_html_page([ link([ rel('openid.server'),
Chris@0: href(Me)
Chris@0: ]),
Chris@0: title('OpenID page of ~w'-[User])
Chris@0: ],
Chris@0: h1('OpenID page of ~w'-[User])).
Chris@0:
Chris@0:
Chris@0: /*******************************
Chris@0: * DEBUG *
Chris@0: *******************************/
Chris@0:
Chris@0: :- http_handler(prefix(/), print_request, []).
Chris@0:
Chris@0: print_request(Request) :-
Chris@0: format('Content-type: text/plain~n~n'),
Chris@0: pp(Request).