Merge branch 'master' of ssh://git.karchnu.fr:2202/WeirdOS/authd

This commit is contained in:
Philippe PITTOLI 2020-03-23 06:59:42 +01:00
commit 817425de8d
3 changed files with 56 additions and 8 deletions

View File

@ -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

View File

@ -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
begin
requests.find(&.type.==(type)).try &.from_json(payload)
rescue e : JSON::ParseException
raise Exception.new "malformed request"
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
begin
requests.find(&.type.==(type)).try &.from_json(payload)
rescue e : JSON::ParseException
raise Exception.new "malformed request: #{e}"
raise MalformedRequest.new message.utype.to_i, payload
end
end
end

View File

@ -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
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