WIP registration.
This commit is contained in:
parent
313536f996
commit
a7a1c54161
35
src/authd.cr
35
src/authd.cr
@ -9,12 +9,14 @@ require "./group.cr"
|
||||
module AuthD
|
||||
enum RequestTypes
|
||||
GET_TOKEN
|
||||
ADD_USER
|
||||
end
|
||||
|
||||
enum ResponseTypes
|
||||
OK
|
||||
MALFORMED_REQUEST
|
||||
INVALID_CREDENTIALS
|
||||
INVALID_USER
|
||||
end
|
||||
|
||||
class GetTokenRequest
|
||||
@ -25,6 +27,17 @@ module AuthD
|
||||
})
|
||||
end
|
||||
|
||||
class AddUserRequest
|
||||
JSON.mapping({
|
||||
login: String,
|
||||
password: String,
|
||||
uid: Int32?,
|
||||
gid: Int32?,
|
||||
home: String?,
|
||||
shell: String?
|
||||
})
|
||||
end
|
||||
|
||||
class Client < IPC::Client
|
||||
property key : String
|
||||
|
||||
@ -49,6 +62,10 @@ module AuthD
|
||||
end
|
||||
end
|
||||
|
||||
def send(type : RequestTypes, payload)
|
||||
send type.value.to_u8, payload
|
||||
end
|
||||
|
||||
def decode_token(token)
|
||||
user, meta = JWT.decode token, @key, "HS256"
|
||||
|
||||
@ -56,6 +73,24 @@ module AuthD
|
||||
|
||||
{user, meta}
|
||||
end
|
||||
|
||||
# FIXME: Extra options may be useful to implement here.
|
||||
def add_user(login : String, password : String) : AuthD::User | Exception
|
||||
send RequestTypes::ADD_USER, {
|
||||
:login => login,
|
||||
:password => password
|
||||
}.to_json
|
||||
|
||||
response = read
|
||||
|
||||
pp! response.type
|
||||
case ResponseTypes.new response.type.to_i
|
||||
when ResponseTypes::OK
|
||||
AuthD::User.from_json response.payload
|
||||
else
|
||||
Exception.new response.payload
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
24
src/main.cr
24
src/main.cr
@ -13,6 +13,12 @@ require "./passwd.cr"
|
||||
|
||||
extend AuthD
|
||||
|
||||
class IPC::RemoteClient
|
||||
def send(type : ResponseTypes, payload : String)
|
||||
send type.value.to_u8, payload
|
||||
end
|
||||
end
|
||||
|
||||
authd_passwd_file = "passwd"
|
||||
authd_group_file = "group"
|
||||
authd_jwt_key = "nico-nico-nii"
|
||||
@ -69,6 +75,24 @@ IPC::Service.new "auth" do |event|
|
||||
|
||||
client.send ResponseTypes::OK.value.to_u8,
|
||||
JWT.encode user.to_h, authd_jwt_key, "HS256"
|
||||
when RequestTypes::ADD_USER
|
||||
begin
|
||||
request = AddUserRequest.from_json payload
|
||||
rescue e
|
||||
client.send ResponseTypes::MALFORMED_REQUEST.value.to_u8, e.message || ""
|
||||
|
||||
next
|
||||
end
|
||||
|
||||
if passwd.user_exists? request.login
|
||||
client.send ResponseTypes::INVALID_USER, "Another user with the same login already exists."
|
||||
|
||||
next
|
||||
end
|
||||
|
||||
user = passwd.add_user request.login, request.password
|
||||
|
||||
client.send ResponseTypes::OK, user.to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -48,6 +48,14 @@ class Passwd
|
||||
end
|
||||
end
|
||||
|
||||
def user_exists?(login : String) : Bool
|
||||
each_user do |user|
|
||||
return true if user.login == login
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
def get_user(uid : Int32) : AuthD::User?
|
||||
each_user do |user|
|
||||
if user.uid == uid
|
||||
@ -150,6 +158,10 @@ class Passwd
|
||||
File.write(@passwd, user.to_csv + "\n", mode: "a")
|
||||
|
||||
add_group login, gid: gid, users: [user.login]
|
||||
|
||||
set_user_groups user
|
||||
|
||||
user
|
||||
end
|
||||
|
||||
def add_group(name, password_hash = "x", gid = nil, users = Array(String).new)
|
||||
|
Loading…
Reference in New Issue
Block a user