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 +4 -4
- data/.ruby-version +1 -0
- data/README.md +1 -1
- data/lib/touth.rb +4 -4
- data/lib/touth/action_controller_support.rb +28 -34
- data/lib/touth/active_record_support.rb +2 -2
- data/lib/touth/authenticator.rb +16 -14
- data/lib/touth/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 688b4aa6d17d873748b0e566d30fe1a5346bf1dc
|
4
|
+
data.tar.gz: 98a98a22385c71a83f5e2f1f41a34aa639156ea3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ade3801f8ed0bb3b8bc29edc1d56c8f58724b96e1a561edc5233491eecf517845d46c6c7f63c6eee9f06a1bb3a09e72223b693b3ba8014fa57d01b73f5b0cdfb
|
7
|
+
data.tar.gz: e83ff2c7a4249a9709b99c5f4563cf93ed6c47a43d945859cef0d6ebc5852ff8a6da55f41c1e2bb64ce1dc5c6434741d444249229e9300161c67db507275d3af
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.2
|
data/README.md
CHANGED
data/lib/touth.rb
CHANGED
@@ -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 :
|
5
|
+
mattr_accessor :token_authorized_resources
|
6
6
|
|
7
7
|
def token_authentication_for(resource_name)
|
8
|
-
|
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
|
-
|
17
|
-
|
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
|
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
|
-
|
60
|
-
|
47
|
+
resource = Authenticator.get_resource token
|
48
|
+
resource_name = Touth.get_resource_name resource.class.name
|
61
49
|
|
62
|
-
|
63
|
-
|
64
|
-
end
|
50
|
+
self.class.token_authorized_resources[resource_name] = resource
|
51
|
+
end
|
65
52
|
|
66
|
-
|
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
|
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.
|
18
|
+
Authenticator.get_resource(token) == self
|
19
19
|
end
|
20
20
|
|
21
21
|
end
|
data/lib/touth/authenticator.rb
CHANGED
@@ -3,17 +3,17 @@ module Touth
|
|
3
3
|
|
4
4
|
module_function
|
5
5
|
|
6
|
-
def issue_access_token(
|
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
|
-
|
11
|
-
|
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
|
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
|
-
!!
|
26
|
+
!!get_resource(token)
|
27
27
|
end
|
28
28
|
|
29
|
-
def
|
29
|
+
def get_resource(token)
|
30
30
|
@access_token_data_cache ||= {}
|
31
|
-
|
31
|
+
resource = @access_token_data_cache[token]
|
32
32
|
|
33
|
-
return
|
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
|
-
|
41
|
+
resource_class, id, expires_at = Marshal.load data
|
40
42
|
|
41
|
-
|
43
|
+
resource = resource_class.find id
|
42
44
|
|
43
|
-
if gen_data_key(
|
44
|
-
@access_token_data_cache[token] =
|
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(
|
53
|
-
Touth.digest [data_sign,
|
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
|
data/lib/touth/version.rb
CHANGED
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.
|
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-
|
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
|