Merge pull request #12 from Lukc/master

IPC.cr API updates.
This commit is contained in:
Philippe Pittoli 2019-07-12 16:00:35 +02:00 committed by GitHub
commit 1a83d49eca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 17 deletions

53
spec/basics.cr Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,