app.py 898 Bytes
Newer Older
Robin Sonnabend's avatar
Robin Sonnabend committed
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
#!/usr/bin/env python3

from flask import Flask, g, request, abort, Response

from wiki import WikiClient, WikiException

import config
domain = getattr(config, "WIKI_DOMAIN", None)

app = Flask(__name__)
app.config.from_object(config)

def try_wiki_login(user, password):
    client = WikiClient(endpoint=config.WIKI_API_ENDPOINT)
    try:
        client.login(user, password, domain=domain)
        client.logout()
        return True
    except WikiException as error:
        print(error)
    return False

@app.route("/")
def index():
    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):
        return Response("Forbidden", status=403)
    return Response("OK", status=200)