roysathome.net/auth/auth.rb

154 lines
4.3 KiB
Ruby
Raw Normal View History

2023-12-11 00:31:30 +00:00
# Install Sinatra: gem install sinatra
2023-12-11 20:56:09 +00:00
require_relative "initialize_database"
require_relative "database_queries"
require_relative "cryptography"
2023-12-11 00:31:30 +00:00
require 'sinatra'
require 'json'
require 'sqlite3'
require 'jwt'
2023-12-13 16:02:09 +00:00
#puts generate_random_string(256)
#
#token = JWT.encode payload, hmac_secret, 'HS256'
##puts token
#
#begin
# decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
# puts "Token is valid!"
# puts "Decoded token: #{decoded_token}"
#
# puts data_value = decoded_token.first['data']
# puts test_value = decoded_token.first['test']
#rescue JWT::DecodeError
# puts "Invalid token or signature!"
#end
2023-12-11 20:56:09 +00:00
#puts decoded_token
2023-12-11 00:31:30 +00:00
2023-12-11 20:56:09 +00:00
initialize_database
2023-12-11 00:31:30 +00:00
2023-12-11 20:56:09 +00:00
post '/auth/login' do
2023-12-12 22:52:36 +00:00
content_type :json
2023-12-11 20:56:09 +00:00
username = params[:username]
password = params[:password]
unless check_if_user_exists username
status 401
2023-12-13 16:02:09 +00:00
return { reply: "Unuthorized Access" }.to_json
end
2023-12-11 20:56:09 +00:00
user_id = get_user_id username
2023-12-12 22:52:36 +00:00
unless check_password_for_user(user_id, password)
2023-12-11 20:56:09 +00:00
status 401
2023-12-12 22:52:36 +00:00
2023-12-13 16:02:09 +00:00
return { reply: "Unuthorized Access" }.to_json
2023-12-11 20:56:09 +00:00
end
2023-12-13 16:02:09 +00:00
reauthJWT = get_reauth_jwt user_id
2023-12-14 00:18:12 +00:00
return {token: reauthJWT}.to_json
end
post '/auth/reauthenticate' do
content_type :json
authorization_header = request.env['HTTP_AUTHORIZATION']
unless authorization_header && authorization_header.match(/^Bearer (.+)/)
status 401
return { reply: 'Unauthorized Access. Token missing or invalid.' }.to_json
end
reauth_token = Regexp.last_match(1)
hmac_secret = 'WChX-tQWbGbj_pGJQREoFAZGC9JWh58KSk8O7KPj-P8Nd-J88g3eSFDVuNe6zddj0ZB3yxjm_IuPNPyLhiSnxlWHImqXR6ajh3OzrzYm0bNb3f5C4IAScphyEdAfYGMcM-HvYOXxxxp5u5mryfiV3JH1CTqL1CzGyO8df7zUpRKXEXZ5SKmUvhfLU0XKCR_28FAZUgPCAi3GywkDDsH0by68j33BU5cnMT8KiEkHOX4wVUVDQc85_AuE7fN3ji_WkhnDCSLXU9dBCcXM3ziFFeX0RbvIRDG0vKdzwt4TOr4Jws7NP9w11GrUGDFKARZqvT7FTxwxO3MM-mmjb2xyGg'
begin
# Verify the token using the secret key
decoded_token = JWT.decode(reauth_token, hmac_secret, true, algorithm: 'HS256')
# At this point, the token is valid
# You can access the claims inside the 'decoded_token' variable
uid = decoded_token.first['uid']
# Your reauthentication logic here...
# Return a response (replace with your own logic)
{ reply: get_jwt(uid) }.to_json
rescue JWT::DecodeError
status 401
return { reply: 'Unauthorized Access. Invalid token.' }.to_json
end
2023-12-11 00:31:30 +00:00
end
2023-12-13 16:02:09 +00:00
def get_reauth_jwt (user_id)
#claims = get_claims user_id
payload = {
sub: 'reauthentication' ,
admin: check_if_user_is_admin(user_id),
iss: 'roysathome.net',
uid: user_id, #Example id
iat: Time.now.to_i,
exp: Time.now.to_i + 3600
}
hmac_secret = 'WChX-tQWbGbj_pGJQREoFAZGC9JWh58KSk8O7KPj-P8Nd-J88g3eSFDVuNe6zddj0ZB3yxjm_IuPNPyLhiSnxlWHImqXR6ajh3OzrzYm0bNb3f5C4IAScphyEdAfYGMcM-HvYOXxxxp5u5mryfiV3JH1CTqL1CzGyO8df7zUpRKXEXZ5SKmUvhfLU0XKCR_28FAZUgPCAi3GywkDDsH0by68j33BU5cnMT8KiEkHOX4wVUVDQc85_AuE7fN3ji_WkhnDCSLXU9dBCcXM3ziFFeX0RbvIRDG0vKdzwt4TOr4Jws7NP9w11GrUGDFKARZqvT7FTxwxO3MM-mmjb2xyGg'
return JWT.encode payload, hmac_secret, 'HS256'
#data: {time: 'now', help: 'no'}.to_json
2023-12-11 00:31:30 +00:00
end
2023-12-14 00:18:12 +00:00
def get_jwt (user_id)
claims = get_claims user_id
payload = {
sub: 'authentication' ,
admin: check_if_user_is_admin(user_id),
iss: 'roysathome.net',
uid: user_id, #Example id
iat: Time.now.to_i,
exp: Time.now.to_i + 60,
claims: claims
}
hmac_secret = 'WChX-tQWbGbj_pGJQREoFAZGC9JWh58KSk8O7KPj-P8Nd-J88g3eSFDVuNe6zddj0ZB3yxjm_IuPNPyLhiSnxlWHImqXR6ajh3OzrzYm0bNb3f5C4IAScphyEdAfYGMcM-HvYOXxxxp5u5mryfiV3JH1CTqL1CzGyO8df7zUpRKXEXZ5SKmUvhfLU0XKCR_28FAZUgPCAi3GywkDDsH0by68j33BU5cnMT8KiEkHOX4wVUVDQc85_AuE7fN3ji_WkhnDCSLXU9dBCcXM3ziFFeX0RbvIRDG0vKdzwt4TOr4Jws7NP9w11GrUGDFKARZqvT7FTxwxO3MM-mmjb2xyGg'
return JWT.encode payload, hmac_secret, 'HS256'
end
2023-12-13 16:02:09 +00:00
def get_claims (user_id)
puts "Getting claims for #{user_id}"
2023-12-11 00:31:30 +00:00
db = SQLite3::Database.new('./database/auth.db')
2023-12-11 20:56:09 +00:00
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
2023-12-13 16:02:09 +00:00
WHERE u.id = ?
', user_id)
2023-12-11 20:56:09 +00:00
2023-12-14 00:18:12 +00:00
claims_list = []
2023-12-11 20:56:09 +00:00
if results.empty?
puts 'No claims found.'
else
results.each do |column|
2023-12-14 00:18:12 +00:00
claim = column[0]
claims_list << claim
2023-12-11 20:56:09 +00:00
end
end
2023-12-14 00:18:12 +00:00
return claims_list
2023-12-11 00:31:30 +00:00
end
# Run the application
# ruby your_file_name.rb