Added LDAP user exists checks in create_user
Some checks reported errors
continuous-integration/drone/push Build encountered an error
continuous-integration/drone/pr Build encountered an error
continuous-integration/drone Build is passing

This commit is contained in:
Dominic Grimm 2022-02-06 18:42:26 +01:00
parent 83367687a8
commit 1780a1d0b7
2 changed files with 5 additions and 29 deletions

View file

@ -1,4 +1,3 @@
require "crystal-argon2"
require "jwt"
module Backend
@ -8,16 +7,6 @@ module Backend
BEARER = "Bearer "
def hash_password(password : String) : String
Argon2::Password.create(password)
end
# def verify_password?(password : String, hash : String) : Bool
# !!Argon2::Password.verify_password(password, hash)
# rescue
# false
# end
private def create_jwt(data, expiration : Int) : String
JWT.encode({"data" => data.to_h, "exp" => expiration}, Backend.config.api.jwt_secret, JWT::Algorithm::HS256)
end

View file

@ -1,5 +1,4 @@
require "ldap"
require "socket"
module Backend
module Api
@ -19,27 +18,15 @@ module Backend
)
end
# -> LDAP server
# @[GraphQL::Field]
# def update_password(context : Context, password : String) : LoginPayload
# context.authenticated!
# if Auth.verify_password?(password, context.user.not_nil!.password)
# raise "New password must be different from old password"
# end
# context.user.not_nil!.update!(password: Auth.hash_password(password))
# LoginPayload.new(
# user: User.new(context.user.not_nil!),
# token: Auth.create_user_jwt(context.user.not_nil!.id.not_nil!.to_i),
# )
# end
@[GraphQL::Field]
def create_user(context : Context, input : UserCreateInput) : User
context.admin!
raise "LDAP user does not exist" unless begin
!!Ldap.user(Ldap.uid(input.username))
rescue LDAP::Client::AuthError
false
end
user = Db::User.create!(username: input.username, role: input.role.to_s)
User.new(user)