2015-04-07 12:42:50 -03:00
|
|
|
import os
|
|
|
|
|
2015-03-10 11:38:57 +01:00
|
|
|
from eve import Eve
|
|
|
|
|
2015-04-07 12:42:50 -03:00
|
|
|
# import random
|
|
|
|
# import string
|
2015-03-10 11:38:57 +01:00
|
|
|
|
2015-03-14 15:08:36 +01:00
|
|
|
from eve.auth import TokenAuth
|
|
|
|
from eve.auth import BasicAuth
|
2015-03-10 11:38:57 +01:00
|
|
|
from eve.io.mongo import Validator
|
2015-04-09 16:36:32 -03:00
|
|
|
from eve.methods.post import post_internal
|
2015-03-12 15:05:10 +01:00
|
|
|
from bson import ObjectId
|
2015-03-10 11:38:57 +01:00
|
|
|
|
2015-04-08 11:48:38 -03:00
|
|
|
from datetime import datetime
|
|
|
|
from datetime import timedelta
|
2015-03-14 15:08:36 +01:00
|
|
|
|
2015-04-14 12:04:50 -03:00
|
|
|
RFC1123_DATE_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
|
|
|
|
|
|
|
|
|
2015-04-07 12:42:50 -03:00
|
|
|
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']
|
2015-04-09 19:06:10 -03:00
|
|
|
user = r.json()['user']
|
2015-04-07 12:42:50 -03:00
|
|
|
else:
|
|
|
|
message = ""
|
|
|
|
valid = False
|
2015-04-10 13:08:45 -03:00
|
|
|
user = None
|
2015-04-09 19:06:10 -03:00
|
|
|
return dict(valid=valid, message=message, user=user)
|
2015-04-07 12:42:50 -03:00
|
|
|
|
|
|
|
|
2015-03-14 15:08:36 +01:00
|
|
|
class TokensAuth(TokenAuth):
|
|
|
|
def check_auth(self, token, allowed_roles, resource, method):
|
2015-04-08 13:14:35 -03:00
|
|
|
if not token:
|
|
|
|
return False
|
2015-04-08 11:48:38 -03:00
|
|
|
tokens = app.data.driver.db['tokens']
|
2015-04-09 19:06:10 -03:00
|
|
|
users = app.data.driver.db['users']
|
2015-04-09 16:36:32 -03:00
|
|
|
lookup = {'token': token, 'expire_time': {"$gt": datetime.now()}}
|
2015-04-08 11:48:38 -03:00
|
|
|
dbtoken = tokens.find_one(lookup)
|
|
|
|
if not dbtoken:
|
|
|
|
validation = validate(token)
|
|
|
|
if validation['valid']:
|
2015-04-09 19:06:10 -03:00
|
|
|
email = validation['user']['email']
|
|
|
|
dbuser = users.find_one({'email': email})
|
|
|
|
tmpname = email.split('@')[0]
|
|
|
|
if not dbuser:
|
|
|
|
user_data = {
|
2015-04-16 15:28:28 +02:00
|
|
|
'first_name': tmpname,
|
|
|
|
'last_name': tmpname,
|
2015-04-09 19:06:10 -03:00
|
|
|
'email': email,
|
|
|
|
'role': ['admin'],
|
|
|
|
}
|
|
|
|
r = post_internal('users', user_data)
|
|
|
|
user_id = r[0]["_id"]
|
|
|
|
else:
|
|
|
|
user_id = dbuser['_id']
|
|
|
|
|
|
|
|
token_data = {
|
|
|
|
'user': user_id,
|
2015-04-08 11:48:38 -03:00
|
|
|
'token': token,
|
2015-04-13 01:21:57 +02:00
|
|
|
'expire_time': datetime.now() + timedelta(hours=1)
|
2015-04-08 11:48:38 -03:00
|
|
|
}
|
2015-04-09 19:06:10 -03:00
|
|
|
post_internal('tokens', token_data)
|
2015-04-08 11:48:38 -03:00
|
|
|
else:
|
2015-04-08 13:14:35 -03:00
|
|
|
return True
|
2015-04-07 12:42:50 -03:00
|
|
|
return validation['valid']
|
2015-04-08 11:48:38 -03:00
|
|
|
"""
|
2015-03-14 15:08:36 +01:00
|
|
|
users = app.data.driver.db['users']
|
2015-04-16 15:28:28 +02:00
|
|
|
lookup = {'first_name': token['username']}
|
2015-03-14 15:08:36 +01:00
|
|
|
if allowed_roles:
|
|
|
|
lookup['role'] = {'$in': allowed_roles}
|
|
|
|
user = users.find_one(lookup)
|
|
|
|
if not user:
|
|
|
|
return False
|
2015-04-08 11:48:38 -03:00
|
|
|
return token
|
|
|
|
"""
|
|
|
|
|
2015-03-14 15:08:36 +01:00
|
|
|
|
|
|
|
class BasicsAuth(BasicAuth):
|
|
|
|
def check_auth(self, username, password, allowed_roles, resource, method):
|
2015-04-08 11:48:38 -03:00
|
|
|
# return username == 'admin' and password == 'secret'
|
|
|
|
return True
|
2015-03-14 15:08:36 +01:00
|
|
|
|
|
|
|
|
2015-04-13 01:21:57 +02:00
|
|
|
class CustomTokenAuth(BasicsAuth):
|
2015-03-27 15:42:28 +01:00
|
|
|
"""Switch between Basic and Token auth"""
|
2015-03-14 15:08:36 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.token_auth = TokensAuth()
|
|
|
|
self.authorized_protected = BasicsAuth.authorized
|
|
|
|
|
|
|
|
def authorized(self, allowed_roles, resource, method):
|
2015-04-09 16:36:32 -03:00
|
|
|
# if resource == 'tokens':
|
|
|
|
if False:
|
2015-04-08 11:48:38 -03:00
|
|
|
return self.authorized_protected(
|
|
|
|
self, allowed_roles, resource, method)
|
2015-03-14 15:08:36 +01:00
|
|
|
else:
|
|
|
|
return self.token_auth.authorized(allowed_roles, resource, method)
|
|
|
|
|
|
|
|
def authorized_protected(self):
|
|
|
|
pass
|
|
|
|
|
2015-04-15 06:43:53 -03:00
|
|
|
def convert_properties(properties, node_schema):
|
|
|
|
for prop in node_schema:
|
2015-04-15 10:25:31 -03:00
|
|
|
if not prop in properties:
|
|
|
|
continue
|
2015-04-15 06:43:53 -03:00
|
|
|
schema_prop = node_schema[prop]
|
|
|
|
prop_type = schema_prop['type']
|
|
|
|
if prop_type == 'dict':
|
|
|
|
properties[prop] = convert_properties(
|
|
|
|
properties[prop], schema_prop['schema'])
|
2015-04-15 10:25:31 -03:00
|
|
|
if prop_type == 'list':
|
2015-04-15 11:51:55 -03:00
|
|
|
if properties[prop] in ['', '[]']:
|
2015-04-15 10:25:31 -03:00
|
|
|
properties[prop] = []
|
|
|
|
for k, val in enumerate(properties[prop]):
|
|
|
|
if not 'schema' in schema_prop:
|
|
|
|
continue
|
|
|
|
item_schema = {'item': schema_prop['schema']}
|
|
|
|
item_prop = {'item': properties[prop][k]}
|
|
|
|
properties[prop][k] = convert_properties(
|
|
|
|
item_prop, item_schema)['item']
|
|
|
|
# Convert datetime string to RFC1123 datetime
|
2015-04-15 06:43:53 -03:00
|
|
|
elif prop_type == 'datetime':
|
|
|
|
prop_val = properties[prop]
|
|
|
|
properties[prop] = datetime.strptime(prop_val, RFC1123_DATE_FORMAT)
|
2015-04-15 10:25:31 -03:00
|
|
|
elif prop_type == 'objectid':
|
|
|
|
prop_val = properties[prop]
|
|
|
|
properties[prop] = ObjectId(prop_val)
|
2015-04-15 06:43:53 -03:00
|
|
|
|
|
|
|
return properties
|
2015-03-14 15:08:36 +01:00
|
|
|
|
2015-03-10 11:38:57 +01:00
|
|
|
class ValidateCustomFields(Validator):
|
2015-03-11 16:03:19 +01:00
|
|
|
def _validate_valid_properties(self, valid_properties, field, value):
|
2015-03-12 15:05:10 +01:00
|
|
|
node_types = app.data.driver.db['node_types']
|
2015-03-11 16:03:19 +01:00
|
|
|
lookup = {}
|
2015-03-12 15:05:10 +01:00
|
|
|
lookup['_id'] = ObjectId(self.document['node_type'])
|
2015-03-11 16:03:19 +01:00
|
|
|
node_type = node_types.find_one(lookup)
|
|
|
|
|
2015-04-15 10:25:31 -03:00
|
|
|
try:
|
|
|
|
value = convert_properties(value, node_type['dyn_schema'])
|
2015-04-15 11:51:55 -03:00
|
|
|
except Exception, e:
|
|
|
|
print ("Error converting: {0}".format(e))
|
2015-04-15 10:25:31 -03:00
|
|
|
print (value)
|
2015-04-14 12:04:50 -03:00
|
|
|
|
2015-03-11 16:03:19 +01:00
|
|
|
v = Validator(node_type['dyn_schema'])
|
|
|
|
val = v.validate(value)
|
2015-04-15 10:25:31 -03:00
|
|
|
|
2015-03-11 16:03:19 +01:00
|
|
|
if val:
|
|
|
|
return True
|
|
|
|
else:
|
2015-04-15 10:25:31 -03:00
|
|
|
try:
|
|
|
|
print (val.errors)
|
|
|
|
except:
|
|
|
|
pass
|
2015-03-27 15:42:28 +01:00
|
|
|
self._error(
|
|
|
|
field, "Error validating properties")
|
2015-03-11 16:03:19 +01:00
|
|
|
|
|
|
|
|
2015-04-10 13:08:45 -03:00
|
|
|
def post_item(entry, data):
|
|
|
|
post_internal(entry, data)
|
2015-03-11 16:03:19 +01:00
|
|
|
|
|
|
|
|
2015-04-13 01:21:57 +02:00
|
|
|
app = Eve(validator=ValidateCustomFields, auth=CustomTokenAuth)
|
2015-04-24 11:57:40 +02:00
|
|
|
|
|
|
|
# The file_server module needs app to be defined
|
|
|
|
from file_server import file_server
|
2015-04-23 16:03:34 -03:00
|
|
|
app.register_blueprint(file_server, url_prefix='/file_server')
|