Merge pull request #9 from Lukc/master

New client request + grooming.
This commit is contained in:
remydev 2019-03-04 20:11:17 +01:00 committed by GitHub
commit 13b28f0064
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 10 deletions

View File

@ -10,6 +10,8 @@ module AuthD
enum RequestTypes
GetToken
AddUser
GetUser
GetUserByCredentials
end
enum ResponseTypes
@ -17,6 +19,7 @@ module AuthD
MalformedRequest
InvalidCredentials
InvalidUser
UserNotFound # For UID-based GetUser requests.
end
class GetTokenRequest
@ -38,6 +41,19 @@ module AuthD
})
end
class GetUserRequest
JSON.mapping({
uid: Int32
})
end
class GetUserByCredentialsRequest
JSON.mapping({
login: String,
password: String
})
end
class Client < IPC::Client
property key : String
@ -48,7 +64,7 @@ module AuthD
end
def get_token?(login : String, password : String)
send RequestTypes::GetToken.value.to_u8, {
send RequestTypes::GetToken, {
:login => login,
:password => password
}.to_json
@ -62,6 +78,33 @@ module AuthD
end
end
def get_user?(login : String, password : String)
send RequestTypes::GetUserByCredentials, {
:login => login,
:password => password
}.to_json
response = read
if response.type == ResponseTypes::Ok.value.to_u8
User.from_json response.payload
else
nil
end
end
def get_user?(uid : Int32)
send RequestTypes::GetUser, {:uid => uid}.to_json
response = read
if response.type == ResponseTypes::Ok.value.to_u8
User.from_json response.payload
else
nil
end
end
def send(type : RequestTypes, payload)
send type.value.to_u8, payload
end

View File

@ -1,11 +1,9 @@
require "uuid"
require "option_parser"
require "openssl"
require "jwt"
require "pg"
require "crecto"
require "ipc"
require "./authd.cr"
@ -93,6 +91,36 @@ IPC::Service.new "auth" do |event|
user = passwd.add_user request.login, request.password
client.send ResponseTypes::Ok, user.to_json
when RequestTypes::GetUserByCredentials
begin
request = GetUserByCredentialsRequest.from_json 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
when RequestTypes::GetUser
begin
request = GetUserRequest.from_json payload
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
end
end
end

View File

@ -59,7 +59,6 @@ class Passwd
def get_user(uid : Int32) : AuthD::User?
each_user do |user|
if user.uid == uid
# FIXME: Check user groups and register them here.
set_user_groups user
return user
@ -76,8 +75,9 @@ class Passwd
each_user do |user|
if user.login == login
# FIXME: XXX: HASH!!!!!
if user.password_hash == hash
set_user_groups user
return user
end
@ -201,6 +201,7 @@ class AuthD::User
@office_phone_number = gecos[2]?
@home_phone_number = gecos[3]?
@other_contact = gecos[4]?
@avatar = gecos[5]? # CAUTION: NON-STANDARD EXTENSION
end
# FIXME: What about those two fields? Keep them, remove them?

View File

@ -8,11 +8,11 @@ class AuthD::User
getter shell : String = "/bin/nologin"
getter groups = Array(String).new
getter full_name : String? = nil
getter avatar : String? = nil
getter location : String? = nil
getter office_phone_number : String? = nil
getter home_phone_number : String? = nil
getter other_contact : String? = nil
getter avatar : String? = nil
JSON.mapping({
login: String,
@ -23,7 +23,6 @@ class AuthD::User
shell: String,
groups: Array(String),
full_name: String?,
avatar: String?,
office_phone_number: String?,
home_phone_number: String?,
other_contact: String?
@ -42,10 +41,10 @@ class AuthD::User
:shell => @shell,
:groups => @groups,
:full_name => @full_name,
:avatar => @avatar,
:office_phone_number => @office_phone_number,
:home_phone_number => @home_phone_number,
:other_contact => @other_contact
:other_contact => @other_contact,
:avatar => @avatar
}
end
end