acts_as_tokenable 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 20f9b171ded1cb0f490e9d361ea3fbb6a961d485
4
- data.tar.gz: cc83021bc9292f12b2e2c710ff53f48ff120c32d
3
+ metadata.gz: 6ff886775475e5a6aa7a916ebd039015bfd5d06d
4
+ data.tar.gz: dd441d8e815f55a4b4e0845b2969c9a108e01e43
5
5
  SHA512:
6
- metadata.gz: da12ff38e37d095d08141c1d327b1e877a7d18bed0363a3583ec7ee23422fb91d69d9ec79b24b35f422f33dfb778bdd06de87f8c9bbfa9e5a4dade4c834735fd
7
- data.tar.gz: c1411d46553033b774bc92b228146db07a607480d3a2b17cd302a9e1fa3466ff3bd80a4ae87ff6b1939b3a3578c1282eb652561602a90f9a2d480e0007f5a3a3
6
+ metadata.gz: 21b3021fb3d0ac2eabe30498fe25655f72e4bca6a172fe36d8cdafb55ce322d074390083b38627b613088a4e85c85711c88380c65a5d56e4cc449cfdba0b506a
7
+ data.tar.gz: 17371a358ade274565b7bee453711107de0331951a92254e9b80febb86fd33d713c4ee225bc5176b38e1df24fac4dd57714ae00f61e035157a547e0ed1ff6442
@@ -1,19 +1,35 @@
1
+ require 'securerandom'
2
+ require 'bcrypt'
3
+
1
4
  module ActsAsTokenable
2
5
  class Token < ActiveRecord::Base
3
6
  before_update { false }
4
7
 
5
8
  belongs_to :tokenable, polymorphic: true
6
9
 
10
+ validates :token_id, presence: true, uniqueness: true
11
+ validates :token_hash, presence: true
12
+
7
13
  scope :active, -> { where('expires_at > ?', DateTime.now) }
8
14
 
9
- before_create :generate_token
15
+ before_validation :generate_token_id
16
+ before_validation :generate_token_hash
10
17
 
11
18
  private
12
19
 
13
- def generate_token
20
+ def generate_token_id
14
21
  begin
15
- self.token = SecureRandom.urlsafe_base64(32, false)
16
- end while self.class.exists?(token: self.token)
22
+ self.token_id = SecureRandom.hex(16)
23
+ end while self.class.exists?(token_id: self.token_id)
24
+ end
25
+
26
+ def generate_token_hash
27
+ secret = SecureRandom.urlsafe_base64(16)
28
+ self.token_hash = BCrypt::Password.create(secret, cost: cost)
29
+ end
30
+
31
+ def cost
32
+ Rails.env.test? ? 1 : 10
17
33
  end
18
34
  end
19
35
  end
@@ -2,7 +2,8 @@ class CreateActsAsTokenableTokens < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :acts_as_tokenable_tokens, :id => false do |t|
4
4
  t.belongs_to :tokenable, polymorphic: true, index: true, index: { name: 'acts_as_tokenable_tokens_polymorphic' }
5
- t.string :token, unique: true
5
+ t.string :token_id, unique: true
6
+ t.string :token_hash
6
7
  t.datetime :expires_at
7
8
 
8
9
  t.timestamps null: false
@@ -5,24 +5,32 @@ module ActsAsTokenable
5
5
  has_many :tokens, as: :tokenable, dependent: :destroy, class_name: '::ActsAsTokenable::Token', :foreign_key => :tokenable_id
6
6
 
7
7
  def add_token(options = {})
8
- default_options = {
9
- :expires_at => 30.days.from_now
10
- }
11
-
12
8
  options = options.reverse_merge(default_options)
13
9
 
14
- self.tokens.create(options).try(:token)
15
- end
16
-
17
- def has_token?(token)
18
- self.tokens.active.find_by(:token => token).present?
10
+ self.tokens.create(options)
19
11
  end
20
12
 
21
13
  class << self
22
- def find_by_token(token)
23
- ActsAsTokenable::Token.active.find_by(:token => token).try(:tokenable)
14
+ def find_by_token(token_id, token_hash)
15
+ token = ActsAsTokenable::Token.active.find_by(:token_id => token_id)
16
+
17
+ return nil unless token
18
+
19
+ if ActiveSupport::SecurityUtils.secure_compare(token.token_hash, token_hash)
20
+ token.try(:tokenable)
21
+ else
22
+ nil
23
+ end
24
24
  end
25
25
  end
26
+
27
+ private
28
+
29
+ def default_options
30
+ {
31
+ :expires_at => 30.days.from_now
32
+ }
33
+ end
26
34
  end
27
35
  end
28
36
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsTokenable
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_tokenable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Novikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-09 00:00:00.000000000 Z
11
+ date: 2015-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bcrypt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.1.10
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.1.10
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: sqlite3
29
43
  requirement: !ruby/object:Gem::Requirement