def edit_channel(): user_id = utils.get_user_id(app) check_status, check_response = utils.request_params_check( app.current_request.json_body, ('channelName', 'channelWebhook', 'channelId')) if not check_status: return check_response channel_name = app.current_request.json_body['channelName'] channel_webhook = app.current_request.json_body['channelWebhook'] channel_id = app.current_request.json_body['channelId'] channel = dynamodb_utils.get_channels(user_id, channel_id=channel_id) if (len(channel) == 0): return utils.make_response(400, {"message": "Channel NOT found!"}) filtered_channel = dynamodb_utils.get_channels(user_id, channel_name=channel_name) if (len(filtered_channel) == 1) and dynamo_json.unmarshall( filtered_channel[0])['CHANNEL_ID'] != channel_id: return utils.make_response( 409, { "message": "Channel with the same name already exists! Please give unique channel name to help differentiate channels." }) response_code, res = dynamodb_utils.update_channel(user_id, channel_id, channel_name, channel_webhook) if (response_code == 200): return utils.make_response(response_code, res) else: return utils.make_response(response_code, res)
def get_custom_uri(key, ttl): password = None try: password = app.current_request.json_body['password'] except: password = None random_uri = str(uuid.uuid4()) if password: password_hash = bcrypt.hashpw( password.encode('utf8'), bcrypt.gensalt(12)) else: password_hash = None item = dict() if password_hash: item["PASSWORD_HASH"] = { "B": password_hash } else: item["PASSWORD_HASH"] = { "NULL": True } item.update({ "RANDOM_URI": { "S": random_uri }, "KEY": { "S": key }, "EXPIRES": { "N": ttl } }) response = DYNAMODB.put_item(TableName=TABLE_NAME, Item=item) if(response["ResponseMetadata"]["HTTPStatusCode"] == 200): return utils.make_response(201, { "URL": random_uri }) else: res = S3.delete_object(Bucket=BUCKET_NAME, Key=key) if(response["ResponseMetadata"]["HTTPStatusCode"] == 204): return utils.make_response(500, { "message": "Sorry an error occured. Please try again later.", "object_delete": True }) else: return utils.make_response(500, { "message": "Sorry an error occured. Please try again later.", "object_delete": False })
def getChannels(): user_id = utils.get_user_id(app) channels = dynamodb_utils.get_channels(user_id) response = [] for channel in channels: response.append( utils.dict_underscore_to_camelcase( dynamo_json.unmarshall(channel))) if len(response) > 0: return utils.make_response(200, {"channels": response}) else: return utils.make_response(404, { "message": "No channels found for the you. Please add a channel." })
def get_asset_with_password(custom_id): print("In POST") password = None try: print(parse_qs(app.current_request.raw_body.decode()).get('password')) password = parse_qs( app.current_request.raw_body.decode()).get('password')[0] print(password) except Exception as e: print(e) with open('chalicelib/unauthorized.html', 'r') as f: unauthorized_page = f.read() return utils.make_response(401, unauthorized_page, { 'Content-Type': 'text/html' }) try: response = DYNAMODB.query(TableName=TABLE_NAME, KeyConditionExpression="RANDOM_URI = :id", FilterExpression="EXPIRES >= :current_time", ExpressionAttributeValues={ ":id": { "S": custom_id }, ":current_time": { "N": str(int(time.time())) } }) print(response) except Exception as e: print(e) return utils.make_response(500, { "message": "Something went wrong on our end. Please try again in some time." }) if bcrypt.checkpw(password.encode('utf-8'), json_util.loads(response["Items"][0])["PASSWORD_HASH"].encode('utf-8')): print("Key", response["Items"][0]["KEY"]["S"]) url = utils.download_url(response["Items"][0]["KEY"]["S"]) return utils.make_response(302, {}, { "Location": url }) else: with open('chalicelib/unauthorized.html', 'r') as f: unauthorized_page = f.read() return utils.make_response(401, unauthorized_page, { 'Content-Type': 'text/html' })
def delete_channel(): user_id = utils.get_user_id(app) check_status, check_response = utils.request_params_check( app.current_request.json_body, ('channelId', )) if not check_status: return check_response channel_id = app.current_request.json_body['channelId'] res = dynamodb_utils.delete_channel(user_id, channel_id) if res == 404: return utils.make_response( res, {"message": "The requested channel NOT found."}) elif res == 200: return utils.make_response( res, {"message": "The requested channel has been deleted."}) else: return utils.make_response(500, {"message": "Oops! Some error occured!"})
def get_asset(custom_id): try: response = DYNAMODB.query(TableName=TABLE_NAME, KeyConditionExpression="RANDOM_URI = :id", FilterExpression="EXPIRES >= :current_time", ExpressionAttributeValues={ ":id": { "S": custom_id }, ":current_time": { "N": str(int(time.time())) } }) print(response) except Exception as e: print(e) return utils.make_response(500, { "message": "Something went wrong on our end. Please try again in some time." }) if(response["ResponseMetadata"]["HTTPStatusCode"] == 200 and response["Count"] > 0): print(response["Items"][0]["PASSWORD_HASH"]) print("Unmarshalled ", json_util.loads(response["Items"][0])) if(json_util.loads(response["Items"][0])["PASSWORD_HASH"]): with open('chalicelib/enter-password.html', 'r') as f: password_page = f.read() res = utils.make_response(200, password_page, { 'Content-Type': 'text/html' }) else: url = utils.download_url(response["Items"][0]["KEY"]["S"]) res = utils.make_response(307, {}, { "Location": url }) else: with open('chalicelib/404.html', 'r') as f: error_page = f.read() res = utils.make_response(404, error_page, { 'Content-Type': 'text/html' }) return res
def broadcast_message(): user_id = utils.get_user_id(app) check_status, check_response = utils.request_params_check( app.current_request.json_body, ('channels', 'message')) if not check_status: return check_response channels = app.current_request.json_body['channels'] message = app.current_request.json_body['message'] if len(channels) <= 0: return utils.make_response(400, {"message": "No channels received!"}) all_channels = dynamodb_utils.get_channels(user_id) all_channels = list(map(dynamo_json.unmarshall, all_channels)) broadcast_channels = [ channel for channel in all_channels if channel['CHANNEL_ID'] in channels ] if len(broadcast_channels) < len(channels): return utils.make_response( 400, {"message": "Some of the channels not found."}) slack_responses = broadcaster.broadcast_message(broadcast_channels, message) status = list() for response in slack_responses: status.append(response['success']) if True in status and False in status: return utils.make_response(207, {"slackResponses": slack_responses}) elif False not in status: return utils.make_response(200, {"slackResponses": slack_responses}) elif True not in status: return utils.make_response( 400, { "message": "None of the messages were sent. Please retry or edit channel webhook to valid URI.", "slackResponses": slack_responses })
def new_channel(): user_id = utils.get_user_id(app) check_status, check_response = utils.request_params_check( app.current_request.json_body, ('channelName', 'channelWebhook')) if not check_status: return check_response channel_name = app.current_request.json_body['channelName'] channel_webhook = app.current_request.json_body['channelWebhook'] channel_id = str(uuid.uuid4()) res = dynamodb_utils.get_channels(user_id, channel_name=channel_name) if (len(res) > 0): return utils.make_response( 409, { "message": "Channel with the same name already exists! Please give unique channel name to help differentiate channels." }) status = dynamodb_utils.add_channel(user_id, channel_id, channel_name, channel_webhook) if status: return utils.make_response( 201, { "message": "New slack channel added.", "data": { 'cognitoUsername': user_id, 'channelId': channel_id, 'channelName': channel_name, 'channelWebhook': channel_webhook } }) else: return utils.make_response( 500, { "message": "An error occurred while adding channel. Please try again in some time." })