forked from SWI-Prolog/swish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpep.pl
180 lines (152 loc) · 5.9 KB
/
pep.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
176
177
178
179
180
/* 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_pep,
[ authorized/2, % +Action, +Options
ws_authorized/2 % +Action, +WSID
]).
:- use_module(library(debug)).
:- use_module(library(option)).
:- use_module(library(error)).
:- use_module(library(http/http_wrapper)).
:- use_module(authenticate).
:- use_module(storage).
:- use_module(config).
/** <module> 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:
- Access to files (download, create, update, delete, list, search)
- Control of the sandboxing
- Access to users (profile management)
*/
:- multifile
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:
%
% * Gitty store actions
% - gitty(download(Obj, Format))
% Attempt to download Obj, one of file(File) or hash(Hash) in
% Format, see storage_get/4 from storage.pl
% - gitty(create(File,Named,Meta))
% Create file name File with the given meta-data. Named is one
% of `named` or `random` and indicates whether the file is named
% by the user or the name is generated by the system.
% - gitty(update(File,PrevMeta,Meta))
% Update File and change meta-data from PrevMeta to Meta.
% - gitty(delete(File,Meta))
% Delete File that has the given meta data.
% * File actions
% - file(update(File,Meta))
% Update (save) a physical file outside the versioned gitty
% store.
% * Social options
% - chat(open)
% Open websocket chat channel
% - chat(post(Message, About))
% Post a chat message about a specific topic
%
% @throws http_reply(forbidden(URL)) if the action is not allowed. Can
% we generate a JSON error object?
authorized(Action, _Options) :-
var(Action),
!,
instantiation_error(Action).
authorized(Action, Options) :-
option(identity(Id), Options),
( authorize(Action, Id)
-> debug(pep, 'Approved gitty action ~p to ~p', [Action, Id])
; debug(pep, 'Denied action ~p to ~p', [Action, Id]),
http_current_request(Request),
option(path(Path), Request),
throw(http_reply(forbidden(Path)))
).
%! 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:chat_add_user_id/3. It
% notably has a key `profile_id` if the user is logged on.
%
% @tbd Generalise. Notably, how do we get the identity as
% authenticate/2 returns?
ws_authorized(Action, _WSUser) :-
var(Action),
!,
instantiation_error(Action).
ws_authorized(chat(post(_,_)), WSUser) :-
_Profile = WSUser.get(profile_id).
:- multifile
approve/2,
deny/2.
authorize(Action, Id) :-
swish_config:approve(Action, Id, Approve),
!,
Approve == true.
authorize(Action, Id) :-
approve(Action, Id), !,
\+ deny(Action, Id).
%! approve(+Action, +Id)
approve(gitty(update(_File,PrevMeta,_Meta)), Auth) :- !,
storage_meta_property(PrevMeta, modify(Modify)),
( memberchk(any, Modify)
-> true
; memberchk(login, Modify)
-> user_property(Auth, login(_))
; storage_meta_property(PrevMeta, identity(Id)),
user_property(Auth, identity(Id))
).
approve(gitty(_), _).
approve(file(update(_File, _Meta)), Auth) :-
user_property(Auth, login(local)).
approve(run(any, _), Auth) :-
user_property(Auth, login(local)).
approve(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`.
/*******************************
* PENGINES *
*******************************/
%! pengines:not_sandboxed(+User, +Application) is semidet.
%
% Called by Pengines to see whether User may call non-sandboxed
% operations in Application.
:- multifile pengines:not_sandboxed/2.
pengines:not_sandboxed(User, Application) :-
authorized(run(any, Application), [identity(User)]).