Skip to content

Commit dfacaf8

Browse files
Add force permission to bypass server policy
1 parent f96501b commit dfacaf8

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Authentication is handled via tokens defined in `tokens.json`. The file has the
3030
"search": true,
3131
"read": true,
3232
"write": true,
33-
"delete": true
33+
"delete": true,
34+
"force":true
3435
},
3536
"token2": {
3637
"search": true,
@@ -47,6 +48,7 @@ The permissions are:
4748
* `read`: Get a bibliography entry based on an identifier
4849
* `write`: Add or modify bibliography entries
4950
* `delete`: Delete bibliography entries
51+
* `force`: Allow bibliography entries writes to bypass server policy
5052

5153
## Server
5254
The server runs inside a Docker container and works on a Git-versioned bibliography file outside the container.

client.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ def resolve_duplicate():
145145
return "i"
146146
return None
147147

148+
def resolve_policy_reject():
149+
print("Your options are")
150+
print(" force entry write to the server (F)")
151+
print(" ignore, do not apply any changes (I)")
152+
print(" abort without changes (A)")
153+
while True:
154+
action = input("Your choice [f/I/a]: ").lower()
155+
if action == "f" or action == "i" or action == "a":
156+
return action
157+
if not action or action == "":
158+
return "i"
159+
return None
160+
148161

149162
def update_local_bib(key, new_entry):
150163
for (idx, entry) in enumerate(bib_database.entries):
@@ -158,8 +171,12 @@ def update_remote_bib(key, new_entry):
158171
if "success" in response.json() and not response.json()["success"]:
159172
show_error(response.json())
160173

161-
def add_remote_bib(key, entry):
162-
response = requests.post(server + "entry/%s" % key, json = {"entry": entry, "token": token})
174+
def add_remote_bib(key, entry, force=False):
175+
if force:
176+
# do not rely on boolean encoding of `force`
177+
response = requests.post(server + "entry/%s" % key, json = {"entry": entry, "token": token, "force": "true"})
178+
else:
179+
response = requests.post(server + "entry/%s" % key, json = {"entry": entry, "token": token})
163180
if "success" in response.json() and not response.json()["success"]:
164181
show_error(response.json())
165182

@@ -270,7 +287,18 @@ def show_error(obj):
270287
response = requests.post(server + "update", json = {"entries": bib_database.entries, "token": token})
271288
result = response.json()
272289
if not result["success"]:
273-
if result["reason"] == "duplicate":
290+
if result["reason"] == "policy":
291+
#print(result["entries"])
292+
for entry in result["entries"]:
293+
print("\n[!] Server policy rejected entry %s. Reason: %s" % (entry["ID"], entry["reason"]))
294+
action = resolve_policy_reject()
295+
if action == "i":
296+
pass
297+
elif action == "a":
298+
sys.exit(1)
299+
elif action == "f":
300+
add_remote_bib(entry["ID"], entry_by_key(entry["ID"]), force=True)
301+
elif result["reason"] == "duplicate":
274302
#print(result["entries"])
275303
for dup in result["entries"]:
276304
print("\n[!] There is already a similar entry for %s on the server (%s) [Levenshtein %d]" % (dup[1], dup[2]["ID"], dup[0]))

server.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ def add_entry(key):
216216
ok, reason = check_token(request.json["token"], "write")
217217
if not ok:
218218
return jsonify(reason)
219+
# check if the client is forcing the server policy
220+
if "force" in request.json:
221+
ok, reason = check_token(request.json["token"], "force")
222+
if not ok:
223+
return jsonify(reason)
219224

220225
if "ID" not in request.json["entry"]:
221226
request.json["entry"]["ID"] = key
@@ -224,7 +229,7 @@ def add_entry(key):
224229
if existing:
225230
return jsonify({"success": False, "reason": "exists", "entry": existing})
226231

227-
if policy:
232+
if policy and "force" not in request.json:
228233
accept, reason = policy.check(request.json["entry"], bib_database.entries)
229234
if not accept:
230235
entry = request.json["entry"]
@@ -276,6 +281,11 @@ def add_entries():
276281
ok, reason = check_token(request.json["token"], "write")
277282
if not ok:
278283
return jsonify(reason)
284+
# check if the client is forcing the server policy
285+
if "force" in request.json:
286+
ok, reason = check_token(request.json["token"], "force")
287+
if not ok:
288+
return jsonify(reason)
279289

280290
dups = []
281291
changes = False
@@ -290,7 +300,7 @@ def add_entries():
290300
if len(dup) == 0:
291301
# new entry, add
292302
if not entry_by_key(entry["ID"]):
293-
if policy:
303+
if policy and "force" not in request.json:
294304
accept, reason = policy.check(entry, bib_database.entries)
295305
if not accept:
296306
entry["reason"] = reason

0 commit comments

Comments
 (0)