commit
13b28f0064
45
src/authd.cr
45
src/authd.cr
|
@ -10,6 +10,8 @@ module AuthD
|
||||||
enum RequestTypes
|
enum RequestTypes
|
||||||
GetToken
|
GetToken
|
||||||
AddUser
|
AddUser
|
||||||
|
GetUser
|
||||||
|
GetUserByCredentials
|
||||||
end
|
end
|
||||||
|
|
||||||
enum ResponseTypes
|
enum ResponseTypes
|
||||||
|
@ -17,6 +19,7 @@ module AuthD
|
||||||
MalformedRequest
|
MalformedRequest
|
||||||
InvalidCredentials
|
InvalidCredentials
|
||||||
InvalidUser
|
InvalidUser
|
||||||
|
UserNotFound # For UID-based GetUser requests.
|
||||||
end
|
end
|
||||||
|
|
||||||
class GetTokenRequest
|
class GetTokenRequest
|
||||||
|
@ -38,6 +41,19 @@ module AuthD
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class GetUserRequest
|
||||||
|
JSON.mapping({
|
||||||
|
uid: Int32
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
class GetUserByCredentialsRequest
|
||||||
|
JSON.mapping({
|
||||||
|
login: String,
|
||||||
|
password: String
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
class Client < IPC::Client
|
class Client < IPC::Client
|
||||||
property key : String
|
property key : String
|
||||||
|
|
||||||
|
@ -48,7 +64,7 @@ module AuthD
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_token?(login : String, password : String)
|
def get_token?(login : String, password : String)
|
||||||
send RequestTypes::GetToken.value.to_u8, {
|
send RequestTypes::GetToken, {
|
||||||
:login => login,
|
:login => login,
|
||||||
:password => password
|
:password => password
|
||||||
}.to_json
|
}.to_json
|
||||||
|
@ -62,6 +78,33 @@ module AuthD
|
||||||
end
|
end
|
||||||
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)
|
def send(type : RequestTypes, payload)
|
||||||
send type.value.to_u8, payload
|
send type.value.to_u8, payload
|
||||||
end
|
end
|
||||||
|
|
34
src/main.cr
34
src/main.cr
|
@ -1,11 +1,9 @@
|
||||||
require "uuid"
|
require "uuid"
|
||||||
require "option_parser"
|
require "option_parser"
|
||||||
|
require "openssl"
|
||||||
|
|
||||||
require "jwt"
|
require "jwt"
|
||||||
|
|
||||||
require "pg"
|
|
||||||
require "crecto"
|
|
||||||
|
|
||||||
require "ipc"
|
require "ipc"
|
||||||
|
|
||||||
require "./authd.cr"
|
require "./authd.cr"
|
||||||
|
@ -93,6 +91,36 @@ IPC::Service.new "auth" do |event|
|
||||||
user = passwd.add_user request.login, request.password
|
user = passwd.add_user request.login, request.password
|
||||||
|
|
||||||
client.send ResponseTypes::Ok, user.to_json
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -59,7 +59,6 @@ class Passwd
|
||||||
def get_user(uid : Int32) : AuthD::User?
|
def get_user(uid : Int32) : AuthD::User?
|
||||||
each_user do |user|
|
each_user do |user|
|
||||||
if user.uid == uid
|
if user.uid == uid
|
||||||
# FIXME: Check user groups and register them here.
|
|
||||||
set_user_groups user
|
set_user_groups user
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
@ -76,8 +75,9 @@ class Passwd
|
||||||
|
|
||||||
each_user do |user|
|
each_user do |user|
|
||||||
if user.login == login
|
if user.login == login
|
||||||
# FIXME: XXX: HASH!!!!!
|
|
||||||
if user.password_hash == hash
|
if user.password_hash == hash
|
||||||
|
set_user_groups user
|
||||||
|
|
||||||
return user
|
return user
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -201,6 +201,7 @@ class AuthD::User
|
||||||
@office_phone_number = gecos[2]?
|
@office_phone_number = gecos[2]?
|
||||||
@home_phone_number = gecos[3]?
|
@home_phone_number = gecos[3]?
|
||||||
@other_contact = gecos[4]?
|
@other_contact = gecos[4]?
|
||||||
|
@avatar = gecos[5]? # CAUTION: NON-STANDARD EXTENSION
|
||||||
end
|
end
|
||||||
|
|
||||||
# FIXME: What about those two fields? Keep them, remove them?
|
# FIXME: What about those two fields? Keep them, remove them?
|
||||||
|
|
|
@ -8,11 +8,11 @@ class AuthD::User
|
||||||
getter shell : String = "/bin/nologin"
|
getter shell : String = "/bin/nologin"
|
||||||
getter groups = Array(String).new
|
getter groups = Array(String).new
|
||||||
getter full_name : String? = nil
|
getter full_name : String? = nil
|
||||||
getter avatar : String? = nil
|
|
||||||
getter location : String? = nil
|
getter location : String? = nil
|
||||||
getter office_phone_number : String? = nil
|
getter office_phone_number : String? = nil
|
||||||
getter home_phone_number : String? = nil
|
getter home_phone_number : String? = nil
|
||||||
getter other_contact : String? = nil
|
getter other_contact : String? = nil
|
||||||
|
getter avatar : String? = nil
|
||||||
|
|
||||||
JSON.mapping({
|
JSON.mapping({
|
||||||
login: String,
|
login: String,
|
||||||
|
@ -23,7 +23,6 @@ class AuthD::User
|
||||||
shell: String,
|
shell: String,
|
||||||
groups: Array(String),
|
groups: Array(String),
|
||||||
full_name: String?,
|
full_name: String?,
|
||||||
avatar: String?,
|
|
||||||
office_phone_number: String?,
|
office_phone_number: String?,
|
||||||
home_phone_number: String?,
|
home_phone_number: String?,
|
||||||
other_contact: String?
|
other_contact: String?
|
||||||
|
@ -42,10 +41,10 @@ class AuthD::User
|
||||||
:shell => @shell,
|
:shell => @shell,
|
||||||
:groups => @groups,
|
:groups => @groups,
|
||||||
:full_name => @full_name,
|
:full_name => @full_name,
|
||||||
:avatar => @avatar,
|
|
||||||
:office_phone_number => @office_phone_number,
|
:office_phone_number => @office_phone_number,
|
||||||
:home_phone_number => @home_phone_number,
|
:home_phone_number => @home_phone_number,
|
||||||
:other_contact => @other_contact
|
:other_contact => @other_contact,
|
||||||
|
:avatar => @avatar
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue