devise_oauth2_providable 1.0.3 → 1.0.4
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.
- data/Rakefile +1 -7
- data/app/controllers/devise/oauth2_providable/authorizations_controller.rb +3 -3
- data/app/models/devise/oauth2_providable/authorization_code.rb +0 -7
- data/db/migrate/20111014160714_create_devise_oauth2_providable_schema.rb +0 -1
- data/lib/devise/oauth2_providable/version.rb +1 -1
- data/spec/controllers/authorizations_controller_spec.rb +16 -0
- data/spec/controllers/protected_controller_spec.rb +2 -2
- data/spec/factories/user_factory.rb +4 -0
- data/spec/integration/oauth2_authorization_token_grant_type_strategy_spec.rb +5 -5
- data/spec/integration/oauth2_refresh_token_grant_type_strategy_spec.rb +57 -5
- data/spec/routing/authorizations_routing_spec.rb +7 -3
- data/spec/routing/tokens_routing_spec.rb +4 -1
- data/spec/spec_helper.rb +5 -5
- data/spec/support/inject_engine_routes_into_application.rb +74 -0
- metadata +10 -7
- data/.rspec +0 -3
- data/spec/setup_database.rb +0 -7
data/Rakefile
CHANGED
@@ -1,11 +1,5 @@
|
|
1
|
-
require
|
2
|
-
Bundler::GemHelper.install_tasks
|
1
|
+
require "bundler/gem_tasks"
|
3
2
|
|
4
|
-
begin
|
5
|
-
require 'bundler/setup'
|
6
|
-
rescue LoadError
|
7
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
8
|
-
end
|
9
3
|
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
10
4
|
load 'rails/tasks/engine.rake'
|
11
5
|
|
@@ -32,15 +32,15 @@ module Devise
|
|
32
32
|
def authorize_endpoint(allow_approval = false)
|
33
33
|
Rack::OAuth2::Server::Authorize.new do |req, res|
|
34
34
|
@client = Client.find_by_identifier(req.client_id) || req.bad_request!
|
35
|
-
res.redirect_uri
|
35
|
+
res.redirect_uri, @redirect_uri = req.verify_redirect_uri!(@client.redirect_uri)
|
36
36
|
if allow_approval
|
37
37
|
if params[:approve].present?
|
38
38
|
case req.response_type
|
39
39
|
when :code
|
40
|
-
authorization_code = current_user.authorization_codes.create(:client => @client
|
40
|
+
authorization_code = current_user.authorization_codes.create!(:client => @client)
|
41
41
|
res.code = authorization_code.token
|
42
42
|
when :token
|
43
|
-
access_token = current_user.access_tokens.create(:client => @client).token
|
43
|
+
access_token = current_user.access_tokens.create!(:client => @client).token
|
44
44
|
bearer_token = Rack::OAuth2::AccessToken::Bearer.new(:access_token => access_token)
|
45
45
|
res.access_token = bearer_token
|
46
46
|
res.uid = current_user.id
|
@@ -1,10 +1,3 @@
|
|
1
1
|
class Devise::Oauth2Providable::AuthorizationCode < ActiveRecord::Base
|
2
2
|
expires_according_to :authorization_code_expires_in
|
3
|
-
|
4
|
-
def access_token
|
5
|
-
@access_token ||= expired! && user.access_tokens.create(:client => client)
|
6
|
-
end
|
7
|
-
def valid_request?(req)
|
8
|
-
self.redirect_uri == req.redirect_uri
|
9
|
-
end
|
10
3
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Devise::Oauth2Providable::AuthorizationsController do
|
4
|
+
describe 'GET #new' do
|
5
|
+
with :user
|
6
|
+
with :client
|
7
|
+
before do
|
8
|
+
sign_in user
|
9
|
+
get :new, :client_id => client.identifier, :redirect_uri => client.redirect_uri, :response_type => 'code', :use_route => 'devise_oauth2_providable'
|
10
|
+
end
|
11
|
+
it { should respond_with :ok }
|
12
|
+
it { should respond_with_content_type :html }
|
13
|
+
it { should assign_to(:redirect_uri) }
|
14
|
+
it { should assign_to(:response_type) }
|
15
|
+
end
|
16
|
+
end
|
@@ -4,9 +4,9 @@ describe ProtectedController do
|
|
4
4
|
|
5
5
|
describe 'get :index' do
|
6
6
|
with :client
|
7
|
+
with :user
|
7
8
|
before do
|
8
|
-
@
|
9
|
-
@token = Devise::Oauth2Providable::AccessToken.create! :client => client, :user => @user
|
9
|
+
@token = Devise::Oauth2Providable::AccessToken.create! :client => client, :user => user
|
10
10
|
end
|
11
11
|
context 'with valid bearer token in header' do
|
12
12
|
before do
|
@@ -3,11 +3,11 @@ require 'spec_helper'
|
|
3
3
|
describe Devise::Strategies::Oauth2AuthorizationCodeGrantTypeStrategy do
|
4
4
|
describe 'POST /oauth2/token' do
|
5
5
|
describe 'with grant_type=authorization_code' do
|
6
|
-
with :client
|
7
6
|
context 'with valid params' do
|
7
|
+
with :client
|
8
|
+
with :user
|
8
9
|
before do
|
9
|
-
@
|
10
|
-
@authorization_code = @user.authorization_codes.create(:client_id => client, :redirect_uri => client.redirect_uri)
|
10
|
+
@authorization_code = user.authorization_codes.create(:client_id => client, :redirect_uri => client.redirect_uri)
|
11
11
|
params = {
|
12
12
|
:grant_type => 'authorization_code',
|
13
13
|
:client_id => client.identifier,
|
@@ -33,9 +33,9 @@ describe Devise::Strategies::Oauth2AuthorizationCodeGrantTypeStrategy do
|
|
33
33
|
end
|
34
34
|
context 'with invalid authorization_code' do
|
35
35
|
with :client
|
36
|
+
with :user
|
36
37
|
before do
|
37
|
-
@
|
38
|
-
@authorization_code = @user.authorization_codes.create(:client_id => client, :redirect_uri => client.redirect_uri)
|
38
|
+
@authorization_code = user.authorization_codes.create(:client_id => client, :redirect_uri => client.redirect_uri)
|
39
39
|
params = {
|
40
40
|
:grant_type => 'authorization_code',
|
41
41
|
:client_id => client.identifier,
|
@@ -5,9 +5,9 @@ describe Devise::Strategies::Oauth2RefreshTokenGrantTypeStrategy do
|
|
5
5
|
describe 'with grant_type=refresh_token' do
|
6
6
|
context 'with valid params' do
|
7
7
|
with :client
|
8
|
+
with :user
|
8
9
|
before do
|
9
|
-
@
|
10
|
-
@refresh_token = client.refresh_tokens.create! :user => @user
|
10
|
+
@refresh_token = client.refresh_tokens.create! :user => user
|
11
11
|
params = {
|
12
12
|
:grant_type => 'refresh_token',
|
13
13
|
:client_id => client.identifier,
|
@@ -32,10 +32,10 @@ describe Devise::Strategies::Oauth2RefreshTokenGrantTypeStrategy do
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
context 'with invalid refresh_token' do
|
35
|
+
with :user
|
36
|
+
with :client
|
35
37
|
before do
|
36
|
-
@
|
37
|
-
client = Devise::Oauth2Providable::Client.create! :name => 'example', :redirect_uri => 'http://localhost', :website => 'http://localhost'
|
38
|
-
@refresh_token = client.refresh_tokens.create! :user => @user
|
38
|
+
@refresh_token = client.refresh_tokens.create! :user => user
|
39
39
|
params = {
|
40
40
|
:grant_type => 'refresh_token',
|
41
41
|
:client_id => client.identifier,
|
@@ -57,6 +57,58 @@ describe Devise::Strategies::Oauth2RefreshTokenGrantTypeStrategy do
|
|
57
57
|
response.body.should match_json(expected)
|
58
58
|
end
|
59
59
|
end
|
60
|
+
context 'with invalid client_id' do
|
61
|
+
with :user
|
62
|
+
with :client
|
63
|
+
before do
|
64
|
+
@refresh_token = client.refresh_tokens.create! :user => user
|
65
|
+
params = {
|
66
|
+
:grant_type => 'refresh_token',
|
67
|
+
:client_id => 'invalid',
|
68
|
+
:client_secret => client.secret,
|
69
|
+
:refresh_token => @refresh_token.token
|
70
|
+
}
|
71
|
+
|
72
|
+
post '/oauth2/token', params
|
73
|
+
end
|
74
|
+
it { response.code.to_i.should == 400 }
|
75
|
+
it { response.content_type.should == 'application/json' }
|
76
|
+
it 'returns json' do
|
77
|
+
token = Devise::Oauth2Providable::AccessToken.last
|
78
|
+
refresh_token = @refresh_token
|
79
|
+
expected = {
|
80
|
+
:error => 'invalid_grant',
|
81
|
+
:error_description => 'invalid refresh token'
|
82
|
+
}
|
83
|
+
response.body.should match_json(expected)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
context 'with invalid client_secret' do
|
87
|
+
with :user
|
88
|
+
with :client
|
89
|
+
before do
|
90
|
+
@refresh_token = client.refresh_tokens.create! :user => user
|
91
|
+
params = {
|
92
|
+
:grant_type => 'refresh_token',
|
93
|
+
:client_id => client.identifier,
|
94
|
+
:client_secret => client.secret,
|
95
|
+
:refresh_token => @refresh_token.token
|
96
|
+
}
|
97
|
+
|
98
|
+
post '/oauth2/token', params
|
99
|
+
end
|
100
|
+
it { response.code.to_i.should == 400 }
|
101
|
+
it { response.content_type.should == 'application/json' }
|
102
|
+
it 'returns json' do
|
103
|
+
token = Devise::Oauth2Providable::AccessToken.last
|
104
|
+
refresh_token = @refresh_token
|
105
|
+
expected = {
|
106
|
+
:error => 'invalid_grant',
|
107
|
+
:error_description => 'invalid refresh token'
|
108
|
+
}
|
109
|
+
response.body.should match_json(expected)
|
110
|
+
end
|
111
|
+
end
|
60
112
|
end
|
61
113
|
end
|
62
114
|
end
|
@@ -1,15 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Devise::Oauth2Providable::AuthorizationsController do
|
4
|
+
before :all do
|
5
|
+
Devise::Oauth2Providable::Engine.load_engine_routes
|
6
|
+
end
|
4
7
|
describe 'routing' do
|
5
8
|
it 'routes POST /oauth2/authorizations' do
|
6
|
-
|
9
|
+
post('/oauth2/authorizations').should route_to('devise/oauth2_providable/authorizations#create')
|
7
10
|
end
|
8
11
|
it 'routes GET /oauth2/authorize' do
|
9
|
-
|
12
|
+
get('/oauth2/authorize').should route_to('devise/oauth2_providable/authorizations#new')
|
10
13
|
end
|
11
14
|
it 'routes POST /oauth2/authorize' do
|
12
|
-
|
15
|
+
#FIXME: this is valid, but the route is not being loaded into the test
|
16
|
+
post('/oauth2/authorize').should route_to('devise/oauth2_providable/authorizations#new')
|
13
17
|
end
|
14
18
|
end
|
15
19
|
end
|
@@ -1,9 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Devise::Oauth2Providable::TokensController do
|
4
|
+
before :all do
|
5
|
+
Devise::Oauth2Providable::Engine.load_engine_routes
|
6
|
+
end
|
4
7
|
describe 'routing' do
|
5
8
|
it 'routes POST /oauth2/token' do
|
6
|
-
|
9
|
+
post('/oauth2/token').should route_to('devise/oauth2_providable/tokens#create')
|
7
10
|
end
|
8
11
|
end
|
9
12
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,15 +1,13 @@
|
|
1
1
|
# Configure Rails Envinronment
|
2
2
|
ENV["RAILS_ENV"] = "test"
|
3
|
-
|
3
|
+
spec_root = File.expand_path('..', __FILE__)
|
4
|
+
require File.expand_path("dummy/config/environment.rb", spec_root)
|
4
5
|
|
5
6
|
require 'pry'
|
6
7
|
require 'rspec/rails'
|
7
8
|
require 'shoulda-matchers'
|
8
|
-
|
9
9
|
require 'factory_girl_rspec'
|
10
|
-
FactoryGirl.definition_file_paths = [
|
11
|
-
File.join(File.dirname(__FILE__), 'factories')
|
12
|
-
]
|
10
|
+
FactoryGirl.definition_file_paths = [File.join(spec_root, 'factories')]
|
13
11
|
FactoryGirl.find_definitions
|
14
12
|
|
15
13
|
ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
|
@@ -27,3 +25,5 @@ RSpec.configure do |config|
|
|
27
25
|
# see http://stackoverflow.com/questions/4401539/rspec-2-how-to-render-views-by-default-for-all-controller-specs
|
28
26
|
config.render_views
|
29
27
|
end
|
28
|
+
|
29
|
+
ActiveRecord::Migrator.migrate(File.expand_path("dummy/db/migrate/", spec_root))
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# see http://www.builtfromsource.com/2011/09/21/testing-routes-with-rails-3-1-engines/
|
2
|
+
module Devise
|
3
|
+
module Oauth2Providable
|
4
|
+
module EngineHacks
|
5
|
+
##
|
6
|
+
# Automatically append all of the current engine's routes to the main
|
7
|
+
# application's route set. This needs to be done for ALL functional tests that
|
8
|
+
# use engine routes, since the mounted routes don't work during tests.
|
9
|
+
#
|
10
|
+
# @param [Symbol] engine_symbol Optional; if provided, uses this symbol to
|
11
|
+
# locate the engine class by name, otherwise uses the module of the calling
|
12
|
+
# test case as the presumed name of the engine.
|
13
|
+
#
|
14
|
+
# @author Jason Hamilton (jhamilton@greatherorift.com)
|
15
|
+
# @author Matthew Ratzloff (matt@urbaninfluence.com)
|
16
|
+
def load_engine_routes(engine_symbol = nil)
|
17
|
+
if engine_symbol
|
18
|
+
engine_name = engine_symbol.to_s.camelize
|
19
|
+
else
|
20
|
+
# No engine provided, so presume the current engine is the one to load
|
21
|
+
engine_name = self.class.name.split("::").first.split("(").last
|
22
|
+
end
|
23
|
+
engine = ("#{engine_name}::Engine").constantize
|
24
|
+
|
25
|
+
engine_name = 'oauth2'
|
26
|
+
engine = Devise::Oauth2Providable::Engine
|
27
|
+
named_routes = engine.routes.named_routes.routes
|
28
|
+
resourced_routes = []
|
29
|
+
|
30
|
+
# Append the routes for this module to the existing routes
|
31
|
+
# ::Rails.application.routes.disable_clear_and_finalize = true
|
32
|
+
# ::Rails.application.routes.clear!
|
33
|
+
# ::Rails.application.routes_reloader.paths.each { |path| load(path) }
|
34
|
+
::Rails.application.routes.draw do
|
35
|
+
|
36
|
+
# unnamed_routes = engine.routes.routes - named_routes.values
|
37
|
+
|
38
|
+
engine.routes.routes.each do |route|
|
39
|
+
# Call the method by hand based on the symbol
|
40
|
+
path = "/#{engine_name.underscore}#{route.path}"
|
41
|
+
requirements = route.requirements
|
42
|
+
if path_helper = named_routes[route]
|
43
|
+
requirements[:as] = path_helper
|
44
|
+
elsif route.requirements[:controller].present?
|
45
|
+
# Presume that all controllers referenced in routes should also be
|
46
|
+
# resources and append that routing on the end so that *_path helpers
|
47
|
+
# will still work
|
48
|
+
resourced_routes << route.requirements[:controller].gsub("#{engine_name.downcase}/", "").to_sym
|
49
|
+
end
|
50
|
+
|
51
|
+
verb = (route.verb.blank? ? "GET" : route.verb).downcase.to_sym
|
52
|
+
send(verb, path, requirements) if respond_to?(verb)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Add each route, once, to the end under a scope to trick path helpers.
|
56
|
+
# This will probably break as soon as there is route name overlap, but
|
57
|
+
# we'll cross that bridge when we get to it.
|
58
|
+
# resourced_routes.uniq!
|
59
|
+
# scope engine_name.downcase do
|
60
|
+
# resourced_routes.each do |resource|
|
61
|
+
# resources resource
|
62
|
+
# end
|
63
|
+
# end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Finalize the routes
|
67
|
+
::Rails.application.routes.finalize!
|
68
|
+
::Rails.application.routes.disable_clear_and_finalize = false
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
Rails::Engine.send(:include, Devise::Oauth2Providable::EngineHacks)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devise_oauth2_providable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 4
|
10
|
+
version: 1.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ryan Sonnek
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-12-05 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rails
|
@@ -175,7 +175,6 @@ extra_rdoc_files: []
|
|
175
175
|
|
176
176
|
files:
|
177
177
|
- .gitignore
|
178
|
-
- .rspec
|
179
178
|
- .rvmrc
|
180
179
|
- CONTRIBUTORS.txt
|
181
180
|
- Gemfile
|
@@ -208,6 +207,7 @@ files:
|
|
208
207
|
- lib/devise/oauth2_providable/version.rb
|
209
208
|
- lib/devise_oauth2_providable.rb
|
210
209
|
- script/rails
|
210
|
+
- spec/controllers/authorizations_controller_spec.rb
|
211
211
|
- spec/controllers/protected_controller_spec.rb
|
212
212
|
- spec/dummy/Rakefile
|
213
213
|
- spec/dummy/app/assets/javascripts/application.js
|
@@ -248,6 +248,7 @@ files:
|
|
248
248
|
- spec/dummy/public/favicon.ico
|
249
249
|
- spec/dummy/script/rails
|
250
250
|
- spec/factories/client_factory.rb
|
251
|
+
- spec/factories/user_factory.rb
|
251
252
|
- spec/integration/oauth2_authorization_token_grant_type_strategy_spec.rb
|
252
253
|
- spec/integration/oauth2_password_grant_type_strategy_spec.rb
|
253
254
|
- spec/integration/oauth2_refresh_token_grant_type_strategy_spec.rb
|
@@ -259,8 +260,8 @@ files:
|
|
259
260
|
- spec/models/user_spec.rb
|
260
261
|
- spec/routing/authorizations_routing_spec.rb
|
261
262
|
- spec/routing/tokens_routing_spec.rb
|
262
|
-
- spec/setup_database.rb
|
263
263
|
- spec/spec_helper.rb
|
264
|
+
- spec/support/inject_engine_routes_into_application.rb
|
264
265
|
- spec/support/match_json.rb
|
265
266
|
homepage: ""
|
266
267
|
licenses: []
|
@@ -296,6 +297,7 @@ signing_key:
|
|
296
297
|
specification_version: 3
|
297
298
|
summary: OAuth2 Provider for Rails3 applications
|
298
299
|
test_files:
|
300
|
+
- spec/controllers/authorizations_controller_spec.rb
|
299
301
|
- spec/controllers/protected_controller_spec.rb
|
300
302
|
- spec/dummy/Rakefile
|
301
303
|
- spec/dummy/app/assets/javascripts/application.js
|
@@ -336,6 +338,7 @@ test_files:
|
|
336
338
|
- spec/dummy/public/favicon.ico
|
337
339
|
- spec/dummy/script/rails
|
338
340
|
- spec/factories/client_factory.rb
|
341
|
+
- spec/factories/user_factory.rb
|
339
342
|
- spec/integration/oauth2_authorization_token_grant_type_strategy_spec.rb
|
340
343
|
- spec/integration/oauth2_password_grant_type_strategy_spec.rb
|
341
344
|
- spec/integration/oauth2_refresh_token_grant_type_strategy_spec.rb
|
@@ -347,6 +350,6 @@ test_files:
|
|
347
350
|
- spec/models/user_spec.rb
|
348
351
|
- spec/routing/authorizations_routing_spec.rb
|
349
352
|
- spec/routing/tokens_routing_spec.rb
|
350
|
-
- spec/setup_database.rb
|
351
353
|
- spec/spec_helper.rb
|
354
|
+
- spec/support/inject_engine_routes_into_application.rb
|
352
355
|
- spec/support/match_json.rb
|
data/.rspec
DELETED
data/spec/setup_database.rb
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
2
|
-
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
3
|
-
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
|
4
|
-
|
5
|
-
ActiveRecord::Schema.define(:version => 1) do
|
6
|
-
|
7
|
-
end
|