authd/src/main.cr

151 lines
3.2 KiB
Crystal
Raw Normal View History

2018-09-22 19:08:28 +02:00
require "uuid"
require "option_parser"
2019-02-19 20:45:19 +01:00
require "openssl"
2018-09-22 19:08:28 +02:00
require "jwt"
require "ipc"
require "./authd.cr"
require "./passwd.cr"
extend AuthD
2019-06-05 22:30:29 +02:00
class IPC::Connection
def send(type : AuthD::ResponseTypes, payload : String)
send type.to_u8, payload
2018-12-19 13:54:19 +01:00
end
end
authd_passwd_file = "passwd"
authd_group_file = "group"
2018-09-22 19:46:48 +02:00
authd_jwt_key = "nico-nico-nii"
2018-09-22 19:08:28 +02:00
OptionParser.parse! do |parser|
parser.on "-u file", "--passwd-file file", "passwd file." do |name|
authd_passwd_file = name
2018-09-22 19:08:28 +02:00
end
parser.on "-g file", "--group-file file", "group file." do |name|
authd_group_file = name
2018-09-22 19:46:48 +02:00
end
parser.on "-K file", "--key-file file", "JWT key file" do |file_name|
authd_jwt_key = File.read(file_name).chomp
2018-09-22 19:08:28 +02:00
end
parser.on "-h", "--help", "Show this help" do
puts parser
2018-09-22 19:08:28 +02:00
exit 0
2018-09-22 19:08:28 +02:00
end
end
passwd = Passwd.new authd_passwd_file, authd_group_file
##
# Provides a JWT-based authentication scheme for service-specific users.
IPC::Service.new "auth" do |event|
2019-06-05 22:30:29 +02:00
if event.is_a? IPC::Exception
puts "oh no"
pp! event
next
end
client = event.connection
case event
when IPC::Event::Message
message = event.message
payload = message.payload
2019-06-05 22:35:35 +02:00
case RequestTypes.new message.type.to_i
2018-12-19 13:57:48 +01:00
when RequestTypes::GetToken
begin
2019-06-05 22:35:35 +02:00
request = GetTokenRequest.from_json String.new payload
rescue e
2018-12-19 13:57:48 +01:00
client.send ResponseTypes::MalformedRequest.value.to_u8, e.message || ""
next
end
user = passwd.get_user request.login, request.password
if user.nil?
2018-12-19 13:57:48 +01:00
client.send ResponseTypes::InvalidCredentials.value.to_u8, ""
next
end
2018-12-19 13:57:48 +01:00
client.send ResponseTypes::Ok.value.to_u8,
2019-06-28 18:20:34 +02:00
JWT.encode user.to_h, authd_jwt_key, JWT::Algorithm::HS256
2018-12-19 13:57:48 +01:00
when RequestTypes::AddUser
2018-12-19 13:54:19 +01:00
begin
2019-06-05 22:35:35 +02:00
request = AddUserRequest.from_json String.new payload
2018-12-19 13:54:19 +01:00
rescue e
2018-12-19 13:57:48 +01:00
client.send ResponseTypes::MalformedRequest.value.to_u8, e.message || ""
2018-12-19 13:54:19 +01:00
next
end
if passwd.user_exists? request.login
2018-12-19 13:57:48 +01:00
client.send ResponseTypes::InvalidUser, "Another user with the same login already exists."
2018-12-19 13:54:19 +01:00
next
end
user = passwd.add_user request.login, request.password
2018-12-19 13:57:48 +01:00
client.send ResponseTypes::Ok, user.to_json
when RequestTypes::GetUserByCredentials
begin
2019-06-05 22:35:35 +02:00
request = GetUserByCredentialsRequest.from_json String.new payload
rescue e
client.send ResponseTypes::MalformedRequest, e.message || ""
next
end
user = passwd.get_user request.login, request.password
if user
client.send ResponseTypes::Ok, user.to_json
else
client.send ResponseTypes::UserNotFound, ""
end
2019-01-07 17:04:20 +01:00
when RequestTypes::GetUser
begin
2019-06-05 22:35:35 +02:00
request = GetUserRequest.from_json String.new payload
2019-01-07 17:04:20 +01:00
rescue e
client.send ResponseTypes::MalformedRequest, e.message || ""
next
end
user = passwd.get_user request.uid
if user
client.send ResponseTypes::Ok, user.to_json
else
client.send ResponseTypes::UserNotFound, ""
end
2019-05-29 16:06:11 +02:00
when RequestTypes::ModUser
begin
2019-06-05 22:35:35 +02:00
request = ModUserRequest.from_json String.new payload
2019-05-29 16:06:11 +02:00
rescue e
client.send ResponseTypes::MalformedRequest, e.message || ""
next
end
password_hash = request.password.try do |s|
Passwd.hash_password s
end
2019-05-29 19:45:03 +02:00
avatar = request.avatar
passwd.mod_user request.uid, password_hash: password_hash, avatar: avatar
2019-05-29 16:06:11 +02:00
client.send ResponseTypes::Ok, ""
end
end
2018-09-22 19:08:28 +02:00
end