doorkeeper 4.4.3 → 5.0.3

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 (223) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.gitlab-ci.yml +16 -0
  4. data/.travis.yml +7 -0
  5. data/Appraisals +2 -2
  6. data/Dangerfile +64 -0
  7. data/Gemfile +1 -1
  8. data/NEWS.md +98 -8
  9. data/README.md +110 -12
  10. data/Rakefile +6 -0
  11. data/UPGRADE.md +2 -0
  12. data/app/assets/stylesheets/doorkeeper/admin/application.css +2 -2
  13. data/app/controllers/doorkeeper/application_controller.rb +6 -3
  14. data/app/controllers/doorkeeper/application_metal_controller.rb +6 -0
  15. data/app/controllers/doorkeeper/applications_controller.rb +46 -24
  16. data/app/controllers/doorkeeper/authorizations_controller.rb +55 -12
  17. data/app/controllers/doorkeeper/authorized_applications_controller.rb +21 -2
  18. data/app/controllers/doorkeeper/token_info_controller.rb +2 -0
  19. data/app/controllers/doorkeeper/tokens_controller.rb +4 -6
  20. data/app/helpers/doorkeeper/dashboard_helper.rb +9 -7
  21. data/app/validators/redirect_uri_validator.rb +5 -2
  22. data/app/views/doorkeeper/applications/_delete_form.html.erb +3 -1
  23. data/app/views/doorkeeper/applications/_form.html.erb +25 -24
  24. data/app/views/doorkeeper/applications/edit.html.erb +1 -1
  25. data/app/views/doorkeeper/applications/index.html.erb +17 -7
  26. data/app/views/doorkeeper/applications/new.html.erb +1 -1
  27. data/app/views/doorkeeper/applications/show.html.erb +6 -6
  28. data/app/views/doorkeeper/authorizations/error.html.erb +1 -1
  29. data/app/views/doorkeeper/authorizations/new.html.erb +4 -0
  30. data/app/views/layouts/doorkeeper/admin.html.erb +15 -15
  31. data/config/locales/en.yml +10 -1
  32. data/doorkeeper.gemspec +25 -26
  33. data/gemfiles/rails_5_2.gemfile +1 -1
  34. data/gemfiles/rails_master.gemfile +4 -1
  35. data/lib/doorkeeper/config.rb +81 -40
  36. data/lib/doorkeeper/engine.rb +6 -0
  37. data/lib/doorkeeper/errors.rb +17 -3
  38. data/lib/doorkeeper/grape/authorization_decorator.rb +2 -0
  39. data/lib/doorkeeper/grape/helpers.rb +3 -1
  40. data/lib/doorkeeper/helpers/controller.rb +9 -2
  41. data/lib/doorkeeper/models/access_grant_mixin.rb +73 -0
  42. data/lib/doorkeeper/models/access_token_mixin.rb +44 -25
  43. data/lib/doorkeeper/models/application_mixin.rb +2 -0
  44. data/lib/doorkeeper/models/concerns/accessible.rb +2 -0
  45. data/lib/doorkeeper/models/concerns/expirable.rb +2 -0
  46. data/lib/doorkeeper/models/concerns/orderable.rb +2 -0
  47. data/lib/doorkeeper/models/concerns/ownership.rb +2 -0
  48. data/lib/doorkeeper/models/concerns/revocable.rb +2 -0
  49. data/lib/doorkeeper/models/concerns/scopes.rb +3 -1
  50. data/lib/doorkeeper/oauth/authorization/code.rb +33 -8
  51. data/lib/doorkeeper/oauth/authorization/context.rb +17 -0
  52. data/lib/doorkeeper/oauth/authorization/token.rb +38 -14
  53. data/lib/doorkeeper/oauth/authorization/uri_builder.rb +2 -0
  54. data/lib/doorkeeper/oauth/authorization_code_request.rb +29 -2
  55. data/lib/doorkeeper/oauth/base_request.rb +22 -9
  56. data/lib/doorkeeper/oauth/base_response.rb +2 -0
  57. data/lib/doorkeeper/oauth/client/credentials.rb +3 -1
  58. data/lib/doorkeeper/oauth/client.rb +1 -1
  59. data/lib/doorkeeper/oauth/client_credentials/creator.rb +4 -1
  60. data/lib/doorkeeper/oauth/client_credentials/issuer.rb +7 -2
  61. data/lib/doorkeeper/oauth/client_credentials/validation.rb +5 -5
  62. data/lib/doorkeeper/oauth/client_credentials_request.rb +1 -3
  63. data/lib/doorkeeper/oauth/code_request.rb +2 -0
  64. data/lib/doorkeeper/oauth/code_response.rb +2 -0
  65. data/lib/doorkeeper/oauth/error.rb +2 -0
  66. data/lib/doorkeeper/oauth/error_response.rb +21 -3
  67. data/lib/doorkeeper/oauth/forbidden_token_response.rb +9 -2
  68. data/lib/doorkeeper/oauth/helpers/scope_checker.rb +2 -8
  69. data/lib/doorkeeper/oauth/helpers/unique_token.rb +2 -0
  70. data/lib/doorkeeper/oauth/helpers/uri_checker.rb +5 -2
  71. data/lib/doorkeeper/oauth/invalid_token_response.rb +18 -0
  72. data/lib/doorkeeper/oauth/password_access_token_request.rb +9 -4
  73. data/lib/doorkeeper/oauth/pre_authorization.rb +43 -11
  74. data/lib/doorkeeper/oauth/refresh_token_request.rb +16 -3
  75. data/lib/doorkeeper/oauth/scopes.rb +3 -1
  76. data/lib/doorkeeper/oauth/token.rb +7 -2
  77. data/lib/doorkeeper/oauth/token_introspection.rb +4 -2
  78. data/lib/doorkeeper/oauth/token_request.rb +2 -0
  79. data/lib/doorkeeper/oauth/token_response.rb +6 -2
  80. data/lib/doorkeeper/oauth.rb +13 -0
  81. data/lib/doorkeeper/orm/active_record/application.rb +75 -12
  82. data/lib/doorkeeper/orm/active_record/stale_records_cleaner.rb +26 -0
  83. data/lib/doorkeeper/orm/active_record.rb +4 -0
  84. data/lib/doorkeeper/rails/helpers.rb +6 -4
  85. data/lib/doorkeeper/rails/routes/mapper.rb +2 -0
  86. data/lib/doorkeeper/rails/routes/mapping.rb +2 -0
  87. data/lib/doorkeeper/rails/routes.rb +23 -8
  88. data/lib/doorkeeper/rake/db.rake +40 -0
  89. data/lib/doorkeeper/rake/setup.rake +6 -0
  90. data/lib/doorkeeper/rake.rb +14 -0
  91. data/lib/doorkeeper/request/authorization_code.rb +1 -1
  92. data/lib/doorkeeper/request/client_credentials.rb +1 -1
  93. data/lib/doorkeeper/request/code.rb +1 -1
  94. data/lib/doorkeeper/request/password.rb +1 -1
  95. data/lib/doorkeeper/request/refresh_token.rb +1 -1
  96. data/lib/doorkeeper/request/strategy.rb +2 -0
  97. data/lib/doorkeeper/request/token.rb +1 -1
  98. data/lib/doorkeeper/request.rb +29 -34
  99. data/lib/doorkeeper/server.rb +2 -0
  100. data/lib/doorkeeper/stale_records_cleaner.rb +20 -0
  101. data/lib/doorkeeper/validations.rb +2 -0
  102. data/lib/doorkeeper/version.rb +6 -24
  103. data/lib/doorkeeper.rb +20 -17
  104. data/lib/generators/doorkeeper/application_owner_generator.rb +23 -18
  105. data/lib/generators/doorkeeper/confidential_applications_generator.rb +32 -0
  106. data/lib/generators/doorkeeper/install_generator.rb +17 -9
  107. data/lib/generators/doorkeeper/migration_generator.rb +23 -18
  108. data/lib/generators/doorkeeper/pkce_generator.rb +32 -0
  109. data/lib/generators/doorkeeper/previous_refresh_token_generator.rb +29 -24
  110. data/lib/generators/doorkeeper/templates/add_confidential_to_applications.rb.erb +13 -0
  111. data/lib/generators/doorkeeper/templates/enable_pkce_migration.rb.erb +6 -0
  112. data/lib/generators/doorkeeper/templates/initializer.rb +96 -13
  113. data/lib/generators/doorkeeper/templates/migration.rb.erb +2 -3
  114. data/lib/generators/doorkeeper/views_generator.rb +3 -1
  115. data/spec/controllers/application_metal_controller_spec.rb +50 -0
  116. data/spec/controllers/applications_controller_spec.rb +123 -14
  117. data/spec/controllers/authorizations_controller_spec.rb +334 -51
  118. data/spec/controllers/protected_resources_controller_spec.rb +60 -18
  119. data/spec/controllers/token_info_controller_spec.rb +4 -12
  120. data/spec/controllers/tokens_controller_spec.rb +17 -20
  121. data/spec/dummy/Rakefile +1 -1
  122. data/spec/dummy/app/assets/config/manifest.js +2 -0
  123. data/spec/dummy/app/controllers/custom_authorizations_controller.rb +1 -1
  124. data/spec/dummy/app/controllers/home_controller.rb +1 -2
  125. data/spec/dummy/config/application.rb +1 -1
  126. data/spec/dummy/config/boot.rb +2 -4
  127. data/spec/dummy/config/environment.rb +1 -1
  128. data/spec/dummy/config/environments/test.rb +5 -6
  129. data/spec/dummy/config/initializers/doorkeeper.rb +12 -6
  130. data/spec/dummy/config/initializers/new_framework_defaults.rb +2 -0
  131. data/spec/dummy/config/initializers/secret_token.rb +1 -1
  132. data/spec/dummy/config/routes.rb +3 -42
  133. data/spec/dummy/config.ru +1 -1
  134. data/spec/dummy/db/migrate/20151223192035_create_doorkeeper_tables.rb +4 -4
  135. data/spec/dummy/db/migrate/20151223200000_add_owner_to_application.rb +1 -1
  136. data/spec/dummy/db/migrate/20170822064514_enable_pkce.rb +6 -0
  137. data/spec/dummy/db/migrate/{20180210183654_add_confidential_to_application.rb → 20180210183654_add_confidential_to_applications.rb} +1 -1
  138. data/spec/dummy/db/schema.rb +36 -36
  139. data/spec/dummy/script/rails +4 -3
  140. data/spec/factories.rb +6 -6
  141. data/spec/generators/application_owner_generator_spec.rb +1 -1
  142. data/spec/generators/confidential_applications_generator_spec.rb +45 -0
  143. data/spec/generators/install_generator_spec.rb +5 -2
  144. data/spec/generators/migration_generator_spec.rb +1 -1
  145. data/spec/generators/pkce_generator_spec.rb +43 -0
  146. data/spec/generators/previous_refresh_token_generator_spec.rb +1 -1
  147. data/spec/generators/templates/routes.rb +0 -1
  148. data/spec/generators/views_generator_spec.rb +2 -2
  149. data/spec/grape/grape_integration_spec.rb +2 -2
  150. data/spec/helpers/doorkeeper/dashboard_helper_spec.rb +1 -1
  151. data/spec/lib/config_spec.rb +105 -39
  152. data/spec/lib/doorkeeper_spec.rb +6 -131
  153. data/spec/lib/models/expirable_spec.rb +0 -3
  154. data/spec/lib/models/revocable_spec.rb +0 -2
  155. data/spec/lib/models/scopes_spec.rb +0 -4
  156. data/spec/lib/oauth/authorization/uri_builder_spec.rb +0 -4
  157. data/spec/lib/oauth/authorization_code_request_spec.rb +17 -7
  158. data/spec/lib/oauth/base_request_spec.rb +49 -11
  159. data/spec/lib/oauth/base_response_spec.rb +1 -1
  160. data/spec/lib/oauth/client/credentials_spec.rb +2 -4
  161. data/spec/lib/oauth/client_credentials/creator_spec.rb +5 -1
  162. data/spec/lib/oauth/client_credentials/issuer_spec.rb +24 -7
  163. data/spec/lib/oauth/client_credentials/validation_spec.rb +4 -4
  164. data/spec/lib/oauth/client_credentials_integration_spec.rb +2 -2
  165. data/spec/lib/oauth/client_credentials_request_spec.rb +3 -5
  166. data/spec/lib/oauth/client_spec.rb +0 -3
  167. data/spec/lib/oauth/code_request_spec.rb +5 -3
  168. data/spec/lib/oauth/code_response_spec.rb +1 -1
  169. data/spec/lib/oauth/error_response_spec.rb +0 -3
  170. data/spec/lib/oauth/error_spec.rb +0 -2
  171. data/spec/lib/oauth/forbidden_token_response_spec.rb +1 -4
  172. data/spec/lib/oauth/helpers/scope_checker_spec.rb +8 -11
  173. data/spec/lib/oauth/helpers/unique_token_spec.rb +0 -1
  174. data/spec/lib/oauth/helpers/uri_checker_spec.rb +22 -13
  175. data/spec/lib/oauth/invalid_token_response_spec.rb +1 -4
  176. data/spec/lib/oauth/password_access_token_request_spec.rb +53 -6
  177. data/spec/lib/oauth/pre_authorization_spec.rb +33 -4
  178. data/spec/lib/oauth/refresh_token_request_spec.rb +22 -14
  179. data/spec/lib/oauth/scopes_spec.rb +0 -3
  180. data/spec/lib/oauth/token_request_spec.rb +8 -9
  181. data/spec/lib/oauth/token_response_spec.rb +0 -1
  182. data/spec/lib/oauth/token_spec.rb +40 -14
  183. data/spec/lib/request/strategy_spec.rb +0 -1
  184. data/spec/lib/server_spec.rb +7 -7
  185. data/spec/lib/stale_records_cleaner_spec.rb +89 -0
  186. data/spec/models/doorkeeper/access_grant_spec.rb +44 -1
  187. data/spec/models/doorkeeper/access_token_spec.rb +80 -32
  188. data/spec/models/doorkeeper/application_spec.rb +293 -221
  189. data/spec/requests/applications/applications_request_spec.rb +134 -1
  190. data/spec/requests/applications/authorized_applications_spec.rb +1 -1
  191. data/spec/requests/endpoints/authorization_spec.rb +3 -3
  192. data/spec/requests/endpoints/token_spec.rb +7 -5
  193. data/spec/requests/flows/authorization_code_errors_spec.rb +2 -2
  194. data/spec/requests/flows/authorization_code_spec.rb +258 -2
  195. data/spec/requests/flows/client_credentials_spec.rb +46 -6
  196. data/spec/requests/flows/implicit_grant_errors_spec.rb +3 -3
  197. data/spec/requests/flows/implicit_grant_spec.rb +38 -11
  198. data/spec/requests/flows/password_spec.rb +61 -3
  199. data/spec/requests/flows/refresh_token_spec.rb +59 -2
  200. data/spec/requests/flows/revoke_token_spec.rb +20 -20
  201. data/spec/requests/flows/skip_authorization_spec.rb +16 -11
  202. data/spec/requests/protected_resources/metal_spec.rb +1 -1
  203. data/spec/requests/protected_resources/private_api_spec.rb +3 -3
  204. data/spec/routing/custom_controller_routes_spec.rb +59 -7
  205. data/spec/routing/default_routes_spec.rb +2 -2
  206. data/spec/routing/scoped_routes_spec.rb +16 -2
  207. data/spec/spec_helper.rb +54 -3
  208. data/spec/spec_helper_integration.rb +2 -74
  209. data/spec/support/dependencies/{factory_girl.rb → factory_bot.rb} +0 -0
  210. data/spec/support/doorkeeper_rspec.rb +20 -0
  211. data/spec/support/helpers/authorization_request_helper.rb +4 -4
  212. data/spec/support/helpers/model_helper.rb +8 -4
  213. data/spec/support/helpers/request_spec_helper.rb +10 -2
  214. data/spec/support/helpers/url_helper.rb +18 -14
  215. data/spec/support/http_method_shim.rb +12 -16
  216. data/spec/support/shared/controllers_shared_context.rb +56 -0
  217. data/spec/validators/redirect_uri_validator_spec.rb +9 -3
  218. data/spec/version/version_spec.rb +3 -3
  219. data/vendor/assets/stylesheets/doorkeeper/bootstrap.min.css +4 -5
  220. metadata +54 -35
  221. data/lib/generators/doorkeeper/add_client_confidentiality_generator.rb +0 -31
  222. data/lib/generators/doorkeeper/templates/add_confidential_to_application_migration.rb.erb +0 -11
  223. data/spec/controllers/application_metal_controller.rb +0 -10
@@ -4,56 +4,107 @@ Doorkeeper.configure do
4
4
 
5
5
  # This block will be called to check whether the resource owner is authenticated or not.
6
6
  resource_owner_authenticator do
7
- fail "Please configure doorkeeper resource_owner_authenticator block located in #{__FILE__}"
7
+ raise "Please configure doorkeeper resource_owner_authenticator block located in #{__FILE__}"
8
8
  # Put your resource owner authentication logic here.
9
9
  # Example implementation:
10
10
  # User.find_by_id(session[:user_id]) || redirect_to(new_user_session_url)
11
11
  end
12
12
 
13
- # If you want to restrict access to the web interface for adding oauth authorized applications, you need to declare the block below.
13
+ # If you didn't skip applications controller from Doorkeeper routes in your application routes.rb
14
+ # file then you need to declare this block in order to restrict access to the web interface for
15
+ # adding oauth authorized applications. In other case it will return 403 Forbidden response
16
+ # every time somebody will try to access the admin web interface.
17
+ #
14
18
  # admin_authenticator do
15
19
  # # Put your admin authentication logic here.
16
20
  # # Example implementation:
17
- # Admin.find_by_id(session[:admin_id]) || redirect_to(new_admin_session_url)
21
+ #
22
+ # if current_user
23
+ # head :forbidden unless current_user.admin?
24
+ # else
25
+ # redirect_to sign_in_url
26
+ # end
18
27
  # end
19
28
 
29
+ # If you are planning to use Doorkeeper in Rails 5 API-only application, then you might
30
+ # want to use API mode that will skip all the views management and change the way how
31
+ # Doorkeeper responds to a requests.
32
+ #
33
+ # api_only
34
+
35
+ # Enforce token request content type to application/x-www-form-urlencoded.
36
+ # It is not enabled by default to not break prior versions of the gem.
37
+ #
38
+ # enforce_content_type
39
+
20
40
  # Authorization Code expiration time (default 10 minutes).
41
+ #
21
42
  # authorization_code_expires_in 10.minutes
22
43
 
23
44
  # Access token expiration time (default 2 hours).
24
45
  # If you want to disable expiration, set this to nil.
46
+ #
25
47
  # access_token_expires_in 2.hours
26
48
 
27
- # Assign a custom TTL for implicit grants.
28
- # custom_access_token_expires_in do |oauth_client|
29
- # oauth_client.application.additional_settings.implicit_oauth_expiration
49
+ # Assign custom TTL for access tokens. Will be used instead of access_token_expires_in
50
+ # option if defined. `context` has the following properties available
51
+ #
52
+ # `client` - the OAuth client application (see Doorkeeper::OAuth::Client)
53
+ # `grant_type` - the grant type of the request (see Doorkeeper::OAuth)
54
+ # `scopes` - the requested scopes (see Doorkeeper::OAuth::Scopes)
55
+ #
56
+ # custom_access_token_expires_in do |context|
57
+ # context.client.application.additional_settings.implicit_oauth_expiration
30
58
  # end
31
59
 
32
60
  # Use a custom class for generating the access token.
33
- # https://github.com/doorkeeper-gem/doorkeeper#custom-access-token-generator
61
+ # See https://github.com/doorkeeper-gem/doorkeeper#custom-access-token-generator
62
+ #
34
63
  # access_token_generator '::Doorkeeper::JWT'
35
64
 
36
65
  # The controller Doorkeeper::ApplicationController inherits from.
37
66
  # Defaults to ActionController::Base.
38
- # https://github.com/doorkeeper-gem/doorkeeper#custom-base-controller
67
+ # See https://github.com/doorkeeper-gem/doorkeeper#custom-base-controller
68
+ #
39
69
  # base_controller 'ApplicationController'
40
70
 
41
- # Reuse access token for the same resource owner within an application (disabled by default)
71
+ # Reuse access token for the same resource owner within an application (disabled by default).
72
+ #
73
+ # This option protects your application from creating new tokens before old valid one becomes
74
+ # expired so your database doesn't bloat. Keep in mind that when this option is `on` Doorkeeper
75
+ # doesn't updates existing token expiration time, it will create a new token instead.
42
76
  # Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/383
77
+ #
43
78
  # reuse_access_token
44
79
 
45
- # Issue access tokens with refresh token (disabled by default)
80
+ # Issue access tokens with refresh token (disabled by default), you may also
81
+ # pass a block which accepts `context` to customize when to give a refresh
82
+ # token or not. Similar to `custom_access_token_expires_in`, `context` has
83
+ # the properties:
84
+ #
85
+ # `client` - the OAuth client application (see Doorkeeper::OAuth::Client)
86
+ # `grant_type` - the grant type of the request (see Doorkeeper::OAuth)
87
+ # `scopes` - the requested scopes (see Doorkeeper::OAuth::Scopes)
88
+ #
46
89
  # use_refresh_token
47
90
 
91
+ # Forbids creating/updating applications with arbitrary scopes that are
92
+ # not in configuration, i.e. `default_scopes` or `optional_scopes`.
93
+ # (disabled by default)
94
+ #
95
+ # enforce_configured_scopes
96
+
48
97
  # Provide support for an owner to be assigned to each registered application (disabled by default)
49
98
  # Optional parameter confirmation: true (default false) if you want to enforce ownership of
50
99
  # a registered application
51
100
  # Note: you must also run the rails g doorkeeper:application_owner generator to provide the necessary support
101
+ #
52
102
  # enable_application_owner confirmation: false
53
103
 
54
104
  # Define access token scopes for your provider
55
105
  # For more information go to
56
106
  # https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes
107
+ #
57
108
  # default_scopes :public
58
109
  # optional_scopes :write, :update
59
110
 
@@ -62,6 +113,7 @@ Doorkeeper.configure do
62
113
  # falls back to the `:client_id` and `:client_secret` params from the `params` object.
63
114
  # Check out https://github.com/doorkeeper-gem/doorkeeper/wiki/Changing-how-clients-are-authenticated
64
115
  # for more information on customization
116
+ #
65
117
  # client_credentials :from_basic, :from_params
66
118
 
67
119
  # Change the way access token is authenticated from the request object.
@@ -69,10 +121,12 @@ Doorkeeper.configure do
69
121
  # falls back to the `:access_token` or `:bearer_token` params from the `params` object.
70
122
  # Check out https://github.com/doorkeeper-gem/doorkeeper/wiki/Changing-how-clients-are-authenticated
71
123
  # for more information on customization
124
+ #
72
125
  # access_token_methods :from_bearer_authorization, :from_access_token_param, :from_bearer_param
73
126
 
74
127
  # Change the native redirect uri for client apps
75
- # When clients register with the following redirect uri, they won't be redirected to any server and the authorization code will be displayed within the provider
128
+ # When clients register with the following redirect uri, they won't be redirected to any server and
129
+ # the authorizationcode will be displayed within the provider
76
130
  # The value can be any string. Use nil to disable this feature. When disabled, clients must provide a valid URL
77
131
  # (Similar behaviour: https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi)
78
132
  #
@@ -90,14 +144,27 @@ Doorkeeper.configure do
90
144
  #
91
145
  # force_ssl_in_redirect_uri { |uri| uri.host != 'localhost' }
92
146
 
93
- # Specify what redirect URI's you want to block during creation. Any redirect
94
- # URI is whitelisted by default.
147
+ # Specify what redirect URI's you want to block during Application creation.
148
+ # Any redirect URI is whitelisted by default.
95
149
  #
96
150
  # You can use this option in order to forbid URI's with 'javascript' scheme
97
151
  # for example.
98
152
  #
99
153
  # forbid_redirect_uri { |uri| uri.scheme.to_s.downcase == 'javascript' }
100
154
 
155
+ # Specify how authorization errors should be handled.
156
+ # By default, doorkeeper renders json errors when access token
157
+ # is invalid, expired, revoked or has invalid scopes.
158
+ #
159
+ # If you want to render error response yourself (i.e. rescue exceptions),
160
+ # set handle_auth_errors to `:raise` and rescue Doorkeeper::Errors::InvalidToken
161
+ # or following specific errors:
162
+ #
163
+ # Doorkeeper::Errors::TokenForbidden, Doorkeeper::Errors::TokenExpired,
164
+ # Doorkeeper::Errors::TokenRevoked, Doorkeeper::Errors::TokenUnknown
165
+ #
166
+ # handle_auth_errors :raise
167
+
101
168
  # Specify what grant flows are enabled in array of Strings. The valid
102
169
  # strings and the flows they enable are:
103
170
  #
@@ -127,13 +194,29 @@ Doorkeeper.configure do
127
194
  # puts "AFTER HOOK FIRED! #{request}, #{response}"
128
195
  # end
129
196
 
197
+ # Hook into Authorization flow in order to implement Single Sign Out
198
+ # or add ny other functionality.
199
+ #
200
+ # before_successful_authorization do |controller|
201
+ # Rails.logger.info(params.inspect)
202
+ # end
203
+ #
204
+ # after_successful_authorization do |controller|
205
+ # controller.session[:logout_urls] <<
206
+ # Doorkeeper::Application
207
+ # .find_by(controller.request.params.slice(:redirect_uri))
208
+ # .logout_uri
209
+ # end
210
+
130
211
  # Under some circumstances you might want to have applications auto-approved,
131
212
  # so that the user skips the authorization step.
132
213
  # For example if dealing with a trusted application.
214
+ #
133
215
  # skip_authorization do |resource_owner, client|
134
216
  # client.superapp? or resource_owner.admin?
135
217
  # end
136
218
 
137
219
  # WWW-Authenticate Realm (default "Doorkeeper").
220
+ #
138
221
  # realm "Doorkeeper"
139
222
  end
@@ -13,7 +13,7 @@ class CreateDoorkeeperTables < ActiveRecord::Migration<%= migration_version %>
13
13
  add_index :oauth_applications, :uid, unique: true
14
14
 
15
15
  create_table :oauth_access_grants do |t|
16
- t.integer :resource_owner_id, null: false
16
+ t.references :resource_owner, null: false
17
17
  t.references :application, null: false
18
18
  t.string :token, null: false
19
19
  t.integer :expires_in, null: false
@@ -31,7 +31,7 @@ class CreateDoorkeeperTables < ActiveRecord::Migration<%= migration_version %>
31
31
  )
32
32
 
33
33
  create_table :oauth_access_tokens do |t|
34
- t.integer :resource_owner_id
34
+ t.references :resource_owner, index: true
35
35
  t.references :application
36
36
 
37
37
  # If you use a custom token generator you may need to change this column
@@ -58,7 +58,6 @@ class CreateDoorkeeperTables < ActiveRecord::Migration<%= migration_version %>
58
58
  end
59
59
 
60
60
  add_index :oauth_access_tokens, :token, unique: true
61
- add_index :oauth_access_tokens, :resource_owner_id
62
61
  add_index :oauth_access_tokens, :refresh_token, unique: true
63
62
  add_foreign_key(
64
63
  :oauth_access_tokens,
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Doorkeeper
2
4
  module Generators
3
5
  class ViewsGenerator < ::Rails::Generators::Base
4
- source_root File.expand_path('../../../../app/views', __FILE__)
6
+ source_root File.expand_path('../../../app/views', __dir__)
5
7
 
6
8
  desc 'Copies default Doorkeeper views and layouts to your application.'
7
9
 
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper_integration'
4
+
5
+ describe Doorkeeper::ApplicationMetalController do
6
+ controller(Doorkeeper::ApplicationMetalController) do
7
+ def index
8
+ render json: {}, status: 200
9
+ end
10
+ end
11
+
12
+ it "lazy run hooks" do
13
+ i = 0
14
+ ActiveSupport.on_load(:doorkeeper_metal_controller) { i += 1 }
15
+
16
+ expect(i).to eq 1
17
+ end
18
+
19
+ describe 'enforce_content_type' do
20
+ before { allow(Doorkeeper.configuration).to receive(:enforce_content_type).and_return(flag) }
21
+
22
+ context 'enabled' do
23
+ let(:flag) { true }
24
+
25
+ it '200 for the correct media type' do
26
+ get :index, params: {}, as: :url_encoded_form
27
+ expect(response).to have_http_status 200
28
+ end
29
+
30
+ it 'returns a 415 for an incorrect media type' do
31
+ get :index, as: :json
32
+ expect(response).to have_http_status 415
33
+ end
34
+ end
35
+
36
+ context 'disabled' do
37
+ let(:flag) { false }
38
+
39
+ it 'returns a 200 for the correct media type' do
40
+ get :index, as: :url_encoded_form
41
+ expect(response).to have_http_status 200
42
+ end
43
+
44
+ it 'returns a 200 for an incorrect media type' do
45
+ get :index, as: :json
46
+ expect(response).to have_http_status 200
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,7 +1,99 @@
1
- require 'spec_helper_integration'
1
+ require 'spec_helper'
2
2
 
3
3
  module Doorkeeper
4
4
  describe ApplicationsController do
5
+ context 'JSON API' do
6
+ render_views
7
+
8
+ before do
9
+ allow(Doorkeeper.configuration).to receive(:api_only).and_return(true)
10
+ allow(Doorkeeper.configuration).to receive(:authenticate_admin).and_return(->(*) { true })
11
+ end
12
+
13
+ it 'creates an application' do
14
+ expect do
15
+ post :create, params: {
16
+ doorkeeper_application: {
17
+ name: 'Example',
18
+ redirect_uri: 'https://example.com'
19
+ }, format: :json
20
+ }
21
+ end.to(change { Doorkeeper::Application.count })
22
+
23
+ expect(response).to be_successful
24
+
25
+ expect(json_response).to include('id', 'name', 'uid', 'secret', 'redirect_uri', 'scopes')
26
+
27
+ expect(json_response['name']).to eq('Example')
28
+ expect(json_response['redirect_uri']).to eq('https://example.com')
29
+ end
30
+
31
+ it 'returns validation errors on wrong create params' do
32
+ expect do
33
+ post :create, params: {
34
+ doorkeeper_application: {
35
+ name: 'Example'
36
+ }, format: :json
37
+ }
38
+ end.not_to(change { Doorkeeper::Application.count })
39
+
40
+ expect(response).to have_http_status(422)
41
+
42
+ expect(json_response).to include('errors')
43
+ end
44
+
45
+ it 'returns application info' do
46
+ application = FactoryBot.create(:application, name: 'Change me')
47
+
48
+ get :show, params: { id: application.id, format: :json }
49
+
50
+ expect(response).to be_successful
51
+
52
+ expect(json_response).to include('id', 'name', 'uid', 'secret', 'redirect_uri', 'scopes')
53
+ end
54
+
55
+ it 'updates application' do
56
+ application = FactoryBot.create(:application, name: 'Change me')
57
+
58
+ put :update, params: {
59
+ id: application.id,
60
+ doorkeeper_application: {
61
+ name: 'Example App',
62
+ redirect_uri: 'https://example.com'
63
+ }, format: :json
64
+ }
65
+
66
+ expect(application.reload.name).to eq 'Example App'
67
+
68
+ expect(json_response).to include('id', 'name', 'uid', 'secret', 'redirect_uri', 'scopes')
69
+ end
70
+
71
+ it 'returns validation errors on wrong update params' do
72
+ application = FactoryBot.create(:application, name: 'Change me')
73
+
74
+ put :update, params: {
75
+ id: application.id,
76
+ doorkeeper_application: {
77
+ name: 'Example App',
78
+ redirect_uri: 'localhost:3000'
79
+ }, format: :json
80
+ }
81
+
82
+ expect(response).to have_http_status(422)
83
+
84
+ expect(json_response).to include('errors')
85
+ end
86
+
87
+ it 'destroys an application' do
88
+ application = FactoryBot.create(:application)
89
+
90
+ delete :destroy, params: { id: application.id, format: :json }
91
+
92
+ expect(response).to have_http_status(204)
93
+ expect(Application.count).to be_zero
94
+ end
95
+ end
96
+
5
97
  context 'when admin is not authenticated' do
6
98
  before do
7
99
  allow(Doorkeeper.configuration).to receive(:authenticate_admin).and_return(proc do
@@ -16,10 +108,13 @@ module Doorkeeper
16
108
 
17
109
  it 'does not create application' do
18
110
  expect do
19
- post :create, doorkeeper_application: {
20
- name: 'Example',
21
- redirect_uri: 'https://example.com' }
22
- end.not_to change { Doorkeeper::Application.count }
111
+ post :create, params: {
112
+ doorkeeper_application: {
113
+ name: 'Example',
114
+ redirect_uri: 'https://example.com'
115
+ }
116
+ }
117
+ end.not_to(change { Doorkeeper::Application.count })
23
118
  end
24
119
  end
25
120
 
@@ -34,34 +129,48 @@ module Doorkeeper
34
129
  first_application = FactoryBot.create(:application)
35
130
  second_application = FactoryBot.create(:application)
36
131
  expect(Doorkeeper::Application).to receive(:ordered_by).and_call_original
132
+
37
133
  get :index
134
+
38
135
  expect(response.body).to have_selector("tbody tr:first-child#application_#{first_application.id}")
39
136
  expect(response.body).to have_selector("tbody tr:last-child#application_#{second_application.id}")
40
137
  end
41
138
 
42
139
  it 'creates application' do
43
140
  expect do
44
- post :create, doorkeeper_application: {
45
- name: 'Example',
46
- redirect_uri: 'https://example.com' }
141
+ post :create, params: {
142
+ doorkeeper_application: {
143
+ name: 'Example',
144
+ redirect_uri: 'https://example.com'
145
+ }
146
+ }
47
147
  end.to change { Doorkeeper::Application.count }.by(1)
148
+
48
149
  expect(response).to be_redirect
49
150
  end
50
151
 
51
152
  it 'does not allow mass assignment of uid or secret' do
52
153
  application = FactoryBot.create(:application)
53
- put :update, id: application.id, doorkeeper_application: {
54
- uid: '1A2B3C4D',
55
- secret: '1A2B3C4D' }
154
+ put :update, params: {
155
+ id: application.id,
156
+ doorkeeper_application: {
157
+ uid: '1A2B3C4D',
158
+ secret: '1A2B3C4D'
159
+ }
160
+ }
56
161
 
57
162
  expect(application.reload.uid).not_to eq '1A2B3C4D'
58
163
  end
59
164
 
60
165
  it 'updates application' do
61
166
  application = FactoryBot.create(:application)
62
- put :update, id: application.id, doorkeeper_application: {
63
- name: 'Example',
64
- redirect_uri: 'https://example.com' }
167
+ put :update, params: {
168
+ id: application.id, doorkeeper_application: {
169
+ name: 'Example',
170
+ redirect_uri: 'https://example.com'
171
+ }
172
+ }
173
+
65
174
  expect(application.reload.name).to eq 'Example'
66
175
  end
67
176
  end