Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SystemStackError if counter column is named otp_counter #26

Closed
garethrees opened this issue Oct 6, 2015 · 2 comments
Closed

SystemStackError if counter column is named otp_counter #26

garethrees opened this issue Oct 6, 2015 · 2 comments

Comments

@garethrees
Copy link
Contributor

Followed the Counter based OTP guide.

$ rails g migration AddOtpSecretKeyToUsers otp_secret_key:string
$ rails g migration AddCounterForOtpToUsers otp_counter:integer
$ rails g migration AddCounterForOtpToUsers my_otp_counter:integer
# Set the default value to `1` manually
$ rake db:migrate
class User < ActiveRecord::Base
  has_one_time_password :counter_based => true
end
u = User.new
# => #<User id: nil, email: nil, otp_counter: 1, otp_secret_key: nil, created_at: nil, updated_at: nil, my_otp_counter: 1>
u.otp_counter
SystemStackError: stack level too deep
        from /usr/local/rbenv/versions/2.0.0-p481/lib/ruby/2.0.0/irb/workspace.rb:86
Maybe IRB bug!
class User < ActiveRecord::Base
  has_one_time_password :counter_based => true, :counter_column_name => :my_otp_counter
end
u = User.new
# => #<User id: nil, email: nil, otp_counter: 1, otp_secret_key: nil, created_at: nil, updated_at: nil, my_otp_counter: 1>
u.otp_counter
# => 1

Not sure why the same problem isn't exhibited by the specs, but the following seems to fix the problem:

module ActiveModel
  module OneTimePassword
    module InstanceMethodsOnActivation
      def otp_counter
        if self.class.otp_counter_column_name != 'otp_counter'
          self.public_send(self.class.otp_counter_column_name)
        else
          super
        end
      end

      def otp_counter=(attr)
        if self.class.otp_counter_column_name != 'otp_counter'
          self.public_send("#{self.class.otp_counter_column_name}=", attr)
        else
          super
        end
      end
    end
  end
end
garethrees added a commit to garethrees/active_model_otp that referenced this issue Oct 6, 2015
@garethrees
Copy link
Contributor Author

I've added a test case to reproduce this garethrees@05baccf

@skunkworker
Copy link

+1 for this, the fix makes it work as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants