From f66f7bd9d9bc540caa259cb9351033b11c75566a Mon Sep 17 00:00:00 2001
From: josephroy99 <joseph.roy@roysathome.net>
Date: Mon, 11 Dec 2023 00:31:30 +0000
Subject: [PATCH] added ruby files

---
 .gitignore        |  3 ++
 auth/Gemfile      | 10 ++++++
 auth/Gemfile.lock | 33 +++++++++++++++++++
 auth/README.md    |  7 +++++
 auth/auth.rb      | 80 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 133 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 auth/Gemfile
 create mode 100644 auth/Gemfile.lock
 create mode 100644 auth/README.md
 create mode 100644 auth/auth.rb

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..85ffe74
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+bin/
+
+*.db
diff --git a/auth/Gemfile b/auth/Gemfile
new file mode 100644
index 0000000..ae64a56
--- /dev/null
+++ b/auth/Gemfile
@@ -0,0 +1,10 @@
+# Gemfile
+
+source "https://rubygems.org"
+
+ruby '3.2.2'
+
+gem 'sinatra'
+gem 'webrick'
+gem 'jwt'
+gem 'sqlite3'
\ No newline at end of file
diff --git a/auth/Gemfile.lock b/auth/Gemfile.lock
new file mode 100644
index 0000000..034e13d
--- /dev/null
+++ b/auth/Gemfile.lock
@@ -0,0 +1,33 @@
+GEM
+  remote: https://rubygems.org/
+  specs:
+    jwt (2.7.1)
+    mustermann (3.0.0)
+      ruby2_keywords (~> 0.0.1)
+    rack (2.2.8)
+    rack-protection (3.1.0)
+      rack (~> 2.2, >= 2.2.4)
+    ruby2_keywords (0.0.5)
+    sinatra (3.1.0)
+      mustermann (~> 3.0)
+      rack (~> 2.2, >= 2.2.4)
+      rack-protection (= 3.1.0)
+      tilt (~> 2.0)
+    sqlite3 (1.6.9-x64-mingw-ucrt)
+    tilt (2.3.0)
+    webrick (1.8.1)
+
+PLATFORMS
+  x64-mingw-ucrt
+
+DEPENDENCIES
+  jwt
+  sinatra
+  sqlite3
+  webrick
+
+RUBY VERSION
+   ruby 3.2.2p53
+
+BUNDLED WITH
+   2.4.22
diff --git a/auth/README.md b/auth/README.md
new file mode 100644
index 0000000..69c483c
--- /dev/null
+++ b/auth/README.md
@@ -0,0 +1,7 @@
+Install dependancies
+
+gem install sinatra webrick
+ruby ./auth -p 4567
+
+To use the Gemfile:
+bundle installd
\ No newline at end of file
diff --git a/auth/auth.rb b/auth/auth.rb
new file mode 100644
index 0000000..4ff9c7f
--- /dev/null
+++ b/auth/auth.rb
@@ -0,0 +1,80 @@
+# Install Sinatra: gem install sinatra
+
+require 'sinatra'
+require 'json'
+require 'sqlite3'
+require 'jwt'
+
+payload = { data: 'test', test: 'hello' }
+hmac_secret = 'my$ecretK3y'
+token = JWT.encode payload, hmac_secret, 'HS256'
+puts token
+
+decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
+puts decoded_token
+
+#########
+
+# Check if the directory exists
+directory_path = './database'
+
+unless File.directory?(directory_path)
+  # If not, create the directory
+  Dir.mkdir(directory_path)
+  puts "Directory '#{directory_path}' created successfully."
+end
+
+db = SQLite3::Database.new('./database/auth.db')
+
+db.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT, hashed_password TEXT)')
+db.execute('CREATE TABLE IF NOT EXISTS claims (id INTEGER PRIMARY KEY, claim TEXT)')
+db.execute('CREATE TABLE IF NOT EXISTS user_claims (id INTEGER PRIMARY KEY, user_id INTEGER, claim_id INTEGER)')
+
+db.execute('INSERT INTO claims (claim) VALUES (?)', 'add_user')
+db.execute('INSERT INTO claims (claim) VALUES (?)', 'remove_user')
+
+# Check if the users table is empty
+result = db.get_first_value('SELECT COUNT(*) FROM users')
+
+if result == 0
+    db.execute('INSERT INTO users (username, hashed_password) VALUES (?, ?)', ['admin', 'password'])
+    puts 'Default admin user added.'
+else
+    puts 'Table already contains data. Skipping default user creation.'
+end
+
+results = db.execute('SELECT * FROM users')
+results.each do |row|
+  puts "ID: #{row[0]}, Username: #{row[1]}, Hashed Password: #{row[2]}"
+end
+
+##############
+
+
+# Define a simple API endpoint
+get '/api/greeting' do
+  content_type :json
+  { greeting: 'Hello, World!' }.to_json
+end
+
+get '/api/greeting/:name' do
+  content_type :json
+  { greeting: "Hello, #{params[:name]}!" }.to_json
+end
+
+post '/auth' do
+  content_type :json
+  { jwt:  }
+end
+
+def get_jwt (username, expiry_time)
+  claims = []
+end
+
+def get_claims (username)
+  db = SQLite3::Database.new('./database/auth.db')
+  db.execute('')
+end
+
+# Run the application
+# ruby your_file_name.rb