summaryrefslogtreecommitdiff
path: root/distix-backend
blob: ea621c0968195b1c1c96fbad294b4b1b8d41a32c (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
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
#!/usr/bin/env python2


import os
import random
import sys
import yaml

import bottle

import distixapi



class AuthenticationPlugin(object):

    name = 'AuthenticationPlugin'

    def apply(self, callback, route):
        def authorize(*args, **kwargs):
            try:
                scopes = distixapi.get_scopes(users, bottle.request)
            except distixapi.AuthenticationError:
                return bottle.abort(401, 'Unauthorized')
            if route['method'].lower() not in scopes:
                return bottle.abort(401, 'Unauthorized')
            return callback(*args, **kwargs)
        return authorize


@bottle.route('/')
def root():
    return 'This is the root'

@bottle.route('/version')
def version():
    return { 'version': '1.0' }


# Command line args.

pid_file = sys.argv[1]
port_file = sys.argv[2]
users_file = sys.argv[3]



log_file = open('log', 'a')
def log(msg):
    log_file.write('{} {}\n'.format(os.getpid(), msg))
    log_file.flush()


# Write pid to named file.

with open(pid_file, 'w') as f:
    f.write('{}\n'.format(os.getpid()))


# Pick a random port and write it to named file.
port = random.randint(1025, 32767)
with open(port_file, 'w') as f:
    f.write('{}\n'.format(port))

users = yaml.safe_load(open(users_file))
bottle.run(port=port, quiet=True, plugins=[AuthenticationPlugin()])