roysathome.net/auth/auth.rb

68 lines
1.3 KiB
Ruby

# Install Sinatra: gem install sinatra
require_relative "initialize_database"
require_relative "database_queries"
require_relative "cryptography"
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
initialize_database
post '/auth/login' do
content_type :json
username = params[:username]
password = params[:password]
user_id = get_user_id username
unless check_password_for_user(user_id, password)
status 401
return { jwt: "Unuthorized Access" }.to_json
end
{ jwt: "Logged in" }.to_json
end
def get_jwt (username, expiry_time)
get_claims username
end
def get_claims (username)
puts "Getting claims for #{username}"
db = SQLite3::Database.new('./database/auth.db')
results = db.execute('
SELECT C.claim
FROM users U
INNER JOIN user_claims UC ON U.id = UC.user_id
INNER JOIN claims C ON UC.claim_id = U.id
WHERE u.username = ?
', username)
if results.empty?
puts 'No claims found.'
else
results.each do |column|
puts "#{column[0]}"
end
end
return results
end
# Run the application
# ruby your_file_name.rb