class StandardFile::AbstractUserManager

Constants

DEFAULT_COST

Public Class Methods

new(user_class) click to toggle source
# File lib/standard_file/abstract/user_manager.rb, line 4
def initialize(user_class)
  @user_class = user_class
end

Public Instance Methods

auth_params(email) click to toggle source
# File lib/standard_file/abstract/user_manager.rb, line 39
def auth_params(email)
  user = @user_class.find_by_email(email)

  if !user
    return nil
  end

  auth_params = {
    :identifier => user.email,
    :pw_cost => user.pw_cost,
    :pw_nonce => user.pw_nonce,
    :version => user.version
  }

  if user.pw_salt
    #v002 only
    auth_params[:pw_salt] = user.pw_salt
  end

  if user.pw_func
    #v001 only
    auth_params[:pw_func] = user.pw_func
    auth_params[:pw_alg] = user.pw_alg
    auth_params[:pw_key_size] = user.pw_key_size
  end

  return auth_params
end
change_pw(user, password, params) click to toggle source
# File lib/standard_file/abstract/user_manager.rb, line 28
def change_pw(user, password, params)
  user.encrypted_password = hash_password(password)
  user.update!(registration_params(params))
  return { user: user, token: jwt(user) }
end
register(email, password, params) click to toggle source
# File lib/standard_file/abstract/user_manager.rb, line 17
def register(email, password, params)
  user = @user_class.find_by_email(email)
  if user
    return {:error => {:message => "This email is already registered.", :status => 401}}
  else
    user = @user_class.new(:email => email, :encrypted_password => hash_password(password))
    user.update!(registration_params(params))
    return { user: user, token: jwt(user) }
  end
end
sign_in(email, password) click to toggle source
# File lib/standard_file/abstract/user_manager.rb, line 8
def sign_in(email, password)
  user = @user_class.find_by_email(email)
  if user and test_password(password, user.encrypted_password)
    return { user: user, token: jwt(user) }
  else
    return {:error => {:message => "Invalid email or password.", :status => 401}}
  end
end
update(user, params) click to toggle source
# File lib/standard_file/abstract/user_manager.rb, line 34
def update(user, params)
  user.update!(registration_params(params))
  return { user: user, token: jwt(user) }
end

Private Instance Methods

hash_password(password) click to toggle source
# File lib/standard_file/abstract/user_manager.rb, line 74
def hash_password(password)
  BCrypt::Password.create(password, cost: DEFAULT_COST).to_s
end
jwt(user) click to toggle source
# File lib/standard_file/abstract/user_manager.rb, line 84
def jwt(user)
  JwtHelper.encode({:user_uuid => user.uuid, :pw_hash => Digest::SHA256.hexdigest(user.encrypted_password)})
end
registration_params(params) click to toggle source
# File lib/standard_file/abstract/user_manager.rb, line 88
def registration_params(params)
  params.permit(:pw_func, :pw_alg, :pw_cost, :pw_key_size, :pw_nonce, :pw_salt, :version)
end
test_password(password, hash) click to toggle source
# File lib/standard_file/abstract/user_manager.rb, line 78
def test_password(password, hash)
  bcrypt = BCrypt::Password.new(hash)
  password = BCrypt::Engine.hash_secret(password, bcrypt.salt)
  return ActiveSupport::SecurityUtils.secure_compare(password, hash)
end