2018-09-22 19:08:28 +02:00
|
|
|
require "uuid"
|
|
|
|
|
|
|
|
require "kemal"
|
|
|
|
require "jwt"
|
|
|
|
|
|
|
|
require "pg"
|
|
|
|
require "crecto"
|
|
|
|
|
2018-09-22 21:23:50 +02:00
|
|
|
require "./user.cr"
|
|
|
|
|
2018-09-22 19:08:28 +02:00
|
|
|
authd_db_name = "authd"
|
|
|
|
authd_db_hostname = "localhost"
|
|
|
|
authd_db_user = "user"
|
2018-09-22 19:46:48 +02:00
|
|
|
authd_db_password = "nico-nico-nii"
|
|
|
|
authd_jwt_key = "nico-nico-nii"
|
2018-09-22 19:08:28 +02:00
|
|
|
|
|
|
|
Kemal.config.extra_options do |parser|
|
2018-09-22 19:46:48 +02:00
|
|
|
parser.on "-d name", "--database-name name", "Database name." do |name|
|
|
|
|
authd_db_name = name
|
2018-09-22 19:08:28 +02:00
|
|
|
end
|
|
|
|
|
2018-09-22 19:46:48 +02:00
|
|
|
parser.on "-u name", "--database-username user", "Database user." do |name|
|
|
|
|
authd_db_user = name
|
2018-09-22 19:08:28 +02:00
|
|
|
end
|
|
|
|
|
2018-09-22 19:46:48 +02:00
|
|
|
parser.on "-a host", "--hostname host", "Database host name." do |host|
|
|
|
|
authd_db_hostname = host
|
2018-09-22 19:08:28 +02:00
|
|
|
end
|
|
|
|
|
2018-09-22 19:46:48 +02:00
|
|
|
parser.on "-P file", "--password-file file", "Password file." do |file_name|
|
2018-09-22 23:10:14 +02:00
|
|
|
authd_db_password = File.read(file_name).gsub /\n$/, ""
|
2018-09-22 19:46:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
parser.on "-K file", "--key-file file", "JWT key file" do |file_name|
|
2018-09-23 16:34:56 +02:00
|
|
|
authd_jwt_key = File.read(file_name).gsub /\n$/, ""
|
2018-09-22 19:08:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
post "/token" do |env|
|
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
|
|
|
username = env.params.json["username"]?
|
|
|
|
password = env.params.json["password"]?
|
|
|
|
|
|
|
|
if ! username.is_a? String
|
|
|
|
next halt env, status_code: 400, response: ({error: "Missing username."}.to_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
if ! password.is_a? String
|
|
|
|
next halt env, status_code: 400, response: ({error: "Missing password."}.to_json)
|
|
|
|
end
|
|
|
|
|
2018-09-23 16:17:48 +02:00
|
|
|
user = DataBase.get_by AuthD::User, username: username, password: password
|
2018-09-22 19:08:28 +02:00
|
|
|
|
|
|
|
if ! user
|
|
|
|
next halt env, status_code: 400, response: ({error: "Invalid user or password."}.to_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
"status" => "success",
|
2018-09-22 21:23:50 +02:00
|
|
|
"token" => JWT.encode user.to_h, authd_jwt_key, "HS256"
|
2018-09-22 19:08:28 +02:00
|
|
|
}.to_json
|
|
|
|
end
|
|
|
|
|
2018-09-23 16:17:48 +02:00
|
|
|
module DataBase
|
2018-09-22 19:08:28 +02:00
|
|
|
extend Crecto::Repo
|
|
|
|
end
|
|
|
|
|
2018-09-23 16:02:35 +02:00
|
|
|
Kemal.run do
|
2018-09-23 16:17:48 +02:00
|
|
|
DataBase.config do |conf|
|
2018-09-22 19:08:28 +02:00
|
|
|
conf.adapter = Crecto::Adapters::Postgres
|
|
|
|
conf.hostname = authd_db_hostname
|
|
|
|
conf.database = authd_db_name
|
|
|
|
conf.username = authd_db_user
|
2018-09-22 19:46:48 +02:00
|
|
|
conf.password = authd_db_password
|
2018-09-22 19:08:28 +02:00
|
|
|
end
|
2018-09-23 16:17:48 +02:00
|
|
|
|
|
|
|
# Dummy query to check DB connection is possible.
|
|
|
|
begin
|
|
|
|
DataBase.all AuthD::User, Crecto::Repo::Query.new
|
|
|
|
rescue e
|
|
|
|
puts "Database connection failed: #{e.message}"
|
|
|
|
|
|
|
|
Kemal.stop
|
|
|
|
end
|
2018-09-22 19:08:28 +02:00
|
|
|
end
|