commit
1a83d49eca
|
@ -0,0 +1,53 @@
|
||||||
|
require "spec"
|
||||||
|
|
||||||
|
require "../src/authd.cr"
|
||||||
|
|
||||||
|
describe "authd" do
|
||||||
|
it "runs basic functions" do
|
||||||
|
# Database setup.
|
||||||
|
File.write "passwd", ""
|
||||||
|
File.write "group", ""
|
||||||
|
|
||||||
|
ENV["IPC_RUNDIR"]="."
|
||||||
|
|
||||||
|
# authd (dæmon) setup.
|
||||||
|
authd_process = Process.new(
|
||||||
|
"./bin/authd",
|
||||||
|
args: [
|
||||||
|
"-u", "passwd",
|
||||||
|
"-g", "group"
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
# Actual test begins here.
|
||||||
|
authd = AuthD::Client.new
|
||||||
|
|
||||||
|
pp! authd.add_user "test", "test"
|
||||||
|
|
||||||
|
# User should be there, we just created it!
|
||||||
|
user = authd.get_user?("test", "test").as AuthD::User
|
||||||
|
|
||||||
|
(user.login == "test").should be_true
|
||||||
|
|
||||||
|
user2 = authd.add_user("test2", "test").as AuthD::User
|
||||||
|
|
||||||
|
(user2.uid != user.uid).should be_true
|
||||||
|
|
||||||
|
authd.mod_user user.uid, password: "oh no"
|
||||||
|
user_bis = authd.get_user?("test", "oh no").as AuthD::User
|
||||||
|
|
||||||
|
user_bis.to_h.should eq(user.to_h)
|
||||||
|
|
||||||
|
# User should be there, we just created it!
|
||||||
|
user2 = authd.get_user?("test2", "test").as AuthD::User
|
||||||
|
|
||||||
|
(user2.uid != user.uid).should be_true
|
||||||
|
|
||||||
|
authd.close
|
||||||
|
|
||||||
|
# authd (dæmon) cleanup.
|
||||||
|
authd_process.kill
|
||||||
|
authd_process.wait
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
20
src/authd.cr
20
src/authd.cr
|
@ -72,7 +72,7 @@ module AuthD
|
||||||
initialize "auth"
|
initialize "auth"
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_token?(login : String, password : String)
|
def get_token?(login : String, password : String) : String?
|
||||||
send RequestTypes::GetToken, {
|
send RequestTypes::GetToken, {
|
||||||
:login => login,
|
:login => login,
|
||||||
:password => password
|
:password => password
|
||||||
|
@ -81,13 +81,13 @@ module AuthD
|
||||||
response = read
|
response = read
|
||||||
|
|
||||||
if response.type == ResponseTypes::Ok.value.to_u8
|
if response.type == ResponseTypes::Ok.value.to_u8
|
||||||
response.payload
|
String.new response.payload
|
||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_user?(login : String, password : String)
|
def get_user?(login : String, password : String) : User?
|
||||||
send RequestTypes::GetUserByCredentials, {
|
send RequestTypes::GetUserByCredentials, {
|
||||||
:login => login,
|
:login => login,
|
||||||
:password => password
|
:password => password
|
||||||
|
@ -96,7 +96,7 @@ module AuthD
|
||||||
response = read
|
response = read
|
||||||
|
|
||||||
if response.type == ResponseTypes::Ok.value.to_u8
|
if response.type == ResponseTypes::Ok.value.to_u8
|
||||||
User.from_json response.payload
|
User.from_json String.new response.payload
|
||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
@ -108,7 +108,7 @@ module AuthD
|
||||||
response = read
|
response = read
|
||||||
|
|
||||||
if response.type == ResponseTypes::Ok.value.to_u8
|
if response.type == ResponseTypes::Ok.value.to_u8
|
||||||
User.from_json response.payload
|
User.from_json String.new response.payload
|
||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
@ -119,7 +119,7 @@ module AuthD
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode_token(token)
|
def decode_token(token)
|
||||||
user, meta = JWT.decode token, @key, "HS256"
|
user, meta = JWT.decode token, @key, JWT::Algorithm::HS256
|
||||||
|
|
||||||
user = AuthD::User.from_json user.to_json
|
user = AuthD::User.from_json user.to_json
|
||||||
|
|
||||||
|
@ -135,12 +135,12 @@ module AuthD
|
||||||
|
|
||||||
response = read
|
response = read
|
||||||
|
|
||||||
pp! response.type
|
payload = String.new response.payload
|
||||||
case ResponseTypes.new response.type.to_i
|
case ResponseTypes.new response.type.to_i
|
||||||
when ResponseTypes::Ok
|
when ResponseTypes::Ok
|
||||||
AuthD::User.from_json response.payload
|
AuthD::User.from_json payload
|
||||||
else
|
else
|
||||||
Exception.new response.payload
|
Exception.new payload
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ module AuthD
|
||||||
when ResponseTypes::Ok
|
when ResponseTypes::Ok
|
||||||
true
|
true
|
||||||
else
|
else
|
||||||
Exception.new response.payload
|
Exception.new String.new response.payload
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -58,7 +58,6 @@ IPC::Service.new "auth" do |event|
|
||||||
when IPC::Event::Message
|
when IPC::Event::Message
|
||||||
message = event.message
|
message = event.message
|
||||||
payload = message.payload
|
payload = message.payload
|
||||||
pp message
|
|
||||||
|
|
||||||
case RequestTypes.new message.type.to_i
|
case RequestTypes.new message.type.to_i
|
||||||
when RequestTypes::GetToken
|
when RequestTypes::GetToken
|
||||||
|
@ -79,7 +78,7 @@ IPC::Service.new "auth" do |event|
|
||||||
end
|
end
|
||||||
|
|
||||||
client.send ResponseTypes::Ok.value.to_u8,
|
client.send ResponseTypes::Ok.value.to_u8,
|
||||||
JWT.encode user.to_h, authd_jwt_key, "HS256"
|
JWT.encode user.to_h, authd_jwt_key, JWT::Algorithm::HS256
|
||||||
when RequestTypes::AddUser
|
when RequestTypes::AddUser
|
||||||
begin
|
begin
|
||||||
request = AddUserRequest.from_json String.new payload
|
request = AddUserRequest.from_json String.new payload
|
||||||
|
@ -97,7 +96,7 @@ IPC::Service.new "auth" do |event|
|
||||||
|
|
||||||
user = passwd.add_user request.login, request.password
|
user = passwd.add_user request.login, request.password
|
||||||
|
|
||||||
client.send ResponseTypes::Ok, user.to_json
|
client.send ResponseTypes::Ok, user.sanitize!.to_json
|
||||||
when RequestTypes::GetUserByCredentials
|
when RequestTypes::GetUserByCredentials
|
||||||
begin
|
begin
|
||||||
request = GetUserByCredentialsRequest.from_json String.new payload
|
request = GetUserByCredentialsRequest.from_json String.new payload
|
||||||
|
@ -109,7 +108,7 @@ IPC::Service.new "auth" do |event|
|
||||||
user = passwd.get_user request.login, request.password
|
user = passwd.get_user request.login, request.password
|
||||||
|
|
||||||
if user
|
if user
|
||||||
client.send ResponseTypes::Ok, user.to_json
|
client.send ResponseTypes::Ok, user.sanitize!.to_json
|
||||||
else
|
else
|
||||||
client.send ResponseTypes::UserNotFound, ""
|
client.send ResponseTypes::UserNotFound, ""
|
||||||
end
|
end
|
||||||
|
@ -124,7 +123,7 @@ IPC::Service.new "auth" do |event|
|
||||||
user = passwd.get_user request.uid
|
user = passwd.get_user request.uid
|
||||||
|
|
||||||
if user
|
if user
|
||||||
client.send ResponseTypes::Ok, user.to_json
|
client.send ResponseTypes::Ok, user.sanitize!.to_json
|
||||||
else
|
else
|
||||||
client.send ResponseTypes::UserNotFound, ""
|
client.send ResponseTypes::UserNotFound, ""
|
||||||
end
|
end
|
||||||
|
|
|
@ -189,11 +189,11 @@ class Passwd
|
||||||
|
|
||||||
user.to_csv
|
user.to_csv
|
||||||
else
|
else
|
||||||
line.join(':') + "\n"
|
line.join(':')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
File.write @passwd, new_passwd.join + "\n"
|
File.write @passwd, new_passwd.join("\n") + "\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,11 @@ class AuthD::User
|
||||||
def initialize(@login, @password_hash, @uid, @gid, @home, @shell)
|
def initialize(@login, @password_hash, @uid, @gid, @home, @shell)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def sanitize!
|
||||||
|
@password_hash = "x"
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
def to_h
|
def to_h
|
||||||
{
|
{
|
||||||
:login => @login,
|
:login => @login,
|
||||||
|
|
Loading…
Reference in New Issue