warden-oauth2-strategies 0.0.7 → 0.0.8

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: 91693eebe7b261a8fac112ba0eaf0e9326010e01
4
- data.tar.gz: c734102cc67e9483dcff30c616ec736f341bd45f
3
+ metadata.gz: d058d337570bf20c44cf52997b3354f8a91b90f9
4
+ data.tar.gz: 8adef9735a0c873a7654bbefa8be3f4ec97a3aae
5
5
  SHA512:
6
- metadata.gz: ee76e1b6713669d752c8562b3a8c778f92a15e1f7802b146f76fd1de97fcfdd3a0b3d8ed4340abf4f3253dec38c4d27ff1ed38ce95552d7073c06bf22fe3cd04
7
- data.tar.gz: 87f1a4f2ede459483285fafb34173d4c0f735e325bf4561de2080aae7424b560e3e57b8f52c59bbd00895e1fef247801f42c3137e4e2c335d2283f03fd51f7de
6
+ metadata.gz: 70bb686e79781c318eb6fa3b4c2d8f7f46b899c08352747b8eef7b22cd1c9f63ed3a8b5727d9ed82ea8cf271b2c3ed4e8daeae8b0a43f741bfa05494b0d5875c
7
+ data.tar.gz: 27a76dc67de1cc68edd4046cecf00b4fe39eddbf58afaa8f4830ae5ac5041234093913c1c7fb8f57d1bf124de9528b63ecb1415db2c9ef021ba3e7a676fa66d5
data/README.md CHANGED
@@ -20,8 +20,9 @@ class MyAPI < Grape::API
20
20
  config.strategies.add :resource_owner_password_credentials, Warden::OAuth2::Strategies::ResourceOwnerPasswordCredentials
21
21
  config.strategies.add :issuing_access_token, Warden::OAuth2::Strategies::IssuingAccessToken
22
22
  config.strategies.add :accessing_protected_resource, Warden::OAuth2::Strategies::AccessingProtectedResource
23
+ config.strategies.add :refresh_token, Warden::OAuth2::Strategies::RefreshToken
23
24
 
24
- config.default_strategies :client_credentials, :resource_owner_password_credentials, :issuing_access_token
25
+ config.default_strategies :client_credentials, :resource_owner_password_credentials, :refresh_token, :issuing_access_token
25
26
  config.default_strategies :bearer, :accessing_protected_resource
26
27
  config.failure_app Warden::OAuth2::FailureApp
27
28
  end
@@ -54,9 +55,10 @@ end
54
55
  Defaults to `ClientCredentialsApplication`.
55
56
  * **resource_owner_password_credentials_model:** A client application class used for resource owner password authentication. See **Models** below.
56
57
  Defaults to `ResourceOwnerPasswordCredentialsApplication`.
58
+ * **refresh_token_model:** A refresh token application class used for refresh token authentication. See **Models** below. Defaults
59
+ to `RefreshTokenApplication`.
57
60
  * **token_model:** An access token class. See **Models** below. Defaults
58
61
  to `AccessToken`.
59
-
60
62
  ## Models
61
63
 
62
64
  You will need to supply data models to back up the persistent facets of
@@ -106,6 +108,24 @@ class ResourceOwnerPasswordCredentialsApplication
106
108
  end
107
109
  ```
108
110
 
111
+ ### Refresh Token Application
112
+
113
+ ```ruby
114
+ class RefreshTokenApplication
115
+ # REQUIRED
116
+ def self.locate(client_id, client_secret = nil)
117
+ # Should return a refresh token application matching the client_id
118
+ # provided, but should ONLY match client_secret if it is
119
+ # provided.
120
+ # the returned value should implement the following interface
121
+ # def valid?
122
+ # Use options[:refresh_token] to check that specified refresh token is valid
123
+ # end
124
+ end
125
+
126
+ end
127
+ ```
128
+
109
129
  ### Access Token
110
130
 
111
131
  ```ruby
@@ -164,6 +184,13 @@ Use `.valid?` on the client application to determine if user credentials are cor
164
184
 
165
185
  **User:** The Warden user is set to the access token returned by `.locate`.
166
186
 
187
+ ### Refresh Token
188
+
189
+ This strategy creates an new access token based on expired access token refresh token.
190
+ Use `.valid?` on the refresh token application to determine if refresh token is valid.
191
+
192
+ **User:** The Warden user is set to the access token returned by `.locate`.
193
+
167
194
  ### Issuing Access Token
168
195
 
169
196
  This strategy is a fallback strategy when cannot issue access token due to unspecified grant_type
data/lib/warden/oauth2.rb CHANGED
@@ -4,11 +4,15 @@ require 'warden/oauth2/version'
4
4
  module Warden
5
5
  module OAuth2
6
6
  class Configuration
7
- attr_accessor :client_credentials_model, :resource_owner_password_credentials_model, :token_model
7
+ attr_accessor :client_credentials_model,
8
+ :resource_owner_password_credentials_model,
9
+ :token_model,
10
+ :refresh_token_model
8
11
 
9
12
  def initialize
10
13
  self.client_credentials_model = ClientCredentialsApplication if defined?(ClientCredentialsApplication)
11
14
  self.resource_owner_password_credentials_model = ResourceOwnerPasswordCredentialsApplication if defined?(ResourceOwnerPasswordCredentialsApplication)
15
+ self.refresh_token_model = RefreshTokenApplication if defined?(RefreshTokenApplication)
12
16
  self.token_model = AccessToken if defined?(AccessToken)
13
17
  end
14
18
  end
@@ -32,6 +36,7 @@ module Warden
32
36
  autoload :IssuingAccessToken, 'warden/oauth2/strategies/issuing_access_token'
33
37
  autoload :AccessingProtectedResource, 'warden/oauth2/strategies/accessing_protected_resource'
34
38
  autoload :Bearer, 'warden/oauth2/strategies/bearer'
39
+ autoload :RefreshToken, 'warden/oauth2/strategies/refresh_token'
35
40
  end
36
41
  end
37
42
  end
@@ -36,7 +36,7 @@ module Warden
36
36
 
37
37
  def error_status
38
38
  case message
39
- when 'invalid_client' then 401
39
+ when 'invalid_client', 'invalid_token' then 401
40
40
  when 'invalid_scope' then 403
41
41
  else 400
42
42
  end
@@ -0,0 +1,29 @@
1
+ module Warden
2
+ module OAuth2
3
+ module Strategies
4
+ class RefreshToken < Client
5
+ def valid?
6
+ params['grant_type'] == 'refresh_token'
7
+ end
8
+ protected
9
+ def model
10
+ Warden::OAuth2.config.refresh_token_model
11
+ end
12
+ def client_authenticated
13
+ if params['refresh_token']
14
+ valid_client = client.valid?(refresh_token: params['refresh_token'])
15
+ if valid_client
16
+ super
17
+ else
18
+ fail("invalid_token")
19
+ self.error_description = "provided refresh token is not valid"
20
+ end
21
+ else
22
+ fail "invalid_request"
23
+ self.error_description = "refresh token is not provided"
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,5 @@
1
1
  module Warden
2
2
  module OAuth2
3
- VERSION = '0.0.7'
3
+ VERSION = '0.0.8'
4
4
  end
5
5
  end
@@ -43,7 +43,7 @@ describe Warden::OAuth2::Strategies::Client do
43
43
  end
44
44
  end
45
45
 
46
- describe '#authorize!' do
46
+ describe '#authenticate!' do
47
47
  it 'should succeed if a client is around' do
48
48
  client_instance = double
49
49
  client_model.stub(:locate).and_return(client_instance)
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Warden::OAuth2::Strategies::RefreshToken do
4
+ let(:strategy){ described_class }
5
+ let(:client_model){ double(:RefreshTokenApplication) }
6
+ subject{ strategy.new({'rack.input' => {}}) }
7
+
8
+ before do
9
+ Warden::OAuth2.config.refresh_token_model = client_model
10
+ end
11
+ describe '#valid?' do
12
+ it 'returns false if the grant type is not specified' do
13
+ subject.stub(:params).and_return({})
14
+ subject.should_not be_valid
15
+ end
16
+
17
+ it 'returns true if the grant type is refresh_token' do
18
+ subject.stub(:params).and_return({'grant_type' => 'refresh_token'})
19
+ subject.should be_valid
20
+ end
21
+
22
+ it 'returns false if the grant type is not password' do
23
+ subject.stub(:params).and_return({'grant_type' => 'whatever'})
24
+ subject.should_not be_valid
25
+ end
26
+ end
27
+
28
+
29
+ describe 'authenticate!' do
30
+ it 'should fail if no refresh token provided' do
31
+ client_model.stub(locate: double)
32
+ subject.stub(:params).and_return('client_id' => 'client_id')
33
+
34
+ subject._run!
35
+
36
+ subject.result.should == :failure
37
+ subject.message.should == "invalid_request"
38
+ subject.error_status.should == 400
39
+ end
40
+
41
+ it 'should succeed if a client is around' do
42
+ client_instance = double
43
+ client_instance.stub(:valid?).with(refresh_token: 'some_token').and_return(true)
44
+ client_model.stub(:locate).with('client_id', nil).and_return(client_instance)
45
+ subject.stub(:params).and_return('client_id' => 'client_id', 'refresh_token' => 'some_token')
46
+ subject._run!
47
+ subject.user.should == client_instance
48
+ subject.result.should == :success
49
+ end
50
+
51
+ it 'should fail if a client is not found' do
52
+ client_model.stub(locate: nil)
53
+ subject.stub(:params).and_return('refresh_token' => 'some_token')
54
+ subject._run!
55
+ subject.result.should == :failure
56
+ subject.message.should == "invalid_client"
57
+ end
58
+
59
+ it 'should fail if client is not valid' do
60
+ client_instance = double(valid?: false)
61
+ client_model.stub(locate: client_instance)
62
+ subject.stub(:params).and_return('client_id' => 'client_id','refresh_token' => 'some_token')
63
+ subject._run!
64
+ subject.user.should == nil
65
+ subject.result.should == :failure
66
+ subject.message.should == "invalid_token"
67
+ subject.error_description.should_not be_empty
68
+ subject.error_status.should == 401
69
+ end
70
+ end
71
+ end
@@ -26,7 +26,7 @@ describe Warden::OAuth2::Strategies::ResourceOwnerPasswordCredentials do
26
26
  end
27
27
  end
28
28
 
29
- describe '#authorize!' do
29
+ describe '#authenticate!' do
30
30
  it 'should fail if a client is around but not valid' do
31
31
  client_instance = double(:client_instance, valid?: false)
32
32
  client_model.stub(locate: client_instance)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warden-oauth2-strategies
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - AirService
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-24 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: warden
@@ -91,6 +91,7 @@ files:
91
91
  - lib/warden/oauth2/strategies/client_credentials.rb
92
92
  - lib/warden/oauth2/strategies/issuing_access_token.rb
93
93
  - lib/warden/oauth2/strategies/public.rb
94
+ - lib/warden/oauth2/strategies/refresh_token.rb
94
95
  - lib/warden/oauth2/strategies/resource_owner_password_credentials.rb
95
96
  - lib/warden/oauth2/strategies/token.rb
96
97
  - lib/warden/oauth2/version.rb
@@ -102,6 +103,7 @@ files:
102
103
  - spec/warden/oauth2/strategies/client_spec.rb
103
104
  - spec/warden/oauth2/strategies/issuing_access_token_spec.rb
104
105
  - spec/warden/oauth2/strategies/public_spec.rb
106
+ - spec/warden/oauth2/strategies/refresh_token_spec.rb
105
107
  - spec/warden/oauth2/strategies/resource_owner_password_credentials_spec.rb
106
108
  - spec/warden/oauth2/strategies/token_spec.rb
107
109
  - warden-oauth2.gemspec
@@ -138,5 +140,6 @@ test_files:
138
140
  - spec/warden/oauth2/strategies/client_spec.rb
139
141
  - spec/warden/oauth2/strategies/issuing_access_token_spec.rb
140
142
  - spec/warden/oauth2/strategies/public_spec.rb
143
+ - spec/warden/oauth2/strategies/refresh_token_spec.rb
141
144
  - spec/warden/oauth2/strategies/resource_owner_password_credentials_spec.rb
142
145
  - spec/warden/oauth2/strategies/token_spec.rb