Check if the token is valid
This commit is contained in:
parent
1715f5dffe
commit
3117111687
@ -1,7 +1,9 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
from eve import Eve
|
from eve import Eve
|
||||||
|
|
||||||
import random
|
# import random
|
||||||
import string
|
# import string
|
||||||
|
|
||||||
from eve.auth import TokenAuth
|
from eve.auth import TokenAuth
|
||||||
from eve.auth import BasicAuth
|
from eve.auth import BasicAuth
|
||||||
@ -9,9 +11,45 @@ from eve.io.mongo import Validator
|
|||||||
from bson import ObjectId
|
from bson import ObjectId
|
||||||
|
|
||||||
|
|
||||||
|
class SystemUtility():
|
||||||
|
def __new__(cls, *args, **kwargs):
|
||||||
|
raise TypeError("Base class may not be instantiated")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def blender_id_endpoint():
|
||||||
|
"""Gets the endpoint for the authentication API. If the env variable
|
||||||
|
is defined, it's possible to override the (default) production address.
|
||||||
|
"""
|
||||||
|
return os.environ.get(
|
||||||
|
'BLENDER_ID_ENDPOINT', "https://www.blender.org/id")
|
||||||
|
|
||||||
|
|
||||||
|
def validate(token):
|
||||||
|
import requests
|
||||||
|
payload = dict(
|
||||||
|
token=token)
|
||||||
|
try:
|
||||||
|
r = requests.post("{0}/u/validate_token".format(
|
||||||
|
SystemUtility.blender_id_endpoint()), data=payload)
|
||||||
|
except requests.exceptions.ConnectionError as e:
|
||||||
|
raise e
|
||||||
|
|
||||||
|
if r.status_code == 200:
|
||||||
|
message = r.json()['message']
|
||||||
|
valid = r.json()['valid']
|
||||||
|
else:
|
||||||
|
message = ""
|
||||||
|
valid = False
|
||||||
|
return dict(valid=valid, message=message)
|
||||||
|
|
||||||
|
|
||||||
class TokensAuth(TokenAuth):
|
class TokensAuth(TokenAuth):
|
||||||
def check_auth(self, token, allowed_roles, resource, method):
|
def check_auth(self, token, allowed_roles, resource, method):
|
||||||
tokens = app.data.driver.db['tokens']
|
# print (token)
|
||||||
|
validation = validate(token)
|
||||||
|
# print validation['message']
|
||||||
|
return validation['valid']
|
||||||
|
"""tokens = app.data.driver.db['tokens']
|
||||||
lookup = {'token': token}
|
lookup = {'token': token}
|
||||||
token = tokens.find_one(lookup)
|
token = tokens.find_one(lookup)
|
||||||
if not token:
|
if not token:
|
||||||
@ -23,7 +61,7 @@ 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):
|
||||||
@ -63,14 +101,11 @@ class ValidateCustomFields(Validator):
|
|||||||
field, "Error validating properties")
|
field, "Error validating properties")
|
||||||
|
|
||||||
|
|
||||||
def add_token(documents):
|
"""def add_token(documents):
|
||||||
# Don't use this in production:
|
|
||||||
# You should at least make sure that the token is unique.
|
|
||||||
# print ("Adding Token")
|
|
||||||
for document in documents:
|
for document in documents:
|
||||||
document["token"] = (''.join(random.choice(string.ascii_uppercase)
|
document["token"] = (''.join(random.choice(string.ascii_uppercase)
|
||||||
for x in range(10)))
|
for x in range(10)))"""
|
||||||
|
|
||||||
|
|
||||||
app = Eve(validator=ValidateCustomFields, auth=MyTokenAuth)
|
app = Eve(validator=ValidateCustomFields, auth=MyTokenAuth)
|
||||||
app.on_insert_tokens += add_token
|
# app.on_insert_tokens += add_token
|
||||||
|
Loading…
x
Reference in New Issue
Block a user