35
36:- module(swish_authenticate,
37 [ authenticate/2, 38 user_property/2 39 ]). 40:- use_module(library(http/http_wrapper)). 41:- use_module(library(debug)). 42:- use_module(library(broadcast)). 43
44:- use_module(config).
68authenticate(Request, Auth) :-
69 http_peer(Request, Peer),
70 http_auth(Request, HTTPAuth),
71 profile_auth(Request, ProfileAuth),
72 Auth2 = HTTPAuth.put(ProfileAuth).put(peer, Peer),
73 identity(Request, Auth2, Auth),
74 debug(authenticate, 'Identity: ~p', [Auth]).
75
76:- multifile
77 swish_config:user_info/3,
78 swish_config:authenticate/2,
79 swish_config:user_profile/2. 80
81http_auth(Request, Auth) :-
82 ( swish_config:authenticate(Request, User) 83 -> true
84 ; swish_config:user_info(Request, local, UserInfo),
85 User = UserInfo.get(user)
86 ),
87 !,
88 Auth = auth{user:User, identity_provider:local, external_identity:User}.
89http_auth(_Request, auth{}).
90
91profile_auth(Request, Auth) :-
92 swish_config:user_profile(Request, Profile),
93 Pattern = _{ identity_provider: _,
94 external_identity: _,
95 profile_id:_},
96 Pattern :< Profile,
97 Auth = auth{}.put(Pattern).
98profile_auth(_, auth{}).
99
100identity(Request, Auth0, Auth) :-
101 _{identity_provider:Provider, external_identity:ExtID} :< Auth0,
102 !,
103 ( swish_config:user_info(Request, Provider, UserInfo),
104 is_dict(UserInfo, Tag),
105 ( var(Tag)
106 -> Tag = user_info
107 ; true
108 )
109 -> true
110 ; UserInfo = user_info{}
111 ),
112 atomic_list_concat([Provider,ExtID], :, Identity),
113 Auth = Auth0.put(_{identity:Identity, user_info:UserInfo}).
114identity(_, Auth, Auth).
138user_property(Identity, Property) :-
139 current_user_property(Property, How),
140 user_property_impl(Property, How, Identity).
141
142user_property_impl(Property, dict, Identity) :- !,
143 Property =.. [Name,Value],
144 Value = Identity.get(Name).
145user_property_impl(Property, broadcast, Identity) :-
146 broadcast_request(identity_property(Identity, Property)).
147user_property_impl(login(By), _, Identity) :-
148 By = Identity.get(identity_provider).
149
150
151current_user_property(peer(_Atom), dict).
152current_user_property(identity(_Atom), dict).
153current_user_property(external_identity(_String), dict).
154current_user_property(identity_provider(_Atom), dict).
155current_user_property(profile_id(_Atom), dict).
156current_user_property(avatar(_String), dict).
157
158current_user_property(login(_IdProvider), derived).
159current_user_property(name(_Name), broadcast).
160current_user_property(email(_Email), broadcast).
161
162
163
172:- multifile pengines:authentication_hook/3. 173
174pengines:authentication_hook(Request, _Application, User) :-
175 authenticate(Request, User)
Authentication access for SWISH
This module (depending on the loaded configuration) identifies the user based on the HTTP request.
pep.pl
for authorization issues. */