2018-09-22 19:08:28 +02:00
|
|
|
require "uuid"
|
2018-11-12 18:51:21 +01:00
|
|
|
require "option_parser"
|
2019-02-19 20:45:19 +01:00
|
|
|
require "openssl"
|
2018-09-22 19:08:28 +02:00
|
|
|
|
|
|
|
require "jwt"
|
|
|
|
|
2018-11-12 18:51:21 +01:00
|
|
|
require "ipc"
|
|
|
|
|
|
|
|
require "./authd.cr"
|
2018-12-17 00:56:03 +01:00
|
|
|
require "./passwd.cr"
|
2018-11-12 18:51:21 +01:00
|
|
|
|
|
|
|
extend AuthD
|
2018-09-22 21:23:50 +02:00
|
|
|
|
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
|
|
|
|
|
2018-12-17 00:56:03 +01:00
|
|
|
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
|
|
|
|
2018-11-12 18:51:21 +01:00
|
|
|
OptionParser.parse! do |parser|
|
2018-12-17 00:56:03 +01:00
|
|
|
parser.on "-u file", "--passwd-file file", "passwd file." do |name|
|
|
|
|
authd_passwd_file = name
|
2018-09-22 19:08:28 +02:00
|
|
|
end
|
|
|
|
|
2018-12-17 00:56:03 +01:00
|
|
|
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|
|
2018-09-24 22:10:24 +02:00
|
|
|
authd_jwt_key = File.read(file_name).chomp
|
2018-09-22 19:08:28 +02:00
|
|
|
end
|
|
|
|
|
2018-11-12 18:51:21 +01:00
|
|
|
parser.on "-h", "--help", "Show this help" do
|
|
|
|
puts parser
|
2018-09-22 19:08:28 +02:00
|
|
|
|
2018-11-12 18:51:21 +01:00
|
|
|
exit 0
|
2018-09-22 19:08:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-17 00:56:03 +01:00
|
|
|
passwd = Passwd.new authd_passwd_file, authd_group_file
|
2018-11-12 18:51:21 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
# 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
|
2018-11-12 18:51:21 +01:00
|
|
|
|
|
|
|
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
|
2018-11-12 18:51:21 +01:00
|
|
|
begin
|
2019-06-05 22:35:35 +02:00
|
|
|
request = GetTokenRequest.from_json String.new payload
|
2018-11-12 18:51:21 +01:00
|
|
|
rescue e
|
2018-12-19 13:57:48 +01:00
|
|
|
client.send ResponseTypes::MalformedRequest.value.to_u8, e.message || ""
|
2018-11-12 18:51:21 +01:00
|
|
|
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2018-12-17 04:39:01 +01:00
|
|
|
user = passwd.get_user request.login, request.password
|
2018-11-12 18:51:21 +01:00
|
|
|
|
|
|
|
if user.nil?
|
2018-12-19 13:57:48 +01:00
|
|
|
client.send ResponseTypes::InvalidCredentials.value.to_u8, ""
|
2018-11-12 18:51:21 +01:00
|
|
|
|
|
|
|
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
|
2019-02-16 22:06:56 +01:00
|
|
|
when RequestTypes::GetUserByCredentials
|
|
|
|
begin
|
2019-06-05 22:35:35 +02:00
|
|
|
request = GetUserByCredentialsRequest.from_json String.new payload
|
2019-02-16 22:06:56 +01:00
|
|
|
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, ""
|
2018-11-12 18:51:21 +01:00
|
|
|
end
|
2018-09-23 16:17:48 +02:00
|
|
|
end
|
2018-09-22 19:08:28 +02:00
|
|
|
end
|
2018-11-12 18:51:21 +01:00
|
|
|
|