From 7e59d4ffa2d8e7af36ec5cc89b1534ce7f627313 Mon Sep 17 00:00:00 2001 From: heyethereum Date: Wed, 10 Jul 2024 21:50:45 +0800 Subject: [PATCH] First commit --- .gitignore | 1 + .../safeqr-signup-post-confirmation-lambda.py | 69 +++++++++ sql/Create_all_tables.sql | 137 ++++++++++++++++++ sql/Drop_all_tables.sql | 16 ++ sql/Dummy_data.sql | 96 ++++++++++++ 5 files changed, 319 insertions(+) create mode 100644 .gitignore create mode 100644 serverless/safeqr-signup-post-confirmation-lambda.py create mode 100644 sql/Create_all_tables.sql create mode 100644 sql/Drop_all_tables.sql create mode 100644 sql/Dummy_data.sql diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..496ee2c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store \ No newline at end of file diff --git a/serverless/safeqr-signup-post-confirmation-lambda.py b/serverless/safeqr-signup-post-confirmation-lambda.py new file mode 100644 index 0000000..7891167 --- /dev/null +++ b/serverless/safeqr-signup-post-confirmation-lambda.py @@ -0,0 +1,69 @@ +import os +import pg8000 + +def lambda_handler(event, context): + print(f"Event: {event}") + + # Extract user attributes from the Cognito event + user_attributes = event['request']['userAttributes'] + + # Extract specific attributes (modify as needed) + user_id = user_attributes['sub'] + email = user_attributes['email'] + name = user_attributes.get('name', 'Default User') + + # Extract providerName from identities + identities = user_attributes.get('identities', []) + provider_name = identities[0]['providerName'] if identities else 'DefaultApp' + print(f"providerName: {provider_name}") + + # Database connection parameters + db_host = os.environ['RDS_HOST'] + db_name = os.environ['RDS_DB_NAME'] + db_user = os.environ['RDS_USER'] + db_password = os.environ['RDS_PASSWORD'] + db_port = 5432 + + # Connect to the database + try: + conn = pg8000.connect( + host=db_host, + port=db_port, + database=db_name, + user=db_user, + password=db_password + ) + + # Create a cursor + cur = conn.cursor() + print("Connection to db success!") + + # SQL query to insert user details + insert_query = """ + INSERT INTO safeqr."user" (id, email, name, roles, source) + VALUES (%s, %s, %s, %s, %s) + ON CONFLICT (id) DO UPDATE + SET email = EXCLUDED.email, name = EXCLUDED.name, source = EXCLUDED.source; + """ + + # Execute the query + cur.execute(insert_query, (user_id, email, name, ['appuser'], provider_name)) + + # Commit the transaction + conn.commit() + + print(f"User {user_id} inserted/updated successfully") + + except Exception as e: + print(f"Database error: {str(e)}") + raise e + + finally: + # Close the cursor and connection + if 'cur' in locals(): + cur.close() + if 'conn' in locals(): + conn.close() + + # Return the event object back to Cognito + return event \ No newline at end of file diff --git a/sql/Create_all_tables.sql b/sql/Create_all_tables.sql new file mode 100644 index 0000000..89a20d4 --- /dev/null +++ b/sql/Create_all_tables.sql @@ -0,0 +1,137 @@ +-- Create schema if it doesn't exist +CREATE SCHEMA IF NOT EXISTS safeqr; + + +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + + +-- user table, need "" because user is a reserved word is postgres + CREATE TABLE safeqr."user" ( + id character varying(255) PRIMARY KEY, + cognitoid character varying, + firstname character varying(255), + lastname character varying(255), + email character varying(255), + source character varying(255), + password character varying(255), + salt character varying(255), + cognito_id character varying(255), + first_name character varying(255), + last_name character varying(255) + ); + +-- Create QR_Code_Types table +CREATE TABLE safeqr.qr_code_types ( + id SERIAL PRIMARY KEY, + type VARCHAR(100) NOT NULL, + description VARCHAR(100), + prefix VARCHAR(50), + table_name VARCHAR(100) +); + +-- Create QR_Code table +-- Allow for nullable user id for testing KIV +CREATE TABLE safeqr.qr_code ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + qr_code_type_id INT REFERENCES safeqr.qr_code_types(id), + user_id VARCHAR(255) REFERENCES safeqr."user"(id) NULL, + contents VARCHAR(4096), + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +-- Create Scan_History table +CREATE TABLE safeqr.scan_history ( + id SERIAL PRIMARY KEY, + qr_code_id UUID REFERENCES safeqr.qr_code(id), + user_id VARCHAR(255) REFERENCES safeqr."user"(id) +); + +-- Create Scan_Bookmark table +CREATE TABLE safeqr.scan_bookmark ( + id SERIAL PRIMARY KEY, + qr_code_id UUID REFERENCES safeqr.qr_code(id), + user_id VARCHAR(255) REFERENCES safeqr."user"(id), + active VARCHAR(7) CHECK (active IN ('ACTIVE', 'DELETED')) +); + +-- Create URL table +CREATE TABLE safeqr.url ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + qr_code_id UUID REFERENCES safeqr.qr_code(id), + url VARCHAR(2048), + query VARCHAR(2048), + fragment VARCHAR(2048) +); + +-- Create Text table +CREATE TABLE safeqr.text ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + qr_code_id UUID REFERENCES safeqr.qr_code(id), + text VARCHAR(2048) +); + +-- Create Phone table +CREATE TABLE safeqr.phone ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + qr_code_id UUID REFERENCES safeqr.qr_code(id), + phone VARCHAR(100) +); + +-- Create SMS table +CREATE TABLE safeqr.sms ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + qr_code_id UUID REFERENCES safeqr.qr_code(id), + phone VARCHAR(100), + message VARCHAR(160) +); + +-- Create Email table +CREATE TABLE safeqr.email ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + qr_code_id UUID REFERENCES safeqr.qr_code(id), + email VARCHAR(200), + title VARCHAR(200), + message VARCHAR(7089) +); + +-- Create GeoLocation table +CREATE TABLE safeqr.geolocation ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + qr_code_id UUID REFERENCES safeqr.qr_code(id), + latitude DOUBLE PRECISION, + longitude DOUBLE PRECISION +); + +-- Create Wifi table +CREATE TABLE safeqr.wifi ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + qr_code_id UUID REFERENCES safeqr.qr_code(id), + ssid VARCHAR(200), + password VARCHAR(200), + encryption VARCHAR(200), + hidden BOOLEAN +); + +-- Create Bitcoin table +CREATE TABLE safeqr.bitcoin ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + qr_code_id UUID REFERENCES safeqr.qr_code(id), + address VARCHAR(200), + amount BIGINT, + message VARCHAR(250) +); + +-- Create VCard table +CREATE TABLE safeqr.vcard ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + qr_code_id UUID REFERENCES safeqr.qr_code(id), + version VARCHAR(50), + name VARCHAR(250), + organisation VARCHAR(250), + title VARCHAR(50), + address VARCHAR(250), + tel_work VARCHAR(250), + tel_cell VARCHAR(250), + email VARCHAR(250), + url VARCHAR(2048) +); \ No newline at end of file diff --git a/sql/Drop_all_tables.sql b/sql/Drop_all_tables.sql new file mode 100644 index 0000000..697c592 --- /dev/null +++ b/sql/Drop_all_tables.sql @@ -0,0 +1,16 @@ +-- Drop the tables with CASCADE to handle dependencies + +DROP TABLE IF EXISTS safeqr.vcard CASCADE; +DROP TABLE IF EXISTS safeqr.bitcoin CASCADE; +DROP TABLE IF EXISTS safeqr.wifi CASCADE; +DROP TABLE IF EXISTS safeqr.geolocation CASCADE; +DROP TABLE IF EXISTS safeqr.email CASCADE; +DROP TABLE IF EXISTS safeqr.sms CASCADE; +DROP TABLE IF EXISTS safeqr.phone CASCADE; +DROP TABLE IF EXISTS safeqr.text CASCADE; +DROP TABLE IF EXISTS safeqr.url CASCADE; +DROP TABLE IF EXISTS safeqr.scan_bookmark CASCADE; +DROP TABLE IF EXISTS safeqr.scan_history CASCADE; +DROP TABLE IF EXISTS safeqr.qr_code CASCADE; +DROP TABLE IF EXISTS safeqr.qr_code_types CASCADE; +DROP TABLE IF EXISTS safeqr."user" CASCADE; \ No newline at end of file diff --git a/sql/Dummy_data.sql b/sql/Dummy_data.sql new file mode 100644 index 0000000..523501f --- /dev/null +++ b/sql/Dummy_data.sql @@ -0,0 +1,96 @@ + +INSERT INTO safeqr."user" ( + id, + cognitoid, + firstname, + lastname, + email, + source, + password, + salt, + cognito_id, + first_name, + last_name +) VALUES ( + 'test-unique-id', + 'test-cognito-id', + 'Test', + 'User', + 'test.user@example.com', + 'test-source', + 'test-password-hash', + 'test-salt', + NULL, + NULL, + NULL +); + + +-- Insert into QR_Code_Types table +-- Insert additional QR code types into the qr_code_types table +INSERT INTO safeqr.qr_code_types (type, description, prefix, table_name) VALUES +('URL', 'Uniform Resource Locator', 'http://', 'url'), +('URL', 'Uniform Resource Locator', 'https://', 'url'), +('EMAIL', 'Email Address', 'mailto:', 'email'), +('PHONE', 'Phone Number', 'tel:', 'phone'), +('SMS', 'Short Message Service', 'smsto:', 'sms'), +('GEOLOCATION', 'Geographic Location', 'geo:', 'geolocation'), +('WIFI', 'Wireless Network Configuration', 'WIFI:', 'wifi'), +('BITCOIN', 'Cryptocurrency Address', 'bitcoin:', 'bitcoin'), +('VCARD', 'Virtual Contact File', 'BEGIN:VCARD', 'vcard'); + + +-- Insert into QR_Code table +INSERT INTO safeqr.qr_code (id, qr_code_type_id, user_id, contents, created_at) VALUES +(gen_random_uuid(), 1, 'test-unique-id', 'http://example.com', CURRENT_TIMESTAMP), +(gen_random_uuid(), 2, NULL, 'https://example.com', CURRENT_TIMESTAMP), +(gen_random_uuid(), 3, 'test-unique-id', 'mailto:test@example.com', CURRENT_TIMESTAMP); + +-- Insert into Scan_History table +INSERT INTO safeqr.scan_history (qr_code_id, user_id) VALUES +((SELECT id FROM safeqr.qr_code LIMIT 1), 'test-unique-id'), +((SELECT id FROM safeqr.qr_code LIMIT 1 OFFSET 1), 'test-unique-id'), +((SELECT id FROM safeqr.qr_code LIMIT 1 OFFSET 2), 'test-unique-id'); + +-- Insert into Scan_Bookmark table +INSERT INTO safeqr.scan_bookmark (qr_code_id, user_id, active) VALUES +((SELECT id FROM safeqr.qr_code LIMIT 1), 'test-unique-id', 'ACTIVE'), +((SELECT id FROM safeqr.qr_code LIMIT 1 OFFSET 1), 'test-unique-id', 'ACTIVE'), +((SELECT id FROM safeqr.qr_code LIMIT 1 OFFSET 2), 'test-unique-id', 'DELETED'); + +-- Insert into URL table +INSERT INTO safeqr.url (id, qr_code_id, url, query, fragment) VALUES +(gen_random_uuid(), (SELECT id FROM safeqr.qr_code WHERE contents LIKE 'http://%' LIMIT 1), 'http://example.com', 'key=value', 'fragment'), +(gen_random_uuid(), (SELECT id FROM safeqr.qr_code WHERE contents LIKE 'https://%' LIMIT 1), 'https://example.com', 'key=value', 'fragment'); + +-- Insert into Text table +INSERT INTO safeqr.text (id, qr_code_id, text) VALUES +(gen_random_uuid(), (SELECT id FROM safeqr.qr_code WHERE contents = 'Some text' LIMIT 1), 'Some example text'); + +-- Insert into Phone table +INSERT INTO safeqr.phone (id, qr_code_id, phone) VALUES +(gen_random_uuid(), (SELECT id FROM safeqr.qr_code LIMIT 1), '+1234567890'); + +-- Insert into SMS table +INSERT INTO safeqr.sms (id, qr_code_id, phone, message) VALUES +(gen_random_uuid(), (SELECT id FROM safeqr.qr_code LIMIT 1), '+1234567890', 'This is a test SMS message.'); + +-- Insert into Email table +INSERT INTO safeqr.email (id, qr_code_id, email, title, message) VALUES +(gen_random_uuid(), (SELECT id FROM safeqr.qr_code WHERE contents LIKE 'mailto:%' LIMIT 1), 'test@example.com', 'Test Email', 'This is a test email body.'); + +-- Insert into GeoLocation table +INSERT INTO safeqr.geolocation (id, qr_code_id, latitude, longitude) VALUES +(gen_random_uuid(), (SELECT id FROM safeqr.qr_code LIMIT 1), 37.7749, -122.4194); + +-- Insert into Wifi table +INSERT INTO safeqr.wifi (id, qr_code_id, ssid, password, encryption, hidden) VALUES +(gen_random_uuid(), (SELECT id FROM safeqr.qr_code LIMIT 1), 'TestSSID', 'password123', 'WPA2', FALSE); + +-- Insert into Bitcoin table +INSERT INTO safeqr.bitcoin (id, qr_code_id, address, amount, message) VALUES +(gen_random_uuid(), (SELECT id FROM safeqr.qr_code LIMIT 1), '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', 5000000000, 'Test Bitcoin transaction'); + +-- Insert into VCard table +INSERT INTO safeqr.vcard (id, qr_code_id, version, name, organisation, title, address, tel_work, tel_cell, email, url) VALUES +(gen_random_uuid(), (SELECT id FROM safeqr.qr_code LIMIT 1), '4.0', 'John Doe', 'Example Corp', 'Developer', '1234 Elm St, Springfield, IL', '+1234567890', '+0987654321', 'johndoe@example.com', 'http://example.com'); \ No newline at end of file