def post_user(): try: utils.flask_validate_request_is_json(request) payload = utils.get_auth0_payload(app, request) now = datetime.datetime.now() uuid = str(utils.generate_uuid()) in_user = utils.get_args( received=request.json, defaultable={ 'id': uuid, 'created_at': now, 'updated_at': now, 'payload': {} }, constant={'type_id': 1}, ) in_user['username'] = payload['sub'] user = db_api.add_row(User, in_user) identity = utils.create_jwt_identity(user, payload) return utils.flask_return_success({ 'jwt': create_jwt(identity), 'user': user.json_dict }) except Exception as e: return utils.flask_handle_exception(e)
def put_event(): try: utils.flask_validate_request_is_json(request) now = datetime.datetime.now() uuid = str(utils.generate_uuid()) user = get_jwt_identity() row = utils.get_args( received=request.json, required={ 'name': str, 'description': str, 'address': str, 'postal_code': str, 'event_at': datetime.datetime }, defaultable={ 'id': uuid, 'created_at': now, 'updated_at': now, 'payload': {}, 'organization_id': 0, 'user_id': user }, ) filters = {'id': row.pop('id', None)} event = db_api.update_row(Event, filters, row) return utils.flask_return_success(event.json_dict) except Exception as e: return utils.flask_handle_exception(e)
def put_user(): try: utils.flask_validate_request_is_json(request) now = datetime.datetime.now() in_user = utils.get_args(received=request.json, defaultable={ 'created_at': now, 'updated_at': now, 'payload': {} }) filters = {'id': get_jwt_identity()} user = db_api.update_row(User, filters, in_user) return utils.flask_return_success(user.json_dict) except Exception as e: return utils.flask_handle_exception(e)
def patch_user(): try: utils.flask_validate_request_is_json(request) now = datetime.datetime.now() in_user = utils.get_args(received=request.json, required={'id': str}, defaultable={'updated_at': now}, optional={ 'created_at': datetime.datetime, 'payload': dict }) filters = {'id': in_user.pop('id', None)} user = db_api.update_row(User, filters, in_user) return utils.flask_return_success(user.json_dict) except Exception as e: return utils.flask_handle_exception(e)
def login(): app.logger.debug(request) try: utils.flask_validate_request_is_json(request) payload = utils.get_auth0_payload(app, request) username_filter = {'username': payload['sub']} user = db_api.get_row(User, username_filter) if user: identity = utils.create_jwt_identity(user, token_payload=payload) return utils.flask_return_success({ 'jwt': create_jwt(identity), 'user': user.json_dict }) else: return utils.flask_return_exception('User not found', 400) except Exception as e: return utils.flask_handle_exception(e)
def login(): app.logger.debug(request) token = utils.get_args(request.json, required={'token': str})['token'] user_payload: Dict[str, Any] = {} try: if "." not in token: # "new" opaque token response = requests.get( url="https://tikkifi.eu.auth0.com/userinfo", headers={"Authorization": f"Bearer {token}"}, ) body = response.json() if 'email' in body: user_payload["email"] = body["email"] if 'name' in body: user_payload["name"] = body["name"] username = body["sub"] else: # legacy token utils.flask_validate_request_is_json(request) payload = utils.get_auth0_payload(app, request) username = payload['sub'] user = db_api.get_row(User, {'username': username}) if not user: uuid = str(utils.generate_uuid()) user_payload = { "id": uuid, "username": username, "payload": {}, "type_id": 1 } user = db_api.add_row(User, user_payload) identity = utils.create_jwt_identity(user) return utils.flask_return_success({ 'jwt': create_jwt(identity), 'user': user_payload if user_payload else user.json_dict }) except Exception as e: return utils.flask_handle_exception(e)
def post_user_event_link(): try: utils.flask_validate_request_is_json(request) now = datetime.datetime.now() user = get_jwt_identity() row = utils.get_args( received=request.json, required={'event_id': str}, defaultable={ 'created_at': now, 'updated_at': now, 'user_id': user, 'payload': {} }, ) obj = db_api.add_row(UserEventLink, row) return utils.flask_return_success(obj.json_dict) except (AppException, FlaskRequestException) as e: return utils.flask_handle_exception(e)
def put_record(): try: utils.flask_validate_request_is_json(request) now = datetime.datetime.now() uuid = str(utils.generate_uuid()) user = get_jwt_identity() row = utils.get_args( received=request.json, defaultable={ 'id': uuid, 'created_at': now, 'updated_at': now, 'payload': {}, 'type_id': 0, 'user_id': user }, optional={'event_id': str}, ) filters = {'id': row.pop('id', None)} # Add created_user which defaults to the user_id, merging it with the # main row object. created_user = utils.get_args( received=request.json, defaultable={'created_user': user}, ) row.update(created_user) # And finally add details of who validated the record and when if provided. validated = utils.get_args( received=request.json, defaultable={'validated_at': now}, optional={'validated_user_id': str}, ) if validated.get('validated_user_id') is not None: row.update(validated) record = db_api.update_row(Record, filters, row) return utils.flask_return_success(record.json_dict) except Exception as e: return utils.flask_handle_exception(e)
def post_record(): try: utils.flask_validate_request_is_json(request) now = datetime.datetime.now() uuid = str(utils.generate_uuid()) row = utils.get_args(received=request.json, optional={'event_id': str}, constant={ 'user_id': get_jwt_identity(), }, defaultable={ 'id': uuid, 'created_at': now, 'updated_at': now, 'payload': {}, 'type_id': 0, }) # Add created_user which defaults to the user_id, merging it with the # main row object. user_id = row['user_id'] created_user = utils.get_args( received=request.json, defaultable={'created_user_id': user_id}, ) row.update(created_user) # And finally add details of who validated the record and when if provided. validated = utils.get_args( received=request.json, defaultable={'validated_at': now}, optional={'validated_user_id': str}, ) if 'validated_user_id' in validated: row.update(validated) record = db_api.add_row(Record, row) return utils.flask_return_success(record.json_dict) except Exception as e: return utils.flask_handle_exception(e)
def test_validate_request_is_json(self): request = self.get_request_mock() request.is_json = True self.assertIsNone(utils.flask_validate_request_is_json(request))