diff --git a/app.py b/app.py index a92823ca88dbd20c90ed4a4f989f4a01da684fe6..9a51043e5c128aff63783eccc12d68e9e98fccbd 100644 --- a/app.py +++ b/app.py @@ -10,12 +10,15 @@ domain = getattr(config, "WIKI_DOMAIN", None) app = Flask(__name__) app.config.from_object(config) -def try_wiki_login(user, password): +def try_wiki_login(user, password, check_admin=False): client = WikiClient(endpoint=config.WIKI_API_ENDPOINT) try: client.login(user, password, domain=domain) + result = True + if check_admin: + result = "sysop" in client.get_rights() client.logout() - return True + return result except WikiException as error: print(error) return False @@ -30,4 +33,14 @@ def index(): if not try_wiki_login(auth.username, auth.password): return Response("Forbidden", status=403) return Response("OK", status=200) - + +@app.route("/admin/") +def admin(): + auth = request.authorization + if not auth: + return Response( + "Please authenticate with your Wiki credentials.", 401, + {"WWW-Authenticate": "Basic realm=\"Wiki\""}) + if not try_wiki_login(auth.username, auth.password, check_admin=True): + return Response("Forbidden", status=403) + return Response("OK", status=200) diff --git a/wiki.py b/wiki.py index 16fadcc1a824df46dd53dae6ef899121f85d7734..f71701c9a13163d3955b88410bff6fb37d606de1 100644 --- a/wiki.py +++ b/wiki.py @@ -70,3 +70,10 @@ class WikiClient: except JSONDecodeError: raise WikiException("Server did not return valid JSON.") + def get_rights(self): + answer = self.do_action("query", meta="userinfo", uiprop="groups") + try: + return answer["query"]["userinfo"]["groups"] + except KeyError as error: + print(error) + return []