Storing tokens (without username)
This commit is contained in:
parent
2d50503687
commit
534e6ad9c3
@ -10,6 +10,8 @@ from eve.auth import BasicAuth
|
|||||||
from eve.io.mongo import Validator
|
from eve.io.mongo import Validator
|
||||||
from bson import ObjectId
|
from bson import ObjectId
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
class SystemUtility():
|
class SystemUtility():
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
@ -45,15 +47,23 @@ def validate(token):
|
|||||||
|
|
||||||
class TokensAuth(TokenAuth):
|
class TokensAuth(TokenAuth):
|
||||||
def check_auth(self, token, allowed_roles, resource, method):
|
def check_auth(self, token, allowed_roles, resource, method):
|
||||||
# print (token)
|
tokens = app.data.driver.db['tokens']
|
||||||
validation = validate(token)
|
lookup = {'token': token, 'updated': {"$gt": datetime.now()}}
|
||||||
# print validation['message']
|
dbtoken = None
|
||||||
|
dbtoken = tokens.find_one(lookup)
|
||||||
|
if not dbtoken:
|
||||||
|
validation = validate(token)
|
||||||
|
if validation['valid']:
|
||||||
|
data = {
|
||||||
|
'username': '',
|
||||||
|
'token': token,
|
||||||
|
'updated': datetime.now()+timedelta(hours=1)
|
||||||
|
}
|
||||||
|
tokens.insert(data)
|
||||||
|
else:
|
||||||
|
validation = {'valid': True}
|
||||||
return validation['valid']
|
return validation['valid']
|
||||||
"""tokens = app.data.driver.db['tokens']
|
"""
|
||||||
lookup = {'token': token}
|
|
||||||
token = tokens.find_one(lookup)
|
|
||||||
if not token:
|
|
||||||
return False
|
|
||||||
users = app.data.driver.db['users']
|
users = app.data.driver.db['users']
|
||||||
lookup = {'firstname': token['username']}
|
lookup = {'firstname': token['username']}
|
||||||
if allowed_roles:
|
if allowed_roles:
|
||||||
@ -61,11 +71,14 @@ class TokensAuth(TokenAuth):
|
|||||||
user = users.find_one(lookup)
|
user = users.find_one(lookup)
|
||||||
if not user:
|
if not user:
|
||||||
return False
|
return False
|
||||||
return token"""
|
return token
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
class BasicsAuth(BasicAuth):
|
class BasicsAuth(BasicAuth):
|
||||||
def check_auth(self, username, password, allowed_roles, resource, method):
|
def check_auth(self, username, password, allowed_roles, resource, method):
|
||||||
return username == 'admin' and password == 'secret'
|
# return username == 'admin' and password == 'secret'
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class MyTokenAuth(BasicsAuth):
|
class MyTokenAuth(BasicsAuth):
|
||||||
@ -75,8 +88,9 @@ class MyTokenAuth(BasicsAuth):
|
|||||||
self.authorized_protected = BasicsAuth.authorized
|
self.authorized_protected = BasicsAuth.authorized
|
||||||
|
|
||||||
def authorized(self, allowed_roles, resource, method):
|
def authorized(self, allowed_roles, resource, method):
|
||||||
if resource=='tokens':
|
if resource == 'tokens':
|
||||||
return self.authorized_protected(self, allowed_roles, resource, method)
|
return self.authorized_protected(
|
||||||
|
self, allowed_roles, resource, method)
|
||||||
else:
|
else:
|
||||||
return self.token_auth.authorized(allowed_roles, resource, method)
|
return self.token_auth.authorized(allowed_roles, resource, method)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user