-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
71 lines (57 loc) · 2.02 KB
/
application.py
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
67
68
69
70
71
import json
from collections import namedtuple
from werkzeug.wrappers import Request, Response
from werkzeug.routing import Map, Rule
from werkzeug.exceptions import HTTPException, NotFound, abort
from jwt_token import JWTToken
from authorize import Auth
Scope = namedtuple('Scope', ['type', 'image', 'actions'])
class DockerAuth(object):
def __init__(self):
self.url_map = Map([
Rule('/v2/token/', endpoint='authorize'),
])
def on_authorize(self, request):
"""
Returns a token if user is authorized for action
"""
scope = None
if request.args.get('scope'):
type_, image, actions = request.args.get('scope').split(':')
actions = actions.split(',')
scope = Scope(type_, image, actions)
if not request.authorization:
abort(401)
if not Auth().check_access(
request.authorization.username,
request.authorization.password,
scope
):
abort(401)
token = JWTToken(
request.args['account'], request.args['service'],
scope
).generate()
res = {
'token': token
}
return Response(json.dumps(res))
def dispatch_request(self, request):
adapter = self.url_map.bind_to_environ(request.environ)
try:
endpoint, values = adapter.match()
return getattr(self, 'on_' + endpoint)(request, **values)
except NotFound, e:
return abort(404)
except HTTPException, e:
return e
def wsgi_app(self, environ, start_response):
request = Request(environ)
response = self.dispatch_request(request)
return response(environ, start_response)
def __call__(self, environ, start_response):
return self.wsgi_app(environ, start_response)
if __name__ == '__main__':
from werkzeug.serving import run_simple
app = DockerAuth()
run_simple('0.0.0.0', 4567, app, use_debugger=True, use_reloader=True)