console1984 icon indicating copy to clipboard operation
console1984 copied to clipboard

Add support for customizable user authentication

Open olivier-thatch opened this issue 8 months ago • 1 comments

Add support for customizable user authentication via a new user_authentication configuration value.

When set, the value should be a proc or other callable object. The proc will receive the username as its sole argument, and should raise an exception if authentication fails.

For example:

    config.console1984.user_authentication = ->(username) do
      user = User.find_by(username: username)
      password = $stdin.getpass("Password: ")

      if user.nil? || !user.valid_password?(password) || !user.is_admin?
        # maybe log the attempt somehow?
        raise "Authentication failed"
      end
    end

Closes #103.

@jorgemanrubia Let me know what you think of this approach! I gave it a try in our actual Rails app with a variant of the example proc above, and AFAICT it works fine. Raising an exception is a bit rough because when authentication fails it prints the whole backtrace, but that's already the case when the gem raises Console1984::Errors::MissingUsername so 🤷

olivier-thatch avatar Jun 26 '24 21:06 olivier-thatch