more token entropy

This commit is contained in:
Harlan T Wood 2016-03-13 00:20:18 -08:00
parent 4f76d3914d
commit 3fbb3d1dc9
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