Merge pull request #484 from harlantwood/token-entropy
more token entropy
This commit is contained in:
commit
e0b2991596
2 changed files with 19 additions and 3 deletions
|
@ -1,11 +1,22 @@
|
||||||
class Token < ActiveRecord::Base
|
class Token < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
before_create :generate_token
|
before_create :assign_token
|
||||||
|
|
||||||
|
CHARS = 32
|
||||||
|
|
||||||
private
|
private
|
||||||
|
def assign_token
|
||||||
|
self.token = generate_token
|
||||||
|
end
|
||||||
|
|
||||||
def generate_token
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe Token, type: :model do
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue