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_pep,
   37          [ authorized/2,               % +Action, +Options
   38            ws_authorized/2             % +Action, +WSID
   39          ]).   40:- use_module(library(debug)).   41:- use_module(library(option)).   42:- use_module(library(error)).   43:- use_module(library(http/http_wrapper)).   44
   45:- use_module(authenticate).   46:- use_module(storage).   47:- use_module(config).

SWISH PEP (Policy Enforcement Point)

This module implements the Policy Enforcement Point. It is called by modules that perform operations that may not be publically accessible. Examples are:

*/

   60:- multifile
   61    swish_config:approve/3.
 authorized(+Action, +Options) is det
Verify that Action is authorized. Options:
indentity(+Identity)
Indentity is the identity dict as collected by autenticate.pl.

Actions defined:

throws
- http_reply(forbidden(URL)) if the action is not allowed. Can we generate a JSON error object?
   97authorized(Action, _Options) :-
   98    var(Action),
   99    !,
  100    instantiation_error(Action).
  101authorized(Action, Options) :-
  102    option(identity(Id), Options),
  103    (   authorize(Action, Id)
  104    ->  debug(pep, 'Approved gitty action ~p to ~p', [Action, Id])
  105    ;   debug(pep, 'Denied action ~p to ~p', [Action, Id]),
  106        http_current_request(Request),
  107        option(path(Path), Request),
  108        throw(http_reply(forbidden(Path)))
  109    ).
 ws_authorized(+Action, +WSUser) is semidet
True when WSUser is allowed to perform action. WSUser is a dict containing the user info as provided by chat_add_user_id/3. It notably has a key profile_id if the user is logged on.
To be done
- Generalise. Notably, how do we get the identity as authenticate/2 returns?
  120ws_authorized(Action, _WSUser) :-
  121    var(Action),
  122    !,
  123    instantiation_error(Action).
  124ws_authorized(chat(post(_,_)), WSUser) :-
  125    _Profile = WSUser.get(profile_id).
  126
  127
  128:- multifile
  129    approve/2,
  130    deny/2.  131
  132authorize(Action, Id) :-
  133    swish_config:approve(Action, Id, Approve),
  134    !,
  135    Approve == true.
  136authorize(Action, Id) :-
  137    approve(Action, Id), !,
  138    \+ deny(Action, Id).
 approve(+Action, +Id)
  142approve(gitty(update(_File,PrevMeta,_Meta)), Auth) :- !,
  143    storage_meta_property(PrevMeta, modify(Modify)),
  144    (   memberchk(any, Modify)
  145    ->  true
  146    ;   memberchk(login, Modify)
  147    ->  user_property(Auth, login(_))
  148    ;   storage_meta_property(PrevMeta, identity(Id)),
  149        user_property(Auth, identity(Id))
  150    ).
  151approve(gitty(_), _).
  152approve(file(update(_File, _Meta)), Auth) :-
  153    user_property(Auth, login(local)).
  154approve(run(any, _), Auth) :-
  155    user_property(Auth, login(local)).
  156approve(chat(open), _).
 deny(+Action, +Id)
 swish_config:approve(+Action, +Identity, -Approve) is semidet
This hook is called by approve/2 and deny/2 before the default rules. If this hook succeeds it must unify Approve with true or false. Action is approved if Approve is true.
  168		 /*******************************
  169		 *           PENGINES		*
  170		 *******************************/
 pengines:not_sandboxed(+User, +Application) is semidet
Called by Pengines to see whether User may call non-sandboxed operations in Application.
  177:- multifile pengines:not_sandboxed/2.  178
  179pengines:not_sandboxed(User, Application) :-
  180    authorized(run(any, Application), [identity(User)])