ModUser request.

ipc07
Luka Vandervelden 2019-05-29 16:06:11 +02:00
parent a83c3c8e5c
commit 6a19ff604a
3 changed files with 51 additions and 6 deletions

View File

@ -12,6 +12,7 @@ module AuthD
AddUser AddUser
GetUser GetUser
GetUserByCredentials GetUserByCredentials
ModUser # Edit user attributes.
end end
enum ResponseTypes enum ResponseTypes
@ -54,6 +55,13 @@ module AuthD
}) })
end end
class ModUserRequest
JSON.mapping({
uid: Int32,
password: String?
})
end
class Client < IPC::Client class Client < IPC::Client
property key : String property key : String
@ -134,6 +142,26 @@ module AuthD
Exception.new response.payload Exception.new response.payload
end end
end end
def mod_user(uid : Int32, password : String?) : Bool | Exception
payload = Hash(String, String|Int32).new
payload["uid"] = uid
password.try do |password|
payload["password"] = password
end
send RequestTypes::ModUser, payload.to_json
response = read
case ResponseTypes.new response.type.to_i
when ResponseTypes::Ok
true
else
Exception.new response.payload
end
end
end end
end end

View File

@ -121,6 +121,21 @@ IPC::Service.new "auth" do |event|
else else
client.send ResponseTypes::UserNotFound, "" client.send ResponseTypes::UserNotFound, ""
end end
when RequestTypes::ModUser
begin
request = ModUserRequest.from_json payload
rescue e
client.send ResponseTypes::MalformedRequest, e.message || ""
next
end
password_hash = request.password.try do |s|
Passwd.hash_password s
end
passwd.mod_user request.uid, password_hash: password_hash
client.send ResponseTypes::Ok, ""
end end
end end
end end

View File

@ -69,9 +69,7 @@ class Passwd
## ##
# Will fail if the user is found but the password is invalid. # Will fail if the user is found but the password is invalid.
def get_user(login : String, password : String) : AuthD::User? def get_user(login : String, password : String) : AuthD::User?
digest = OpenSSL::Digest.new("sha256") hash = Passwd.hash_password password
digest << password
hash = digest.hexdigest
each_user do |user| each_user do |user|
if user.login == login if user.login == login
@ -138,6 +136,12 @@ class Passwd
gid gid
end end
def self.hash_password(password)
digest = OpenSSL::Digest.new("sha256")
digest << password
digest.hexdigest
end
def add_user(login, password = nil, uid = nil, gid = nil, home = "/", shell = "/bin/nologin") def add_user(login, password = nil, uid = nil, gid = nil, home = "/", shell = "/bin/nologin")
# FIXME: If user already exists, exception? Replacement? # FIXME: If user already exists, exception? Replacement?
@ -146,9 +150,7 @@ class Passwd
gid = get_free_gid if gid.nil? gid = get_free_gid if gid.nil?
password_hash = if password password_hash = if password
digest = OpenSSL::Digest.new("sha256") Passwd.hash_password password
digest << password
digest.hexdigest
else else
"x" "x"
end end