forked from SWI-Prolog/swish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthenticate.pl
175 lines (147 loc) · 5.98 KB
/
authenticate.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
170
171
172
173
174
175
/* Part of SWISH
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2017, VU University Amsterdam
CWI Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(swish_authenticate,
[ authenticate/2, % +Request, -Authentity
user_property/2 % +Authentity, ?Property
]).
:- use_module(library(http/http_wrapper)).
:- use_module(library(debug)).
:- use_module(library(broadcast)).
:- use_module(config).
/** <module> Authentication access for SWISH
This module (depending on the loaded configuration) identifies the user
based on the HTTP request.
@see pep.pl for _authorization_ issues.
*/
%! authenticate(+Request, -Identity:dict) is det.
%
% Establish the identity behind the HTTP Request. There are two
% scenarios.
%
% - The entire server is protected using HTTP authentication. In
% this case this predicate may throw an HTTP challenge or a
% forbidden exception.
% - The server allows for mixed anonymous and logged in usage. Login
% may use HTTP or federated login (oauth2).
%
% @throws http_reply(_) HTTP authentication and permission exceptions
% if config-available/auth_http_always.pl is enabled.
authenticate(Request, Auth) :-
http_peer(Request, Peer),
http_auth(Request, HTTPAuth),
profile_auth(Request, ProfileAuth),
Auth2 = HTTPAuth.put(ProfileAuth).put(peer, Peer),
identity(Request, Auth2, Auth),
debug(authenticate, 'Identity: ~p', [Auth]).
:- multifile
swish_config:user_info/3,
swish_config:authenticate/2,
swish_config:user_profile/2.
http_auth(Request, Auth) :-
( swish_config:authenticate(Request, User) % throws http_reply(_)
-> true
; swish_config:user_info(Request, local, UserInfo),
User = UserInfo.get(user)
),
!,
Auth = auth{user:User, identity_provider:local, external_identity:User}.
http_auth(_Request, auth{}).
profile_auth(Request, Auth) :-
swish_config:user_profile(Request, Profile),
Pattern = _{ identity_provider: _,
external_identity: _,
profile_id:_},
Pattern :< Profile,
Auth = auth{}.put(Pattern).
profile_auth(_, auth{}).
identity(Request, Auth0, Auth) :-
_{identity_provider:Provider, external_identity:ExtID} :< Auth0,
!,
( swish_config:user_info(Request, Provider, UserInfo),
is_dict(UserInfo, Tag),
( var(Tag)
-> Tag = user_info
; true
)
-> true
; UserInfo = user_info{}
),
atomic_list_concat([Provider,ExtID], :, Identity),
Auth = Auth0.put(_{identity:Identity, user_info:UserInfo}).
identity(_, Auth, Auth).
%! user_property(+Identity, ?Property) is nondet.
%
% True when Identity has Property. Defined properties are:
%
% - peer(Atom)
% Remote IP address
% - identity(Atom)
% Identity as provided by some identity provider
% - identity_provider(Atom)
% Subsystem that identified the user
% - external_identity(Atom)
% Identity as provided by the identity_provider
% - profile_id(Atom)
% Identifier of the profile we have on this user.
% - login(Atom)
% Same as identity_provider(Atom)
% - name(Atom)
% Name associated with the identity
% - email(Atom)
% Email associated with the identity
user_property(Identity, Property) :-
current_user_property(Property, How),
user_property_impl(Property, How, Identity).
user_property_impl(Property, dict, Identity) :- !,
Property =.. [Name,Value],
Value = Identity.get(Name).
user_property_impl(Property, broadcast, Identity) :-
broadcast_request(identity_property(Identity, Property)).
user_property_impl(login(By), _, Identity) :-
By = Identity.get(identity_provider).
current_user_property(peer(_Atom), dict).
current_user_property(identity(_Atom), dict).
current_user_property(external_identity(_String), dict).
current_user_property(identity_provider(_Atom), dict).
current_user_property(profile_id(_Atom), dict).
current_user_property(avatar(_String), dict).
current_user_property(login(_IdProvider), derived).
current_user_property(name(_Name), broadcast).
current_user_property(email(_Email), broadcast).
/*******************************
* PENGINE HOOKS *
*******************************/
%! pengines:authentication_hook(+Request, +Application, -User)
%
% Is called from the /pengine/create request to establish the logged
% in user.
:- multifile pengines:authentication_hook/3.
pengines:authentication_hook(Request, _Application, User) :-
authenticate(Request, User).