add jwt test, and added downcase for all username database queries.

Signed-off-by: Joseph.Roy <Joseph.Roy@PSV-JOSEPHR-LAP>
This commit is contained in:
Joseph.Roy 2023-12-13 10:02:55 +00:00
parent d91d31ca3e
commit 35440e6b43
2 changed files with 15 additions and 5 deletions

View File

@ -9,12 +9,22 @@ require 'json'
require 'sqlite3'
require 'jwt'
payload = { data: 'test', test: 'hello' }
payload = { data: {time: 'now', help: 'no'}.to_json, 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' }
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
#puts decoded_token
initialize_database

View File

@ -7,13 +7,13 @@ def create_new_user(username, password)
db.execute('
INSERT INTO users (username, hashed_password)
VALUES (?, ?)
', [username, create_password_for_user(password)])
', [username.downcase, create_password_for_user(password)])
end
def check_if_user_exists(username)
db = SQLite3::Database.new('./database/auth.db')
result = db.get_first_value('SELECT COUNT(*) FROM users WHERE username = ?', username)
result = db.get_first_value('SELECT COUNT(*) FROM users WHERE username = ?', username.downcase)
if result > 0
return true
@ -30,7 +30,7 @@ def get_user_id(username)
FROM users U
WHERE U.username = ?
', [username])
', [username.downcase])
return user_id
end