User -> AuthD::User, authd_user : AuthD::User

This commit is contained in:
Luka Vandervelden 2018-09-22 21:42:21 +02:00
parent db209117f8
commit ee69c365ba
4 changed files with 20 additions and 5 deletions

View File

@ -72,7 +72,7 @@ if user_password.nil?
exit 1
end
user = User.new
user = AuthD::User.new
user.username = user_name
user.password = user_password
user.perms = user_perms

View File

@ -2,8 +2,10 @@
require "kemal"
require "jwt"
require "./user.cr"
class HTTP::Server::Context
property authd_user : Hash(String, JSON::Any)?
property authd_user : AuthD::User?
end
class AuthD::Middleware < Kemal::Handler
@ -26,7 +28,20 @@ class AuthD::Middleware < Kemal::Handler
payload, header = JWT.decode x_token, @key, "HS256"
if payload
context.authd_user = payload
context.authd_user = AuthD::User.new.tap do |u|
u.username = payload["username"].as_s?
u.realname = payload["realname"].as_s?
u.avatar = payload["avatar"].as_s?
u.perms = Array(String).new
payload["perms"].as_a.tap do |perms|
perms.each do |perm|
if perm.class == String
u.perms! << perm.as_s
end
end
end
end
end
end

View File

@ -50,7 +50,7 @@ post "/token" do |env|
next halt env, status_code: 400, response: ({error: "Missing password."}.to_json)
end
user = MyRepo.get_by(User, username: username, password: password)
user = MyRepo.get_by AuthD::User, username: username, password: password
if ! user
next halt env, status_code: 400, response: ({error: "Invalid user or password."}.to_json)

View File

@ -2,7 +2,7 @@
require "pg"
require "crecto"
class User < Crecto::Model
class AuthD::User < Crecto::Model
schema "users" do # table name
field :username, String
field :realname, String