ModUser request.
parent
a83c3c8e5c
commit
6a19ff604a
28
src/authd.cr
28
src/authd.cr
|
@ -12,6 +12,7 @@ module AuthD
|
|||
AddUser
|
||||
GetUser
|
||||
GetUserByCredentials
|
||||
ModUser # Edit user attributes.
|
||||
end
|
||||
|
||||
enum ResponseTypes
|
||||
|
@ -54,6 +55,13 @@ module AuthD
|
|||
})
|
||||
end
|
||||
|
||||
class ModUserRequest
|
||||
JSON.mapping({
|
||||
uid: Int32,
|
||||
password: String?
|
||||
})
|
||||
end
|
||||
|
||||
class Client < IPC::Client
|
||||
property key : String
|
||||
|
||||
|
@ -134,6 +142,26 @@ module AuthD
|
|||
Exception.new response.payload
|
||||
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
|
||||
|
||||
|
|
15
src/main.cr
15
src/main.cr
|
@ -121,6 +121,21 @@ IPC::Service.new "auth" do |event|
|
|||
else
|
||||
client.send ResponseTypes::UserNotFound, ""
|
||||
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
|
||||
|
|
|
@ -69,9 +69,7 @@ class Passwd
|
|||
##
|
||||
# Will fail if the user is found but the password is invalid.
|
||||
def get_user(login : String, password : String) : AuthD::User?
|
||||
digest = OpenSSL::Digest.new("sha256")
|
||||
digest << password
|
||||
hash = digest.hexdigest
|
||||
hash = Passwd.hash_password password
|
||||
|
||||
each_user do |user|
|
||||
if user.login == login
|
||||
|
@ -138,6 +136,12 @@ class Passwd
|
|||
gid
|
||||
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")
|
||||
# FIXME: If user already exists, exception? Replacement?
|
||||
|
||||
|
@ -146,9 +150,7 @@ class Passwd
|
|||
gid = get_free_gid if gid.nil?
|
||||
|
||||
password_hash = if password
|
||||
digest = OpenSSL::Digest.new("sha256")
|
||||
digest << password
|
||||
digest.hexdigest
|
||||
Passwd.hash_password password
|
||||
else
|
||||
"x"
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue