diff --git a/spec/basics.cr b/spec/basics.cr new file mode 100644 index 0000000..600bb89 --- /dev/null +++ b/spec/basics.cr @@ -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 + diff --git a/src/authd.cr b/src/authd.cr index fa978e3..b5ad647 100644 --- a/src/authd.cr +++ b/src/authd.cr @@ -72,7 +72,7 @@ module AuthD initialize "auth" end - def get_token?(login : String, password : String) + def get_token?(login : String, password : String) : String? send RequestTypes::GetToken, { :login => login, :password => password @@ -81,13 +81,13 @@ module AuthD response = read if response.type == ResponseTypes::Ok.value.to_u8 - response.payload + String.new response.payload else nil end end - def get_user?(login : String, password : String) + def get_user?(login : String, password : String) : User? send RequestTypes::GetUserByCredentials, { :login => login, :password => password @@ -96,7 +96,7 @@ module AuthD response = read if response.type == ResponseTypes::Ok.value.to_u8 - User.from_json response.payload + User.from_json String.new response.payload else nil end @@ -108,7 +108,7 @@ module AuthD response = read if response.type == ResponseTypes::Ok.value.to_u8 - User.from_json response.payload + User.from_json String.new response.payload else nil end @@ -119,7 +119,7 @@ module AuthD end 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 @@ -135,12 +135,12 @@ module AuthD response = read - pp! response.type + payload = String.new response.payload case ResponseTypes.new response.type.to_i when ResponseTypes::Ok - AuthD::User.from_json response.payload + AuthD::User.from_json payload else - Exception.new response.payload + Exception.new payload end end @@ -164,7 +164,7 @@ module AuthD when ResponseTypes::Ok true else - Exception.new response.payload + Exception.new String.new response.payload end end end diff --git a/src/main.cr b/src/main.cr index 31a4546..d0a99c8 100644 --- a/src/main.cr +++ b/src/main.cr @@ -58,7 +58,6 @@ IPC::Service.new "auth" do |event| when IPC::Event::Message message = event.message payload = message.payload - pp message case RequestTypes.new message.type.to_i when RequestTypes::GetToken @@ -79,7 +78,7 @@ IPC::Service.new "auth" do |event| end 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 begin 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 - client.send ResponseTypes::Ok, user.to_json + client.send ResponseTypes::Ok, user.sanitize!.to_json when RequestTypes::GetUserByCredentials begin 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 if user - client.send ResponseTypes::Ok, user.to_json + client.send ResponseTypes::Ok, user.sanitize!.to_json else client.send ResponseTypes::UserNotFound, "" end @@ -124,7 +123,7 @@ IPC::Service.new "auth" do |event| user = passwd.get_user request.uid if user - client.send ResponseTypes::Ok, user.to_json + client.send ResponseTypes::Ok, user.sanitize!.to_json else client.send ResponseTypes::UserNotFound, "" end diff --git a/src/passwd.cr b/src/passwd.cr index 4c5ad3d..7fc38ee 100644 --- a/src/passwd.cr +++ b/src/passwd.cr @@ -189,11 +189,11 @@ class Passwd user.to_csv else - line.join(':') + "\n" + line.join(':') end end - File.write @passwd, new_passwd.join + "\n" + File.write @passwd, new_passwd.join("\n") + "\n" end end diff --git a/src/user.cr b/src/user.cr index 9f70a13..4fd999c 100644 --- a/src/user.cr +++ b/src/user.cr @@ -34,6 +34,11 @@ class AuthD::User def initialize(@login, @password_hash, @uid, @gid, @home, @shell) end + def sanitize! + @password_hash = "x" + self + end + def to_h { :login => @login,