Client: code simplification. Token-handler: works.

master
Philippe Pittoli 2024-03-14 04:36:03 +01:00
parent 7786f1d3b8
commit b3aa91fd07
7 changed files with 76 additions and 59 deletions

6
src/client.cr Normal file
View File

@ -0,0 +1,6 @@
require "authd"
require "ipc"
require "./storage.cr"
require "./network.cr"
require "./lib"
require "./client/lib/*"

View File

@ -1,16 +0,0 @@
def authd_get_token(login : String? = nil, pass : String? = nil)
authd = AuthD::Client.new
response = authd.login? login, pass
case response
when Response::Login
authd.close
uid = response.uid
token = response.token
Baguette::Log.info "Authenticated as #{login} #{uid}, token: #{token}"
return token
end
authd.close
raise "Cannot authenticate to authd with login #{login}: #{response}."
end

View File

@ -1,13 +1,11 @@
require "../../requests/*"
class DNSManager::Client < IPC
property server_fd : Int32 = -1
property server_fd : Int32 = -1
def initialize
super()
fd = self.connect "dnsmanager"
if fd.nil?
raise "couldn't connect to 'auth' IPC service"
raise "couldn't connect to 'dnsmanager' IPC service"
end
@server_fd = fd
end
@ -16,10 +14,33 @@ class DNSManager::Client < IPC
# Simple users.
#
def authd_get_token(login : String? = nil, pass : String? = nil)
authd = AuthD::Client.new
response = authd.login? login, pass
case response
when AuthD::Response::Login
authd.close
uid = response.uid
token = response.token
Baguette::Log.info "Authenticated as #{login} #{uid}, token: #{token}"
return token
end
authd.close
raise "Cannot authenticate to authd with login #{login}: #{response}."
end
def login(login : String, pass : String)
# Authd authentication, get the token and quit right away.
token = authd_get_token login: login, pass: pass
login token
end
def login(token : String)
request = Request::Login.new token
send_now request
parse_message [ Response::Logged ], read
response = parse_message [ Response::Logged ], read
raise "cannot connect to dnsmanagerd" unless response.class == Response::Logged
response
end
#
@ -123,6 +144,14 @@ class DNSManager::Client < IPC
parse_message [ Response::Success ], read
end
def use_token(token_uuid : String, address : String)
request = Request::UseToken.new token_uuid, address
send_now request
response = parse_message [ Response::Success ], read
raise "token update failed" unless response.class == Response::Success
response
end
#
# Utils
#

View File

@ -1,16 +1,10 @@
require "authd"
require "ipc"
require "../network.cr"
require "../storage.cr"
require "yaml"
require "baguette-crystal-base"
require "../client.cr"
require "../config"
require "./lib/*"
require "../lib"
class Context
class_property command = "not-implemented"
class_property args : Array(String)? = nil
@ -256,27 +250,19 @@ def main
if authd_config.login.nil? || authd_config.pass.nil?
Baguette::Log.info "no authd login."
else
Baguette::Log.info "authd login..."
login = authd_config.login.not_nil!
pass = authd_config.pass.not_nil!
# Authd authentication, get the token and quit right away.
token = authd_get_token login: login, pass: pass
# Then push the token to the dnsmanager daemon.
logged_message = dnsmanagerd.login token
case logged_message
when DNSManager::Response::Logged
Baguette::Log.info "logged to dnsmanagerd as '#{login}'"
Baguette::Log.debug "from logging message, accepted domains:"
logged_message.accepted_domains.each do |d|
Baguette::Log.debug "- #{d}"
end
Baguette::Log.debug "from logging message, owned domains:"
logged_message.my_domains.each do |d|
Baguette::Log.debug "- #{d}"
end
else
Baguette::Log.info "not logged to dnsmanagerd?"
end
#logged_message = dnsmanagerd.login login, pass
dnsmanagerd.login login, pass
Baguette::Log.info "logged to dnsmanagerd as '#{login}'"
#Baguette::Log.debug "from logging message, accepted domains:"
#logged_message.accepted_domains.each do |d|
# Baguette::Log.debug "- #{d}"
#end
#Baguette::Log.debug "from logging message, owned domains:"
#logged_message.my_domains.each do |d|
# Baguette::Log.debug "- #{d}"
#end
end
actions = Actions.new dnsmanagerd, config

View File

@ -25,10 +25,7 @@ class DNSManager::Request
end
def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON
user = dnsmanagerd.get_logged_user event
return Response::ErrorUserNotLogged.new unless user
return Response::InsufficientRights.new unless user.admin
dnsmanagerd.storage.use_token user.uid, @token, @address
dnsmanagerd.storage.use_token @token, @address
end
end
DNSManager.requests << UseToken

View File

@ -333,10 +333,7 @@ class DNSManager::Storage
token
end
def use_token(user_id : Int32, token_uuid : String, address : String) : IPC::JSON
puts "use token #{token_uuid} address #{address}"
user_data = user_must_be_admin! user_id
def use_token(token_uuid : String, address : String) : IPC::JSON
token = token_must_exist! token_uuid
zone = zone_must_exist! token.domain
rr = zone.rr_must_exist! token.rrid

View File

@ -1,24 +1,42 @@
require "authd"
require "ipc"
require "../src/client.cr"
require "http/server"
port = ARGV[0].to_i rescue 8080
addr = ARGV[1] rescue "127.0.0.1"
# TODO: connect to `dnsmanagerd`.
#login = ENV["LOGIN"]
#pass = ENV["PASS"]
dnsmanagerd = DNSManager::Client.new
#dnsmanagerd.login login, pass
server = HTTP::Server.new do |context|
pp! context.request.remote_address
ip = case remote = context.request.remote_address
when Socket::IPAddress
remote.address
else
""
end
case context.request.path
when /^\/token-update\/(?<token>[a-z-]+)/
when /^\/token-update\/(?<token>[a-z0-9-]+)/
token = $~["token"]
if token.nil?
context.response.status_code = 404
context.response.print "invalid token"
else
context.response.content_type = "text/html"
context.response.print "coucou, token: #{token}"
# TODO: send update to `dnsmanagerd`.
context.response.print "coucou, token: #{token} from #{ip}"
begin
dnsmanagerd.use_token token, ip
context.response.print "; Success!" # 🔻
rescue e
puts "error while updating token: #{e}"
context.response.print "; The command failed!" # ⚠
end
end
else
context.response.status_code = 404