User -> AuthD::User, authd_user : AuthD::User
parent
db209117f8
commit
ee69c365ba
|
@ -72,7 +72,7 @@ if user_password.nil?
|
||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
|
|
||||||
user = User.new
|
user = AuthD::User.new
|
||||||
user.username = user_name
|
user.username = user_name
|
||||||
user.password = user_password
|
user.password = user_password
|
||||||
user.perms = user_perms
|
user.perms = user_perms
|
||||||
|
|
19
src/authd.cr
19
src/authd.cr
|
@ -2,8 +2,10 @@
|
||||||
require "kemal"
|
require "kemal"
|
||||||
require "jwt"
|
require "jwt"
|
||||||
|
|
||||||
|
require "./user.cr"
|
||||||
|
|
||||||
class HTTP::Server::Context
|
class HTTP::Server::Context
|
||||||
property authd_user : Hash(String, JSON::Any)?
|
property authd_user : AuthD::User?
|
||||||
end
|
end
|
||||||
|
|
||||||
class AuthD::Middleware < Kemal::Handler
|
class AuthD::Middleware < Kemal::Handler
|
||||||
|
@ -26,7 +28,20 @@ class AuthD::Middleware < Kemal::Handler
|
||||||
payload, header = JWT.decode x_token, @key, "HS256"
|
payload, header = JWT.decode x_token, @key, "HS256"
|
||||||
|
|
||||||
if payload
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ post "/token" do |env|
|
||||||
next halt env, status_code: 400, response: ({error: "Missing password."}.to_json)
|
next halt env, status_code: 400, response: ({error: "Missing password."}.to_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
user = MyRepo.get_by(User, username: username, password: password)
|
user = MyRepo.get_by AuthD::User, username: username, password: password
|
||||||
|
|
||||||
if ! user
|
if ! user
|
||||||
next halt env, status_code: 400, response: ({error: "Invalid user or password."}.to_json)
|
next halt env, status_code: 400, response: ({error: "Invalid user or password."}.to_json)
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
require "pg"
|
require "pg"
|
||||||
require "crecto"
|
require "crecto"
|
||||||
|
|
||||||
class User < Crecto::Model
|
class AuthD::User < Crecto::Model
|
||||||
schema "users" do # table name
|
schema "users" do # table name
|
||||||
field :username, String
|
field :username, String
|
||||||
field :realname, String
|
field :realname, String
|
||||||
|
|
Loading…
Reference in New Issue