From 8275a77576395fbacc25f89e414ab10ab1a3234c Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Sat, 22 Sep 2018 21:23:50 +0200 Subject: [PATCH] Various. - JWT key can be set from command-line (through a file). - User class split to a separate file to allow use by other tools. - Minor style changes. --- src/main.cr | 27 ++++----------------------- src/user.cr | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 src/user.cr diff --git a/src/main.cr b/src/main.cr index d92206b..4c1fea0 100644 --- a/src/main.cr +++ b/src/main.cr @@ -6,6 +6,8 @@ require "jwt" require "pg" require "crecto" +require "./user.cr" + authd_db_name = "authd" authd_db_hostname = "localhost" authd_db_user = "user" @@ -34,27 +36,6 @@ Kemal.config.extra_options do |parser| end end -class User < Crecto::Model - schema "users" do # table name - field :username, String - field :realname, String - field :avatar, String - field :password, String - field :perms, Array(String) - end - - validate_required [:username, :password, :perms] - - def to_h - { - :username => @username, - :realname => @realname, - :perms => @perms, - :avatar => @avatar - } - end -end - post "/token" do |env| env.response.content_type = "application/json" @@ -77,7 +58,7 @@ post "/token" do |env| { "status" => "success", - "token" => JWT.encode(user.to_h, authd_jwt_key, "HS256") + "token" => JWT.encode user.to_h, authd_jwt_key, "HS256" }.to_json end @@ -85,7 +66,7 @@ module MyRepo extend Crecto::Repo end -Kemal.run do +Kemal.run 12051 do MyRepo.config do |conf| conf.adapter = Crecto::Adapters::Postgres conf.hostname = authd_db_hostname diff --git a/src/user.cr b/src/user.cr new file mode 100644 index 0000000..08af4cd --- /dev/null +++ b/src/user.cr @@ -0,0 +1,25 @@ + +require "pg" +require "crecto" + +class User < Crecto::Model + schema "users" do # table name + field :username, String + field :realname, String + field :avatar, String + field :password, String + field :perms, Array(String) + end + + validate_required [:username, :password, :perms] + + def to_h + { + :username => @username, + :realname => @realname, + :perms => @perms, + :avatar => @avatar + } + end +end +