touth 1.1.2 → 1.2.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: b5c9606601957745782f3a95f89890c4e04158f3
4
- data.tar.gz: ae2ba2ffb33fc6123c1845d3a8f037bc969e6377
3
+ metadata.gz: 688b4aa6d17d873748b0e566d30fe1a5346bf1dc
4
+ data.tar.gz: 98a98a22385c71a83f5e2f1f41a34aa639156ea3
5
5
  SHA512:
6
- metadata.gz: f0a2521d1fe43298dda2b6d18bde9029e89618d24ae9c2663494668b4c33be6cb7e828a101b3860c8d0b5e8774b49bac84d9c46152db19aee2442389433f645f
7
- data.tar.gz: ff8ce766e8ff563746aec8a1307283793c0950a21ac68caade746eb827de1106bad7f1c383aad89f06aa287d0a85b02720ca4fc1d5f7bdfec9049b4a9a5dbabf
6
+ metadata.gz: ade3801f8ed0bb3b8bc29edc1d56c8f58724b96e1a561edc5233491eecf517845d46c6c7f63c6eee9f06a1bb3a09e72223b693b3ba8014fa57d01b73f5b0cdfb
7
+ data.tar.gz: e83ff2c7a4249a9709b99c5f4563cf93ed6c47a43d945859cef0d6ebc5852ff8a6da55f41c1e2bb64ce1dc5c6434741d444249229e9300161c67db507275d3af
@@ -0,0 +1 @@
1
+ 2.1.2
data/README.md CHANGED
@@ -23,7 +23,7 @@ gem 'touth'
23
23
  ```ruby
24
24
  class UserAccount < ActiveRecord::Base
25
25
 
26
- acts_as_token_authenticatable
26
+ has_access_token
27
27
 
28
28
  end
29
29
  ```
@@ -45,6 +45,10 @@ module Touth
45
45
  OpenSSL::HMAC.digest @digest_method, self.client_secret_key, data
46
46
  end
47
47
 
48
+ def get_resource_name(name)
49
+ name.to_s.gsub('::', '_').underscore
50
+ end
51
+
48
52
  def method_missing(method_name, *args, &block)
49
53
  if @config.respond_to? method_name
50
54
  @config.send method_name, *args, &block
@@ -59,10 +63,6 @@ module Touth
59
63
 
60
64
  end
61
65
 
62
- def self.get_resource_name(name)
63
- name.to_s.gsub('::', '_').underscore
64
- end
65
-
66
66
  end
67
67
 
68
68
 
@@ -2,24 +2,28 @@ module Touth
2
2
  module ActionControllerSupport
3
3
  module ClassMethods
4
4
 
5
- mattr_accessor :access_token_resources
5
+ mattr_accessor :token_authorized_resources
6
6
 
7
7
  def token_authentication_for(resource_name)
8
- resource_name = Touth.get_resource_name resource_name
9
-
10
- self.access_token_resources ||= {}
11
-
12
- define_method "#{resource_name}_signed_in?" do
13
- !!self.class.access_token_resources[resource_name]
14
- end
8
+ self.token_authorized_resources ||= {}
15
9
 
16
- define_method "current_#{resource_name}" do
17
- self.class.access_token_resources[resource_name]
10
+ unless @_init_token_authenticator_hook
11
+ prepend_before_action :set_token_authorized_resource!
12
+ @_init_token_authenticator_hook = true
18
13
  end
19
14
 
15
+ resource_name = Touth.get_resource_name resource_name
20
16
  callback_name = "authenticate_#{resource_name}!".to_sym
21
17
 
22
18
  unless method_defined? callback_name
19
+ define_method "#{resource_name}_signed_in?" do
20
+ !!self.class.token_authorized_resources[resource_name]
21
+ end
22
+
23
+ define_method "current_#{resource_name}" do
24
+ self.class.token_authorized_resources[resource_name]
25
+ end
26
+
23
27
  define_method callback_name do
24
28
  authenticate_token_for! resource_name
25
29
  end
@@ -35,35 +39,25 @@ module Touth
35
39
 
36
40
  protected
37
41
 
38
- def authenticate_token_for!(resource_name)
39
- if Touth.allow_raise
40
- set_token_authorized_model resource_name
41
- else
42
- begin
43
- set_token_authorized_model resource_name
44
- rescue
45
- return unauthorized_token_error
46
- end
47
- end
48
- end
49
-
50
- def set_token_authorized_model(resource_name)
51
- resource_name = Touth.get_resource_name resource_name
52
-
42
+ def set_token_authorized_resource!
53
43
  token = request.headers[Touth.header_name]
54
44
 
55
- unless token && Authenticator.valid_access_token?(token)
56
- raise InvalidAccessTokenError, 'access token is not valid'
57
- end
45
+ return unless token && Authenticator.valid_access_token?(token)
58
46
 
59
- model = Authenticator.get_model token
60
- model_name = Touth.get_resource_name model.class.name
47
+ resource = Authenticator.get_resource token
48
+ resource_name = Touth.get_resource_name resource.class.name
61
49
 
62
- unless model_name == resource_name
63
- raise ResourceConflictError, 'attempted %s to login, expected %s' % [model, resource_name]
64
- end
50
+ self.class.token_authorized_resources[resource_name] = resource
51
+ end
65
52
 
66
- self.class.access_token_resources[model_name] = model
53
+ def authenticate_token_for!(resource_name)
54
+ unless self.class.token_authorized_resources[resource_name]
55
+ if Touth.allow_raise
56
+ raise InvalidAccessTokenError, 'access token is not valid'
57
+ else
58
+ return unauthorized_token_error
59
+ end
60
+ end
67
61
  end
68
62
 
69
63
  def unauthorized_token_error
@@ -2,7 +2,7 @@ module Touth
2
2
  module ActiveRecordSupport
3
3
  module ClassMethods
4
4
 
5
- def acts_as_token_authenticatable
5
+ def has_access_token
6
6
  include Touth::ActiveRecordSupport::InstanceMethods
7
7
  end
8
8
 
@@ -15,7 +15,7 @@ module Touth
15
15
  end
16
16
 
17
17
  def valid_access_token?(token)
18
- Authenticator.get_model(token) == self
18
+ Authenticator.get_resource(token) == self
19
19
  end
20
20
 
21
21
  end
@@ -3,17 +3,17 @@ module Touth
3
3
 
4
4
  module_function
5
5
 
6
- def issue_access_token(model, lifetime = Touth.access_token_lifetime)
6
+ def issue_access_token(resource, lifetime = Touth.access_token_lifetime)
7
7
  expires_at = Time.now.to_i + lifetime
8
8
 
9
9
  data = Marshal.dump([
10
- model.class,
11
- model.id,
10
+ resource.class,
11
+ resource.id,
12
12
  expires_at,
13
13
  ])
14
14
 
15
15
  data_sign = Touth.digest data
16
- data_key = gen_data_key model, data_sign
16
+ data_key = gen_data_key resource, data_sign
17
17
 
18
18
  [
19
19
  data_sign,
@@ -23,25 +23,27 @@ module Touth
23
23
  end
24
24
 
25
25
  def valid_access_token?(token)
26
- !!get_model(token)
26
+ !!get_resource(token)
27
27
  end
28
28
 
29
- def get_model(token)
29
+ def get_resource(token)
30
30
  @access_token_data_cache ||= {}
31
- model = @access_token_data_cache[token]
31
+ resource = @access_token_data_cache[token]
32
32
 
33
- return model if model
33
+ return resource if resource
34
+
35
+ @access_token_data_cache[token] = nil
34
36
 
35
37
  begin
36
38
  data_sign, data_key, data = [token].pack('H*').unpack 'A32A32A*'
37
39
 
38
40
  if data_sign == Touth.digest(data)
39
- model_class, id, expires_at = Marshal.load data
41
+ resource_class, id, expires_at = Marshal.load data
40
42
 
41
- model = model_class.find id
43
+ resource = resource_class.find id
42
44
 
43
- if gen_data_key(model, data_sign) == data_key && Time.now.to_i < expires_at
44
- @access_token_data_cache[token] = model
45
+ if gen_data_key(resource, data_sign) == data_key && Time.now.to_i < expires_at
46
+ @access_token_data_cache[token] = resource
45
47
  end
46
48
  end
47
49
  rescue
@@ -49,8 +51,8 @@ module Touth
49
51
  end
50
52
  end
51
53
 
52
- def gen_data_key(model, data_sign)
53
- Touth.digest [data_sign, model.send(Touth.password_field)].join
54
+ def gen_data_key(resource, data_sign)
55
+ Touth.digest [data_sign, resource.send(Touth.password_field)].join
54
56
  end
55
57
 
56
58
  end
@@ -1,3 +1,3 @@
1
1
  module Touth
2
- VERSION = '1.1.2'
2
+ VERSION = '1.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: touth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuki Iwanaga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-23 00:00:00.000000000 Z
11
+ date: 2014-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -66,6 +66,7 @@ extensions: []
66
66
  extra_rdoc_files: []
67
67
  files:
68
68
  - ".gitignore"
69
+ - ".ruby-version"
69
70
  - Gemfile
70
71
  - LICENSE
71
72
  - README.md