Authd library added to provide an auto-check middleware.

ipc07
Luka Vandervelden 2018-09-22 21:25:03 +02:00
parent a92b94cccb
commit db209117f8
1 changed files with 37 additions and 0 deletions

37
src/authd.cr Normal file
View File

@ -0,0 +1,37 @@
require "kemal"
require "jwt"
class HTTP::Server::Context
property authd_user : Hash(String, JSON::Any)?
end
class AuthD::Middleware < Kemal::Handler
property key : String = ""
@configured = false
@configurator : Proc(Middleware, Nil)
def initialize(&block : Proc(Middleware, Nil))
@configurator = block
end
def call(context)
unless @configured
@configured = true
@configurator.call self
end
context.request.headers["X-Token"]?.try do |x_token|
payload, header = JWT.decode x_token, @key, "HS256"
if payload
context.authd_user = payload
end
end
call_next context
end
end