-
Notifications
You must be signed in to change notification settings - Fork 18
/
oauth2.py
55 lines (44 loc) · 1.92 KB
/
oauth2.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
# -*- coding: utf-8 -*-
"""
A demostration of a simple BearerToken class.
Active tokens are stored in redis via the Flask-Sentinel extension. When
a request hits a API endpoint all we need to do is verify that a token
is provided with the request and that said token is active.
See https://github.com/pyeve/flask-sentinel
:copyright: (c) 2015 by Nicola Iarocci.
:license: BSD, see LICENSE for more details.
"""
from eve.auth import BasicAuth
from flask import request
from redis import StrictRedis
class BearerAuth(BasicAuth):
""" Overrides Eve's built-in basic authorization scheme and uses Redis to
validate bearer token
"""
def __init__(self):
super(BearerAuth, self).__init__()
self.redis = StrictRedis()
self.redis.connection_pool = ConnectionPool.from_url(os.environ.get(
'REDIS_URL',
'redis://localhost:6379'))
def check_auth(self, token, allowed_roles, resource, method):
""" Check if API request is authorized.
Examines token in header and checks Redis cache to see if token is
valid. If so, request is allowed.
:param token: OAuth 2.0 access token submitted.
:param allowed_roles: Allowed user roles.
:param resource: Resource being requested.
:param method: HTTP method being executed (POST, GET, etc.)
"""
return token and self.redis.get(token)
def authorized(self, allowed_roles, resource, method):
""" Validates the the current request is allowed to pass through.
:param allowed_roles: allowed roles for the current request, can be a
string or a list of roles.
:param resource: resource being requested.
"""
try:
token = request.headers.get('Authorization').split(' ')[1]
except:
token = None
return self.check_auth(token, allowed_roles, resource, method)