4

I'm trying to figure out the steps performed by Gitlab, a Ruby on Rails application, to arrive at the authentication token they use.

My knowledge of ruby is basic, and I don't know Ruby on Rails. From what I have figured out, starting with their user.rb model, Gitlab uses a module called Devise to generate the authentication token. Looking at the Devise token_generator.rb module, it seems that the default is a PBKDF2 SHA1 hash with a SHA256 digest, but I also don't know how the configuration file is processed.

The KeyGenerator class is self contained and I am able to duplicate it in another language. But the TokenGenerator class is going to take me a while to figure out without learning some Ruby on Rails:

  class TokenGenerator
    def initialize(key_generator, digest="SHA256")
      @key_generator = key_generator
      @digest = digest
    end

    def digest(klass, column, value)
      value.present? && OpenSSL::HMAC.hexdigest(@digest, key_for(column), value.to_s)
    end

    def generate(klass, column)
      key = key_for(column)

      loop do
        raw = Devise.friendly_token
        enc = OpenSSL::HMAC.hexdigest(@digest, key, raw)
        break [raw, enc] unless klass.to_adapter.find_first({ column => enc })
      end
    end

    private

    def key_for(column)
      @key_generator.generate_key("Devise #{column}")
    end
  end

I realize I'm being lazy and impatient, but I was hoping somebody familiar with Gitlab and Ruby on Rails could help me find out:

  1. Where in this rails app does TokenGenerator.generate get called for the user creation, and what are the default klass and column values being passed to it?
  2. Where is "Devise #{column}" read from, and what is its default value?

In other words, I would like to know what parameters supplied by the users end up as factors to this authentication token. The plain text password? The encrypted password? Just a random value?

Ed I
  • 7,008
  • 3
  • 41
  • 50

1 Answers1

1

Not exactly an answer but a tip to get there. You can try to inspect the state of those methods when they get called using debugger or pry. Here is how to do that in a gem you don't own. Best way to debug third-party gems in ruby

Community
  • 1
  • 1
Ismael Abreu
  • 16,443
  • 6
  • 61
  • 75