Merge pull request #484 from harlantwood/token-entropy

more token entropy
This commit is contained in:
Connor Turland 2016-03-14 07:51:37 +11:00
commit e0b2991596
2 changed files with 19 additions and 3 deletions

View file

@ -1,11 +1,22 @@
class Token < ActiveRecord::Base
belongs_to :user
before_create :generate_token
before_create :assign_token
CHARS = 32
private
def assign_token
self.token = generate_token
end
def generate_token
self.token = SecureRandom.uuid.gsub(/\-/,'')
loop do
candidate = SecureRandom.base64(CHARS).gsub(/\W/, '')
if candidate.size >= CHARS
return candidate[0...CHARS]
end
end
end
end

View file

@ -1,5 +1,10 @@
require 'rails_helper'
RSpec.describe Token, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
context "#generate_token" do
subject (:token) { Token.new }
it "should generate an alphanumeric token of 32 characters" do
expect(token.send(:generate_token)).to match /^[a-zA-Z0-9]{32}$/
end
end
end