summaryrefslogtreecommitdiff
path: root/login.py
blob: 24f62a16e714ad04f28a2548e72ba387983a99e1 (plain)
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
#!/usr/bin/env python3.4
import os
import sys
import functions


def run(config):
    conf_users = config['users'].split(',')
    conf_secret = config['secret']
    conf_validsec = int(config['validsec'])
    conf_jid = config['jid']
    conf_jid_pw = config['jid_pw']

    # reading the credential supplied in a pipe from apache
    username = sys.stdin.readline().strip()
    password = sys.stdin.readline().strip()

    if password == "" and username in conf_users:
        # avoid spamming by allowing only one message sent at a time
        lockfile = os.path.basename(__file__)
        with functions.file_lock("/tmp/lock." + lockfile):
            message = functions.token_message(username, conf_secret, conf_validsec,
                                              os.getenv("URI"), os.getenv("HTTP_HOST"))
            if os.getenv("SKIP_XMPP"):  # used for testing
                print(message)
            else:
                functions.send_message(conf_jid, conf_jid_pw, username, message)
    elif username in conf_users:
        if functions.verify_token(username, password, conf_secret, conf_validsec):
            return os.EX_OK

    return os.EX_NOPERM  # fail by default


if __name__ == "__main__":
    config = dict(map(lambda kv: kv.split("="),
                      os.getenv("CONTEXT").split(";")))
    sys.exit(run(config))