Merge branch 'master' of ssh://git.karchnu.fr:2202/WeirdOS/authd
This commit is contained in:
commit
817425de8d
12
shard.yml
12
shard.yml
@ -11,6 +11,18 @@ description: |
|
|||||||
targets:
|
targets:
|
||||||
authd:
|
authd:
|
||||||
main: src/main.cr
|
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
|
crystal: 0.31
|
||||||
|
|
||||||
|
25
src/authd.cr
25
src/authd.cr
@ -9,6 +9,15 @@ require "./user.cr"
|
|||||||
class AuthD::Exception < Exception
|
class AuthD::Exception < Exception
|
||||||
end
|
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
|
class AuthD::Response
|
||||||
include JSON::Serializable
|
include JSON::Serializable
|
||||||
|
|
||||||
@ -145,9 +154,11 @@ class AuthD::Response
|
|||||||
payload = String.new message.payload
|
payload = String.new message.payload
|
||||||
type = Type.new message.utype.to_i
|
type = Type.new message.utype.to_i
|
||||||
|
|
||||||
requests.find(&.type.==(type)).try &.from_json(payload)
|
begin
|
||||||
rescue e : JSON::ParseException
|
requests.find(&.type.==(type)).try &.from_json(payload)
|
||||||
raise Exception.new "malformed request"
|
rescue e : JSON::ParseException
|
||||||
|
raise MalformedRequest.new message.utype.to_i, payload
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -334,9 +345,11 @@ class AuthD::Request
|
|||||||
payload = String.new message.payload
|
payload = String.new message.payload
|
||||||
type = Type.new message.utype.to_i
|
type = Type.new message.utype.to_i
|
||||||
|
|
||||||
requests.find(&.type.==(type)).try &.from_json(payload)
|
begin
|
||||||
rescue e : JSON::ParseException
|
requests.find(&.type.==(type)).try &.from_json(payload)
|
||||||
raise Exception.new "malformed request: #{e}"
|
rescue e : JSON::ParseException
|
||||||
|
raise MalformedRequest.new message.utype.to_i, payload
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
27
src/main.cr
27
src/main.cr
@ -1,6 +1,7 @@
|
|||||||
require "uuid"
|
require "uuid"
|
||||||
require "option_parser"
|
require "option_parser"
|
||||||
require "openssl"
|
require "openssl"
|
||||||
|
require "colorize"
|
||||||
|
|
||||||
require "jwt"
|
require "jwt"
|
||||||
require "ipc"
|
require "ipc"
|
||||||
@ -50,7 +51,11 @@ class AuthD::Service
|
|||||||
def handle_request(request : AuthD::Request?, connection : IPC::Connection)
|
def handle_request(request : AuthD::Request?, connection : IPC::Connection)
|
||||||
case request
|
case request
|
||||||
when Request::GetToken
|
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
|
if user.password_hash != hash_password request.password
|
||||||
return Response::Error.new "invalid credentials"
|
return Response::Error.new "invalid credentials"
|
||||||
@ -391,6 +396,14 @@ class AuthD::Service
|
|||||||
@users_per_uid.get? token_payload.uid.to_s
|
@users_per_uid.get? token_payload.uid.to_s
|
||||||
end
|
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
|
def run
|
||||||
##
|
##
|
||||||
# Provides a JWT-based authentication scheme for service-specific users.
|
# Provides a JWT-based authentication scheme for service-specific users.
|
||||||
@ -406,12 +419,22 @@ class AuthD::Service
|
|||||||
begin
|
begin
|
||||||
request = Request.from_ipc event.message
|
request = Request.from_ipc event.message
|
||||||
|
|
||||||
|
info "<< #{request.class.name.sub /^Request::/, ""}"
|
||||||
|
|
||||||
response = handle_request request, event.connection
|
response = handle_request request, event.connection
|
||||||
|
|
||||||
event.connection.send response
|
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
|
rescue e
|
||||||
STDERR.puts "error: #{e.message}"
|
error "#{e.message}"
|
||||||
|
response = Response::Error.new e.message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
info ">> #{response.class.name.sub /^Response::/, ""}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user