def reset_password(): response = model_user.reset_password(request.headers.get('key')) if response is None: _(400, 'No user associated with the provided key', stop=True) if response is False: _(500, 'Something wrong resetting the password', stop=True) return jsonify({'status': 200, 'password': response})
def default(): if request.headers.get('email') is None or not request.headers.get( 'email'): _(412, 'The parameter email is required', stop=True) if request.headers.get('token') is None or not request.headers.get( 'token'): _(412, 'The parameter token is required', stop=True) return f()
def ott_generate(): response = model_user.ott_generate(request.headers.get('email'), request.headers.get('token')) if response is None or response is False: _(401, 'The provided token is invalid or expired', stop=True) return jsonify({ 'status': 200, 'expiration_time': response['expiration_time'], 'token': response['token'] })
def authenticate(): response = model_user.authenticate(request.headers.get('email'), request.headers.get('password')) if response is None or response is False: _(401, 'Wrong login', stop=True) return jsonify({ 'status': 200, 'expiration_time': response['expiration_time'], 'token': response['token'] })
def default(): if request.headers.get( 'old_password') is None or not request.headers.get( 'old_password'): _(412, 'The parameter old_password is required', stop=True) if request.headers.get( 'new_password') is None or not request.headers.get( 'new_password'): _(412, 'The parameter new_password is required', stop=True) return f()
def authorize(): response = model_user.verify_auth_token(request.headers.get('email'), request.headers.get('token')) if response is False or response is None: _(401, 'The provided token is invalid or expired', stop=True) return jsonify({ 'status': 200, 'id': response['id'], 'email': response['email'] })
def clean(): now = service_timestamp_get() try: TokenBanlist.query.filter( TokenBanlist.expiration_time < now).delete() db.session.commit() db.session.close() return True except BaseException as e: _(500, str(e)) return False
def add(expiration_time, token): now = service_timestamp_get() try: m = TokenBanlist(now, expiration_time, token) db.session.add(m) db.session.commit() db.session.close() return True except BaseException as e: _(500, str(e)) return False
def logout(id): m = User.find_by_id(id) if m is None: return False try: m.nonce = None db.session.commit() return True except BaseException as e: _(500, str(e)) return False
def token_refresh(): response = model_user.refresh_token(request.headers.get('email'), request.headers.get('token')) if response is False: _(401, 'The provided token is invalid or expired', stop=True) if response is None: _(500, 'Something wrong refreshing the token', stop=True) return jsonify({ 'status': 200, 'expiration_time': response['expiration_time'], 'token': response['token'] })
def signup(email, password): now = service_timestamp_get() password_hash = User.password_hash(password) try: m = User(now, now, email, password_hash) db.session.add(m) db.session.commit() db.session.close() response = base64.b64encode(bytes(str(now) + email, "utf-8")) return str(response)[2:-1] except BaseException as e: _(500, str(e)) return False
def get(): response = False try: http = urllib3.PoolManager() r = http.request('GET', config_app().data['service_timer_baseurl']) payload = json.loads(r.data.decode('utf-8')) if payload['posix'] is not None: response = int(payload['posix']['timestamp']) except Exception as e: _(500, str(e)) response = None pass return response
def refresh_nonce(id): m = User.find_by_id(id) if m is None: return False try: m.nonce = User.generate_password(letters=True, punctuation=True, digits=True, _range=[64, 64]) db.session.commit() return True except BaseException as e: _(500, str(e)) return False
def activate(key): now = service_timestamp_get() try: params = base64.b64decode(str.encode(key)).decode( 'utf-8', 'ignore') except BaseException as e: _(500, str(e)) _(406, 'The provided key has an invalid format', stop=True) insert_date = params[:10] email = params[10:] m = User.query.filter_by(insert_date=insert_date, email=email).first() if m is None: return m else: if m.is_active: _(412, 'This user cannot be activated', stop=True) else: try: m.last_update_date = now m.is_active = True db.session.commit() db.session.close() return True except BaseException as e: _(500, str(e)) return False return False
def default(): if request.headers.get('email') is None or not request.headers.get( 'email'): _(412, 'The parameter email is required', stop=True) if request.headers.get('password') is None or not request.headers.get( 'password'): _(412, 'The parameter password is required', stop=True) if model_user.user_exists_by_email(request.headers.get('email')): _(409, 'The provided email is invalid or unavailable', stop=True) if service_email_validate(request.headers.get('email')) is None: _(403, 'The required service email is unreachable', stop=True) if service_email_validate(request.headers.get('email')) is False: _(412, 'The provided email is invalid', stop=True) return f()
def update_email_by_id(id, email): now = service_timestamp_get() m = User.find_by_id(id) if m is None: return m try: model_history.add_by_user_id(id, 'email', m.email) m.last_update_date = now m.email = email m.is_active = False db.session.commit() db.session.close() return True except BaseException as e: _(500, str(e)) return False
def set_stripe_id(email, token, stripe_id): attrs = User.verify_auth_token(email, token) if attrs is False or attrs is None or attrs['email'] != email: return None m = User.find_by_email(email) if m is None: return m if m.stripe_id is not None: return False try: m.stripe_id = stripe_id db.session.commit() db.session.close() return True except BaseException as e: _(500, str(e)) return False
def update_password_by_id(id, old_password, new_password): now = service_timestamp_get() m = User.find_by_id(id) if m is None: return m if User.verify_password(old_password, m.password) is False: _(406, 'The old_password is invalid', stop=True) try: model_history.add_by_user_id(id, 'password', m.password) m.last_update_date = now m.password = User.password_hash(new_password) #m.is_active = False db.session.commit() db.session.close() return True except BaseException as e: _(500, str(e)) return False
def validate(email): response = False try: http = urllib3.PoolManager() params = { 'secret': config_app().data['service_email_validate_secret'], 'email': email } r = http.request('GET', config_app().data['service_email_validate_baseurl'], None, params) payload = json.loads(r.data.decode('utf-8')) if payload['valid'] is not None: response = payload['valid'] except Exception as e: _(500, str(e)) response = None pass return response
def default(*args, **kwargs): try: secret = request.headers.get('secret') except: return _(412, 'The parameter secret is empty or missing', stop=True) model = model_default.get_by_secret(secret) if model is not None: if config_app().data['limit'].get(model.limit, None) is None: return _(501, 'The requested functionality is not supported', stop=True) if model is not None: counter = model_default.get_usage(model) if counter >= config_app().data['limit'][model.limit]: return _(429, 'The usage limit has been reached', stop=True) else: return _(401, 'The secret used is not authorized', stop=True) return f(*args, **kwargs)
def activate(): response = model_user.activate(request.headers.get('key')) if response is None: _(400, 'No user associated to the provided email', stop=True) if response is False: _(401, 'The provided key is invalid', stop=True) _(200, 'The email has ben activated', stop=True)
def logout(): m_user = json.loads(authorize().data.decode('utf-8')) if m_user is None: _(401, 'No user associated to the provided parameters', stop=True) response = model_user.logout(m_user['id']) if response is False: _(500, 'Something wrong logging out the user', stop=True) _(200, 'The user has been logged out', stop=True)
def default(): if request.headers.get('email') is None or not request.headers.get( 'email'): _(412, 'The parameter email is required', stop=True) if service_email_validate(request.headers.get('email')) is None: _(403, 'The required service email is unreachable', stop=True) if service_email_validate(request.headers.get('email')) is False: _(412, 'The provided email is invalid', stop=True) return f()
def set_stripe_id(): response = model_user.set_stripe_id(request.headers.get('email'), request.headers.get('token'), request.headers.get('stripe_id')) if response is None: _(401, 'The provided token is invalid or expired', stop=True) if response is False: _(401, 'stripe_id cannot be updated', stop=True) _(200, 'Stripe ID has been updated', stop=True)
def reset_password(token): now = service_timestamp_get() s = Serializer(config_app().data['secret_key']) try: data = s.loads(token) except SignatureExpired: return False except BadSignature: return False m = db.session.query(User).get(data['id']) if m is None: return m try: model_history.add_by_user_id(m.id, 'password', m.password) m.last_update_date = now new_password = User.generate_password() print('New password has been generated', new_password) m.password = User.password_hash(new_password) db.session.commit() db.session.close() return str(base64.b64encode(bytes(new_password, "utf-8")))[1:] except BaseException as e: _(500, str(e)) return False
def update_email(): m_user = json.loads(authorize().data.decode('utf-8')) response = model_user.update_email_by_id(m_user['id'], request.headers.get('new_email')) if response is None: _(400, 'No user associated to the provided parameters', stop=True) if response is False: _(500, 'Something wrong saving your data', stop=True) _(200, 'The email has been updated and the account needs to be activated again', stop=True)
def default(*args, **kwargs): m = model_ban.get() if isinstance(m, model_ban): if model_ban.grant(m) is False: return _(423, 'Your signature is banned, try again later', stop=True) #print(m.id, m.counter) #else: #return _(500, 'Something wrong managing your signature', stop=True) #if m is None: #if model_ban.add() is not True: #return _(500, 'Something wrong adding your signature', stop=True) #else: #if m.is_banned is True: #return _(423, 'Your signature is banned, try again later', stop=True) #if model_ban.update(m) is not True: #return _(500, 'Something wrong managing your signature', stop=True) return f(*args, **kwargs)
def signup(): response = model_user.signup(request.headers.get('email'), request.headers.get('password')) if response is False: _(500, 'Something wrong creating the account', stop=True) return jsonify({'status': 200, 'key': response})
def is_valid(): response = model_user.is_valid(request.headers.get('email')) if response is False: _(400, 'No user associated to the provided parameters', stop=True) return jsonify({'status': 200, 'key': response})
def update_registry(): m_user = json.loads(authorize().data.decode('utf-8')) if model_registry.update_registry_by_id(m_user['id'], request.headers) is False: _(500, 'Something wrong updating the registry', stop=True) _(200, 'The registry has been updated', stop=True)