1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@cs.vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (C): 2014-2017, VU University Amsterdam 7 CWI, Amsterdam 8 9 This program is free software; you can redistribute it and/or 10 modify it under the terms of the GNU General Public License 11 as published by the Free Software Foundation; either version 2 12 of the License, or (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public 20 License along with this library; if not, write to the Free Software 21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 23 As a special exception, if you link this library with other files, 24 compiled with a Free Software compiler, to produce an executable, this 25 library does not by itself cause the resulting executable to be covered 26 by the GNU General Public License. This exception does not however 27 invalidate any other reasons why the executable file might be covered by 28 the GNU General Public License. 29*/ 30 31:- module(cp_swish_authenticate, 32 [ 33 ]). 34:- use_module(library(pengines), []). 35:- use_module(library(broadcast)). 36:- use_module(user(user_db)). 37 38/** <module> SWISH login management 39 40This module provides basic login and password management facilities for 41SWISH. You can create an authenticated SWISH server by 42 43 1. Loading this library 44 2. Add one or more users to the passwd file using swish_add_user/3 45 46 == 47 ?- swish_add_user("Bob", "Bob's secret", []). 48 == 49 50As a result, trying to create the first pengine (e.g., using _|Run!|_), 51the server will challenge the user. The logged in user is available 52through pengine_user/1. 53*/ 54 55:- multifile 56 swish_config:reply_logged_in/1, 57 swish_config:reply_logged_out/1, 58 swish_config:authenticate/2, 59 swish_config:user_info/3. 60 61swish_configauthenticate(_Request, User) :- 62 logged_on(User). 63 64swish_configuser_info(_Request, local, Info) :- 65 logged_on(User), 66 cp_user_info(User, Info). 67 68cp_user_info(User, Info) :- 69 findall(Name-Value, cp_user_property(User, Name, Value), Pairs), 70 dict_pairs(Info, u, Pairs). 71 72cp_user_property(User, user, User). 73cp_user_property(User, name, RealName) :- 74 user_property(User, realname(RealName)). 75cp_user_property(User, email, Email) :- 76 user_property(User, email(Email)). 77cp_user_property(User, group, Group) :- 78 user_property(User, allow(Allow)), 79 ( memberchk(admin(_), Allow) 80 -> Group = admin 81 ; memberchk(write(_,_), Allow) 82 -> Group = writer 83 ; Group = reader 84 ). 85 86:- listen(identity_property(Identity, Property), 87 cp_identity_property(Identity, Property)). 88 89cp_identity_property(Identity, Property) :- 90 _{user:User, identity_provider:local} :< Identity, 91 Property =.. [Name,Value], 92 cp_user_property(User, Name, Value). 93 94 95 /******************************* 96 * LINK LOGIN/LOGOUT TO PROFILE * 97 *******************************/ 98 99:- listen(cliopatria(login(User, _Session)), 100 cp_logged_in(User)). 101:- listen(cliopatria(logout(User)), 102 cp_logged_out(User)). 103 104cp_logged_in(User) :- 105 cp_user_info(User, Info), 106 IdInfo = Info.put(_{identity_provider:local, external_identity:User}), 107 swish_config:reply_logged_in([user_info(IdInfo), reply(none)]). 108 109cp_logged_out(_User) :- 110 swish_config:reply_logged_out([reply(none)])