doorkeeper 4.4.0 → 5.6.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (284) hide show
  1. checksums.yaml +5 -5
  2. data/{NEWS.md → CHANGELOG.md} +471 -16
  3. data/README.md +108 -403
  4. data/app/assets/stylesheets/doorkeeper/admin/application.css +2 -2
  5. data/app/controllers/doorkeeper/application_controller.rb +8 -5
  6. data/app/controllers/doorkeeper/application_metal_controller.rb +7 -11
  7. data/app/controllers/doorkeeper/applications_controller.rb +62 -27
  8. data/app/controllers/doorkeeper/authorizations_controller.rb +112 -18
  9. data/app/controllers/doorkeeper/authorized_applications_controller.rb +22 -3
  10. data/app/controllers/doorkeeper/token_info_controller.rb +16 -4
  11. data/app/controllers/doorkeeper/tokens_controller.rb +104 -35
  12. data/app/helpers/doorkeeper/dashboard_helper.rb +9 -7
  13. data/app/views/doorkeeper/applications/_delete_form.html.erb +3 -1
  14. data/app/views/doorkeeper/applications/_form.html.erb +27 -26
  15. data/app/views/doorkeeper/applications/edit.html.erb +1 -1
  16. data/app/views/doorkeeper/applications/index.html.erb +17 -7
  17. data/app/views/doorkeeper/applications/new.html.erb +1 -1
  18. data/app/views/doorkeeper/applications/show.html.erb +38 -17
  19. data/app/views/doorkeeper/authorizations/error.html.erb +4 -2
  20. data/app/views/doorkeeper/authorizations/form_post.html.erb +15 -0
  21. data/app/views/doorkeeper/authorizations/new.html.erb +16 -10
  22. data/app/views/layouts/doorkeeper/admin.html.erb +16 -14
  23. data/config/locales/en.yml +28 -5
  24. data/lib/doorkeeper/config/abstract_builder.rb +28 -0
  25. data/lib/doorkeeper/config/option.rb +82 -0
  26. data/lib/doorkeeper/config/validations.rb +53 -0
  27. data/lib/doorkeeper/config.rb +477 -142
  28. data/lib/doorkeeper/engine.rb +17 -4
  29. data/lib/doorkeeper/errors.rb +25 -16
  30. data/lib/doorkeeper/grant_flow/fallback_flow.rb +15 -0
  31. data/lib/doorkeeper/grant_flow/flow.rb +44 -0
  32. data/lib/doorkeeper/grant_flow/registry.rb +50 -0
  33. data/lib/doorkeeper/grant_flow.rb +45 -0
  34. data/lib/doorkeeper/grape/authorization_decorator.rb +6 -4
  35. data/lib/doorkeeper/grape/helpers.rb +13 -7
  36. data/lib/doorkeeper/helpers/controller.rb +43 -10
  37. data/lib/doorkeeper/models/access_grant_mixin.rb +97 -3
  38. data/lib/doorkeeper/models/access_token_mixin.rb +273 -67
  39. data/lib/doorkeeper/models/application_mixin.rb +50 -5
  40. data/lib/doorkeeper/models/concerns/accessible.rb +2 -0
  41. data/lib/doorkeeper/models/concerns/expirable.rb +7 -3
  42. data/lib/doorkeeper/models/concerns/expiration_time_sql_math.rb +88 -0
  43. data/lib/doorkeeper/models/concerns/orderable.rb +2 -0
  44. data/lib/doorkeeper/models/concerns/ownership.rb +4 -7
  45. data/lib/doorkeeper/models/concerns/polymorphic_resource_owner.rb +30 -0
  46. data/lib/doorkeeper/models/concerns/resource_ownerable.rb +47 -0
  47. data/lib/doorkeeper/models/concerns/reusable.rb +19 -0
  48. data/lib/doorkeeper/models/concerns/revocable.rb +3 -27
  49. data/lib/doorkeeper/models/concerns/scopes.rb +12 -2
  50. data/lib/doorkeeper/models/concerns/secret_storable.rb +106 -0
  51. data/lib/doorkeeper/oauth/authorization/code.rb +54 -12
  52. data/lib/doorkeeper/oauth/authorization/context.rb +17 -0
  53. data/lib/doorkeeper/oauth/authorization/token.rb +64 -24
  54. data/lib/doorkeeper/oauth/authorization/uri_builder.rb +7 -5
  55. data/lib/doorkeeper/oauth/authorization_code_request.rb +69 -11
  56. data/lib/doorkeeper/oauth/base_request.rb +36 -24
  57. data/lib/doorkeeper/oauth/base_response.rb +2 -0
  58. data/lib/doorkeeper/oauth/client/credentials.rb +5 -5
  59. data/lib/doorkeeper/oauth/client.rb +10 -11
  60. data/lib/doorkeeper/oauth/client_credentials/creator.rb +44 -4
  61. data/lib/doorkeeper/oauth/client_credentials/issuer.rb +16 -9
  62. data/lib/doorkeeper/oauth/client_credentials/validator.rb +55 -0
  63. data/lib/doorkeeper/oauth/client_credentials_request.rb +10 -11
  64. data/lib/doorkeeper/oauth/code_request.rb +8 -12
  65. data/lib/doorkeeper/oauth/code_response.rb +27 -15
  66. data/lib/doorkeeper/oauth/error.rb +3 -1
  67. data/lib/doorkeeper/oauth/error_response.rb +34 -14
  68. data/lib/doorkeeper/oauth/forbidden_token_response.rb +11 -3
  69. data/lib/doorkeeper/oauth/helpers/scope_checker.rb +23 -18
  70. data/lib/doorkeeper/oauth/helpers/unique_token.rb +20 -3
  71. data/lib/doorkeeper/oauth/helpers/uri_checker.rb +42 -6
  72. data/lib/doorkeeper/oauth/hooks/context.rb +21 -0
  73. data/lib/doorkeeper/oauth/invalid_request_response.rb +43 -0
  74. data/lib/doorkeeper/oauth/invalid_token_response.rb +29 -4
  75. data/lib/doorkeeper/oauth/nonstandard.rb +39 -0
  76. data/lib/doorkeeper/oauth/password_access_token_request.rb +43 -10
  77. data/lib/doorkeeper/oauth/pre_authorization.rb +136 -26
  78. data/lib/doorkeeper/oauth/refresh_token_request.rb +67 -31
  79. data/lib/doorkeeper/oauth/scopes.rb +8 -4
  80. data/lib/doorkeeper/oauth/token.rb +12 -8
  81. data/lib/doorkeeper/oauth/token_introspection.rb +99 -25
  82. data/lib/doorkeeper/oauth/token_request.rb +8 -20
  83. data/lib/doorkeeper/oauth/token_response.rb +13 -10
  84. data/lib/doorkeeper/oauth.rb +13 -0
  85. data/lib/doorkeeper/orm/active_record/access_grant.rb +5 -30
  86. data/lib/doorkeeper/orm/active_record/access_token.rb +5 -43
  87. data/lib/doorkeeper/orm/active_record/application.rb +6 -57
  88. data/lib/doorkeeper/orm/active_record/mixins/access_grant.rb +63 -0
  89. data/lib/doorkeeper/orm/active_record/mixins/access_token.rb +77 -0
  90. data/lib/doorkeeper/orm/active_record/mixins/application.rb +210 -0
  91. data/lib/doorkeeper/orm/active_record/redirect_uri_validator.rb +66 -0
  92. data/lib/doorkeeper/orm/active_record/stale_records_cleaner.rb +36 -0
  93. data/lib/doorkeeper/orm/active_record.rb +31 -20
  94. data/lib/doorkeeper/rails/helpers.rb +10 -8
  95. data/lib/doorkeeper/rails/routes/abstract_router.rb +35 -0
  96. data/lib/doorkeeper/rails/routes/mapper.rb +4 -2
  97. data/lib/doorkeeper/rails/routes/mapping.rb +9 -7
  98. data/lib/doorkeeper/rails/routes/registry.rb +45 -0
  99. data/lib/doorkeeper/rails/routes.rb +45 -25
  100. data/lib/doorkeeper/rake/db.rake +40 -0
  101. data/lib/doorkeeper/rake/setup.rake +6 -0
  102. data/lib/doorkeeper/rake.rb +14 -0
  103. data/lib/doorkeeper/request/authorization_code.rb +6 -4
  104. data/lib/doorkeeper/request/client_credentials.rb +3 -3
  105. data/lib/doorkeeper/request/code.rb +1 -1
  106. data/lib/doorkeeper/request/password.rb +4 -3
  107. data/lib/doorkeeper/request/refresh_token.rb +6 -5
  108. data/lib/doorkeeper/request/strategy.rb +4 -2
  109. data/lib/doorkeeper/request/token.rb +1 -1
  110. data/lib/doorkeeper/request.rb +61 -34
  111. data/lib/doorkeeper/secret_storing/base.rb +64 -0
  112. data/lib/doorkeeper/secret_storing/bcrypt.rb +60 -0
  113. data/lib/doorkeeper/secret_storing/plain.rb +33 -0
  114. data/lib/doorkeeper/secret_storing/sha256_hash.rb +26 -0
  115. data/lib/doorkeeper/server.rb +9 -11
  116. data/lib/doorkeeper/stale_records_cleaner.rb +24 -0
  117. data/lib/doorkeeper/validations.rb +2 -0
  118. data/lib/doorkeeper/version.rb +7 -29
  119. data/lib/doorkeeper.rb +180 -65
  120. data/lib/generators/doorkeeper/application_owner_generator.rb +24 -18
  121. data/lib/generators/doorkeeper/confidential_applications_generator.rb +33 -0
  122. data/lib/generators/doorkeeper/enable_polymorphic_resource_owner_generator.rb +39 -0
  123. data/lib/generators/doorkeeper/install_generator.rb +19 -9
  124. data/lib/generators/doorkeeper/migration_generator.rb +23 -18
  125. data/lib/generators/doorkeeper/pkce_generator.rb +33 -0
  126. data/lib/generators/doorkeeper/previous_refresh_token_generator.rb +28 -22
  127. data/{spec/dummy/db/migrate/20180210183654_add_confidential_to_application.rb → lib/generators/doorkeeper/templates/add_confidential_to_applications.rb.erb} +2 -2
  128. data/lib/generators/doorkeeper/templates/add_owner_to_application_migration.rb.erb +3 -1
  129. data/lib/generators/doorkeeper/templates/add_previous_refresh_token_to_access_tokens.rb.erb +2 -0
  130. data/lib/generators/doorkeeper/templates/enable_pkce_migration.rb.erb +8 -0
  131. data/lib/generators/doorkeeper/templates/enable_polymorphic_resource_owner_migration.rb.erb +17 -0
  132. data/lib/generators/doorkeeper/templates/initializer.rb +402 -32
  133. data/lib/generators/doorkeeper/templates/migration.rb.erb +47 -18
  134. data/lib/generators/doorkeeper/views_generator.rb +8 -4
  135. data/vendor/assets/stylesheets/doorkeeper/bootstrap.min.css +4 -5
  136. metadata +97 -309
  137. data/.coveralls.yml +0 -1
  138. data/.github/ISSUE_TEMPLATE.md +0 -25
  139. data/.github/PULL_REQUEST_TEMPLATE.md +0 -17
  140. data/.gitignore +0 -19
  141. data/.hound.yml +0 -2
  142. data/.rspec +0 -1
  143. data/.rubocop.yml +0 -17
  144. data/.travis.yml +0 -38
  145. data/Appraisals +0 -18
  146. data/CODE_OF_CONDUCT.md +0 -46
  147. data/CONTRIBUTING.md +0 -47
  148. data/Gemfile +0 -10
  149. data/RELEASING.md +0 -10
  150. data/Rakefile +0 -20
  151. data/SECURITY.md +0 -15
  152. data/app/validators/redirect_uri_validator.rb +0 -44
  153. data/doorkeeper.gemspec +0 -32
  154. data/gemfiles/rails_4_2.gemfile +0 -13
  155. data/gemfiles/rails_5_0.gemfile +0 -12
  156. data/gemfiles/rails_5_1.gemfile +0 -12
  157. data/gemfiles/rails_5_2.gemfile +0 -12
  158. data/gemfiles/rails_master.gemfile +0 -14
  159. data/lib/doorkeeper/oauth/client_credentials/validation.rb +0 -45
  160. data/lib/generators/doorkeeper/add_client_confidentiality_generator.rb +0 -31
  161. data/lib/generators/doorkeeper/templates/add_confidential_to_application_migration.rb.erb +0 -11
  162. data/spec/controllers/application_metal_controller.rb +0 -10
  163. data/spec/controllers/applications_controller_spec.rb +0 -69
  164. data/spec/controllers/authorizations_controller_spec.rb +0 -218
  165. data/spec/controllers/protected_resources_controller_spec.rb +0 -309
  166. data/spec/controllers/token_info_controller_spec.rb +0 -56
  167. data/spec/controllers/tokens_controller_spec.rb +0 -274
  168. data/spec/dummy/Rakefile +0 -7
  169. data/spec/dummy/app/controllers/application_controller.rb +0 -3
  170. data/spec/dummy/app/controllers/custom_authorizations_controller.rb +0 -7
  171. data/spec/dummy/app/controllers/full_protected_resources_controller.rb +0 -12
  172. data/spec/dummy/app/controllers/home_controller.rb +0 -17
  173. data/spec/dummy/app/controllers/metal_controller.rb +0 -11
  174. data/spec/dummy/app/controllers/semi_protected_resources_controller.rb +0 -11
  175. data/spec/dummy/app/helpers/application_helper.rb +0 -5
  176. data/spec/dummy/app/models/user.rb +0 -5
  177. data/spec/dummy/app/views/home/index.html.erb +0 -0
  178. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  179. data/spec/dummy/config/application.rb +0 -23
  180. data/spec/dummy/config/boot.rb +0 -9
  181. data/spec/dummy/config/database.yml +0 -15
  182. data/spec/dummy/config/environment.rb +0 -5
  183. data/spec/dummy/config/environments/development.rb +0 -29
  184. data/spec/dummy/config/environments/production.rb +0 -62
  185. data/spec/dummy/config/environments/test.rb +0 -44
  186. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  187. data/spec/dummy/config/initializers/doorkeeper.rb +0 -107
  188. data/spec/dummy/config/initializers/new_framework_defaults.rb +0 -6
  189. data/spec/dummy/config/initializers/secret_token.rb +0 -8
  190. data/spec/dummy/config/initializers/session_store.rb +0 -8
  191. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  192. data/spec/dummy/config/locales/doorkeeper.en.yml +0 -5
  193. data/spec/dummy/config/routes.rb +0 -52
  194. data/spec/dummy/config.ru +0 -4
  195. data/spec/dummy/db/migrate/20111122132257_create_users.rb +0 -11
  196. data/spec/dummy/db/migrate/20120312140401_add_password_to_users.rb +0 -7
  197. data/spec/dummy/db/migrate/20151223192035_create_doorkeeper_tables.rb +0 -62
  198. data/spec/dummy/db/migrate/20151223200000_add_owner_to_application.rb +0 -9
  199. data/spec/dummy/db/migrate/20160320211015_add_previous_refresh_token_to_access_tokens.rb +0 -13
  200. data/spec/dummy/db/schema.rb +0 -68
  201. data/spec/dummy/public/404.html +0 -26
  202. data/spec/dummy/public/422.html +0 -26
  203. data/spec/dummy/public/500.html +0 -26
  204. data/spec/dummy/public/favicon.ico +0 -0
  205. data/spec/dummy/script/rails +0 -6
  206. data/spec/factories.rb +0 -28
  207. data/spec/generators/application_owner_generator_spec.rb +0 -41
  208. data/spec/generators/install_generator_spec.rb +0 -31
  209. data/spec/generators/migration_generator_spec.rb +0 -41
  210. data/spec/generators/previous_refresh_token_generator_spec.rb +0 -57
  211. data/spec/generators/templates/routes.rb +0 -3
  212. data/spec/generators/views_generator_spec.rb +0 -27
  213. data/spec/grape/grape_integration_spec.rb +0 -135
  214. data/spec/helpers/doorkeeper/dashboard_helper_spec.rb +0 -24
  215. data/spec/lib/config_spec.rb +0 -437
  216. data/spec/lib/doorkeeper_spec.rb +0 -150
  217. data/spec/lib/models/expirable_spec.rb +0 -50
  218. data/spec/lib/models/revocable_spec.rb +0 -59
  219. data/spec/lib/models/scopes_spec.rb +0 -43
  220. data/spec/lib/oauth/authorization/uri_builder_spec.rb +0 -41
  221. data/spec/lib/oauth/authorization_code_request_spec.rb +0 -108
  222. data/spec/lib/oauth/base_request_spec.rb +0 -155
  223. data/spec/lib/oauth/base_response_spec.rb +0 -45
  224. data/spec/lib/oauth/client/credentials_spec.rb +0 -90
  225. data/spec/lib/oauth/client_credentials/creator_spec.rb +0 -44
  226. data/spec/lib/oauth/client_credentials/issuer_spec.rb +0 -86
  227. data/spec/lib/oauth/client_credentials/validation_spec.rb +0 -54
  228. data/spec/lib/oauth/client_credentials_integration_spec.rb +0 -27
  229. data/spec/lib/oauth/client_credentials_request_spec.rb +0 -105
  230. data/spec/lib/oauth/client_spec.rb +0 -39
  231. data/spec/lib/oauth/code_request_spec.rb +0 -43
  232. data/spec/lib/oauth/code_response_spec.rb +0 -34
  233. data/spec/lib/oauth/error_response_spec.rb +0 -61
  234. data/spec/lib/oauth/error_spec.rb +0 -23
  235. data/spec/lib/oauth/forbidden_token_response_spec.rb +0 -23
  236. data/spec/lib/oauth/helpers/scope_checker_spec.rb +0 -64
  237. data/spec/lib/oauth/helpers/unique_token_spec.rb +0 -20
  238. data/spec/lib/oauth/helpers/uri_checker_spec.rb +0 -213
  239. data/spec/lib/oauth/invalid_token_response_spec.rb +0 -56
  240. data/spec/lib/oauth/password_access_token_request_spec.rb +0 -96
  241. data/spec/lib/oauth/pre_authorization_spec.rb +0 -155
  242. data/spec/lib/oauth/refresh_token_request_spec.rb +0 -166
  243. data/spec/lib/oauth/scopes_spec.rb +0 -149
  244. data/spec/lib/oauth/token_request_spec.rb +0 -96
  245. data/spec/lib/oauth/token_response_spec.rb +0 -85
  246. data/spec/lib/oauth/token_spec.rb +0 -116
  247. data/spec/lib/request/strategy_spec.rb +0 -53
  248. data/spec/lib/server_spec.rb +0 -59
  249. data/spec/models/doorkeeper/access_grant_spec.rb +0 -36
  250. data/spec/models/doorkeeper/access_token_spec.rb +0 -418
  251. data/spec/models/doorkeeper/application_spec.rb +0 -286
  252. data/spec/requests/applications/applications_request_spec.rb +0 -94
  253. data/spec/requests/applications/authorized_applications_spec.rb +0 -30
  254. data/spec/requests/endpoints/authorization_spec.rb +0 -71
  255. data/spec/requests/endpoints/token_spec.rb +0 -71
  256. data/spec/requests/flows/authorization_code_errors_spec.rb +0 -76
  257. data/spec/requests/flows/authorization_code_spec.rb +0 -149
  258. data/spec/requests/flows/client_credentials_spec.rb +0 -86
  259. data/spec/requests/flows/implicit_grant_errors_spec.rb +0 -32
  260. data/spec/requests/flows/implicit_grant_spec.rb +0 -61
  261. data/spec/requests/flows/password_spec.rb +0 -197
  262. data/spec/requests/flows/refresh_token_spec.rb +0 -174
  263. data/spec/requests/flows/revoke_token_spec.rb +0 -157
  264. data/spec/requests/flows/skip_authorization_spec.rb +0 -59
  265. data/spec/requests/protected_resources/metal_spec.rb +0 -14
  266. data/spec/requests/protected_resources/private_api_spec.rb +0 -81
  267. data/spec/routing/custom_controller_routes_spec.rb +0 -75
  268. data/spec/routing/default_routes_spec.rb +0 -39
  269. data/spec/routing/scoped_routes_spec.rb +0 -31
  270. data/spec/spec_helper.rb +0 -4
  271. data/spec/spec_helper_integration.rb +0 -74
  272. data/spec/support/dependencies/factory_girl.rb +0 -2
  273. data/spec/support/helpers/access_token_request_helper.rb +0 -11
  274. data/spec/support/helpers/authorization_request_helper.rb +0 -41
  275. data/spec/support/helpers/config_helper.rb +0 -9
  276. data/spec/support/helpers/model_helper.rb +0 -72
  277. data/spec/support/helpers/request_spec_helper.rb +0 -88
  278. data/spec/support/helpers/url_helper.rb +0 -56
  279. data/spec/support/http_method_shim.rb +0 -38
  280. data/spec/support/orm/active_record.rb +0 -3
  281. data/spec/support/shared/controllers_shared_context.rb +0 -65
  282. data/spec/support/shared/models_shared_examples.rb +0 -52
  283. data/spec/validators/redirect_uri_validator_spec.rb +0 -123
  284. data/spec/version/version_spec.rb +0 -15
@@ -1,26 +1,32 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Doorkeeper
2
4
  module AccessTokenMixin
3
5
  extend ActiveSupport::Concern
4
6
 
5
7
  include OAuth::Helpers
6
8
  include Models::Expirable
9
+ include Models::Reusable
7
10
  include Models::Revocable
8
11
  include Models::Accessible
9
12
  include Models::Orderable
13
+ include Models::SecretStorable
10
14
  include Models::Scopes
15
+ include Models::ResourceOwnerable
16
+ include Models::ExpirationTimeSqlMath
11
17
 
12
18
  module ClassMethods
13
19
  # Returns an instance of the Doorkeeper::AccessToken with
14
- # specific token value.
20
+ # specific plain text token value.
15
21
  #
16
22
  # @param token [#to_s]
17
- # token value (any object that responds to `#to_s`)
23
+ # Plain text token value (any object that responds to `#to_s`)
18
24
  #
19
25
  # @return [Doorkeeper::AccessToken, nil] AccessToken object or nil
20
26
  # if there is no record with such token
21
27
  #
22
28
  def by_token(token)
23
- find_by(token: token.to_s)
29
+ find_by_plaintext_token(:token, token)
24
30
  end
25
31
 
26
32
  # Returns an instance of the Doorkeeper::AccessToken
@@ -33,7 +39,22 @@ module Doorkeeper
33
39
  # if there is no record with such refresh token
34
40
  #
35
41
  def by_refresh_token(refresh_token)
36
- find_by(refresh_token: refresh_token.to_s)
42
+ find_by_plaintext_token(:refresh_token, refresh_token)
43
+ end
44
+
45
+ # Returns an instance of the Doorkeeper::AccessToken
46
+ # found by previous refresh token. Keep in mind that value
47
+ # of the previous_refresh_token isn't encrypted using
48
+ # secrets strategy.
49
+ #
50
+ # @param previous_refresh_token [#to_s]
51
+ # previous refresh token value (any object that responds to `#to_s`)
52
+ #
53
+ # @return [Doorkeeper::AccessToken, nil] AccessToken object or nil
54
+ # if there is no record with such refresh token
55
+ #
56
+ def by_previous_refresh_token(previous_refresh_token)
57
+ find_by(refresh_token: previous_refresh_token)
37
58
  end
38
59
 
39
60
  # Revokes AccessToken records that have not been revoked and associated
@@ -41,22 +62,24 @@ module Doorkeeper
41
62
  #
42
63
  # @param application_id [Integer]
43
64
  # ID of the Application
44
- # @param resource_owner [ActiveRecord::Base]
45
- # instance of the Resource Owner model
65
+ # @param resource_owner [ActiveRecord::Base, Integer]
66
+ # instance of the Resource Owner model or it's ID
46
67
  #
47
68
  def revoke_all_for(application_id, resource_owner, clock = Time)
48
- where(application_id: application_id,
49
- resource_owner_id: resource_owner.id,
50
- revoked_at: nil).
51
- update_all(revoked_at: clock.now.utc)
69
+ by_resource_owner(resource_owner)
70
+ .where(
71
+ application_id: application_id,
72
+ revoked_at: nil,
73
+ )
74
+ .update_all(revoked_at: clock.now.utc)
52
75
  end
53
76
 
54
- # Looking for not expired Access Token with a matching set of scopes
77
+ # Looking for not revoked Access Token with a matching set of scopes
55
78
  # that belongs to specific Application and Resource Owner.
56
79
  #
57
80
  # @param application [Doorkeeper::Application]
58
81
  # Application instance
59
- # @param resource_owner_or_id [ActiveRecord::Base, Integer]
82
+ # @param resource_owner [ActiveRecord::Base, Integer]
60
83
  # Resource Owner model instance or it's ID
61
84
  # @param scopes [String, Doorkeeper::OAuth::Scopes]
62
85
  # set of scopes
@@ -64,37 +87,76 @@ module Doorkeeper
64
87
  # @return [Doorkeeper::AccessToken, nil] Access Token instance or
65
88
  # nil if matching record was not found
66
89
  #
67
- def matching_token_for(application, resource_owner_or_id, scopes)
68
- resource_owner_id = if resource_owner_or_id.respond_to?(:to_key)
69
- resource_owner_or_id.id
70
- else
71
- resource_owner_or_id
72
- end
73
- token = last_authorized_token_for(application.try(:id), resource_owner_id)
74
- if token && scopes_match?(token.scopes, scopes, application.try(:scopes))
75
- token
90
+ def matching_token_for(application, resource_owner, scopes, include_expired: true)
91
+ tokens = authorized_tokens_for(application&.id, resource_owner)
92
+ tokens = tokens.not_expired unless include_expired
93
+ find_matching_token(tokens, application, scopes)
94
+ end
95
+
96
+ # Interface to enumerate access token records in batches in order not
97
+ # to bloat the memory. Could be overloaded in any ORM extension.
98
+ #
99
+ def find_access_token_in_batches(relation, **args, &block)
100
+ relation.find_in_batches(**args, &block)
101
+ end
102
+
103
+ # Enumerates AccessToken records in batches to find a matching token.
104
+ # Batching is required in order not to pollute the memory if Application
105
+ # has huge amount of associated records.
106
+ #
107
+ # ActiveRecord 5.x - 6.x ignores custom ordering so we can't perform a
108
+ # database sort by created_at, so we need to load all the matching records,
109
+ # sort them and find latest one.
110
+ #
111
+ # @param relation [ActiveRecord::Relation]
112
+ # Access tokens relation
113
+ # @param application [Doorkeeper::Application]
114
+ # Application instance
115
+ # @param scopes [String, Doorkeeper::OAuth::Scopes]
116
+ # set of scopes
117
+ #
118
+ # @return [Doorkeeper::AccessToken, nil] Access Token instance or
119
+ # nil if matching record was not found
120
+ #
121
+ def find_matching_token(relation, application, scopes)
122
+ return nil unless relation
123
+
124
+ matching_tokens = []
125
+ batch_size = Doorkeeper.configuration.token_lookup_batch_size
126
+
127
+ find_access_token_in_batches(relation, batch_size: batch_size) do |batch|
128
+ tokens = batch.select do |token|
129
+ scopes_match?(token.scopes, scopes, application&.scopes)
130
+ end
131
+
132
+ matching_tokens.concat(tokens)
76
133
  end
134
+
135
+ matching_tokens.max_by(&:created_at)
77
136
  end
78
137
 
79
- # Checks whether the token scopes match the scopes from the parameters or
80
- # Application scopes (if present).
138
+ # Checks whether the token scopes match the scopes from the parameters
81
139
  #
82
140
  # @param token_scopes [#to_s]
83
141
  # set of scopes (any object that responds to `#to_s`)
84
- # @param param_scopes [String]
142
+ # @param param_scopes [Doorkeeper::OAuth::Scopes]
85
143
  # scopes from params
86
- # @param app_scopes [String]
144
+ # @param app_scopes [Doorkeeper::OAuth::Scopes]
87
145
  # Application scopes
88
146
  #
89
- # @return [Boolean] true if all scopes are blank or matches
147
+ # @return [Boolean] true if the param scopes match the token scopes,
148
+ # and all the param scopes are defined in the application (or in the
149
+ # server configuration if the application doesn't define any scopes),
90
150
  # and false in other cases
91
151
  #
92
152
  def scopes_match?(token_scopes, param_scopes, app_scopes)
93
- (!token_scopes.present? && !param_scopes.present?) ||
94
- Doorkeeper::OAuth::Helpers::ScopeChecker.match?(
95
- token_scopes.to_s,
96
- param_scopes,
97
- app_scopes
153
+ return true if token_scopes.empty? && param_scopes.empty?
154
+
155
+ (token_scopes.sort == param_scopes.sort) &&
156
+ Doorkeeper::OAuth::Helpers::ScopeChecker.valid?(
157
+ scope_str: param_scopes.to_s,
158
+ server_scopes: Doorkeeper.config.scopes,
159
+ app_scopes: app_scopes,
98
160
  )
99
161
  end
100
162
 
@@ -104,59 +166,124 @@ module Doorkeeper
104
166
  #
105
167
  # @param application [Doorkeeper::Application]
106
168
  # Application instance
107
- # @param resource_owner_id [ActiveRecord::Base, Integer]
169
+ # @param resource_owner [ActiveRecord::Base, Integer]
108
170
  # Resource Owner model instance or it's ID
109
171
  # @param scopes [#to_s]
110
172
  # set of scopes (any object that responds to `#to_s`)
111
- # @param expires_in [Integer]
173
+ # @param token_attributes [Hash]
174
+ # Additional attributes to use when creating a token
175
+ # @option token_attributes [Integer] :expires_in
112
176
  # token lifetime in seconds
113
- # @param use_refresh_token [Boolean]
177
+ # @option token_attributes [Boolean] :use_refresh_token
114
178
  # whether to use the refresh token
115
179
  #
116
180
  # @return [Doorkeeper::AccessToken] existing record or a new one
117
181
  #
118
- def find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token)
119
- if Doorkeeper.configuration.reuse_access_token
120
- access_token = matching_token_for(application, resource_owner_id, scopes)
121
- if access_token && !access_token.expired?
122
- return access_token
123
- end
182
+ def find_or_create_for(application:, resource_owner:, scopes:, **token_attributes)
183
+ if Doorkeeper.config.reuse_access_token
184
+ access_token = matching_token_for(application, resource_owner, scopes, include_expired: false)
185
+
186
+ return access_token if access_token&.reusable?
124
187
  end
125
188
 
126
- create!(
127
- application_id: application.try(:id),
128
- resource_owner_id: resource_owner_id,
129
- scopes: scopes.to_s,
130
- expires_in: expires_in,
131
- use_refresh_token: use_refresh_token
189
+ create_for(
190
+ application: application,
191
+ resource_owner: resource_owner,
192
+ scopes: scopes,
193
+ **token_attributes,
132
194
  )
133
195
  end
134
196
 
135
- # Looking for not revoked Access Token record that belongs to specific
197
+ # Creates a not expired AccessToken record with a matching set of
198
+ # scopes that belongs to specific Application and Resource Owner.
199
+ #
200
+ # @param application [Doorkeeper::Application]
201
+ # Application instance
202
+ # @param resource_owner [ActiveRecord::Base, Integer]
203
+ # Resource Owner model instance or it's ID
204
+ # @param scopes [#to_s]
205
+ # set of scopes (any object that responds to `#to_s`)
206
+ # @param token_attributes [Hash]
207
+ # Additional attributes to use when creating a token
208
+ # @option token_attributes [Integer] :expires_in
209
+ # token lifetime in seconds
210
+ # @option token_attributes [Boolean] :use_refresh_token
211
+ # whether to use the refresh token
212
+ #
213
+ # @return [Doorkeeper::AccessToken] new access token
214
+ #
215
+ def create_for(application:, resource_owner:, scopes:, **token_attributes)
216
+ token_attributes[:application] = application
217
+ token_attributes[:scopes] = scopes.to_s
218
+
219
+ if Doorkeeper.config.polymorphic_resource_owner?
220
+ token_attributes[:resource_owner] = resource_owner
221
+ else
222
+ token_attributes[:resource_owner_id] = resource_owner_id_for(resource_owner)
223
+ end
224
+
225
+ create!(token_attributes)
226
+ end
227
+
228
+ # Looking for not revoked Access Token records that belongs to specific
136
229
  # Application and Resource Owner.
137
230
  #
138
231
  # @param application_id [Integer]
139
232
  # ID of the Application model instance
140
- # @param resource_owner_id [Integer]
233
+ # @param resource_owner [ActiveRecord::Base, Integer]
234
+ # Resource Owner model instance or it's ID
235
+ #
236
+ # @return [ActiveRecord::Relation]
237
+ # collection of matching AccessToken objects
238
+ #
239
+ def authorized_tokens_for(application_id, resource_owner)
240
+ by_resource_owner(resource_owner).where(
241
+ application_id: application_id,
242
+ revoked_at: nil,
243
+ )
244
+ end
245
+
246
+ # Convenience method for backwards-compatibility, return the last
247
+ # matching token for the given Application and Resource Owner.
248
+ #
249
+ # @param application_id [Integer]
250
+ # ID of the Application model instance
251
+ # @param resource_owner [ActiveRecord::Base, Integer]
141
252
  # ID of the Resource Owner model instance
142
253
  #
143
254
  # @return [Doorkeeper::AccessToken, nil] matching AccessToken object or
144
255
  # nil if nothing was found
145
256
  #
146
- def last_authorized_token_for(application_id, resource_owner_id)
147
- ordered_by(:created_at, :desc).
148
- find_by(application_id: application_id,
149
- resource_owner_id: resource_owner_id,
150
- revoked_at: nil)
257
+ def last_authorized_token_for(application_id, resource_owner)
258
+ authorized_tokens_for(application_id, resource_owner)
259
+ .ordered_by(:created_at, :desc)
260
+ .first
261
+ end
262
+
263
+ ##
264
+ # Determines the secret storing transformer
265
+ # Unless configured otherwise, uses the plain secret strategy
266
+ #
267
+ # @return [Doorkeeper::SecretStoring::Base]
268
+ #
269
+ def secret_strategy
270
+ ::Doorkeeper.config.token_secret_strategy
271
+ end
272
+
273
+ ##
274
+ # Determine the fallback storing strategy
275
+ # Unless configured, there will be no fallback
276
+ def fallback_secret_strategy
277
+ ::Doorkeeper.config.token_secret_fallback_strategy
151
278
  end
152
279
  end
153
280
 
154
281
  # Access Token type: Bearer.
155
- # @see https://tools.ietf.org/html/rfc6750
282
+ # @see https://datatracker.ietf.org/doc/html/rfc6750
156
283
  # The OAuth 2.0 Authorization Framework: Bearer Token Usage
157
284
  #
158
285
  def token_type
159
- 'bearer'
286
+ "Bearer"
160
287
  end
161
288
 
162
289
  def use_refresh_token?
@@ -169,12 +296,16 @@ module Doorkeeper
169
296
  # @return [Hash] hash with token data
170
297
  def as_json(_options = {})
171
298
  {
172
- resource_owner_id: resource_owner_id,
173
- scopes: scopes,
174
- expires_in_seconds: expires_in_seconds,
175
- application: { uid: application.try(:uid) },
176
- created_at: created_at.to_i
177
- }
299
+ resource_owner_id: resource_owner_id,
300
+ scope: scopes,
301
+ expires_in: expires_in_seconds,
302
+ application: { uid: application.try(:uid) },
303
+ created_at: created_at.to_i,
304
+ }.tap do |json|
305
+ if Doorkeeper.configuration.polymorphic_resource_owner?
306
+ json[:resource_owner_type] = resource_owner_type
307
+ end
308
+ end
178
309
  end
179
310
 
180
311
  # Indicates whether the token instance have the same credential
@@ -186,7 +317,22 @@ module Doorkeeper
186
317
  #
187
318
  def same_credential?(access_token)
188
319
  application_id == access_token.application_id &&
320
+ same_resource_owner?(access_token)
321
+ end
322
+
323
+ # Indicates whether the token instance have the same credential
324
+ # as the other Access Token.
325
+ #
326
+ # @param access_token [Doorkeeper::AccessToken] other token
327
+ #
328
+ # @return [Boolean] true if credentials are same of false in other cases
329
+ #
330
+ def same_resource_owner?(access_token)
331
+ if Doorkeeper.configuration.polymorphic_resource_owner?
332
+ resource_owner == access_token.resource_owner
333
+ else
189
334
  resource_owner_id == access_token.resource_owner_id
335
+ end
190
336
  end
191
337
 
192
338
  # Indicates if token is acceptable for specific scopes.
@@ -200,18 +346,63 @@ module Doorkeeper
200
346
  accessible? && includes_scope?(*scopes)
201
347
  end
202
348
 
349
+ # We keep a volatile copy of the raw refresh token for initial communication
350
+ # The stored refresh_token may be mapped and not available in cleartext.
351
+ def plaintext_refresh_token
352
+ if secret_strategy.allows_restoring_secrets?
353
+ secret_strategy.restore_secret(self, :refresh_token)
354
+ else
355
+ @raw_refresh_token
356
+ end
357
+ end
358
+
359
+ # We keep a volatile copy of the raw token for initial communication
360
+ # The stored refresh_token may be mapped and not available in cleartext.
361
+ #
362
+ # Some strategies allow restoring stored secrets (e.g. symmetric encryption)
363
+ # while hashing strategies do not, so you cannot rely on this value
364
+ # returning a present value for persisted tokens.
365
+ def plaintext_token
366
+ if secret_strategy.allows_restoring_secrets?
367
+ secret_strategy.restore_secret(self, :token)
368
+ else
369
+ @raw_token
370
+ end
371
+ end
372
+
373
+ # Revokes token with `:refresh_token` equal to `:previous_refresh_token`
374
+ # and clears `:previous_refresh_token` attribute.
375
+ #
376
+ def revoke_previous_refresh_token!
377
+ return if !self.class.refresh_token_revoked_on_use? || previous_refresh_token.blank?
378
+
379
+ old_refresh_token&.revoke
380
+ update_attribute(:previous_refresh_token, "")
381
+ end
382
+
203
383
  private
204
384
 
385
+ # Searches for Access Token record with `:refresh_token` equal to
386
+ # `:previous_refresh_token` value.
387
+ #
388
+ # @return [Doorkeeper::AccessToken, nil]
389
+ # Access Token record or nil if nothing found
390
+ #
391
+ def old_refresh_token
392
+ @old_refresh_token ||= self.class.by_previous_refresh_token(previous_refresh_token)
393
+ end
394
+
205
395
  # Generates refresh token with UniqueToken generator.
206
396
  #
207
397
  # @return [String] refresh token value
208
398
  #
209
399
  def generate_refresh_token
210
- self.refresh_token = UniqueToken.generate
400
+ @raw_refresh_token = UniqueToken.generate
401
+ secret_strategy.store_secret(self, :refresh_token, @raw_refresh_token)
211
402
  end
212
403
 
213
404
  # Generates and sets the token value with the
214
- # configured Generator class (see Doorkeeper.configuration).
405
+ # configured Generator class (see Doorkeeper.config).
215
406
  #
216
407
  # @return [String] generated token value
217
408
  #
@@ -223,17 +414,32 @@ module Doorkeeper
223
414
  def generate_token
224
415
  self.created_at ||= Time.now.utc
225
416
 
226
- self.token = token_generator.generate(
417
+ @raw_token = token_generator.generate(attributes_for_token_generator)
418
+ secret_strategy.store_secret(self, :token, @raw_token)
419
+ @raw_token
420
+ end
421
+
422
+ # Set of attributes that would be passed to token generator to
423
+ # generate unique token based on them.
424
+ #
425
+ # @return [Hash] set of attributes
426
+ #
427
+ def attributes_for_token_generator
428
+ {
227
429
  resource_owner_id: resource_owner_id,
228
430
  scopes: scopes,
229
431
  application: application,
230
432
  expires_in: expires_in,
231
- created_at: created_at
232
- )
433
+ created_at: created_at,
434
+ }.tap do |attributes|
435
+ if Doorkeeper.config.polymorphic_resource_owner?
436
+ attributes[:resource_owner] = resource_owner
437
+ end
438
+ end
233
439
  end
234
440
 
235
441
  def token_generator
236
- generator_name = Doorkeeper.configuration.access_token_generator
442
+ generator_name = Doorkeeper.config.access_token_generator
237
443
  generator = generator_name.constantize
238
444
 
239
445
  return generator if generator.respond_to?(:generate)
@@ -1,11 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Doorkeeper
2
4
  module ApplicationMixin
3
5
  extend ActiveSupport::Concern
4
6
 
5
7
  include OAuth::Helpers
6
8
  include Models::Orderable
9
+ include Models::SecretStorable
7
10
  include Models::Scopes
8
11
 
12
+ # :nodoc
9
13
  module ClassMethods
10
14
  # Returns an instance of the Doorkeeper::Application with
11
15
  # specific UID and secret.
@@ -16,14 +20,15 @@ module Doorkeeper
16
20
  # @param uid [#to_s] UID (any object that responds to `#to_s`)
17
21
  # @param secret [#to_s] secret (any object that responds to `#to_s`)
18
22
  #
19
- # @return [Doorkeeper::Application, nil] Application instance or nil
20
- # if there is no record with such credentials
23
+ # @return [Doorkeeper::Application, nil]
24
+ # Application instance or nil if there is no record with such credentials
21
25
  #
22
26
  def by_uid_and_secret(uid, secret)
23
27
  app = by_uid(uid)
24
28
  return unless app
25
29
  return app if secret.blank? && !app.confidential?
26
- return unless app.secret == secret
30
+ return unless app.secret_matches?(secret)
31
+
27
32
  app
28
33
  end
29
34
 
@@ -37,15 +42,55 @@ module Doorkeeper
37
42
  def by_uid(uid)
38
43
  find_by(uid: uid.to_s)
39
44
  end
45
+
46
+ ##
47
+ # Determines the secret storing transformer
48
+ # Unless configured otherwise, uses the plain secret strategy
49
+ def secret_strategy
50
+ ::Doorkeeper.config.application_secret_strategy
51
+ end
52
+
53
+ ##
54
+ # Determine the fallback storing strategy
55
+ # Unless configured, there will be no fallback
56
+ def fallback_secret_strategy
57
+ ::Doorkeeper.config.application_secret_fallback_strategy
58
+ end
40
59
  end
41
60
 
42
61
  # Set an application's valid redirect URIs.
43
62
  #
44
- # @param uris [String, Array] Newline-separated string or array the URI(s)
63
+ # @param uris [String, Array<String>] Newline-separated string or array the URI(s)
64
+ #
65
+ # @return [String] The redirect URI(s) separated by newlines.
45
66
  #
46
- # @return [String] The redirect URI(s) seperated by newlines.
47
67
  def redirect_uri=(uris)
48
68
  super(uris.is_a?(Array) ? uris.join("\n") : uris)
49
69
  end
70
+
71
+ # Check whether the given plain text secret matches our stored secret
72
+ #
73
+ # @param input [#to_s] Plain secret provided by user
74
+ # (any object that responds to `#to_s`)
75
+ #
76
+ # @return [Boolean] Whether the given secret matches the stored secret
77
+ # of this application.
78
+ #
79
+ def secret_matches?(input)
80
+ # return false if either is nil, since secure_compare depends on strings
81
+ # but Application secrets MAY be nil depending on confidentiality.
82
+ return false if input.nil? || secret.nil?
83
+
84
+ # When matching the secret by comparer function, all is well.
85
+ return true if secret_strategy.secret_matches?(input, secret)
86
+
87
+ # When fallback lookup is enabled, ensure applications
88
+ # with plain secrets can still be found
89
+ if fallback_secret_strategy
90
+ fallback_secret_strategy.secret_matches?(input, secret)
91
+ else
92
+ false
93
+ end
94
+ end
50
95
  end
51
96
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Doorkeeper
2
4
  module Models
3
5
  module Accessible
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Doorkeeper
2
4
  module Models
3
5
  module Expirable
@@ -6,7 +8,7 @@ module Doorkeeper
6
8
  #
7
9
  # @return [Boolean] true if object expired and false in other case
8
10
  def expired?
9
- expires_in && Time.now.utc > expires_at
11
+ !!(expires_in && Time.now.utc > expires_at)
10
12
  end
11
13
 
12
14
  # Calculates expiration time in seconds.
@@ -15,6 +17,7 @@ module Doorkeeper
15
17
  # or nil if object never expires.
16
18
  def expires_in_seconds
17
19
  return nil if expires_in.nil?
20
+
18
21
  expires = expires_at - Time.now.utc
19
22
  expires_sec = expires.seconds.round(0)
20
23
  expires_sec > 0 ? expires_sec : 0
@@ -22,10 +25,11 @@ module Doorkeeper
22
25
 
23
26
  # Expiration time (date time of creation + TTL).
24
27
  #
25
- # @return [Time] expiration time in UTC
28
+ # @return [Time, nil] expiration time in UTC
29
+ # or nil if the object never expires.
26
30
  #
27
31
  def expires_at
28
- created_at + expires_in.seconds
32
+ expires_in && created_at + expires_in.seconds
29
33
  end
30
34
  end
31
35
  end