doorkeeper 0.4.2 → 0.5.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of doorkeeper might be problematic. Click here for more details.

Files changed (118) hide show
  1. data/.gitignore +2 -0
  2. data/.travis.yml +5 -1
  3. data/CHANGELOG.md +29 -0
  4. data/Gemfile +12 -4
  5. data/README.md +76 -7
  6. data/Rakefile +1 -25
  7. data/app/assets/javascripts/doorkeeper/application.js +0 -7
  8. data/app/controllers/doorkeeper/application_controller.rb +1 -27
  9. data/app/controllers/doorkeeper/applications_controller.rb +14 -6
  10. data/app/controllers/doorkeeper/authorized_applications_controller.rb +1 -1
  11. data/app/controllers/doorkeeper/token_info_controller.rb +11 -0
  12. data/app/controllers/doorkeeper/tokens_controller.rb +11 -8
  13. data/app/validators/redirect_uri_validator.rb +12 -0
  14. data/app/views/doorkeeper/applications/_form.html.erb +3 -3
  15. data/app/views/doorkeeper/applications/edit.html.erb +1 -1
  16. data/app/views/doorkeeper/applications/index.html.erb +4 -4
  17. data/app/views/doorkeeper/applications/new.html.erb +1 -1
  18. data/app/views/doorkeeper/applications/show.html.erb +3 -3
  19. data/app/views/doorkeeper/authorizations/new.html.erb +2 -2
  20. data/app/views/doorkeeper/authorized_applications/index.html.erb +1 -1
  21. data/config/locales/en.yml +35 -0
  22. data/doorkeeper.gemspec +3 -3
  23. data/gemfiles/gemfile.rails-3.1.x +10 -0
  24. data/gemfiles/gemfile.rails-3.2.x +10 -0
  25. data/lib/doorkeeper.rb +10 -3
  26. data/lib/doorkeeper/config.rb +56 -38
  27. data/lib/doorkeeper/doorkeeper_for.rb +2 -0
  28. data/lib/doorkeeper/engine.rb +3 -32
  29. data/lib/doorkeeper/helpers/controller.rb +29 -0
  30. data/lib/doorkeeper/helpers/filter.rb +4 -18
  31. data/{app/models/doorkeeper → lib/doorkeeper/models}/access_grant.rb +7 -7
  32. data/{app/models/doorkeeper → lib/doorkeeper/models}/access_token.rb +27 -24
  33. data/lib/doorkeeper/models/accessible.rb +9 -0
  34. data/lib/doorkeeper/models/active_record/access_grant.rb +5 -0
  35. data/lib/doorkeeper/models/active_record/access_token.rb +15 -0
  36. data/lib/doorkeeper/models/active_record/application.rb +18 -0
  37. data/lib/doorkeeper/models/application.rb +38 -0
  38. data/lib/doorkeeper/models/expirable.rb +6 -4
  39. data/lib/doorkeeper/models/mongoid/access_grant.rb +22 -0
  40. data/lib/doorkeeper/models/mongoid/access_token.rb +35 -0
  41. data/lib/doorkeeper/models/mongoid/application.rb +22 -0
  42. data/lib/doorkeeper/models/mongoid/revocable.rb +15 -0
  43. data/lib/doorkeeper/models/mongoid/scopes.rb +15 -0
  44. data/lib/doorkeeper/models/ownership.rb +16 -0
  45. data/lib/doorkeeper/models/revocable.rb +1 -1
  46. data/lib/doorkeeper/models/scopes.rb +9 -5
  47. data/lib/doorkeeper/oauth/access_token_request.rb +2 -2
  48. data/lib/doorkeeper/oauth/authorization.rb +1 -0
  49. data/lib/doorkeeper/oauth/authorization/code.rb +5 -3
  50. data/lib/doorkeeper/oauth/client.rb +2 -2
  51. data/lib/doorkeeper/oauth/client_credentials_request.rb +4 -1
  52. data/lib/doorkeeper/oauth/helpers/unique_token.rb +2 -5
  53. data/lib/doorkeeper/oauth/password_access_token_request.rb +2 -5
  54. data/lib/doorkeeper/oauth/token.rb +36 -0
  55. data/lib/doorkeeper/rails/routes.rb +77 -0
  56. data/lib/doorkeeper/rails/routes/mapper.rb +28 -0
  57. data/lib/doorkeeper/rails/routes/mapping.rb +39 -0
  58. data/lib/doorkeeper/version.rb +1 -1
  59. data/lib/generators/doorkeeper/application_owner_generator.rb +15 -0
  60. data/lib/generators/doorkeeper/install_generator.rb +2 -9
  61. data/lib/generators/doorkeeper/migration_generator.rb +15 -0
  62. data/lib/generators/doorkeeper/templates/README +15 -1
  63. data/lib/generators/doorkeeper/templates/add_owner_to_application_migration.rb +7 -0
  64. data/lib/generators/doorkeeper/templates/initializer.rb +31 -15
  65. data/lib/generators/doorkeeper/templates/migration.rb +7 -4
  66. data/lib/generators/doorkeeper/views_generator.rb +1 -1
  67. data/script/run_all +3 -0
  68. data/spec/controllers/applications_controller_spec.rb +1 -1
  69. data/spec/controllers/authorizations_controller_spec.rb +4 -4
  70. data/spec/controllers/protected_resources_controller_spec.rb +7 -7
  71. data/spec/controllers/token_info_controller_spec.rb +54 -0
  72. data/spec/controllers/tokens_controller_spec.rb +3 -2
  73. data/spec/dummy/app/controllers/custom_authorizations_controller.rb +7 -0
  74. data/spec/dummy/app/models/user.rb +16 -5
  75. data/spec/dummy/config/application.rb +4 -7
  76. data/spec/dummy/config/boot.rb +3 -7
  77. data/spec/dummy/config/initializers/doorkeeper.rb +13 -0
  78. data/spec/dummy/config/mongoid.yml +7 -0
  79. data/spec/dummy/config/routes.rb +29 -1
  80. data/spec/dummy/db/migrate/20120312140401_add_password_to_users.rb +1 -1
  81. data/spec/dummy/db/migrate/20120524202412_create_doorkeeper_tables.rb +6 -4
  82. data/spec/dummy/db/schema.rb +5 -3
  83. data/spec/generators/application_owner_generator_spec.rb +23 -0
  84. data/spec/generators/install_generator_spec.rb +1 -6
  85. data/spec/generators/migration_generator_spec.rb +20 -0
  86. data/spec/lib/config_spec.rb +72 -4
  87. data/spec/lib/models/expirable_spec.rb +8 -11
  88. data/spec/lib/models/revocable_spec.rb +1 -1
  89. data/spec/lib/oauth/access_token_request_spec.rb +15 -9
  90. data/spec/lib/oauth/authorization_request_spec.rb +1 -0
  91. data/spec/lib/oauth/client_credentials_request_spec.rb +15 -9
  92. data/spec/lib/oauth/client_spec.rb +5 -8
  93. data/spec/lib/oauth/helpers/unique_token_spec.rb +2 -20
  94. data/spec/lib/oauth/password_access_token_request_spec.rb +16 -9
  95. data/spec/lib/oauth/token_spec.rb +83 -0
  96. data/spec/models/doorkeeper/access_token_spec.rb +41 -1
  97. data/spec/models/doorkeeper/application_spec.rb +53 -20
  98. data/spec/requests/flows/authorization_code_spec.rb +1 -1
  99. data/spec/requests/flows/client_credentials_spec.rb +2 -0
  100. data/spec/requests/flows/password_spec.rb +25 -0
  101. data/spec/requests/flows/refresh_token_spec.rb +5 -2
  102. data/spec/requests/protected_resources/private_api_spec.rb +10 -3
  103. data/spec/routing/custom_controller_routes_spec.rb +44 -0
  104. data/spec/routing/default_routes_spec.rb +32 -0
  105. data/spec/spec_helper.rb +1 -0
  106. data/spec/spec_helper_integration.rb +18 -8
  107. data/spec/support/dependencies/factory_girl.rb +0 -3
  108. data/spec/support/orm/active_record.rb +11 -0
  109. data/spec/support/orm/mongoid.rb +26 -0
  110. data/spec/support/shared/controllers_shared_context.rb +2 -2
  111. data/spec/support/shared/models_shared_examples.rb +16 -0
  112. data/spec/validators/redirect_uri_validator_spec.rb +40 -0
  113. metadata +61 -37
  114. data/app/helpers/doorkeeper/application_helper.rb +0 -4
  115. data/app/models/doorkeeper/application.rb +0 -54
  116. data/config/routes.rb +0 -9
  117. data/lib/tasks/doorkeeper_tasks.rake +0 -4
  118. data/spec/support/dependencies/database_cleaner.rb +0 -16
@@ -8,6 +8,6 @@
8
8
 
9
9
  <div class="span6">
10
10
  <h3>Actions</h3>
11
- <p><%= link_to 'Back to application list', applications_path %></p>
11
+ <p><%= link_to 'Back to application list', oauth_applications_path %></p>
12
12
  </div>
13
13
 
@@ -17,7 +17,7 @@
17
17
 
18
18
  <div class="span6">
19
19
  <h3>Actions</h3>
20
- <p><%= link_to 'List all', applications_path %></p>
21
- <p><%= link_to 'Edit', edit_application_path(@application) %></p>
22
- <p><%= link_to 'Remove', @application, :method => :delete, :confirm => "Are you sure?" %></p>
20
+ <p><%= link_to 'List all', oauth_applications_path %></p>
21
+ <p><%= link_to 'Edit', edit_oauth_application_path(@application) %></p>
22
+ <p><%= link_to 'Remove', [:oauth, @application], :method => :delete, :data => { :confirm => "Are you sure?" } %></p>
23
23
  </div>
@@ -15,7 +15,7 @@
15
15
  <% end %>
16
16
 
17
17
  <div class="inline_block">
18
- <%= form_for @authorization, :as => :authorization, :url => authorization_path, :method => :post do |f| %>
18
+ <%= form_for @authorization, :as => :authorization, :url => oauth_authorization_path, :method => :post do |f| %>
19
19
  <%= f.hidden_field :client_id %>
20
20
  <%= f.hidden_field :redirect_uri %>
21
21
  <%= f.hidden_field :state %>
@@ -25,7 +25,7 @@
25
25
  <% end %>
26
26
  </div>
27
27
  <div class="inline_block">
28
- <%= form_for @authorization, :as => :authorization, :url => authorization_path, :method => :delete do |f| %>
28
+ <%= form_for @authorization, :as => :authorization, :url => oauth_authorization_path, :method => :delete do |f| %>
29
29
  <%= f.hidden_field :client_id %>
30
30
  <%= f.hidden_field :redirect_uri %>
31
31
  <%= f.hidden_field :state %>
@@ -17,7 +17,7 @@
17
17
  <tr>
18
18
  <td><%= application.name %></td>
19
19
  <td><%= application.created_at %></td>
20
- <td><%= link_to 'Revoke', authorized_application_path(application), :confirm => 'Are you sure?', :method => :delete, :class => 'btn danger' %></td>
20
+ <td><%= link_to 'Revoke', oauth_authorized_application_path(application), :data => { :confirm => 'Are you sure?' }, :method => :delete, :class => 'btn danger' %></td>
21
21
  </tr>
22
22
  <% end %>
23
23
  </tbody>
@@ -1,4 +1,24 @@
1
1
  en:
2
+ activerecord:
3
+ errors:
4
+ models:
5
+ application:
6
+ attributes:
7
+ redirect_uri:
8
+ fragment_present: 'cannot contain a fragment.'
9
+ has_query_parameter: 'cannot contain a query parameter.'
10
+ invalid_uri: 'must be a valid URI.'
11
+ relative_uri: 'must be an absolute URI.'
12
+ mongoid:
13
+ errors:
14
+ models:
15
+ application:
16
+ attributes:
17
+ redirect_uri:
18
+ fragment_present: 'cannot contain a fragment.'
19
+ has_query_parameter: 'cannot contain a query parameter.'
20
+ invalid_uri: 'must be a valid URI.'
21
+ relative_uri: 'must be an absolute URI.'
2
22
  doorkeeper:
3
23
  errors:
4
24
  messages:
@@ -11,6 +31,10 @@ en:
11
31
  server_error: 'The authorization server encountered an unexpected condition which prevented it from fulfilling the request.'
12
32
  temporarily_unavailable: 'The authorization server is currently unable to handle the request due to a temporary overloading or maintenance of the server.'
13
33
 
34
+ #configuration error messages
35
+ credential_flow_not_configured: 'Resource Owner Password Credentials flow failed due to Doorkeeper.configure.resource_owner_from_credentials being unconfigured.'
36
+ resource_owner_authenticator_not_configured: 'Resource Owner find failed due to Doorkeeper.configure.resource_owner_authenticator being unconfiged.'
37
+
14
38
  # Access grant errors
15
39
  unsupported_response_type: 'The authorization server does not support this response type.'
16
40
 
@@ -21,3 +45,14 @@ en:
21
45
 
22
46
  # Password Access token errors
23
47
  invalid_resource_owner: 'The provided resource owner credentials are not valid, or resource owner cannot be found'
48
+ flash:
49
+ applications:
50
+ create:
51
+ notice: 'Application created.'
52
+ destroy:
53
+ notice: 'Application deleted.'
54
+ update:
55
+ notice: 'Application updated.'
56
+ authorized_applications:
57
+ destroy:
58
+ notice: 'Application revoked.'
data/doorkeeper.gemspec CHANGED
@@ -18,11 +18,11 @@ Gem::Specification.new do |s|
18
18
  s.add_dependency "railties", "~> 3.1"
19
19
 
20
20
  s.add_development_dependency "sqlite3", "~> 1.3.5"
21
- s.add_development_dependency "rspec-rails", "~> 2.10.0"
21
+ s.add_development_dependency "rspec-rails", "~> 2.11.0"
22
22
  s.add_development_dependency "capybara", "~> 1.1.2"
23
23
  s.add_development_dependency "generator_spec", "~> 0.8.5"
24
24
  s.add_development_dependency "factory_girl", "~> 2.6.4"
25
- s.add_development_dependency "timecop", "~> 0.3.5"
26
- s.add_development_dependency "database_cleaner", "~> 0.7.1"
25
+ s.add_development_dependency "timecop", "~> 0.4.3"
26
+ s.add_development_dependency "database_cleaner", "~> 0.8"
27
27
  s.add_development_dependency "bcrypt-ruby", "~> 3.0.1"
28
28
  end
@@ -2,6 +2,16 @@ source 'http://rubygems.org'
2
2
 
3
3
  gem 'rails', '~> 3.1.0'
4
4
  gem 'jquery-rails'
5
+
6
+ group :mongoid do
7
+ gem 'mongoid', '~> 2.4'
8
+ gem 'bson_ext', '~> 1.6.0'
9
+ end
10
+
11
+ group :active_record do
12
+ gem 'activerecord', '~> 3.1'
13
+ end
14
+
5
15
  gem 'doorkeeper', :path => '../'
6
16
 
7
17
  gemspec :path => '../'
@@ -2,6 +2,16 @@ source 'http://rubygems.org'
2
2
 
3
3
  gem 'rails', '~> 3.2.0'
4
4
  gem 'jquery-rails'
5
+
6
+ group :mongoid do
7
+ gem 'mongoid', '~> 2.4'
8
+ gem 'bson_ext', '~> 1.6.0'
9
+ end
10
+
11
+ group :active_record do
12
+ gem 'activerecord', '~> 3.1'
13
+ end
14
+
5
15
  gem 'doorkeeper', :path => '../'
6
16
 
7
17
  gemspec :path => '../'
data/lib/doorkeeper.rb CHANGED
@@ -16,6 +16,7 @@ module Doorkeeper
16
16
  autoload :ClientCredentialsRequest, "doorkeeper/oauth/client_credentials_request"
17
17
  autoload :Authorization, "doorkeeper/oauth/authorization"
18
18
  autoload :Client, "doorkeeper/oauth/client"
19
+ autoload :Token, "doorkeeper/oauth/token"
19
20
 
20
21
  module Helpers
21
22
  autoload :ScopeChecker, "doorkeeper/oauth/helpers/scope_checker"
@@ -25,13 +26,19 @@ module Doorkeeper
25
26
  end
26
27
 
27
28
  module Models
28
- autoload :Expirable, "doorkeeper/models/expirable"
29
- autoload :Revocable, "doorkeeper/models/revocable"
30
- autoload :Scopes, "doorkeeper/models/scopes"
29
+ autoload :Scopes, 'doorkeeper/models/scopes'
30
+ autoload :Expirable, 'doorkeeper/models/expirable'
31
+ autoload :Revocable, 'doorkeeper/models/revocable'
32
+ autoload :Accessible, 'doorkeeper/models/accessible'
31
33
  end
32
34
 
33
35
  module Helpers
34
36
  autoload :Filter, "doorkeeper/helpers/filter"
37
+ autoload :Controller, "doorkeeper/helpers/controller"
38
+ end
39
+
40
+ module Rails
41
+ autoload :Routes, "doorkeeper/rails/routes"
35
42
  end
36
43
 
37
44
  def self.configured?
@@ -1,37 +1,30 @@
1
1
  module Doorkeeper
2
2
  def self.configure(&block)
3
3
  @config = Config::Builder.new(&block).build
4
+ enable_orm
5
+ setup_application_owner if @config.enable_application_owner?
4
6
  end
5
7
 
6
8
  def self.configuration
7
9
  @config
8
10
  end
9
11
 
10
- class Config
11
- class Builder
12
- # Helper class to migrate scopes using authorization_scopes block
13
- # It will be removed in v0.5.x
14
- class ScopesMigrator
15
- attr_accessor :default_scopes, :optional_scopes, :translations
16
-
17
- def initialize
18
- @default_scopes, @optional_scopes, @translations = [], [], {}
19
- end
20
-
21
- def scope(scope, options = {})
22
- if options[:default]
23
- @optional_scopes << scope
24
- else
25
- @default_scopes << scope
26
- end
27
- @translations[scope] = options[:description]
28
- end
12
+ def self.enable_orm
13
+ require "doorkeeper/models/#{@config.orm}/access_grant"
14
+ require "doorkeeper/models/#{@config.orm}/access_token"
15
+ require "doorkeeper/models/#{@config.orm}/application"
16
+ require 'doorkeeper/models/access_grant'
17
+ require 'doorkeeper/models/access_token'
18
+ require 'doorkeeper/models/application'
19
+ end
29
20
 
30
- def migrate(&block)
31
- self.instance_eval(&block)
32
- end
33
- end
21
+ def self.setup_application_owner
22
+ require File.join(File.dirname(__FILE__), 'models', 'ownership')
23
+ Doorkeeper::Application.send :include, Doorkeeper::Models::Ownership
24
+ end
34
25
 
26
+ class Config
27
+ class Builder
35
28
  def initialize(&block)
36
29
  @config = Config.new
37
30
  instance_eval(&block)
@@ -41,6 +34,15 @@ module Doorkeeper
41
34
  @config
42
35
  end
43
36
 
37
+ def enable_application_owner(opts={})
38
+ @config.instance_variable_set("@enable_application_owner", true)
39
+ confirm_application_owner if opts[:confirmation].present? && opts[:confirmation]
40
+ end
41
+
42
+ def confirm_application_owner
43
+ @config.instance_variable_set("@confirm_application_owner", true)
44
+ end
45
+
44
46
  def default_scopes(*scopes)
45
47
  @config.instance_variable_set("@default_scopes", Doorkeeper::OAuth::Scopes.from_array(scopes))
46
48
  end
@@ -53,17 +55,12 @@ module Doorkeeper
53
55
  @config.instance_variable_set("@client_credentials", methods)
54
56
  end
55
57
 
56
- def use_refresh_token
57
- @config.instance_variable_set("@refresh_token_enabled", true)
58
+ def access_token_methods(*methods)
59
+ @config.instance_variable_set("@access_token_methods", methods)
58
60
  end
59
61
 
60
- # DEPRECATED: use default/optional scopes
61
- def authorization_scopes(&block)
62
- migrator = ScopesMigrator.new
63
- migrator.migrate(&block)
64
- self.default_scopes *migrator.default_scopes
65
- self.optional_scopes *migrator.optional_scopes
66
- @config.instance_variable_set("@authorization_scopes", migrator)
62
+ def use_refresh_token
63
+ @config.instance_variable_set("@refresh_token_enabled", true)
67
64
  end
68
65
  end
69
66
 
@@ -102,6 +99,7 @@ module Doorkeeper
102
99
 
103
100
  Builder.instance_eval do
104
101
  define_method name do |*args, &block|
102
+ # TODO: is builder_class option being used?
105
103
  value = unless attribute_builder
106
104
  block ? block : args.first
107
105
  else
@@ -130,15 +128,36 @@ module Doorkeeper
130
128
 
131
129
  extend Option
132
130
 
133
- option :resource_owner_authenticator, :as => :authenticate_resource_owner
134
- option :admin_authenticator, :as => :authenticate_admin
135
- option :resource_owner_from_credentials
131
+ option :resource_owner_authenticator,
132
+ :as => :authenticate_resource_owner,
133
+ :default => lambda{|routes|
134
+ logger.warn(I18n.translate('doorkeeper.errors.messages.resource_owner_authenticator_not_configured'))
135
+ nil
136
+ }
137
+ option :admin_authenticator,
138
+ :as => :authenticate_admin,
139
+ :default => lambda{|routes| }
140
+ option :resource_owner_from_credentials,
141
+ :default => lambda{|routes|
142
+ logger.warn(I18n.translate('doorkeeper.errors.messages.credential_flow_not_configured'))
143
+ nil
144
+ }
136
145
  option :access_token_expires_in, :default => 7200
146
+ option :authorization_code_expires_in,:default => 600
147
+ option :orm, :default => :active_record
137
148
 
138
149
  def refresh_token_enabled?
139
150
  !!@refresh_token_enabled
140
151
  end
141
152
 
153
+ def enable_application_owner?
154
+ !!@enable_application_owner
155
+ end
156
+
157
+ def confirm_application_owner?
158
+ !!@confirm_application_owner
159
+ end
160
+
142
161
  def default_scopes
143
162
  @default_scopes ||= Doorkeeper::OAuth::Scopes.new
144
163
  end
@@ -155,9 +174,8 @@ module Doorkeeper
155
174
  @client_credentials ||= [:from_basic, :from_params]
156
175
  end
157
176
 
158
- # DEPRECATED: use default/optional scopes
159
- def authorization_scopes
160
- @authorization_scopes
177
+ def access_token_methods
178
+ @access_token_methods ||= [:from_bearer_authorization, :from_access_token_param, :from_bearer_param]
161
179
  end
162
180
  end
163
181
  end
@@ -11,6 +11,7 @@ module Doorkeeper
11
11
  end
12
12
  end
13
13
 
14
+ # TODO: move this to Token class
14
15
  def validate_token(token)
15
16
  return false unless token
16
17
  token.accessible? and validate_token_scopes(token)
@@ -33,6 +34,7 @@ module Doorkeeper
33
34
  @filter_options[:unless] = unless_block
34
35
  end
35
36
 
37
+ # TODO: move this to Token class
36
38
  def validate_token_scopes(token)
37
39
  return true if @scopes.blank?
38
40
  token.scopes.any? { |scope| @scopes.include? scope}
@@ -1,37 +1,8 @@
1
1
  module Doorkeeper
2
2
  class Engine < Rails::Engine
3
- isolate_namespace Doorkeeper
4
-
5
- config.generators do |g|
6
- g.test_framework :rspec, :view_specs => false
7
- end
8
-
9
- initializer "doorkeeper.deprecations" do
10
- if Doorkeeper.installed?
11
- if Doorkeeper.configuration.authorization_scopes.present?
12
- warning = <<-WARN
13
- [DOORKEEPER]
14
- Configuration for `authorization_scopes` will no longer be supported. Use default_scopes/optional_scopes instead.
15
- ATTENTION: The :description option could not be migrated because doorkeeper now uses localization files.
16
- Place this in your config/locales/en.yml
17
- en:
18
- doorkeeper:
19
- scopes:
20
- WARN
21
- puts warning
22
- Doorkeeper.configuration.authorization_scopes.translations.each do |scope, translation|
23
- puts " #{scope}: #{translation}"
24
- end
25
- end
26
-
27
- if Doorkeeper::AccessToken.columns_hash["resource_owner_id"].null == false
28
- warn <<-WARN
29
- [DOORKEEPER]
30
- In order to use the Client Credentials flow, you have to migrate the oauth_access_tokens table:
31
- change_column :oauth_access_tokens, :resource_owner_id, :integer, :null => true
32
- WARN
33
- end
34
- end
3
+ initializer "doorkeeper.routes" do
4
+ Doorkeeper::Rails::Routes.warn_if_using_mount_method!
5
+ Doorkeeper::Rails::Routes.install!
35
6
  end
36
7
 
37
8
  initializer "doorkeeper.helpers" do
@@ -0,0 +1,29 @@
1
+ module Doorkeeper
2
+ module Helpers
3
+ module Controller
4
+ def self.included(base)
5
+ base.send :private,
6
+ :authenticate_resource_owner!,
7
+ :authenticate_admin!,
8
+ :current_resource_owner,
9
+ :resource_owner_from_credentials
10
+ end
11
+
12
+ def authenticate_resource_owner!
13
+ current_resource_owner
14
+ end
15
+
16
+ def current_resource_owner
17
+ instance_eval &Doorkeeper.configuration.authenticate_resource_owner
18
+ end
19
+
20
+ def resource_owner_from_credentials
21
+ instance_eval &Doorkeeper.configuration.resource_owner_from_credentials
22
+ end
23
+
24
+ def authenticate_admin!
25
+ instance_eval &Doorkeeper.configuration.authenticate_admin
26
+ end
27
+ end
28
+ end
29
+ end
@@ -7,6 +7,7 @@ module Doorkeeper
7
7
 
8
8
  before_filter doorkeeper_for.filter_options do
9
9
  return if doorkeeper_for.validate_token(doorkeeper_token)
10
+ # TODO: use ErrorRespose class for this
10
11
  render_options = doorkeeper_unauthorized_render_options
11
12
  if render_options.nil? || render_options.empty?
12
13
  head :unauthorized
@@ -21,27 +22,12 @@ module Doorkeeper
21
22
 
22
23
  def self.included(base)
23
24
  base.extend ClassMethods
24
- base.send :private,
25
- :doorkeeper_token,
26
- :get_doorkeeper_token,
27
- :authorization_bearer_token,
28
- :doorkeeper_unauthorized_render_options
25
+ base.send :private, :doorkeeper_token, :doorkeeper_unauthorized_render_options
29
26
  end
30
27
 
31
28
  def doorkeeper_token
32
- @token ||= get_doorkeeper_token
33
- end
34
-
35
- def get_doorkeeper_token
36
- token = params[:access_token] || params[:bearer_token] || authorization_bearer_token
37
- if token
38
- AccessToken.find_by_token(token)
39
- end
40
- end
41
-
42
- def authorization_bearer_token
43
- header = request.env['HTTP_AUTHORIZATION']
44
- header.gsub(/^Bearer /, '') if header && header.match(/^Bearer /)
29
+ methods = Doorkeeper.configuration.access_token_methods
30
+ @token ||= OAuth::Token.authenticate request, *methods
45
31
  end
46
32
 
47
33
  def doorkeeper_unauthorized_render_options