View source with raw comments or as raw
    1/*  Part of SWISH
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@cs.vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2017, VU University Amsterdam
    7			 CWI Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(swish_authenticate,
   37          [ authenticate/2,                     % +Request, -Authentity
   38            user_property/2                     % +Authentity, ?Property
   39          ]).   40:- use_module(library(http/http_wrapper)).   41:- use_module(library(debug)).   42:- use_module(library(broadcast)).   43
   44:- use_module(config).

Authentication access for SWISH

This module (depending on the loaded configuration) identifies the user based on the HTTP request.

See also
- pep.pl for authorization issues. */
 authenticate(+Request, -Identity:dict) is det
Establish the identity behind the HTTP Request. There are two scenarios.
throws
- http_reply(_) HTTP authentication and permission exceptions if config-available/auth_http_always.pl is enabled.
   68authenticate(Request, Auth) :-
   69    http_peer(Request, Peer),
   70    http_auth(Request, Auth0),
   71    profile_auth(Request, Auth1),
   72    Auth2 = Auth0.put(Auth1).put(peer, Peer),
   73    identity(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)   % throws http_reply(_)
   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    Auth = _{identity_provider: _,
   94             external_identity: _,
   95             profile_id:_},
   96    Auth :< Profile,
   97    !.
   98profile_auth(_, auth{}).
   99
  100identity(Auth0, Auth) :-
  101    _{identity_provider:Provider, external_identity:ExtID} :< Auth0,
  102    !,
  103    atomic_list_concat([Provider,ExtID], :, Identity),
  104    Auth = Auth0.put(identity, Identity).
  105identity(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
  129user_property(Identity, Property) :-
  130    current_user_property(Property, How),
  131    user_property_impl(Property, How, Identity).
  132
  133user_property_impl(Property, dict, Identity) :- !,
  134    Property =.. [Name,Value],
  135    Value = Identity.get(Name).
  136user_property_impl(Property, broadcast, Identity) :-
  137    broadcast_request(identity_property(Identity, Property)).
  138user_property_impl(login(By), _, Identity) :-
  139    By = Identity.get(identity_provider).
  140
  141
  142current_user_property(peer(_Atom),                dict).
  143current_user_property(identity(_Atom),            dict).
  144current_user_property(external_identity(_String), dict).
  145current_user_property(identity_provider(_Atom),   dict).
  146current_user_property(profile_id(_Atom),          dict).
  147
  148current_user_property(login(_IdProvider),         derived).
  149current_user_property(name(_Name),                broadcast).
  150current_user_property(email(_Email),              broadcast).
  151
  152
  153		 /*******************************
  154		 *        PENGINE HOOKS		*
  155		 *******************************/
 pengines:authentication_hook(+Request, +Application, -User)
Is called from the /pengine/create request to establish the logged in user.
  162:- multifile pengines:authentication_hook/3.  163
  164pengines:authentication_hook(Request, _Application, User) :-
  165    authenticate(Request, User)