Compare commits

...

3 commits

3 changed files with 111 additions and 48 deletions

View file

@ -134,65 +134,69 @@ Subcommands:
.Bl -tag -width "change-password" -compact .Bl -tag -width "change-password" -compact
.It Li add .It Li add
Adding a user to the DB. Adding a user to the DB.
.It "" .br
.Nm add .Nm add
.Ar login .Ar login
.Ar email-address .Ar email-address
.It Li migrate .It Li migrate
Adding a user from old code base. Adding a user from old code base.
.It "" .br
.Nm migrate .Nm migrate
.Ar login .Ar login
.Ar hashed-password-old-algorithm .Ar hashed-password-old-algorithm
.It Li mod .It Li mod
Modify a user account. Modify a user account.
.It "" .br
.Nm mod .Nm mod
.Ar userid .Ar userid
.Bq Fl e Ar email | Fl P Ar profile .Bq Fl e Ar email | Fl P Ar profile
.It Li change-password .It Li change-password
Change the password of a user (requires admin). Change the password of a user (requires admin).
.It "" .br
.Nm change-password .Nm change-password
.Ar userid .Ar userid
.It Li delete .It Li delete
Remove user. Remove user.
.It "" .br
.Nm delete .Nm delete
.Ar userid .Ar userid
.Op Ar userid...
.It Li validate .It Li validate
Validate user. Validate user.
.It "" .br
.Nm validate .Nm validate
.Ar login .Ar login
.Ar activation-key .Ar activation-key
.It Li get .It Li get
Get user info. Get user info.
.It "" .br
.Nm get .Nm get
.Ar login .Ar login
.Op Ar login...
.It Li search .It Li search
Search user. Search user.
.It "" .br
.Nm search .Nm search
.Ar login .Ar login
.Op Ar login...
.It Li recover .It Li recover
Recover user password. Recover user password.
.It "" .br
.Nm recover .Nm recover
.Ar login .Ar login
.Op Ar login...
.It Li register .It Li register
Register a user (requires activation from the token sent by email). Register a user (requires activation from the token sent by email).
.It "" .br
.Nm register .Nm register
.Ar login .Ar login
.Ar email-address .Ar email-address
@ -215,7 +219,7 @@ permission set user-id application resource permission
.br .br
Available permissions: Available permissions:
.Em none read edit admin . .Em none read edit admin .
.It "" .br
Example: Example:
.Nm authctl .Nm authctl
.Ar permission set 1000 dnsmanager .Ar permission set 1000 dnsmanager
@ -227,7 +231,7 @@ permission check user-id application resource
.br .br
Available permissions: Available permissions:
.Em none read edit admin . .Em none read edit admin .
.It "" .br
Example: Example:
.Nm authctl .Nm authctl
.Ar permission check 1000 forum .Ar permission check 1000 forum

View file

@ -35,18 +35,35 @@ opt_email = -> (parser : OptionParser) {
# Unrecognized parameters are used to create commands with multiple arguments. # Unrecognized parameters are used to create commands with multiple arguments.
# Example: user add login email # Example: user add _login email phone_
# Here, login and email are unrecognized arguments. # Here, login, email and phone are unrecognized arguments.
# Still, the "user add" command expect them. # Still, the "user add" command expect them.
unrecognized_args_to_context_args = -> (parser : OptionParser, n_expected_args : Int32) { unrecognized_args_to_context_args = -> (parser : OptionParser,
nexact : Int32?,
at_least : Int32?) {
# With the right args, these will be interpreted as serialized data. # With the right args, these will be interpreted as serialized data.
parser.unknown_args do |args| parser.unknown_args do |args|
if args.size != n_expected_args
Baguette::Log.error "expected number of arguments: #{n_expected_args}, received: #{args.size}" # either we test with the exact expected number of arguments or the least.
Baguette::Log.error "args: #{args}" if exact = nexact
if args.size != exact
Baguette::Log.error "Wrong number of parameters: expected #{exact}, got #{args.size}"
Baguette::Log.error "#{parser}"
exit 1
end
elsif least = at_least
if args.size < least
Baguette::Log.error "Wrong number of parameters: expected at least #{least}, got #{args.size}"
Baguette::Log.error "#{parser}"
exit 1
end
else
Baguette::Log.error "Number of parameters not even provided!"
Baguette::Log.error "#{parser}" Baguette::Log.error "#{parser}"
exit 1 exit 1
end end
args.each do |arg| args.each do |arg|
Baguette::Log.debug "Unrecognized argument: #{arg} (adding to Context.args)" Baguette::Log.debug "Unrecognized argument: #{arg} (adding to Context.args)"
if Context.args.nil? if Context.args.nil?
@ -76,7 +93,7 @@ parser = OptionParser.new do |parser|
opt_profile.call parser opt_profile.call parser
opt_help.call parser opt_help.call parser
# login email # login email
unrecognized_args_to_context_args.call parser, 2 unrecognized_args_to_context_args.call parser, 2, nil
end end
parser.on "exit", "Kill the service." do parser.on "exit", "Kill the service." do
@ -85,7 +102,7 @@ parser = OptionParser.new do |parser|
Context.command = "exit" Context.command = "exit"
opt_authd_login.call parser opt_authd_login.call parser
opt_help.call parser opt_help.call parser
unrecognized_args_to_context_args.call parser, 0 unrecognized_args_to_context_args.call parser, 0, nil
end end
parser.on "migration-script", "Add a batch of users from old code base." do parser.on "migration-script", "Add a batch of users from old code base." do
@ -96,7 +113,7 @@ parser = OptionParser.new do |parser|
opt_profile.call parser opt_profile.call parser
opt_help.call parser opt_help.call parser
# user-db.txt # user-db.txt
unrecognized_args_to_context_args.call parser, 1 unrecognized_args_to_context_args.call parser, 1, nil
end end
parser.on "user", "Operations on users." do parser.on "user", "Operations on users." do
@ -110,7 +127,7 @@ parser = OptionParser.new do |parser|
opt_profile.call parser opt_profile.call parser
opt_help.call parser opt_help.call parser
# login email # login email
unrecognized_args_to_context_args.call parser, 2 unrecognized_args_to_context_args.call parser, 2, nil
end end
parser.on "migrate", "Add a user from old code base." do parser.on "migrate", "Add a user from old code base." do
@ -121,7 +138,7 @@ parser = OptionParser.new do |parser|
opt_profile.call parser opt_profile.call parser
opt_help.call parser opt_help.call parser
# login password-hash-brkn # login password-hash-brkn
unrecognized_args_to_context_args.call parser, 2 unrecognized_args_to_context_args.call parser, 2, nil
end end
parser.on "mod", "Modify a user account." do parser.on "mod", "Modify a user account." do
@ -133,7 +150,7 @@ parser = OptionParser.new do |parser|
opt_profile.call parser opt_profile.call parser
opt_help.call parser opt_help.call parser
# userid # userid
unrecognized_args_to_context_args.call parser, 1 unrecognized_args_to_context_args.call parser, 1, nil
end end
parser.on "change-password", "Change the password of a user (requires admin)." do parser.on "change-password", "Change the password of a user (requires admin)." do
@ -145,18 +162,18 @@ parser = OptionParser.new do |parser|
opt_profile.call parser opt_profile.call parser
opt_help.call parser opt_help.call parser
# userid # userid
unrecognized_args_to_context_args.call parser, 1 unrecognized_args_to_context_args.call parser, 1, nil
end end
parser.on "delete", "Remove user." do parser.on "delete", "Remove user." do
parser.banner = "Usage: user delete userid [opt]" parser.banner = "Usage: user delete userid [userid ...]"
Baguette::Log.info "Remove user." Baguette::Log.info "Remove user."
Context.command = "user-delete" Context.command = "user-delete"
# You can either be the owner of the account, or an admin. # You can either be the owner of the account, or an admin.
opt_authd_login.call parser opt_authd_login.call parser
opt_help.call parser opt_help.call parser
# userid # userid
unrecognized_args_to_context_args.call parser, 1 unrecognized_args_to_context_args.call parser, nil, 1
end end
parser.on "validate", "Validate user." do parser.on "validate", "Validate user." do
@ -166,7 +183,7 @@ parser = OptionParser.new do |parser|
# No need to be authenticated. # No need to be authenticated.
opt_help.call parser opt_help.call parser
# login activation-key # login activation-key
unrecognized_args_to_context_args.call parser, 2 unrecognized_args_to_context_args.call parser, 2, nil
end end
parser.on "get", "Get user info." do parser.on "get", "Get user info." do
@ -176,17 +193,17 @@ parser = OptionParser.new do |parser|
opt_authd_login.call parser opt_authd_login.call parser
opt_help.call parser opt_help.call parser
# login # login
unrecognized_args_to_context_args.call parser, 1 unrecognized_args_to_context_args.call parser, nil, 1
end end
parser.on "search", "Search user." do parser.on "search", "Search user." do
parser.banner = "Usage: user search login [opt]" parser.banner = "Usage: user search login [login...]"
Baguette::Log.info "Search user." Baguette::Log.info "Search user."
Context.command = "user-search" Context.command = "user-search"
opt_authd_login.call parser opt_authd_login.call parser
opt_help.call parser opt_help.call parser
# login # login
unrecognized_args_to_context_args.call parser, 1 unrecognized_args_to_context_args.call parser, nil, 1
end end
parser.on "recover", "Recover user password." do parser.on "recover", "Recover user password." do
@ -196,7 +213,7 @@ parser = OptionParser.new do |parser|
# No need to be authenticated. # No need to be authenticated.
opt_help.call parser opt_help.call parser
# login # login
unrecognized_args_to_context_args.call parser, 1 unrecognized_args_to_context_args.call parser, nil, 1
end end
@ -208,7 +225,7 @@ parser = OptionParser.new do |parser|
opt_profile.call parser opt_profile.call parser
opt_help.call parser opt_help.call parser
# login email # login email
unrecognized_args_to_context_args.call parser, 2 unrecognized_args_to_context_args.call parser, 2, nil
end end
end end
@ -226,7 +243,7 @@ END
opt_authd_login.call parser opt_authd_login.call parser
opt_help.call parser opt_help.call parser
# userid application resource permission # userid application resource permission
unrecognized_args_to_context_args.call parser, 4 unrecognized_args_to_context_args.call parser, 4, nil
end end
parser.on "check", "Check permissions." do parser.on "check", "Check permissions." do
@ -241,7 +258,7 @@ END
opt_authd_login.call parser opt_authd_login.call parser
opt_help.call parser opt_help.call parser
# userid application resource # userid application resource
unrecognized_args_to_context_args.call parser, 3 unrecognized_args_to_context_args.call parser, 3, nil
end end
end end

View file

@ -1,3 +1,4 @@
require "baguette-crystal-base"
require "option_parser" require "option_parser"
require "yaml" require "yaml"
require "./authd.cr" require "./authd.cr"
@ -242,11 +243,12 @@ class Actions
def user_deletion def user_deletion
args = Context.args.not_nil! args = Context.args.not_nil!
userid = args[0].to_u32 args.each do |u|
Baguette::Log.info "Removing user #{u}"
res = authd.delete userid userid = u.to_u32
res = authd.delete userid
puts res puts res
end
end end
def user_validation def user_validation
@ -256,18 +258,21 @@ class Actions
end end
def user_search def user_search
args = Context.args.not_nil! args = Context.args.not_nil!
login = args[0] args.each do |login|
pp! authd.search_user login pp! authd.search_user login
end
end end
def user_get def user_get
args = Context.args.not_nil! args = Context.args.not_nil!
login = args[0] args.each do |login|
pp! authd.get_user? login pp! authd.get_user? login
end
end end
def user_recovery def user_recovery
args = Context.args.not_nil! args = Context.args.not_nil!
login = args[0] args.each do |login|
pp! authd.ask_password_recovery login pp! authd.ask_password_recovery login
end
end end
def permission_check def permission_check
@ -305,6 +310,26 @@ def main
# Authd connection. # Authd connection.
authd = AuthD::Client.new authd = AuthD::Client.new
# Read configuration.
simulation, no_configuration, configuration_file = Baguette::Configuration.option_parser
# Authd configuration.
authentication_config = if no_configuration
Baguette::Log.info "do not load a configuration file."
Baguette::Configuration::Auth.new
else
# Configuration file is for dnsmanagerd.
Baguette::Configuration::Auth.get || Baguette::Configuration::Auth.new
end
# FIXME I guess: why isn't this working?
#Baguette::Configuration.verbosity = authentication_config.verbosity
if key_file = authentication_config.secret_key_file
authentication_config.secret_key = File.read(key_file).chomp
end
# TODO: when I have the time, clean up this redundant piece of code. In the meantime, it works.
if login = Context.authd_login if login = Context.authd_login
pass = if p = Context.authd_pass pass = if p = Context.authd_pass
p p
@ -317,11 +342,28 @@ def main
case response case response
when Response::Login when Response::Login
uid = response.uid uid = response.uid
token = response.token #token = response.token
Baguette::Log.info "Authenticated as #{login} #{uid}, token: #{token}" Baguette::Log.info "Authenticated as #{login} #{uid}"
else else
raise "Cannot authenticate to authd with login #{login}: #{response}." raise "Cannot authenticate to authd with login #{login}: #{response}."
end end
else
Baguette::Log.info "no authd login from CLI."
if authentication_config.login.nil? || authentication_config.pass.nil?
Baguette::Log.info "no authd login from configuration either."
else
login = authentication_config.login.not_nil!
pass = authentication_config.pass.not_nil!
response = authd.login? login, pass
case response
when Response::Login
uid = response.uid
#token = response.token
Baguette::Log.info "Authenticated as #{login} #{uid}"
else
raise "Cannot authenticate to authd with login #{login}: #{response}."
end
end
end end
actions = Actions.new authd actions = Actions.new authd