# Generating Secure Tokens on Your ActiveRecord Models

*Published on May 31, 2023*

Tags: Rails Internals

---


Sometimes, you may want to use token-based authentication to grant your users access to some resource for some time, after which the token expires and needs to be regenerated.

You could manually create a token on your models as follows:

```ruby
class User < ApplicationRecord
  before_create :generate_token
  
  private
  
  def generate_token
    self.token = SecureRandom.base58(24)
  end
end
```

However, a better solution is to use the&nbsp;[`has_secure_token`](https://api.rubyonrails.org/classes/ActiveRecord/SecureToken/ClassMethods.html#method-i-has_secure_token)&nbsp;macro provided by Rails. By default, it will use the name&nbsp;`:token`&nbsp;and length 24, but you can provide a different name and length for your token.

```ruby
class User < ApplicationRecord
  has_secure_token :access_code, length: 30
end
```

Here's a passing test that shows how it works.

```ruby
test 'user has a secure access code which can be regenerated' do
  user = create(:user)
  
  assert user.access_code  # rpWRC...
  
  assert_equal 30, user.access_code.length
  
  user.regenerate_access_code  # J6K9u...
end
```

**Note:**&nbsp;If you're using it for the first time, don't forget to create the backing column for your model.

```ruby
class AddAccessCodeToUser < ActiveRecord::Migration[7.0]
  def change
    add_column :users, :access_code, :string
  end
end
```

For the most part, you can be assured that the generated token will be unique. However, consider adding a unique index in the database just in case, to deal with this highly unlikely scenario.

### Regenerating Tokens

Sometimes, for security reasons, you may want to expire the user's token or access code after some time. This is useful if they haven't been active on your application for a while and you want to make sure the old token isn't compromised.

You can regenerate the token using the&nbsp;`regenerate_{token}`&nbsp;method on your model.

```ruby
def handle_inactive_member(user)
  user.regenerate_access_code
end
```

## Behind the Scenes

If you open the&nbsp;`secure_token.rb`&nbsp;file in the Rails codebase, you'll notice that the `ActiveRecord::SecureToken`&nbsp;module is a&nbsp;[Rails Concern](/how-rails-concerns-work-and-how-to-use-them).

[Concerns in Rails: Everything You Need to Know](/how-rails-concerns-work-and-how-to-use-them)

Here's a simplified version of the source code.

```ruby
# activerecord/lib/active_record/secure_token.rb

module ActiveRecord
  module SecureToken
    extend ActiveSupport::Concern

    module ClassMethods
      def has_secure_token(attribute = :token, length: MIN_LENGTH)
        require "active_support/core_ext/securerandom"
        
        define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_unique_secure_token(length: length) }
        
        before_create { send("#{attribute}=", self.class.generate_unique_secure_token(length: length)) unless send("#{attribute}?") }
      end

      def generate_unique_secure_token(length: MIN_LENGTH)
        SecureRandom.base58(length)
      end
    end
  end
end
```

The very first thing to note is that Rails will only load the&nbsp;[`securerandom`](https://ruby-doc.org/3.2.0/stdlibs/securerandom/SecureRandom.html)&nbsp;library when you actually use this macro. This library is an interface to secure random number generators.

When you call the&nbsp;`has_secure_token`&nbsp;macro, two things happen:

1. It dynamically creates the&nbsp;`regenerate_token`&nbsp;method using the&nbsp;[`define_method`](https://ruby-doc.org/3.2.0/Module.html#method-i-define_method)&nbsp;in Ruby. All it does is update the existing token, setting a new value.
2. Adds a&nbsp;[`before_create`](https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)&nbsp;callback that fires before the model is saved. It calls the&nbsp;`model.token=`&nbsp;method, generating and setting a secure and unique token.

Finally, the&nbsp;`generate_unique_secure_token`&nbsp;method uses the&nbsp;[`SecureRandom`](https://api.rubyonrails.org/classes/SecureRandom.html#method-c-base58)&nbsp;class to generate a random base58 string with the given length.

That's it.

