Grooming, preparing for split.
parent
bcead2b9a2
commit
7798d119ed
11
src/authd.cr
11
src/authd.cr
|
@ -4,7 +4,6 @@ require "jwt"
|
||||||
require "ipc"
|
require "ipc"
|
||||||
|
|
||||||
require "./user.cr"
|
require "./user.cr"
|
||||||
require "./group.cr"
|
|
||||||
|
|
||||||
module AuthD
|
module AuthD
|
||||||
enum RequestTypes
|
enum RequestTypes
|
||||||
|
@ -94,7 +93,7 @@ module AuthD
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_user?(login : String, password : String) : User?
|
def get_user?(login : String, password : String) : Passwd::User?
|
||||||
send RequestTypes::GetUserByCredentials, {
|
send RequestTypes::GetUserByCredentials, {
|
||||||
:login => login,
|
:login => login,
|
||||||
:password => password
|
:password => password
|
||||||
|
@ -103,7 +102,7 @@ module AuthD
|
||||||
response = read
|
response = read
|
||||||
|
|
||||||
if response.type == ResponseTypes::Ok.value.to_u8
|
if response.type == ResponseTypes::Ok.value.to_u8
|
||||||
User.from_json String.new response.payload
|
Passwd::User.from_json String.new response.payload
|
||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
@ -128,13 +127,13 @@ module AuthD
|
||||||
def decode_token(token)
|
def decode_token(token)
|
||||||
user, meta = JWT.decode token, @key, JWT::Algorithm::HS256
|
user, meta = JWT.decode token, @key, JWT::Algorithm::HS256
|
||||||
|
|
||||||
user = AuthD::User.from_json user.to_json
|
user = Passwd::User.from_json user.to_json
|
||||||
|
|
||||||
{user, meta}
|
{user, meta}
|
||||||
end
|
end
|
||||||
|
|
||||||
# FIXME: Extra options may be useful to implement here.
|
# FIXME: Extra options may be useful to implement here.
|
||||||
def add_user(login : String, password : String) : AuthD::User | Exception
|
def add_user(login : String, password : String) : Passwd::User | Exception
|
||||||
send RequestTypes::AddUser, {
|
send RequestTypes::AddUser, {
|
||||||
:shared_key => @key,
|
:shared_key => @key,
|
||||||
:login => login,
|
:login => login,
|
||||||
|
@ -146,7 +145,7 @@ module AuthD
|
||||||
payload = String.new response.payload
|
payload = String.new response.payload
|
||||||
case ResponseTypes.new response.type.to_i
|
case ResponseTypes.new response.type.to_i
|
||||||
when ResponseTypes::Ok
|
when ResponseTypes::Ok
|
||||||
AuthD::User.from_json payload
|
Passwd::User.from_json payload
|
||||||
else
|
else
|
||||||
Exception.new payload
|
Exception.new payload
|
||||||
end
|
end
|
||||||
|
|
11
src/group.cr
11
src/group.cr
|
@ -1,11 +0,0 @@
|
||||||
|
|
||||||
class AuthD::Group
|
|
||||||
getter name : String
|
|
||||||
getter password_hash : String
|
|
||||||
getter gid : Int32
|
|
||||||
getter users = Array(String).new
|
|
||||||
|
|
||||||
def initialize(@name, @password_hash, @gid, @users)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
require "csv"
|
require "csv"
|
||||||
require "uuid"
|
|
||||||
require "base64"
|
|
||||||
|
|
||||||
require "./user.cr"
|
|
||||||
require "./group.cr"
|
|
||||||
|
|
||||||
# FIXME: Should we work on arrays and convert to CSV at the last second when adding rows?
|
# FIXME: Should we work on arrays and convert to CSV at the last second when adding rows?
|
||||||
|
# FIXME: Use split, not CSV.
|
||||||
|
# FIXME: Prevent using ':' in fields.
|
||||||
|
|
||||||
class Passwd
|
class Passwd
|
||||||
@passwd : String
|
@passwd : String
|
||||||
|
@ -31,9 +28,9 @@ class Passwd
|
||||||
CSV.parse File.read(@group), separator: ':'
|
CSV.parse File.read(@group), separator: ':'
|
||||||
end
|
end
|
||||||
|
|
||||||
private def set_user_groups(user : AuthD::User)
|
private def set_user_groups(user : Passwd::User)
|
||||||
group_as_array.each do |line|
|
group_as_array.each do |line|
|
||||||
group = AuthD::Group.new line
|
group = Passwd::Group.new line
|
||||||
|
|
||||||
if group.users.any? { |name| name == user.login }
|
if group.users.any? { |name| name == user.login }
|
||||||
user.groups << group.name
|
user.groups << group.name
|
||||||
|
@ -43,7 +40,7 @@ class Passwd
|
||||||
|
|
||||||
def each_user(&block)
|
def each_user(&block)
|
||||||
passwd_as_array.each do |line|
|
passwd_as_array.each do |line|
|
||||||
yield AuthD::User.new line
|
yield Passwd::User.new line
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -55,7 +52,7 @@ class Passwd
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_user(uid : Int32) : AuthD::User?
|
def get_user(uid : Int32) : Passwd::User?
|
||||||
each_user do |user|
|
each_user do |user|
|
||||||
if user.uid == uid
|
if user.uid == uid
|
||||||
set_user_groups user
|
set_user_groups user
|
||||||
|
@ -67,7 +64,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) : Passwd::User?
|
||||||
hash = Passwd.hash_password password
|
hash = Passwd.hash_password password
|
||||||
|
|
||||||
each_user do |user|
|
each_user do |user|
|
||||||
|
@ -84,20 +81,20 @@ class Passwd
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_all_users
|
def get_all_users
|
||||||
users = Array(AuthD::User).new
|
users = Array(Passwd::User).new
|
||||||
|
|
||||||
passwd_as_array.each do |line|
|
passwd_as_array.each do |line|
|
||||||
users << AuthD::User.new line
|
users << Passwd::User.new line
|
||||||
end
|
end
|
||||||
|
|
||||||
users
|
users
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_all_groups
|
def get_all_groups
|
||||||
groups = Array(AuthD::Group).new
|
groups = Array(Passwd::Group).new
|
||||||
|
|
||||||
group_as_array.each do |line|
|
group_as_array.each do |line|
|
||||||
groups << AuthD::Group.new line
|
groups << Passwd::Group.new line
|
||||||
end
|
end
|
||||||
|
|
||||||
groups
|
groups
|
||||||
|
@ -154,7 +151,7 @@ class Passwd
|
||||||
"x"
|
"x"
|
||||||
end
|
end
|
||||||
|
|
||||||
user = AuthD::User.new login, password_hash, uid, gid, home, shell
|
user = Passwd::User.new login, password_hash, uid, gid, home, shell
|
||||||
|
|
||||||
File.write(@passwd, user.to_csv + "\n", mode: "a")
|
File.write(@passwd, user.to_csv + "\n", mode: "a")
|
||||||
|
|
||||||
|
@ -168,7 +165,7 @@ class Passwd
|
||||||
def add_group(name, password_hash = "x", gid = nil, users = Array(String).new)
|
def add_group(name, password_hash = "x", gid = nil, users = Array(String).new)
|
||||||
gid = get_free_gid if gid.nil?
|
gid = get_free_gid if gid.nil?
|
||||||
|
|
||||||
group = AuthD::Group.new name, password_hash, gid, users
|
group = Passwd::Group.new name, password_hash, gid, users
|
||||||
|
|
||||||
File.write(@group, group.to_csv + "\n", mode: "a")
|
File.write(@group, group.to_csv + "\n", mode: "a")
|
||||||
end
|
end
|
||||||
|
@ -176,7 +173,7 @@ class Passwd
|
||||||
# FIXME: Edit other important fields.
|
# FIXME: Edit other important fields.
|
||||||
def mod_user(uid, password_hash : String? = nil)
|
def mod_user(uid, password_hash : String? = nil)
|
||||||
new_passwd = passwd_as_array.map do |line|
|
new_passwd = passwd_as_array.map do |line|
|
||||||
user = AuthD::User.new line
|
user = Passwd::User.new line
|
||||||
|
|
||||||
if uid == user.uid
|
if uid == user.uid
|
||||||
password_hash.try do |hash|
|
password_hash.try do |hash|
|
||||||
|
@ -193,7 +190,15 @@ class Passwd
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class AuthD::Group
|
class Passwd::Group
|
||||||
|
getter name : String
|
||||||
|
getter password_hash : String
|
||||||
|
getter gid : Int32
|
||||||
|
getter users = Array(String).new
|
||||||
|
|
||||||
|
def initialize(@name, @password_hash, @gid, @users = [] of String)
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(line : Array(String))
|
def initialize(line : Array(String))
|
||||||
@name = line[0]
|
@name = line[0]
|
||||||
@password_hash = line[1]
|
@password_hash = line[1]
|
||||||
|
@ -207,7 +212,36 @@ class AuthD::Group
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class AuthD::User
|
class Passwd::User
|
||||||
|
getter uid : Int32
|
||||||
|
getter gid : Int32
|
||||||
|
getter login : String
|
||||||
|
getter password_hash : String
|
||||||
|
getter home : String = "/"
|
||||||
|
getter shell : String = "/bin/nologin"
|
||||||
|
getter groups = Array(String).new
|
||||||
|
getter full_name : String? = nil
|
||||||
|
getter location : String? = nil
|
||||||
|
getter office_phone_number : String? = nil
|
||||||
|
getter home_phone_number : String? = nil
|
||||||
|
getter other_contact : String? = nil
|
||||||
|
|
||||||
|
def initialize(
|
||||||
|
@login,
|
||||||
|
@password_hash,
|
||||||
|
@uid,
|
||||||
|
@gid,
|
||||||
|
@home = "",
|
||||||
|
@shell = "",
|
||||||
|
@full_name = nil,
|
||||||
|
@location = nil,
|
||||||
|
@office_phone_number = nil,
|
||||||
|
@home_phone_number = nil,
|
||||||
|
@other_contact = nil
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Caution: will raise on invalid entries.
|
||||||
def initialize(line : Array(String))
|
def initialize(line : Array(String))
|
||||||
@login = line[0]
|
@login = line[0]
|
||||||
@password_hash = line[1]
|
@password_hash = line[1]
|
||||||
|
@ -223,7 +257,6 @@ class AuthD::User
|
||||||
@other_contact = gecos[4]?
|
@other_contact = gecos[4]?
|
||||||
end
|
end
|
||||||
|
|
||||||
# FIXME: What about those two fields? Keep them, remove them?
|
|
||||||
@home = line[5]
|
@home = line[5]
|
||||||
@shell = line[6]
|
@shell = line[6]
|
||||||
end
|
end
|
||||||
|
@ -244,3 +277,4 @@ class AuthD::User
|
||||||
[@full_name || "", @location || "", @office_phone_number || "", @home_phone_number || "", @other_contact || ""].join ","
|
[@full_name || "", @location || "", @office_phone_number || "", @home_phone_number || "", @other_contact || ""].join ","
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
24
src/user.cr
24
src/user.cr
|
@ -1,21 +1,8 @@
|
||||||
|
|
||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
class AuthD::User
|
require "./passwd.cr"
|
||||||
getter uid : Int32
|
|
||||||
getter gid : Int32
|
|
||||||
getter login : String
|
|
||||||
getter password_hash : String
|
|
||||||
getter home : String = "/"
|
|
||||||
getter shell : String = "/bin/nologin"
|
|
||||||
getter groups = Array(String).new
|
|
||||||
getter full_name : 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
|
|
||||||
|
|
||||||
|
class Passwd::User
|
||||||
JSON.mapping({
|
JSON.mapping({
|
||||||
login: String,
|
login: String,
|
||||||
password_hash: String,
|
password_hash: String,
|
||||||
|
@ -28,12 +15,8 @@ class AuthD::User
|
||||||
office_phone_number: String?,
|
office_phone_number: String?,
|
||||||
home_phone_number: String?,
|
home_phone_number: String?,
|
||||||
other_contact: String?,
|
other_contact: String?,
|
||||||
avatar: String?
|
|
||||||
})
|
})
|
||||||
|
|
||||||
def initialize(@login, @password_hash, @uid, @gid, @home, @shell)
|
|
||||||
end
|
|
||||||
|
|
||||||
def sanitize!
|
def sanitize!
|
||||||
@password_hash = "x"
|
@password_hash = "x"
|
||||||
self
|
self
|
||||||
|
@ -51,8 +34,7 @@ class AuthD::User
|
||||||
:full_name => @full_name,
|
:full_name => @full_name,
|
||||||
: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