-
Notifications
You must be signed in to change notification settings - Fork 40
/
openid_example.pl
169 lines (139 loc) · 5.1 KB
/
openid_example.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2007-2010, University of Amsterdam,
VU University Amsterdam
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
:- asserta(file_search_path(library, '..')).
:- use_module(library(uri)).
:- use_module(library(http/http_openid)).
:- use_module(library(http/http_host)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_path)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_error)).
:- use_module(library(pprint)).
http:location(openid, root(openid), []).
:- multifile
http_openid:openid_hook/1.
http_openid:openid_hook(trusted(_OpenID, Server)) :-
debug(openid(test), 'Trusting server ~q', [Server]).
%% server
%
% Create demo server and client. After starting the server,
% contact http://localhost:8000/
server :-
debug(openid(_)),
Port = 8000,
http_server(http_dispatch,
[ port(Port)
]),
debug(openid(test), 'Server started at http://localhost:~w/', [Port]).
assoc :-
openid_associate('http://localhost:8000/openid/server', Handle, Assoc),
writeln(Handle-Assoc).
%% secret(+Request) is det.
%
% Example of a handler that requires an OpenID login. If the user
% is not logged it, it will be redirected to the login page, from
% there to the OpenID server and back here. All this is completely
% transparent to us.
:- http_handler(root('secret'), secret, []).
secret(Request) :-
openid_user(Request, User, []),
reply_html_page(title('Secret'),
[ 'You\'ve reached the secret page as user ', %'
a(href(User), User)
]).
%% root(+Request).
%% allow(+Request).
%
% Shows an indirect login.
:- http_handler(root(.), root, []).
:- http_handler(root('test/verify'), openid_verify([return_to(allow)]), []).
:- http_handler(root('test/allow'), allow, []).
root(_Request) :-
reply_html_page(title('Demo OpenID consumer'),
[ h1('OpenID consumer'),
form([ name(login),
action('/test/verify'),
method('GET')
],
[ div([ 'OpenID: ',
input([ name(openid_url),
size(40),
value('http://localhost:8000/user/bob') % test
]),
input([type(submit), value('Verify!')])
])
]),
p([ 'Or go directly to the ', a(href=secret, 'secret page') ])
]).
allow(Request) :-
openid_authenticate(Request, Server, Identity, _ReturnTo),
reply_html_page(title('Success'),
[ h1('OpenID login succeeded'),
p([ 'The OpenID server ',
a(href(Server),Server),
' verified you as ',
a(href(Identity), Identity)
])
]).
/*******************************
* OpenID SERVER *
*******************************/
:- http_handler(root('user/'), user_page, [prefix]).
:- http_handler(openid(server), openid_server([]), []).
:- http_handler(openid(grant), openid_grant, []).
:- multifile
http_openid:openid_hook/1.
http_openid:openid_hook(grant(_Request, Options)) :-
debug(openid(test), 'Granting access to ~p', [Options]).
%% user_page(+Request) is det.
%
% Generate a page for user as /user/<user>.
user_page(Request) :-
http_current_host(Request, Host, Port,
[ global(true)
]),
http_location_by_id(openid_server, ServerLocation),
uri_authority_data(host, AComp, Host),
uri_authority_data(port, AComp, Port),
uri_authority_components(Authority, AComp),
uri_data(scheme, Components, http),
uri_data(authority, Components, Authority),
uri_data(path, Components, ServerLocation),
uri_components(OpenIDServer, Components),
memberchk(path_info(User), Request),
reply_html_page([ link([ rel('openid.server'),
href(OpenIDServer)
]),
title('OpenID page of ~w'-[User])
],
h1('OpenID page of ~w'-[User])).
/*******************************
* DEBUG *
*******************************/
% :- http_handler(root(.), print_request, [prefix]).
print_request(Request) :-
format('Content-type: text/plain~n~n'),
print_term(Request).