anjlab-devise-oauth2-providable 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. data/.gitignore +40 -0
  2. data/.rvmrc +1 -0
  3. data/CONTRIBUTORS.txt +6 -0
  4. data/Gemfile +8 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +158 -0
  7. data/Rakefile +5 -0
  8. data/app/controllers/devise/oauth2_providable/authorizations_controller.rb +58 -0
  9. data/app/controllers/devise/oauth2_providable/tokens_controller.rb +19 -0
  10. data/app/models/devise/oauth2_providable/access_token.rb +24 -0
  11. data/app/models/devise/oauth2_providable/authorization_code.rb +4 -0
  12. data/app/models/devise/oauth2_providable/client.rb +24 -0
  13. data/app/models/devise/oauth2_providable/refresh_token.rb +7 -0
  14. data/app/views/devise/oauth2_providable/authorizations/_form.html.erb +7 -0
  15. data/app/views/devise/oauth2_providable/authorizations/error.html.erb +4 -0
  16. data/app/views/devise/oauth2_providable/authorizations/new.html.erb +4 -0
  17. data/config/routes.rb +9 -0
  18. data/db/migrate/20111014160714_create_devise_oauth2_providable_schema.rb +55 -0
  19. data/devise_oauth2_providable.gemspec +30 -0
  20. data/lib/anjlab-devise-oauth2-providable.rb +1 -0
  21. data/lib/devise/oauth2_providable/engine.rb +16 -0
  22. data/lib/devise/oauth2_providable/expirable_token.rb +57 -0
  23. data/lib/devise/oauth2_providable/models/oauth2_authorization_code_grantable.rb +6 -0
  24. data/lib/devise/oauth2_providable/models/oauth2_password_grantable.rb +6 -0
  25. data/lib/devise/oauth2_providable/models/oauth2_providable.rb +14 -0
  26. data/lib/devise/oauth2_providable/models/oauth2_refresh_token_grantable.rb +6 -0
  27. data/lib/devise/oauth2_providable/strategies/oauth2_authorization_code_grant_type_strategy.rb +21 -0
  28. data/lib/devise/oauth2_providable/strategies/oauth2_grant_type_strategy.rb +38 -0
  29. data/lib/devise/oauth2_providable/strategies/oauth2_password_grant_type_strategy.rb +22 -0
  30. data/lib/devise/oauth2_providable/strategies/oauth2_providable_strategy.rb +25 -0
  31. data/lib/devise/oauth2_providable/strategies/oauth2_refresh_token_grant_type_strategy.rb +22 -0
  32. data/lib/devise/oauth2_providable/version.rb +5 -0
  33. data/lib/devise_oauth2_providable.rb +41 -0
  34. data/script/rails +6 -0
  35. data/spec/controllers/authorizations_controller_spec.rb +32 -0
  36. data/spec/controllers/protected_controller_spec.rb +42 -0
  37. data/spec/dummy/Rakefile +7 -0
  38. data/spec/dummy/app/assets/javascripts/application.js +7 -0
  39. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  40. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  41. data/spec/dummy/app/controllers/protected_controller.rb +6 -0
  42. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  43. data/spec/dummy/app/mailers/.gitkeep +0 -0
  44. data/spec/dummy/app/models/.gitkeep +0 -0
  45. data/spec/dummy/app/models/user.rb +15 -0
  46. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  47. data/spec/dummy/config.ru +4 -0
  48. data/spec/dummy/config/application.rb +50 -0
  49. data/spec/dummy/config/boot.rb +10 -0
  50. data/spec/dummy/config/database.yml +25 -0
  51. data/spec/dummy/config/environment.rb +5 -0
  52. data/spec/dummy/config/environments/development.rb +30 -0
  53. data/spec/dummy/config/environments/production.rb +60 -0
  54. data/spec/dummy/config/environments/test.rb +39 -0
  55. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  56. data/spec/dummy/config/initializers/devise.rb +216 -0
  57. data/spec/dummy/config/initializers/inflections.rb +10 -0
  58. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  59. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  60. data/spec/dummy/config/initializers/session_store.rb +8 -0
  61. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  62. data/spec/dummy/config/locales/devise.en.yml +57 -0
  63. data/spec/dummy/config/locales/en.yml +5 -0
  64. data/spec/dummy/config/routes.rb +7 -0
  65. data/spec/dummy/db/migrate/20120521101005_create_users.rb +5 -0
  66. data/spec/dummy/db/migrate/20120521101006_add_devise_to_users.rb +53 -0
  67. data/spec/dummy/db/migrate/20120521101407_create_devise_oauth2_providable_schema.devise_oauth2_providable.rb +56 -0
  68. data/spec/dummy/db/schema.rb +88 -0
  69. data/spec/dummy/lib/assets/.gitkeep +0 -0
  70. data/spec/dummy/public/404.html +26 -0
  71. data/spec/dummy/public/422.html +26 -0
  72. data/spec/dummy/public/500.html +26 -0
  73. data/spec/dummy/public/favicon.ico +0 -0
  74. data/spec/dummy/script/rails +6 -0
  75. data/spec/factories.rb +19 -0
  76. data/spec/integration/oauth2_authorization_token_grant_type_strategy_spec.rb +136 -0
  77. data/spec/integration/oauth2_password_grant_type_strategy_spec.rb +198 -0
  78. data/spec/integration/oauth2_refresh_token_grant_type_strategy_spec.rb +138 -0
  79. data/spec/lib/devise_oauth2_providable_spec.rb +7 -0
  80. data/spec/models/access_token_spec.rb +51 -0
  81. data/spec/models/authorization_code_spec.rb +21 -0
  82. data/spec/models/client_spec.rb +22 -0
  83. data/spec/models/refresh_token_spec.rb +23 -0
  84. data/spec/models/user_spec.rb +6 -0
  85. data/spec/routing/authorizations_routing_spec.rb +17 -0
  86. data/spec/routing/tokens_routing_spec.rb +11 -0
  87. data/spec/spec_helper.rb +28 -0
  88. data/spec/support/match_json.rb +6 -0
  89. metadata +334 -0
@@ -0,0 +1,136 @@
1
+ require 'spec_helper'
2
+
3
+ describe Devise::Strategies::Oauth2AuthorizationCodeGrantTypeStrategy do
4
+ describe 'POST /oauth2/token' do
5
+ describe 'with grant_type=authorization_code' do
6
+ context 'with valid params' do
7
+ let(:user) { create(:user) }
8
+ let(:client) { create(:client) }
9
+ before do
10
+ @authorization_code = user.authorization_codes.create(:client => client, :redirect_uri => client.redirect_uri)
11
+ params = {
12
+ :grant_type => 'authorization_code',
13
+ :client_id => client.identifier,
14
+ :client_secret => client.secret,
15
+ :code => @authorization_code.token
16
+ }
17
+
18
+ post '/oauth2/token', params
19
+ end
20
+ it { response.code.to_i.should == 200 }
21
+ it { response.content_type.should == 'application/json' }
22
+ it 'returns json' do
23
+ token = Devise::Oauth2Providable::AccessToken.last
24
+ refresh_token = Devise::Oauth2Providable::RefreshToken.last
25
+ expected = {
26
+ :token_type => 'bearer',
27
+ :expires_in => 899,
28
+ :refresh_token => refresh_token.token,
29
+ :access_token => token.token
30
+ }
31
+ response.body.should match_json(expected)
32
+ end
33
+ end
34
+ context 'with expired authorization_code' do
35
+ let(:user) { create(:user) }
36
+ let(:client) { create(:client) }
37
+ before do
38
+ timenow = 2.days.from_now
39
+ Time.stub!(:now).and_return(timenow)
40
+ @authorization_code = user.authorization_codes.create(:client_id => client, :redirect_uri => client.redirect_uri)
41
+ params = {
42
+ :grant_type => 'authorization_code',
43
+ :client_id => client.identifier,
44
+ :client_secret => client.secret,
45
+ :code => @authorization_code.token
46
+ }
47
+ Time.stub!(:now).and_return(timenow + 10.minutes)
48
+
49
+ post '/oauth2/token', params
50
+ end
51
+ it { response.code.to_i.should == 400 }
52
+ it { response.content_type.should == 'application/json' }
53
+ it 'returns json' do
54
+ expected = {
55
+ :error => 'invalid_grant',
56
+ :error_description => 'invalid authorization code request'
57
+ }
58
+ response.body.should match_json(expected)
59
+ end
60
+ end
61
+ context 'with invalid authorization_code' do
62
+ let(:user) { create(:user) }
63
+ let(:client) { create(:client) }
64
+ before do
65
+ @authorization_code = user.authorization_codes.create(:client_id => client, :redirect_uri => client.redirect_uri)
66
+ params = {
67
+ :grant_type => 'authorization_code',
68
+ :client_id => client.identifier,
69
+ :client_secret => client.secret,
70
+ :code => 'invalid'
71
+ }
72
+
73
+ post '/oauth2/token', params
74
+ end
75
+ it { response.code.to_i.should == 400 }
76
+ it { response.content_type.should == 'application/json' }
77
+ it 'returns json' do
78
+ expected = {
79
+ :error => 'invalid_grant',
80
+ :error_description => 'invalid authorization code request'
81
+ }
82
+ response.body.should match_json(expected)
83
+ end
84
+ end
85
+ context 'with invalid client_secret' do
86
+ let(:user) { create(:user) }
87
+ let(:client) { create(:client) }
88
+ before do
89
+ @authorization_code = user.authorization_codes.create(:client_id => client, :redirect_uri => client.redirect_uri)
90
+ params = {
91
+ :grant_type => 'authorization_code',
92
+ :client_id => client.identifier,
93
+ :client_secret => 'invalid',
94
+ :code => @authorization_code.token
95
+ }
96
+
97
+ post '/oauth2/token', params
98
+ end
99
+ it { response.code.to_i.should == 400 }
100
+ it { response.content_type.should == 'application/json' }
101
+ it 'returns json' do
102
+ expected = {
103
+ :error => 'invalid_client',
104
+ :error_description => 'invalid client credentials'
105
+ }
106
+ response.body.should match_json(expected)
107
+ end
108
+ end
109
+ context 'with invalid client_id' do
110
+ let(:user) { create(:user) }
111
+ let(:client) { create(:client) }
112
+ before do
113
+ @authorization_code = user.authorization_codes.create(:client_id => client, :redirect_uri => client.redirect_uri)
114
+ params = {
115
+ :grant_type => 'authorization_code',
116
+ :client_id => 'invalid',
117
+ :client_secret => client.secret,
118
+ :code => @authorization_code.token
119
+ }
120
+
121
+ post '/oauth2/token', params
122
+ end
123
+ it { response.code.to_i.should == 400 }
124
+ it { response.content_type.should == 'application/json' }
125
+ it 'returns json' do
126
+ expected = {
127
+ :error => 'invalid_client',
128
+ :error_description => 'invalid client credentials'
129
+ }
130
+ response.body.should match_json(expected)
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+
@@ -0,0 +1,198 @@
1
+ require 'spec_helper'
2
+
3
+ describe Devise::Strategies::Oauth2PasswordGrantTypeStrategy do
4
+ describe 'POST /oauth2/token' do
5
+ describe 'with grant_type=password' do
6
+ context 'with valid params' do
7
+ let(:client) { create(:client) }
8
+ before do
9
+ @user = create(:user)
10
+
11
+ params = {
12
+ :grant_type => 'password',
13
+ :client_id => client.identifier,
14
+ :client_secret => client.secret,
15
+ :username => @user.email,
16
+ :password => 'test123456'
17
+ }
18
+
19
+ post '/oauth2/token', params
20
+ end
21
+ it { response.code.to_i.should == 200 }
22
+ it { response.content_type.should == 'application/json' }
23
+ it 'returns json' do
24
+ token = Devise::Oauth2Providable::AccessToken.last
25
+ expected = token.token_response
26
+ response.body.should match_json(expected)
27
+ end
28
+ end
29
+ context 'with valid params and client id/secret in basic auth header' do
30
+ let(:client) { create(:client) }
31
+ before do
32
+ @user = create :user
33
+
34
+ params = {
35
+ :grant_type => 'password',
36
+ :username => @user.email,
37
+ :password => 'test123456'
38
+ }
39
+
40
+ auth_header = ActionController::HttpAuthentication::Basic.encode_credentials client.identifier, client.secret
41
+ post '/oauth2/token', params, 'HTTP_AUTHORIZATION' => auth_header
42
+ end
43
+ it { response.code.to_i.should == 200 }
44
+ it { response.content_type.should == 'application/json' }
45
+ it 'returns json' do
46
+ token = Devise::Oauth2Providable::AccessToken.last
47
+ expected = token.token_response
48
+ response.body.should match_json(expected)
49
+ end
50
+ end
51
+ context 'with invalid client id in basic auth header' do
52
+ let(:client) { create(:client) }
53
+ before do
54
+ @user = create :user
55
+ params = {
56
+ :grant_type => 'password',
57
+ :username => @user.email,
58
+ :password => 'test123456'
59
+ }
60
+ auth_header = ActionController::HttpAuthentication::Basic.encode_credentials 'invalid client id', client.secret
61
+ post '/oauth2/token', params, 'HTTP_AUTHORIZATION' => auth_header
62
+ end
63
+ it { response.code.to_i.should == 400 }
64
+ it { response.content_type.should == 'application/json' }
65
+ it 'returns json' do
66
+ expected = {
67
+ :error_description => "invalid client credentials",
68
+ :error => "invalid_client"
69
+ }
70
+ response.body.should match_json(expected)
71
+ end
72
+ end
73
+ context 'with invalid client secret in basic auth header' do
74
+ let(:client) { create(:client) }
75
+ before do
76
+ @user = create :user
77
+ params = {
78
+ :grant_type => 'password',
79
+ :username => @user.email,
80
+ :password => 'test123456'
81
+ }
82
+ auth_header = ActionController::HttpAuthentication::Basic.encode_credentials client.identifier, 'invalid secret'
83
+ post '/oauth2/token', params, 'HTTP_AUTHORIZATION' => auth_header
84
+ end
85
+ it { response.code.to_i.should == 400 }
86
+ it { response.content_type.should == 'application/json' }
87
+ it 'returns json' do
88
+ expected = {
89
+ :error_description => "invalid client credentials",
90
+ :error => "invalid_client"
91
+ }
92
+ response.body.should match_json(expected)
93
+ end
94
+ end
95
+ context 'with invalid password' do
96
+ let(:client) { create(:client) }
97
+ before do
98
+ @user = create :user
99
+
100
+ params = {
101
+ :grant_type => 'password',
102
+ :client_id => client.identifier,
103
+ :client_secret => client.secret,
104
+ :username => @user.email,
105
+ :password => 'bar'
106
+ }
107
+
108
+ post '/oauth2/token', params
109
+ end
110
+ it { response.code.to_i.should == 400 }
111
+ it { response.content_type.should == 'application/json' }
112
+ it 'returns json' do
113
+ expected = {
114
+ :error_description => "invalid password authentication request",
115
+ :error => "invalid_grant"
116
+ }
117
+ response.body.should match_json(expected)
118
+ end
119
+ end
120
+ context 'with invalid user' do
121
+ let(:client) { create(:client) }
122
+ before do
123
+ @user = create :user
124
+
125
+ params = {
126
+ :grant_type => 'password',
127
+ :client_id => client.identifier,
128
+ :client_secret => client.secret,
129
+ :username => 'bla@bla.com',
130
+ :password => 'bar'
131
+ }
132
+
133
+ post '/oauth2/token', params
134
+ end
135
+ it { response.code.to_i.should == 400 }
136
+ it { response.content_type.should == 'application/json' }
137
+ it 'returns json' do
138
+ expected = {
139
+ :error_description => "invalid password authentication request",
140
+ :error => "invalid_grant"
141
+ }
142
+ response.body.should match_json(expected)
143
+ end
144
+ end
145
+ context 'with invalid client_id' do
146
+ let(:client) { create(:client) }
147
+ before do
148
+ @user = create :user
149
+
150
+ params = {
151
+ :grant_type => 'password',
152
+ :client_id => 'invalid',
153
+ :client_secret => client.secret,
154
+ :username => @user.email,
155
+ :password => 'test123456'
156
+ }
157
+
158
+ post '/oauth2/token', params
159
+ end
160
+ it { response.code.to_i.should == 400 }
161
+ it { response.content_type.should == 'application/json' }
162
+ it 'returns json' do
163
+ expected = {
164
+ :error_description => "invalid client credentials",
165
+ :error => "invalid_client"
166
+ }
167
+ response.body.should match_json(expected)
168
+ end
169
+ end
170
+ context 'with invalid client_secret' do
171
+ let(:client) { create(:client) }
172
+ before do
173
+ @user = create :user
174
+
175
+ params = {
176
+ :grant_type => 'password',
177
+ :client_id => client.identifier,
178
+ :client_secret => 'invalid',
179
+ :username => @user.email,
180
+ :password => 'test123456'
181
+ }
182
+
183
+ post '/oauth2/token', params
184
+ end
185
+ it { response.code.to_i.should == 400 }
186
+ it { response.content_type.should == 'application/json' }
187
+ it 'returns json' do
188
+ expected = {
189
+ :error_description => "invalid client credentials",
190
+ :error => "invalid_client"
191
+ }
192
+ response.body.should match_json(expected)
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end
198
+
@@ -0,0 +1,138 @@
1
+ require 'spec_helper'
2
+
3
+ describe Devise::Strategies::Oauth2RefreshTokenGrantTypeStrategy do
4
+ describe 'POST /oauth2/token' do
5
+ describe 'with grant_type=refresh_token' do
6
+ context 'with valid params' do
7
+ let(:user) { create(:user) }
8
+ let(:client) { create(:client) }
9
+ before do
10
+ @refresh_token = client.refresh_tokens.create! :user => user
11
+ params = {
12
+ :grant_type => 'refresh_token',
13
+ :client_id => client.identifier,
14
+ :client_secret => client.secret,
15
+ :refresh_token => @refresh_token.token
16
+ }
17
+
18
+ post '/oauth2/token', params
19
+ end
20
+ it { response.code.to_i.should == 200 }
21
+ it { response.content_type.should == 'application/json' }
22
+ it 'returns json' do
23
+ token = Devise::Oauth2Providable::AccessToken.last
24
+ refresh_token = @refresh_token
25
+ expected = {
26
+ :token_type => 'bearer',
27
+ :expires_in => 899,
28
+ :refresh_token => refresh_token.token,
29
+ :access_token => token.token
30
+ }
31
+ response.body.should match_json(expected)
32
+ end
33
+ end
34
+ context 'with expired refresh_token' do
35
+ let(:user) { create(:user) }
36
+ let(:client) { create(:client) }
37
+ before do
38
+ timenow = 2.days.from_now
39
+ Time.stub!(:now).and_return(timenow)
40
+ @refresh_token = client.refresh_tokens.create! :user => user
41
+ params = {
42
+ :grant_type => 'refresh_token',
43
+ :client_id => client.identifier,
44
+ :client_secret => client.secret,
45
+ :refresh_token => @refresh_token.token
46
+ }
47
+ Time.stub!(:now).and_return(timenow + 2.months)
48
+
49
+ post '/oauth2/token', params
50
+ end
51
+ it { response.code.to_i.should == 400 }
52
+ it { response.content_type.should == 'application/json' }
53
+ it 'returns json' do
54
+ expected = {
55
+ :error => 'invalid_grant',
56
+ :error_description => 'invalid refresh token'
57
+ }
58
+ response.body.should match_json(expected)
59
+ end
60
+ end
61
+ context 'with invalid refresh_token' do
62
+ let(:user) { create(:user) }
63
+ let(:client) { create(:client) }
64
+ before do
65
+ @refresh_token = client.refresh_tokens.create! :user => user
66
+ params = {
67
+ :grant_type => 'refresh_token',
68
+ :client_id => client.identifier,
69
+ :client_secret => client.secret,
70
+ :refresh_token => 'invalid'
71
+ }
72
+
73
+ post '/oauth2/token', params
74
+ end
75
+ it { response.code.to_i.should == 400 }
76
+ it { response.content_type.should == 'application/json' }
77
+ it 'returns json' do
78
+ token = Devise::Oauth2Providable::AccessToken.last
79
+ refresh_token = @refresh_token
80
+ expected = {
81
+ :error => 'invalid_grant',
82
+ :error_description => 'invalid refresh token'
83
+ }
84
+ response.body.should match_json(expected)
85
+ end
86
+ end
87
+ context 'with invalid client_id' do
88
+ let(:user) { create(:user) }
89
+ let(:client) { create(:client) }
90
+ before do
91
+ @refresh_token = client.refresh_tokens.create! :user => user
92
+ params = {
93
+ :grant_type => 'refresh_token',
94
+ :client_id => 'invalid',
95
+ :client_secret => client.secret,
96
+ :refresh_token => @refresh_token.token
97
+ }
98
+
99
+ post '/oauth2/token', params
100
+ end
101
+ it { response.code.to_i.should == 400 }
102
+ it { response.content_type.should == 'application/json' }
103
+ it 'returns json' do
104
+ expected = {
105
+ :error => 'invalid_client',
106
+ :error_description => 'invalid client credentials'
107
+ }
108
+ response.body.should match_json(expected)
109
+ end
110
+ end
111
+ context 'with invalid client_secret' do
112
+ let(:user) { create(:user) }
113
+ let(:client) { create(:client) }
114
+ before do
115
+ @refresh_token = client.refresh_tokens.create! :user => user
116
+ params = {
117
+ :grant_type => 'refresh_token',
118
+ :client_id => client.identifier,
119
+ :client_secret => 'invalid',
120
+ :refresh_token => @refresh_token.token
121
+ }
122
+
123
+ post '/oauth2/token', params
124
+ end
125
+ it { response.code.to_i.should == 400 }
126
+ it { response.content_type.should == 'application/json' }
127
+ it 'returns json' do
128
+ expected = {
129
+ :error => 'invalid_client',
130
+ :error_description => 'invalid client credentials'
131
+ }
132
+ response.body.should match_json(expected)
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+