Client: code simplification. Token-handler: works.
parent
7786f1d3b8
commit
b3aa91fd07
|
@ -0,0 +1,6 @@
|
||||||
|
require "authd"
|
||||||
|
require "ipc"
|
||||||
|
require "./storage.cr"
|
||||||
|
require "./network.cr"
|
||||||
|
require "./lib"
|
||||||
|
require "./client/lib/*"
|
|
@ -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
|
|
|
@ -1,13 +1,11 @@
|
||||||
require "../../requests/*"
|
|
||||||
|
|
||||||
class DNSManager::Client < IPC
|
class DNSManager::Client < IPC
|
||||||
property server_fd : Int32 = -1
|
property server_fd : Int32 = -1
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
super()
|
super()
|
||||||
fd = self.connect "dnsmanager"
|
fd = self.connect "dnsmanager"
|
||||||
if fd.nil?
|
if fd.nil?
|
||||||
raise "couldn't connect to 'auth' IPC service"
|
raise "couldn't connect to 'dnsmanager' IPC service"
|
||||||
end
|
end
|
||||||
@server_fd = fd
|
@server_fd = fd
|
||||||
end
|
end
|
||||||
|
@ -16,10 +14,33 @@ class DNSManager::Client < IPC
|
||||||
# Simple users.
|
# 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)
|
def login(token : String)
|
||||||
request = Request::Login.new token
|
request = Request::Login.new token
|
||||||
send_now request
|
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
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -123,6 +144,14 @@ class DNSManager::Client < IPC
|
||||||
parse_message [ Response::Success ], read
|
parse_message [ Response::Success ], read
|
||||||
end
|
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
|
# Utils
|
||||||
#
|
#
|
||||||
|
|
|
@ -1,16 +1,10 @@
|
||||||
require "authd"
|
|
||||||
require "ipc"
|
|
||||||
require "../network.cr"
|
|
||||||
require "../storage.cr"
|
|
||||||
require "yaml"
|
require "yaml"
|
||||||
|
|
||||||
require "baguette-crystal-base"
|
require "baguette-crystal-base"
|
||||||
|
|
||||||
|
require "../client.cr"
|
||||||
require "../config"
|
require "../config"
|
||||||
|
|
||||||
require "./lib/*"
|
|
||||||
require "../lib"
|
|
||||||
|
|
||||||
class Context
|
class Context
|
||||||
class_property command = "not-implemented"
|
class_property command = "not-implemented"
|
||||||
class_property args : Array(String)? = nil
|
class_property args : Array(String)? = nil
|
||||||
|
@ -256,27 +250,19 @@ def main
|
||||||
if authd_config.login.nil? || authd_config.pass.nil?
|
if authd_config.login.nil? || authd_config.pass.nil?
|
||||||
Baguette::Log.info "no authd login."
|
Baguette::Log.info "no authd login."
|
||||||
else
|
else
|
||||||
Baguette::Log.info "authd login..."
|
|
||||||
login = authd_config.login.not_nil!
|
login = authd_config.login.not_nil!
|
||||||
pass = authd_config.pass.not_nil!
|
pass = authd_config.pass.not_nil!
|
||||||
# Authd authentication, get the token and quit right away.
|
#logged_message = dnsmanagerd.login login, pass
|
||||||
token = authd_get_token login: login, pass: pass
|
dnsmanagerd.login login, pass
|
||||||
# Then push the token to the dnsmanager daemon.
|
Baguette::Log.info "logged to dnsmanagerd as '#{login}'"
|
||||||
logged_message = dnsmanagerd.login token
|
#Baguette::Log.debug "from logging message, accepted domains:"
|
||||||
case logged_message
|
#logged_message.accepted_domains.each do |d|
|
||||||
when DNSManager::Response::Logged
|
# Baguette::Log.debug "- #{d}"
|
||||||
Baguette::Log.info "logged to dnsmanagerd as '#{login}'"
|
#end
|
||||||
Baguette::Log.debug "from logging message, accepted domains:"
|
#Baguette::Log.debug "from logging message, owned domains:"
|
||||||
logged_message.accepted_domains.each do |d|
|
#logged_message.my_domains.each do |d|
|
||||||
Baguette::Log.debug "- #{d}"
|
# Baguette::Log.debug "- #{d}"
|
||||||
end
|
#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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
actions = Actions.new dnsmanagerd, config
|
actions = Actions.new dnsmanagerd, config
|
||||||
|
|
|
@ -25,10 +25,7 @@ class DNSManager::Request
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON
|
def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON
|
||||||
user = dnsmanagerd.get_logged_user event
|
dnsmanagerd.storage.use_token @token, @address
|
||||||
return Response::ErrorUserNotLogged.new unless user
|
|
||||||
return Response::InsufficientRights.new unless user.admin
|
|
||||||
dnsmanagerd.storage.use_token user.uid, @token, @address
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
DNSManager.requests << UseToken
|
DNSManager.requests << UseToken
|
||||||
|
|
|
@ -333,10 +333,7 @@ class DNSManager::Storage
|
||||||
token
|
token
|
||||||
end
|
end
|
||||||
|
|
||||||
def use_token(user_id : Int32, token_uuid : String, address : String) : IPC::JSON
|
def use_token(token_uuid : String, address : String) : IPC::JSON
|
||||||
puts "use token #{token_uuid} address #{address}"
|
|
||||||
|
|
||||||
user_data = user_must_be_admin! user_id
|
|
||||||
token = token_must_exist! token_uuid
|
token = token_must_exist! token_uuid
|
||||||
zone = zone_must_exist! token.domain
|
zone = zone_must_exist! token.domain
|
||||||
rr = zone.rr_must_exist! token.rrid
|
rr = zone.rr_must_exist! token.rrid
|
||||||
|
|
|
@ -1,24 +1,42 @@
|
||||||
|
require "authd"
|
||||||
require "ipc"
|
require "ipc"
|
||||||
|
require "../src/client.cr"
|
||||||
require "http/server"
|
require "http/server"
|
||||||
|
|
||||||
port = ARGV[0].to_i rescue 8080
|
port = ARGV[0].to_i rescue 8080
|
||||||
addr = ARGV[1] rescue "127.0.0.1"
|
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|
|
server = HTTP::Server.new do |context|
|
||||||
pp! context.request.remote_address
|
pp! context.request.remote_address
|
||||||
|
ip = case remote = context.request.remote_address
|
||||||
|
when Socket::IPAddress
|
||||||
|
remote.address
|
||||||
|
else
|
||||||
|
""
|
||||||
|
end
|
||||||
|
|
||||||
case context.request.path
|
case context.request.path
|
||||||
when /^\/token-update\/(?<token>[a-z-]+)/
|
when /^\/token-update\/(?<token>[a-z0-9-]+)/
|
||||||
token = $~["token"]
|
token = $~["token"]
|
||||||
if token.nil?
|
if token.nil?
|
||||||
context.response.status_code = 404
|
context.response.status_code = 404
|
||||||
context.response.print "invalid token"
|
context.response.print "invalid token"
|
||||||
else
|
else
|
||||||
context.response.content_type = "text/html"
|
context.response.content_type = "text/html"
|
||||||
context.response.print "coucou, token: #{token}"
|
context.response.print "coucou, token: #{token} from #{ip}"
|
||||||
# TODO: send update to `dnsmanagerd`.
|
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
|
end
|
||||||
else
|
else
|
||||||
context.response.status_code = 404
|
context.response.status_code = 404
|
||||||
|
|
Loading…
Reference in New Issue