crystal-gauntlet/src/lib/accounts.cr

46 lines
1.2 KiB
Crystal
Raw Normal View History

2022-12-30 17:04:27 +01:00
require "uri"
2022-12-30 18:34:55 +01:00
require "crypto/bcrypt/password"
2022-12-30 17:04:27 +01:00
include CrystalGauntlet
module CrystalGauntlet::Accounts
extend self
2022-12-31 07:28:06 +01:00
def get_account_id_from_params(params : URI::Params) : Int32 | Nil
if params["accountID"]? && params["accountID"]? != "0"
# todo: validate password
params["accountID"].to_i32
else
nil
end
end
def get_ext_id_from_params(params : URI::Params) : String | Nil
2022-12-30 17:04:27 +01:00
if params.has_key?("udid") && params["udid"] != ""
# todo: numeric id check
params["udid"]
2022-12-31 07:28:06 +01:00
elsif params.has_key?("accountID") && params["accountID"] != "" && params["accountID"] != "0"
2022-12-30 17:04:27 +01:00
# todo: validate password
2022-12-31 07:28:06 +01:00
params["accountID"]
2022-12-30 17:04:27 +01:00
else
2022-12-31 07:28:06 +01:00
nil
2022-12-30 17:04:27 +01:00
end
end
def get_user_id(ext_id : String) : Int32
2022-12-30 17:04:27 +01:00
DATABASE.query("select id from users where udid = ? or account_id = ?", ext_id, ext_id) do |rs|
2022-12-30 18:34:55 +01:00
if rs.move_next
2022-12-30 17:04:27 +01:00
return rs.read(Int32)
else
raise "no user associated with account?!"
end
end
end
2022-12-30 18:34:55 +01:00
2022-12-31 07:28:06 +01:00
def verify_gjp(account_id : Int32, gjp : String) : Bool
2022-12-30 18:34:55 +01:00
hash = DATABASE.scalar("select password from accounts where id = ?", account_id).as(String)
bcrypt = Crypto::Bcrypt::Password.new(hash)
bcrypt.verify(GJP.decrypt(gjp))
end
2022-12-30 17:04:27 +01:00
end