doorkeeper 3.1.0 → 4.0.0

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 (89) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +8 -10
  3. data/CONTRIBUTING.md +2 -0
  4. data/Gemfile +8 -4
  5. data/NEWS.md +57 -2
  6. data/README.md +48 -40
  7. data/Rakefile +1 -1
  8. data/app/controllers/doorkeeper/application_metal_controller.rb +1 -2
  9. data/app/controllers/doorkeeper/applications_controller.rb +2 -2
  10. data/app/controllers/doorkeeper/authorizations_controller.rb +1 -1
  11. data/app/controllers/doorkeeper/authorized_applications_controller.rb +1 -1
  12. data/app/controllers/doorkeeper/tokens_controller.rb +1 -1
  13. data/app/helpers/doorkeeper/dashboard_helper.rb +13 -11
  14. data/app/views/doorkeeper/applications/show.html.erb +1 -1
  15. data/app/views/layouts/doorkeeper/admin.html.erb +1 -1
  16. data/config/locales/en.yml +1 -0
  17. data/doorkeeper.gemspec +7 -6
  18. data/lib/doorkeeper/config.rb +10 -15
  19. data/lib/doorkeeper/engine.rb +11 -7
  20. data/lib/doorkeeper/helpers/controller.rb +1 -1
  21. data/lib/doorkeeper/models/access_grant_mixin.rb +9 -5
  22. data/lib/doorkeeper/models/access_token_mixin.rb +28 -22
  23. data/lib/doorkeeper/models/application_mixin.rb +3 -7
  24. data/lib/doorkeeper/models/concerns/expirable.rb +2 -2
  25. data/lib/doorkeeper/models/concerns/ownership.rb +6 -1
  26. data/lib/doorkeeper/models/concerns/revocable.rb +19 -2
  27. data/lib/doorkeeper/oauth/authorization/uri_builder.rb +1 -1
  28. data/lib/doorkeeper/oauth/client/credentials.rb +1 -1
  29. data/lib/doorkeeper/oauth/client_credentials/issuer.rb +2 -1
  30. data/lib/doorkeeper/oauth/client_credentials_request.rb +7 -4
  31. data/lib/doorkeeper/oauth/code_response.rb +13 -14
  32. data/lib/doorkeeper/oauth/helpers/scope_checker.rb +1 -1
  33. data/lib/doorkeeper/oauth/helpers/uri_checker.rb +2 -1
  34. data/lib/doorkeeper/oauth/password_access_token_request.rb +6 -10
  35. data/lib/doorkeeper/oauth/refresh_token_request.rb +23 -11
  36. data/lib/doorkeeper/oauth/scopes.rb +2 -2
  37. data/lib/doorkeeper/oauth/token.rb +6 -5
  38. data/lib/doorkeeper/oauth/token_response.rb +1 -1
  39. data/lib/doorkeeper/orm/active_record/access_token.rb +8 -0
  40. data/lib/doorkeeper/orm/active_record/application.rb +2 -7
  41. data/lib/doorkeeper/orm/active_record.rb +0 -16
  42. data/lib/doorkeeper/rails/helpers.rb +1 -1
  43. data/lib/doorkeeper/rails/routes/mapper.rb +1 -1
  44. data/lib/doorkeeper/rails/routes.rb +2 -1
  45. data/lib/doorkeeper/request/password.rb +11 -1
  46. data/lib/doorkeeper/version.rb +1 -1
  47. data/lib/doorkeeper.rb +1 -1
  48. data/lib/generators/doorkeeper/previous_refresh_token_generator.rb +29 -0
  49. data/lib/generators/doorkeeper/templates/add_owner_to_application_migration.rb +1 -1
  50. data/lib/generators/doorkeeper/templates/add_previous_refresh_token_to_access_tokens.rb +11 -0
  51. data/lib/generators/doorkeeper/templates/initializer.rb +2 -2
  52. data/lib/generators/doorkeeper/templates/migration.rb +23 -5
  53. data/spec/controllers/authorizations_controller_spec.rb +0 -14
  54. data/spec/controllers/protected_resources_controller_spec.rb +47 -18
  55. data/spec/dummy/app/controllers/full_protected_resources_controller.rb +4 -4
  56. data/spec/dummy/app/controllers/home_controller.rb +1 -1
  57. data/spec/dummy/app/controllers/metal_controller.rb +1 -1
  58. data/spec/dummy/app/controllers/semi_protected_resources_controller.rb +3 -3
  59. data/spec/dummy/app/models/user.rb +0 -4
  60. data/spec/dummy/config/application.rb +2 -36
  61. data/spec/dummy/config/environment.rb +1 -1
  62. data/spec/dummy/config/environments/test.rb +4 -15
  63. data/spec/dummy/config/initializers/active_record_belongs_to_required_by_default.rb +6 -0
  64. data/spec/dummy/config/initializers/doorkeeper.rb +2 -2
  65. data/spec/dummy/db/migrate/{20130902165751_create_doorkeeper_tables.rb → 20151223192035_create_doorkeeper_tables.rb} +24 -5
  66. data/spec/dummy/db/migrate/{20130902175349_add_owner_to_application.rb → 20151223200000_add_owner_to_application.rb} +0 -0
  67. data/spec/dummy/db/migrate/20160320211015_add_previous_refresh_token_to_access_tokens.rb +11 -0
  68. data/spec/dummy/db/schema.rb +23 -22
  69. data/spec/lib/config_spec.rb +2 -2
  70. data/spec/lib/models/revocable_spec.rb +27 -4
  71. data/spec/lib/oauth/authorization_code_request_spec.rb +1 -1
  72. data/spec/lib/oauth/code_response_spec.rb +34 -0
  73. data/spec/lib/oauth/password_access_token_request_spec.rb +5 -5
  74. data/spec/lib/oauth/refresh_token_request_spec.rb +34 -3
  75. data/spec/lib/oauth/scopes_spec.rb +0 -1
  76. data/spec/lib/oauth/token_spec.rb +12 -5
  77. data/spec/models/doorkeeper/access_token_spec.rb +45 -1
  78. data/spec/models/doorkeeper/application_spec.rb +2 -10
  79. data/spec/requests/flows/password_spec.rb +26 -5
  80. data/spec/requests/flows/refresh_token_spec.rb +87 -17
  81. data/spec/spec_helper_integration.rb +3 -0
  82. data/spec/support/helpers/model_helper.rb +27 -5
  83. data/spec/support/http_method_shim.rb +24 -0
  84. data/spec/support/shared/controllers_shared_context.rb +13 -4
  85. data/spec/support/shared/models_shared_examples.rb +1 -1
  86. metadata +52 -32
  87. data/lib/generators/doorkeeper/application_scopes_generator.rb +0 -34
  88. data/lib/generators/doorkeeper/templates/add_scopes_to_oauth_applications.rb +0 -5
  89. data/spec/dummy/db/migrate/20141209001746_add_scopes_to_oauth_applications.rb +0 -5
@@ -10,20 +10,21 @@ module Doorkeeper
10
10
  include ActiveModel::MassAssignmentSecurity if defined?(::ProtectedAttributes)
11
11
 
12
12
  included do
13
- belongs_to :application,
14
- class_name: 'Doorkeeper::Application',
15
- inverse_of: :access_tokens
13
+ belongs_to_options = {
14
+ class_name: 'Doorkeeper::Application',
15
+ inverse_of: :access_tokens
16
+ }
17
+ if defined?(ActiveRecord::Base) && ActiveRecord::VERSION::MAJOR >= 5
18
+ belongs_to_options[:optional] = true
19
+ end
20
+
21
+ belongs_to :application, belongs_to_options
16
22
 
17
23
  validates :token, presence: true, uniqueness: true
18
24
  validates :refresh_token, uniqueness: true, if: :use_refresh_token?
19
25
 
20
26
  attr_writer :use_refresh_token
21
27
 
22
- if respond_to?(:attr_accessible)
23
- attr_accessible :application_id, :resource_owner_id, :expires_in,
24
- :scopes, :use_refresh_token
25
- end
26
-
27
28
  before_validation :generate_token, on: :create
28
29
  before_validation :generate_refresh_token,
29
30
  on: :create,
@@ -32,18 +33,18 @@ module Doorkeeper
32
33
 
33
34
  module ClassMethods
34
35
  def by_token(token)
35
- where(token: token.to_s).limit(1).to_a.first
36
+ find_by(token: token.to_s)
36
37
  end
37
38
 
38
39
  def by_refresh_token(refresh_token)
39
- where(refresh_token: refresh_token.to_s).first
40
+ find_by(refresh_token: refresh_token.to_s)
40
41
  end
41
42
 
42
43
  def revoke_all_for(application_id, resource_owner)
43
44
  where(application_id: application_id,
44
45
  resource_owner_id: resource_owner.id,
45
46
  revoked_at: nil).
46
- map(&:revoke)
47
+ each(&:revoke)
47
48
  end
48
49
 
49
50
  def matching_token_for(application, resource_owner_or_id, scopes)
@@ -74,6 +75,7 @@ module Doorkeeper
74
75
  return access_token
75
76
  end
76
77
  end
78
+
77
79
  create!(
78
80
  application_id: application.try(:id),
79
81
  resource_owner_id: resource_owner_id,
@@ -84,13 +86,10 @@ module Doorkeeper
84
86
  end
85
87
 
86
88
  def last_authorized_token_for(application_id, resource_owner_id)
87
- where(application_id: application_id,
88
- resource_owner_id: resource_owner_id,
89
- revoked_at: nil).
90
- send(order_method, created_at_desc).
91
- limit(1).
92
- to_a.
93
- first
89
+ send(order_method, created_at_desc).
90
+ find_by(application_id: application_id,
91
+ resource_owner_id: resource_owner_id,
92
+ revoked_at: nil)
94
93
  end
95
94
  end
96
95
 
@@ -99,6 +98,7 @@ module Doorkeeper
99
98
  end
100
99
 
101
100
  def use_refresh_token?
101
+ @use_refresh_token ||= false
102
102
  !!@use_refresh_token
103
103
  end
104
104
 
@@ -108,7 +108,7 @@ module Doorkeeper
108
108
  scopes: scopes,
109
109
  expires_in_seconds: expires_in_seconds,
110
110
  application: { uid: application.try(:uid) },
111
- created_at: created_at.to_i,
111
+ created_at: created_at.to_i
112
112
  }
113
113
  end
114
114
 
@@ -129,10 +129,16 @@ module Doorkeeper
129
129
  end
130
130
 
131
131
  def generate_token
132
+ self.created_at ||= Time.now.utc
133
+
132
134
  generator = Doorkeeper.configuration.access_token_generator.constantize
133
- self.token = generator.generate(resource_owner_id: resource_owner_id,
134
- scopes: scopes, application: application,
135
- expires_in: expires_in)
135
+ self.token = generator.generate(
136
+ resource_owner_id: resource_owner_id,
137
+ scopes: scopes,
138
+ application: application,
139
+ expires_in: expires_in,
140
+ created_at: created_at
141
+ )
136
142
  rescue NoMethodError
137
143
  raise Errors::UnableToGenerateToken, "#{generator} does not respond to `.generate`."
138
144
  rescue NameError
@@ -15,19 +15,15 @@ module Doorkeeper
15
15
  validates :redirect_uri, redirect_uri: true
16
16
 
17
17
  before_validation :generate_uid, :generate_secret, on: :create
18
-
19
- if respond_to?(:attr_accessible)
20
- attr_accessible :name, :redirect_uri, :scopes
21
- end
22
18
  end
23
19
 
24
20
  module ClassMethods
25
21
  def by_uid_and_secret(uid, secret)
26
- where(uid: uid.to_s, secret: secret.to_s).limit(1).to_a.first
22
+ find_by(uid: uid.to_s, secret: secret.to_s)
27
23
  end
28
24
 
29
25
  def by_uid(uid)
30
- where(uid: uid.to_s).limit(1).to_a.first
26
+ find_by(uid: uid.to_s)
31
27
  end
32
28
  end
33
29
 
@@ -35,7 +31,7 @@ module Doorkeeper
35
31
 
36
32
  def has_scopes?
37
33
  Doorkeeper.configuration.orm != :active_record ||
38
- Application.new.attributes.include?("scopes")
34
+ Doorkeeper::Application.column_names.include?("scopes")
39
35
  end
40
36
 
41
37
  def generate_uid
@@ -2,12 +2,12 @@ module Doorkeeper
2
2
  module Models
3
3
  module Expirable
4
4
  def expired?
5
- expires_in && Time.now > expired_time
5
+ expires_in && Time.now.utc > expired_time
6
6
  end
7
7
 
8
8
  def expires_in_seconds
9
9
  return nil if expires_in.nil?
10
- expires = (created_at + expires_in.seconds) - Time.now
10
+ expires = (created_at + expires_in.seconds) - Time.now.utc
11
11
  expires_sec = expires.seconds.round(0)
12
12
  expires_sec > 0 ? expires_sec : 0
13
13
  end
@@ -4,7 +4,12 @@ module Doorkeeper
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
- belongs_to :owner, polymorphic: true
7
+ belongs_to_options = { polymorphic: true }
8
+ if defined?(ActiveRecord::Base) && ActiveRecord::VERSION::MAJOR >= 5
9
+ belongs_to_options[:optional] = true
10
+ end
11
+
12
+ belongs_to :owner, belongs_to_options
8
13
  validates :owner, presence: true, if: :validate_owner?
9
14
  end
10
15
 
@@ -2,11 +2,28 @@ module Doorkeeper
2
2
  module Models
3
3
  module Revocable
4
4
  def revoke(clock = Time)
5
- update_attribute :revoked_at, clock.now
5
+ update_attribute :revoked_at, clock.now.utc
6
6
  end
7
7
 
8
8
  def revoked?
9
- !!(revoked_at && revoked_at <= Time.now)
9
+ !!(revoked_at && revoked_at <= Time.now.utc)
10
+ end
11
+
12
+ def revoke_previous_refresh_token!
13
+ return unless refresh_token_revoked_on_use?
14
+ old_refresh_token.revoke if old_refresh_token
15
+ update_attribute :previous_refresh_token, ""
16
+ end
17
+
18
+ private
19
+
20
+ def old_refresh_token
21
+ @old_refresh_token ||=
22
+ AccessToken.by_refresh_token(previous_refresh_token)
23
+ end
24
+
25
+ def refresh_token_revoked_on_use?
26
+ AccessToken.refresh_token_revoked_on_use?
10
27
  end
11
28
  end
12
29
  end
@@ -20,7 +20,7 @@ module Doorkeeper
20
20
  end
21
21
 
22
22
  def build_query(parameters = {})
23
- parameters = parameters.reject { |k, v| v.blank? }
23
+ parameters = parameters.reject { |_, v| v.blank? }
24
24
  super parameters
25
25
  end
26
26
  end
@@ -7,7 +7,7 @@ module Doorkeeper
7
7
  def self.from_request(request, *credentials_methods)
8
8
  credentials_methods.inject(nil) do |credentials, method|
9
9
  method = self.method(method) if method.is_a?(Symbol)
10
- credentials = Credentials.new *method.call(request)
10
+ credentials = Credentials.new(*method.call(request))
11
11
  break credentials unless credentials.blank?
12
12
  end
13
13
  end
@@ -7,7 +7,8 @@ module Doorkeeper
7
7
  attr_accessor :token, :validation, :error
8
8
 
9
9
  def initialize(server, validation)
10
- @server, @validation = server, validation
10
+ @server = server
11
+ @validation = validation
11
12
  end
12
13
 
13
14
  def create(client, scopes, creator = Creator.new)
@@ -8,9 +8,11 @@ module Doorkeeper
8
8
  include Validations
9
9
  include OAuth::RequestConcern
10
10
 
11
- attr_accessor :issuer, :server, :client, :original_scopes
11
+ attr_accessor :server, :client, :original_scopes
12
12
  attr_reader :response
13
- alias :error_response :response
13
+ attr_writer :issuer
14
+
15
+ alias_method :error_response, :response
14
16
 
15
17
  delegate :error, to: :issuer
16
18
 
@@ -19,8 +21,9 @@ module Doorkeeper
19
21
  end
20
22
 
21
23
  def initialize(server, client, parameters = {})
22
- @client, @server = client, server
23
- @response = nil
24
+ @client = client
25
+ @server = server
26
+ @response = nil
24
27
  @original_scopes = parameters[:scope]
25
28
  end
26
29
 
@@ -7,7 +7,8 @@ module Doorkeeper
7
7
  attr_accessor :pre_auth, :auth, :response_on_fragment
8
8
 
9
9
  def initialize(pre_auth, auth, options = {})
10
- @pre_auth, @auth = pre_auth, auth
10
+ @pre_auth = pre_auth
11
+ @auth = auth
11
12
  @response_on_fragment = options[:response_on_fragment]
12
13
  end
13
14
 
@@ -18,20 +19,18 @@ module Doorkeeper
18
19
  def redirect_uri
19
20
  if URIChecker.native_uri? pre_auth.redirect_uri
20
21
  auth.native_redirect
22
+ elsif response_on_fragment
23
+ uri_with_fragment(
24
+ pre_auth.redirect_uri,
25
+ access_token: auth.token.token,
26
+ token_type: auth.token.token_type,
27
+ expires_in: auth.token.expires_in_seconds,
28
+ state: pre_auth.state
29
+ )
21
30
  else
22
- if response_on_fragment
23
- uri_with_fragment(
24
- pre_auth.redirect_uri,
25
- access_token: auth.token.token,
26
- token_type: auth.token.token_type,
27
- expires_in: auth.token.expires_in,
28
- state: pre_auth.state
29
- )
30
- else
31
- uri_with_query pre_auth.redirect_uri,
32
- code: auth.token.token,
33
- state: pre_auth.state
34
- end
31
+ uri_with_query pre_auth.redirect_uri,
32
+ code: auth.token.token,
33
+ state: pre_auth.state
35
34
  end
36
35
  end
37
36
  end
@@ -13,7 +13,7 @@ module Doorkeeper
13
13
 
14
14
  def valid?
15
15
  scope_str.present? &&
16
- scope_str !~ /[\n|\r|\t]/ &&
16
+ scope_str !~ /[\n\r\t]/ &&
17
17
  @valid_scopes.has_scopes?(parsed_scopes)
18
18
  end
19
19
 
@@ -10,7 +10,8 @@ module Doorkeeper
10
10
  end
11
11
 
12
12
  def self.matches?(url, client_url)
13
- url, client_url = as_uri(url), as_uri(client_url)
13
+ url = as_uri(url)
14
+ client_url = as_uri(client_url)
14
15
  url.query = nil
15
16
  url == client_url
16
17
  end
@@ -9,19 +9,15 @@ module Doorkeeper
9
9
  validate :resource_owner, error: :invalid_grant
10
10
  validate :scopes, error: :invalid_scope
11
11
 
12
- attr_accessor :server, :resource_owner, :credentials, :access_token
13
- attr_accessor :client
12
+ attr_accessor :server, :client, :resource_owner, :parameters,
13
+ :access_token
14
14
 
15
- def initialize(server, credentials, resource_owner, parameters = {})
15
+ def initialize(server, client, resource_owner, parameters = {})
16
16
  @server = server
17
17
  @resource_owner = resource_owner
18
- @credentials = credentials
18
+ @client = client
19
+ @parameters = parameters
19
20
  @original_scopes = parameters[:scope]
20
-
21
- if credentials
22
- @client = Application.by_uid_and_secret credentials.uid,
23
- credentials.secret
24
- end
25
21
  end
26
22
 
27
23
  private
@@ -40,7 +36,7 @@ module Doorkeeper
40
36
  end
41
37
 
42
38
  def validate_client
43
- !credentials || !!client
39
+ !parameters[:client_id] || !!client
44
40
  end
45
41
  end
46
42
  end
@@ -12,7 +12,9 @@ module Doorkeeper
12
12
  validate :scope, error: :invalid_scope
13
13
 
14
14
  attr_accessor :access_token, :client, :credentials, :refresh_token,
15
- :server
15
+ :server, :refresh_token_parameter
16
+
17
+ private :refresh_token_parameter, :refresh_token_parameter=
16
18
 
17
19
  def initialize(server, refresh_token, credentials, parameters = {})
18
20
  @server = server
@@ -29,34 +31,44 @@ module Doorkeeper
29
31
 
30
32
  private
31
33
 
32
- attr_reader :refresh_token_parameter
33
-
34
34
  def before_successful_response
35
35
  refresh_token.transaction do
36
36
  refresh_token.lock!
37
37
  raise Errors::InvalidTokenReuse if refresh_token.revoked?
38
38
 
39
- refresh_token.revoke
39
+ refresh_token.revoke unless refresh_token_revoked_on_use?
40
40
  create_access_token
41
41
  end
42
42
  end
43
43
 
44
+ def refresh_token_revoked_on_use?
45
+ Doorkeeper::AccessToken.refresh_token_revoked_on_use?
46
+ end
47
+
44
48
  def default_scopes
45
49
  refresh_token.scopes
46
50
  end
47
51
 
48
52
  def create_access_token
49
- expires_in = Authorization::Token.access_token_expires_in(
50
- server,
51
- client
52
- )
53
+ @access_token = AccessToken.create!(access_token_attributes)
54
+ end
53
55
 
54
- @access_token = AccessToken.create!(
56
+ def access_token_attributes
57
+ {
55
58
  application_id: refresh_token.application_id,
56
59
  resource_owner_id: refresh_token.resource_owner_id,
57
60
  scopes: scopes.to_s,
58
- expires_in: expires_in,
59
- use_refresh_token: true)
61
+ expires_in: access_token_expires_in,
62
+ use_refresh_token: true
63
+ }.tap do |attributes|
64
+ if refresh_token_revoked_on_use?
65
+ attributes[:previous_refresh_token] = refresh_token.refresh_token
66
+ end
67
+ end
68
+ end
69
+
70
+ def access_token_expires_in
71
+ Authorization::Token.access_token_expires_in(server, client)
60
72
  end
61
73
 
62
74
  def validate_token_presence
@@ -46,14 +46,14 @@ module Doorkeeper
46
46
 
47
47
  def +(other)
48
48
  if other.is_a? Scopes
49
- self.class.from_array(self.all + other.all)
49
+ self.class.from_array(all + other.all)
50
50
  else
51
51
  super(other)
52
52
  end
53
53
  end
54
54
 
55
55
  def <=>(other)
56
- self.map(&:to_s).sort <=> other.map(&:to_s).sort
56
+ map(&:to_s).sort <=> other.map(&:to_s).sort
57
57
  end
58
58
 
59
59
  def &(other)
@@ -26,12 +26,11 @@ module Doorkeeper
26
26
 
27
27
  def token_from_basic_header(header, pattern)
28
28
  encoded_header = token_from_header(header, pattern)
29
- token, _ = decode_basic_credentials(encoded_header)
30
- token
29
+ decode_basic_credentials_token(encoded_header)
31
30
  end
32
31
 
33
- def decode_basic_credentials(encoded_header)
34
- Base64.decode64(encoded_header).split(/:/, 2)
32
+ def decode_basic_credentials_token(encoded_header)
33
+ Base64.decode64(encoded_header).split(/:/, 2).first
35
34
  end
36
35
 
37
36
  def token_from_header(header, pattern)
@@ -55,7 +54,9 @@ module Doorkeeper
55
54
 
56
55
  def self.authenticate(request, *methods)
57
56
  if token = from_request(request, *methods)
58
- AccessToken.by_token(token)
57
+ access_token = AccessToken.by_token(token)
58
+ access_token.revoke_previous_refresh_token! if access_token
59
+ access_token
59
60
  end
60
61
  end
61
62
  end
@@ -14,7 +14,7 @@ module Doorkeeper
14
14
  'expires_in' => token.expires_in_seconds,
15
15
  'refresh_token' => token.refresh_token,
16
16
  'scope' => token.scopes_string,
17
- 'created_at' => token.created_at.to_i,
17
+ 'created_at' => token.created_at.to_i
18
18
  }.reject { |_, value| value.blank? }
19
19
  end
20
20
 
@@ -10,10 +10,18 @@ module Doorkeeper
10
10
  end
11
11
  private_class_method :delete_all_for
12
12
 
13
+ def self.active_for(resource_owner)
14
+ where(resource_owner_id: resource_owner.id, revoked_at: nil)
15
+ end
16
+
13
17
  def self.order_method
14
18
  :order
15
19
  end
16
20
 
21
+ def self.refresh_token_revoked_on_use?
22
+ column_names.include?('previous_refresh_token')
23
+ end
24
+
17
25
  def self.created_at_desc
18
26
  'created_at desc'
19
27
  end
@@ -11,14 +11,9 @@ module Doorkeeper
11
11
  end
12
12
  has_many :authorized_applications, through: :authorized_tokens, source: :application
13
13
 
14
- def self.column_names_with_table
15
- self.column_names.map { |c| "#{table_name}.#{c}" }
16
- end
17
-
18
14
  def self.authorized_for(resource_owner)
19
- joins(:authorized_applications).
20
- where(AccessToken.table_name => { resource_owner_id: resource_owner.id, revoked_at: nil }).
21
- group(column_names_with_table.join(','))
15
+ resource_access_tokens = AccessToken.active_for(resource_owner)
16
+ where(id: resource_access_tokens.select(:application_id).distinct)
22
17
  end
23
18
  end
24
19
  end
@@ -18,22 +18,6 @@ module Doorkeeper
18
18
 
19
19
  Doorkeeper::Application.send :include, Doorkeeper::Models::Ownership
20
20
  end
21
-
22
- def self.check_requirements!(_config)
23
- if ::ActiveRecord::Base.connected? &&
24
- ::ActiveRecord::Base.connection.table_exists?(
25
- Doorkeeper::Application.table_name
26
- )
27
- unless Doorkeeper::Application.new.attributes.include?("scopes")
28
- migration_path = '../../../generators/doorkeeper/templates/add_scopes_to_oauth_applications.rb'
29
- puts <<-MSG.squish
30
- [doorkeeper] Missing column: `oauth_applications.scopes`.
31
- Create the following migration and run `rake db:migrate`.
32
- MSG
33
- puts File.read(File.expand_path(migration_path, __FILE__))
34
- end
35
- end
36
- end
37
21
  end
38
22
  end
39
23
  end
@@ -6,7 +6,7 @@ module Doorkeeper
6
6
  def doorkeeper_authorize!(*scopes)
7
7
  @_doorkeeper_scopes = scopes.presence || Doorkeeper.configuration.default_scopes
8
8
 
9
- if !valid_doorkeeper_token?
9
+ unless valid_doorkeeper_token?
10
10
  doorkeeper_render_error
11
11
  end
12
12
  end
@@ -7,7 +7,7 @@ module Doorkeeper
7
7
  end
8
8
 
9
9
  def map(&block)
10
- self.instance_eval(&block) if block
10
+ instance_eval(&block) if block
11
11
  @mapping
12
12
  end
13
13
 
@@ -18,7 +18,8 @@ module Doorkeeper
18
18
  attr_accessor :routes
19
19
 
20
20
  def initialize(routes, &block)
21
- @routes, @block = routes, block
21
+ @routes = routes
22
+ @block = block
22
23
  end
23
24
 
24
25
  def generate_routes!(options)
@@ -8,11 +8,21 @@ module Doorkeeper
8
8
  def request
9
9
  @request ||= OAuth::PasswordAccessTokenRequest.new(
10
10
  Doorkeeper.configuration,
11
- credentials,
11
+ client,
12
12
  resource_owner,
13
13
  parameters
14
14
  )
15
15
  end
16
+
17
+ private
18
+
19
+ def client
20
+ if credentials
21
+ server.client
22
+ elsif parameters[:client_id]
23
+ server.client_via_uid
24
+ end
25
+ end
16
26
  end
17
27
  end
18
28
  end
@@ -1,3 +1,3 @@
1
1
  module Doorkeeper
2
- VERSION = "3.1.0"
2
+ VERSION = "4.0.0".freeze
3
3
  end
data/lib/doorkeeper.rb CHANGED
@@ -54,7 +54,7 @@ module Doorkeeper
54
54
  end
55
55
 
56
56
  def self.database_installed?
57
- [AccessToken, AccessGrant, Application].all? { |model| model.table_exists? }
57
+ [AccessToken, AccessGrant, Application].all?(&:table_exists?)
58
58
  end
59
59
 
60
60
  def self.installed?
@@ -0,0 +1,29 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ class Doorkeeper::PreviousRefreshTokenGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+ source_root File.expand_path('../templates', __FILE__)
6
+ desc 'Support revoke refresh token on access token use'
7
+
8
+ def self.next_migration_number(path)
9
+ ActiveRecord::Generators::Base.next_migration_number(path)
10
+ end
11
+
12
+ def previous_refresh_token
13
+ if no_previous_refresh_token_column?
14
+ migration_template(
15
+ 'add_previous_refresh_token_to_access_tokens.rb',
16
+ 'db/migrate/add_previous_refresh_token_to_access_tokens.rb'
17
+ )
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def no_previous_refresh_token_column?
24
+ !ActiveRecord::Base.connection.column_exists?(
25
+ :oauth_access_tokens,
26
+ :previous_refresh_token
27
+ )
28
+ end
29
+ end
@@ -4,4 +4,4 @@ class AddOwnerToApplication < ActiveRecord::Migration
4
4
  add_column :oauth_applications, :owner_type, :string, null: true
5
5
  add_index :oauth_applications, [:owner_id, :owner_type]
6
6
  end
7
- end
7
+ end
@@ -0,0 +1,11 @@
1
+ class AddPreviousRefreshTokenToAccessTokens < ActiveRecord::Migration
2
+ def change
3
+ add_column(
4
+ :oauth_access_tokens,
5
+ :previous_refresh_token,
6
+ :string,
7
+ default: "",
8
+ null: false
9
+ )
10
+ end
11
+ end