#!/usr/bin/env python2 import os import random import sys import bottle import distixapi users = { 'users': { 'fooser': { 'salt': 'nacl', 'password': distixapi.encrypt_password('nacl', 'password'), 'scopes': ['get', 'put'], }, 'no': { 'salt': 'nacl', 'password': distixapi.encrypt_password('nacl', 'password'), 'scopes': [], }, }, } 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] 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)) bottle.run(port=port, quiet=True, plugins=[AuthenticationPlugin()])