Skip to content

Commit

Permalink
feat: customisable token lengths
Browse files Browse the repository at this point in the history
This allows the length of the random string used for tokens to be set. By default, 64 (up from 44).
  • Loading branch information
adamcooke committed May 2, 2022
1 parent ed6f138 commit 41431a6
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 4 deletions.
1 change: 0 additions & 1 deletion authie.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Gem::Specification.new do |s|
s.email = ['me@adamcooke.io']

s.add_dependency 'activerecord', '>= 5.0', '< 8.0'
s.add_dependency 'secure_random_string'

s.add_development_dependency 'appraisal', '2.4.1'
s.add_development_dependency 'rails', '>= 5.0', '< 8.0'
Expand Down
2 changes: 2 additions & 0 deletions lib/authie/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ class Config
attr_accessor :persistent_session_length
attr_accessor :sudo_session_timeout
attr_accessor :browser_id_cookie_name
attr_accessor :session_token_length
attr_accessor :events

def initialize
@session_inactivity_timeout = 12.hours
@persistent_session_length = 2.months
@sudo_session_timeout = 10.minutes
@browser_id_cookie_name = :browser_id
@session_token_length = 64
@events = EventManager.new
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/authie/session_model.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'active_record/base'
require 'secure_random_string'
require 'securerandom'
require 'authie/config'

module Authie
Expand Down Expand Up @@ -119,7 +119,7 @@ def shorten_strings
end

def set_new_token
self.temporary_token = SecureRandomString.new(44)
self.temporary_token = SecureRandom.alphanumeric(Authie.config.session_token_length)
self.token_hash = self.class.hash_token(temporary_token)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/session_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
it 'generates a new token' do
session_model.save!
expect(session_model.temporary_token).to be_a String
expect(session_model.temporary_token).to match(/\A[A-Za-z0-9]{44}\z/)
expect(session_model.temporary_token).to match(/\A[A-Za-z0-9]{64}\z/)
end

it 'stores the newly generated token as a SHA256 hash' do
Expand Down

0 comments on commit 41431a6

Please sign in to comment.