diff --git a/shard.yml b/shard.yml index 96b2d16..037e0f3 100644 --- a/shard.yml +++ b/shard.yml @@ -11,6 +11,18 @@ description: | targets: authd: main: src/main.cr + auth-user-add: + main: utils/authd-user-add.cr + auth-user-allow: + main: utils/authd-user-allow.cr + auth-user-ask-for-new-password: + main: utils/authd-user-ask-for-new-password.cr + auth-user-get: + main: utils/authd-user-get.cr + auth-user-mod: + main: utils/authd-user-mod.cr + auth-user-validate: + main: utils/authd-user-validate.cr crystal: 0.31 diff --git a/src/authd.cr b/src/authd.cr index a68126e..a185a0f 100644 --- a/src/authd.cr +++ b/src/authd.cr @@ -9,6 +9,15 @@ require "./user.cr" class AuthD::Exception < Exception end +class AuthD::MalformedRequest < Exception + getter ipc_type : Int32 + getter payload : String + + def initialize(@ipc_type, @payload) + @message = "malformed payload" + end +end + class AuthD::Response include JSON::Serializable @@ -145,9 +154,11 @@ class AuthD::Response payload = String.new message.payload type = Type.new message.utype.to_i - requests.find(&.type.==(type)).try &.from_json(payload) - rescue e : JSON::ParseException - raise Exception.new "malformed request" + begin + requests.find(&.type.==(type)).try &.from_json(payload) + rescue e : JSON::ParseException + raise MalformedRequest.new message.utype.to_i, payload + end end end @@ -334,9 +345,11 @@ class AuthD::Request payload = String.new message.payload type = Type.new message.utype.to_i - requests.find(&.type.==(type)).try &.from_json(payload) - rescue e : JSON::ParseException - raise Exception.new "malformed request: #{e}" + begin + requests.find(&.type.==(type)).try &.from_json(payload) + rescue e : JSON::ParseException + raise MalformedRequest.new message.utype.to_i, payload + end end end diff --git a/src/main.cr b/src/main.cr index ec1645d..1f02eb5 100644 --- a/src/main.cr +++ b/src/main.cr @@ -1,6 +1,7 @@ require "uuid" require "option_parser" require "openssl" +require "colorize" require "jwt" require "ipc" @@ -50,7 +51,11 @@ class AuthD::Service def handle_request(request : AuthD::Request?, connection : IPC::Connection) case request when Request::GetToken - user = @users_per_login.get request.login + begin + user = @users_per_login.get request.login + rescue e : DODB::MissingEntry + return Response::Error.new "invalid credentials" + end if user.password_hash != hash_password request.password return Response::Error.new "invalid credentials" @@ -391,6 +396,14 @@ class AuthD::Service @users_per_uid.get? token_payload.uid.to_s end + def info(message) + STDOUT << ":: ".colorize(:green) << message.colorize(:white) << "\n" + end + + def error(message) + STDOUT << "!! ".colorize(:red) << message.colorize(:red) << "\n" + end + def run ## # Provides a JWT-based authentication scheme for service-specific users. @@ -406,12 +419,22 @@ class AuthD::Service begin request = Request.from_ipc event.message + info "<< #{request.class.name.sub /^Request::/, ""}" + response = handle_request request, event.connection event.connection.send response + rescue e : MalformedRequest + error "#{e.message}" + error " .. type was: #{e.ipc_type}" + error " .. payload was: #{e.payload}" + response = Response::Error.new e.message rescue e - STDERR.puts "error: #{e.message}" + error "#{e.message}" + response = Response::Error.new e.message end + + info ">> #{response.class.name.sub /^Response::/, ""}" end end end