doorkeeper 4.2.0 → 5.6.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (273) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1119 -0
  3. data/README.md +112 -349
  4. data/app/assets/stylesheets/doorkeeper/admin/application.css +2 -2
  5. data/app/controllers/doorkeeper/application_controller.rb +6 -7
  6. data/app/controllers/doorkeeper/application_metal_controller.rb +7 -11
  7. data/app/controllers/doorkeeper/applications_controller.rb +65 -20
  8. data/app/controllers/doorkeeper/authorizations_controller.rb +115 -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 +118 -38
  12. data/app/helpers/doorkeeper/dashboard_helper.rb +10 -6
  13. data/app/views/doorkeeper/applications/_delete_form.html.erb +4 -3
  14. data/app/views/doorkeeper/applications/_form.html.erb +33 -21
  15. data/app/views/doorkeeper/applications/edit.html.erb +1 -1
  16. data/app/views/doorkeeper/applications/index.html.erb +18 -6
  17. data/app/views/doorkeeper/applications/new.html.erb +1 -1
  18. data/app/views/doorkeeper/applications/show.html.erb +40 -16
  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 +17 -11
  22. data/app/views/doorkeeper/authorized_applications/_delete_form.html.erb +1 -2
  23. data/app/views/doorkeeper/authorized_applications/index.html.erb +0 -1
  24. data/app/views/layouts/doorkeeper/admin.html.erb +16 -14
  25. data/config/locales/en.yml +36 -9
  26. data/lib/doorkeeper/config/abstract_builder.rb +28 -0
  27. data/lib/doorkeeper/config/option.rb +82 -0
  28. data/lib/doorkeeper/config/validations.rb +53 -0
  29. data/lib/doorkeeper/config.rb +551 -155
  30. data/lib/doorkeeper/engine.rb +19 -6
  31. data/lib/doorkeeper/errors.rb +55 -10
  32. data/lib/doorkeeper/grant_flow/fallback_flow.rb +15 -0
  33. data/lib/doorkeeper/grant_flow/flow.rb +44 -0
  34. data/lib/doorkeeper/grant_flow/registry.rb +50 -0
  35. data/lib/doorkeeper/grant_flow.rb +45 -0
  36. data/lib/doorkeeper/grape/authorization_decorator.rb +6 -4
  37. data/lib/doorkeeper/grape/helpers.rb +24 -12
  38. data/lib/doorkeeper/helpers/controller.rb +49 -27
  39. data/lib/doorkeeper/models/access_grant_mixin.rb +100 -21
  40. data/lib/doorkeeper/models/access_token_mixin.rb +383 -75
  41. data/lib/doorkeeper/models/application_mixin.rb +72 -25
  42. data/lib/doorkeeper/models/concerns/accessible.rb +6 -0
  43. data/lib/doorkeeper/models/concerns/expirable.rb +20 -6
  44. data/lib/doorkeeper/models/concerns/expiration_time_sql_math.rb +88 -0
  45. data/lib/doorkeeper/models/concerns/orderable.rb +15 -0
  46. data/lib/doorkeeper/models/concerns/ownership.rb +4 -7
  47. data/lib/doorkeeper/models/concerns/polymorphic_resource_owner.rb +30 -0
  48. data/lib/doorkeeper/models/concerns/resource_ownerable.rb +47 -0
  49. data/lib/doorkeeper/models/concerns/reusable.rb +19 -0
  50. data/lib/doorkeeper/models/concerns/revocable.rb +12 -18
  51. data/lib/doorkeeper/models/concerns/scopes.rb +12 -2
  52. data/lib/doorkeeper/models/concerns/secret_storable.rb +106 -0
  53. data/lib/doorkeeper/oauth/authorization/code.rb +54 -12
  54. data/lib/doorkeeper/oauth/authorization/context.rb +17 -0
  55. data/lib/doorkeeper/oauth/authorization/token.rb +72 -28
  56. data/lib/doorkeeper/oauth/authorization/uri_builder.rb +22 -18
  57. data/lib/doorkeeper/oauth/authorization_code_request.rb +77 -17
  58. data/lib/doorkeeper/oauth/base_request.rb +67 -0
  59. data/lib/doorkeeper/oauth/base_response.rb +31 -0
  60. data/lib/doorkeeper/oauth/client/credentials.rb +23 -10
  61. data/lib/doorkeeper/oauth/client.rb +10 -12
  62. data/lib/doorkeeper/oauth/client_credentials/creator.rb +44 -4
  63. data/lib/doorkeeper/oauth/client_credentials/issuer.rb +21 -13
  64. data/lib/doorkeeper/oauth/client_credentials/validator.rb +55 -0
  65. data/lib/doorkeeper/oauth/client_credentials_request.rb +20 -16
  66. data/lib/doorkeeper/oauth/code_request.rb +9 -13
  67. data/lib/doorkeeper/oauth/code_response.rb +28 -15
  68. data/lib/doorkeeper/oauth/error.rb +5 -3
  69. data/lib/doorkeeper/oauth/error_response.rb +43 -20
  70. data/lib/doorkeeper/oauth/forbidden_token_response.rb +11 -3
  71. data/lib/doorkeeper/oauth/helpers/scope_checker.rb +23 -18
  72. data/lib/doorkeeper/oauth/helpers/unique_token.rb +20 -3
  73. data/lib/doorkeeper/oauth/helpers/uri_checker.rb +53 -3
  74. data/lib/doorkeeper/oauth/hooks/context.rb +21 -0
  75. data/lib/doorkeeper/oauth/invalid_request_response.rb +47 -0
  76. data/lib/doorkeeper/oauth/invalid_token_response.rb +31 -5
  77. data/lib/doorkeeper/oauth/nonstandard.rb +39 -0
  78. data/lib/doorkeeper/oauth/password_access_token_request.rb +46 -14
  79. data/lib/doorkeeper/oauth/pre_authorization.rb +138 -28
  80. data/lib/doorkeeper/oauth/refresh_token_request.rb +74 -41
  81. data/lib/doorkeeper/oauth/scopes.rb +26 -12
  82. data/lib/doorkeeper/oauth/token.rb +25 -23
  83. data/lib/doorkeeper/oauth/token_introspection.rb +204 -0
  84. data/lib/doorkeeper/oauth/token_request.rb +9 -22
  85. data/lib/doorkeeper/oauth/token_response.rb +13 -10
  86. data/lib/doorkeeper/oauth.rb +13 -0
  87. data/lib/doorkeeper/orm/active_record/access_grant.rb +6 -4
  88. data/lib/doorkeeper/orm/active_record/access_token.rb +5 -25
  89. data/lib/doorkeeper/orm/active_record/application.rb +6 -15
  90. data/lib/doorkeeper/orm/active_record/mixins/access_grant.rb +63 -0
  91. data/lib/doorkeeper/orm/active_record/mixins/access_token.rb +77 -0
  92. data/lib/doorkeeper/orm/active_record/mixins/application.rb +210 -0
  93. data/lib/doorkeeper/orm/active_record/redirect_uri_validator.rb +66 -0
  94. data/lib/doorkeeper/orm/active_record/stale_records_cleaner.rb +36 -0
  95. data/lib/doorkeeper/orm/active_record.rb +34 -12
  96. data/lib/doorkeeper/rails/helpers.rb +14 -15
  97. data/lib/doorkeeper/rails/routes/abstract_router.rb +35 -0
  98. data/lib/doorkeeper/rails/routes/mapper.rb +3 -1
  99. data/lib/doorkeeper/rails/routes/mapping.rb +10 -8
  100. data/lib/doorkeeper/rails/routes/registry.rb +45 -0
  101. data/lib/doorkeeper/rails/routes.rb +50 -29
  102. data/lib/doorkeeper/rake/db.rake +40 -0
  103. data/lib/doorkeeper/rake/setup.rake +6 -0
  104. data/lib/doorkeeper/rake.rb +14 -0
  105. data/lib/doorkeeper/request/authorization_code.rb +12 -4
  106. data/lib/doorkeeper/request/client_credentials.rb +3 -3
  107. data/lib/doorkeeper/request/code.rb +1 -1
  108. data/lib/doorkeeper/request/password.rb +5 -14
  109. data/lib/doorkeeper/request/refresh_token.rb +6 -5
  110. data/lib/doorkeeper/request/strategy.rb +4 -2
  111. data/lib/doorkeeper/request/token.rb +1 -1
  112. data/lib/doorkeeper/request.rb +62 -29
  113. data/lib/doorkeeper/secret_storing/base.rb +64 -0
  114. data/lib/doorkeeper/secret_storing/bcrypt.rb +60 -0
  115. data/lib/doorkeeper/secret_storing/plain.rb +33 -0
  116. data/lib/doorkeeper/secret_storing/sha256_hash.rb +26 -0
  117. data/lib/doorkeeper/server.rb +9 -19
  118. data/lib/doorkeeper/stale_records_cleaner.rb +24 -0
  119. data/lib/doorkeeper/validations.rb +5 -2
  120. data/lib/doorkeeper/version.rb +12 -1
  121. data/lib/doorkeeper.rb +180 -57
  122. data/lib/generators/doorkeeper/application_owner_generator.rb +28 -13
  123. data/lib/generators/doorkeeper/confidential_applications_generator.rb +33 -0
  124. data/lib/generators/doorkeeper/enable_polymorphic_resource_owner_generator.rb +39 -0
  125. data/lib/generators/doorkeeper/install_generator.rb +19 -9
  126. data/lib/generators/doorkeeper/migration_generator.rb +27 -10
  127. data/lib/generators/doorkeeper/pkce_generator.rb +33 -0
  128. data/lib/generators/doorkeeper/previous_refresh_token_generator.rb +31 -19
  129. data/lib/generators/doorkeeper/templates/add_confidential_to_applications.rb.erb +13 -0
  130. data/lib/generators/doorkeeper/templates/add_owner_to_application_migration.rb.erb +9 -0
  131. data/lib/generators/doorkeeper/templates/{add_previous_refresh_token_to_access_tokens.rb → add_previous_refresh_token_to_access_tokens.rb.erb} +3 -1
  132. data/lib/generators/doorkeeper/templates/enable_pkce_migration.rb.erb +8 -0
  133. data/lib/generators/doorkeeper/templates/enable_polymorphic_resource_owner_migration.rb.erb +17 -0
  134. data/lib/generators/doorkeeper/templates/initializer.rb +436 -33
  135. data/lib/generators/doorkeeper/templates/migration.rb.erb +98 -0
  136. data/lib/generators/doorkeeper/views_generator.rb +8 -4
  137. data/vendor/assets/stylesheets/doorkeeper/bootstrap.min.css +4 -5
  138. metadata +129 -281
  139. data/.gitignore +0 -14
  140. data/.hound.yml +0 -13
  141. data/.rspec +0 -1
  142. data/.travis.yml +0 -20
  143. data/CONTRIBUTING.md +0 -47
  144. data/Gemfile +0 -14
  145. data/NEWS.md +0 -593
  146. data/RELEASING.md +0 -17
  147. data/Rakefile +0 -20
  148. data/app/validators/redirect_uri_validator.rb +0 -34
  149. data/doorkeeper.gemspec +0 -28
  150. data/lib/doorkeeper/oauth/client/methods.rb +0 -18
  151. data/lib/doorkeeper/oauth/client_credentials/validation.rb +0 -45
  152. data/lib/doorkeeper/oauth/request_concern.rb +0 -48
  153. data/lib/generators/doorkeeper/templates/add_owner_to_application_migration.rb +0 -7
  154. data/lib/generators/doorkeeper/templates/migration.rb +0 -68
  155. data/spec/controllers/application_metal_controller.rb +0 -10
  156. data/spec/controllers/applications_controller_spec.rb +0 -58
  157. data/spec/controllers/authorizations_controller_spec.rb +0 -189
  158. data/spec/controllers/protected_resources_controller_spec.rb +0 -300
  159. data/spec/controllers/token_info_controller_spec.rb +0 -52
  160. data/spec/controllers/tokens_controller_spec.rb +0 -88
  161. data/spec/dummy/Rakefile +0 -7
  162. data/spec/dummy/app/controllers/application_controller.rb +0 -3
  163. data/spec/dummy/app/controllers/custom_authorizations_controller.rb +0 -7
  164. data/spec/dummy/app/controllers/full_protected_resources_controller.rb +0 -12
  165. data/spec/dummy/app/controllers/home_controller.rb +0 -17
  166. data/spec/dummy/app/controllers/metal_controller.rb +0 -11
  167. data/spec/dummy/app/controllers/semi_protected_resources_controller.rb +0 -11
  168. data/spec/dummy/app/helpers/application_helper.rb +0 -5
  169. data/spec/dummy/app/models/user.rb +0 -5
  170. data/spec/dummy/app/views/home/index.html.erb +0 -0
  171. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  172. data/spec/dummy/config/application.rb +0 -23
  173. data/spec/dummy/config/boot.rb +0 -9
  174. data/spec/dummy/config/database.yml +0 -15
  175. data/spec/dummy/config/environment.rb +0 -5
  176. data/spec/dummy/config/environments/development.rb +0 -29
  177. data/spec/dummy/config/environments/production.rb +0 -62
  178. data/spec/dummy/config/environments/test.rb +0 -44
  179. data/spec/dummy/config/initializers/active_record_belongs_to_required_by_default.rb +0 -6
  180. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  181. data/spec/dummy/config/initializers/doorkeeper.rb +0 -96
  182. data/spec/dummy/config/initializers/secret_token.rb +0 -9
  183. data/spec/dummy/config/initializers/session_store.rb +0 -8
  184. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  185. data/spec/dummy/config/locales/doorkeeper.en.yml +0 -5
  186. data/spec/dummy/config/routes.rb +0 -52
  187. data/spec/dummy/config.ru +0 -4
  188. data/spec/dummy/db/migrate/20111122132257_create_users.rb +0 -9
  189. data/spec/dummy/db/migrate/20120312140401_add_password_to_users.rb +0 -5
  190. data/spec/dummy/db/migrate/20151223192035_create_doorkeeper_tables.rb +0 -60
  191. data/spec/dummy/db/migrate/20151223200000_add_owner_to_application.rb +0 -7
  192. data/spec/dummy/db/migrate/20160320211015_add_previous_refresh_token_to_access_tokens.rb +0 -11
  193. data/spec/dummy/db/schema.rb +0 -67
  194. data/spec/dummy/public/404.html +0 -26
  195. data/spec/dummy/public/422.html +0 -26
  196. data/spec/dummy/public/500.html +0 -26
  197. data/spec/dummy/public/favicon.ico +0 -0
  198. data/spec/dummy/script/rails +0 -6
  199. data/spec/factories.rb +0 -28
  200. data/spec/generators/application_owner_generator_spec.rb +0 -22
  201. data/spec/generators/install_generator_spec.rb +0 -31
  202. data/spec/generators/migration_generator_spec.rb +0 -20
  203. data/spec/generators/templates/routes.rb +0 -3
  204. data/spec/generators/views_generator_spec.rb +0 -27
  205. data/spec/helpers/doorkeeper/dashboard_helper_spec.rb +0 -24
  206. data/spec/lib/config_spec.rb +0 -334
  207. data/spec/lib/doorkeeper_spec.rb +0 -28
  208. data/spec/lib/models/expirable_spec.rb +0 -51
  209. data/spec/lib/models/revocable_spec.rb +0 -59
  210. data/spec/lib/models/scopes_spec.rb +0 -43
  211. data/spec/lib/oauth/authorization/uri_builder_spec.rb +0 -42
  212. data/spec/lib/oauth/authorization_code_request_spec.rb +0 -80
  213. data/spec/lib/oauth/client/credentials_spec.rb +0 -47
  214. data/spec/lib/oauth/client/methods_spec.rb +0 -54
  215. data/spec/lib/oauth/client_credentials/creator_spec.rb +0 -44
  216. data/spec/lib/oauth/client_credentials/issuer_spec.rb +0 -86
  217. data/spec/lib/oauth/client_credentials/validation_spec.rb +0 -54
  218. data/spec/lib/oauth/client_credentials_integration_spec.rb +0 -27
  219. data/spec/lib/oauth/client_credentials_request_spec.rb +0 -104
  220. data/spec/lib/oauth/client_spec.rb +0 -39
  221. data/spec/lib/oauth/code_request_spec.rb +0 -45
  222. data/spec/lib/oauth/code_response_spec.rb +0 -34
  223. data/spec/lib/oauth/error_response_spec.rb +0 -61
  224. data/spec/lib/oauth/error_spec.rb +0 -23
  225. data/spec/lib/oauth/forbidden_token_response_spec.rb +0 -23
  226. data/spec/lib/oauth/helpers/scope_checker_spec.rb +0 -64
  227. data/spec/lib/oauth/helpers/unique_token_spec.rb +0 -20
  228. data/spec/lib/oauth/helpers/uri_checker_spec.rb +0 -104
  229. data/spec/lib/oauth/invalid_token_response_spec.rb +0 -28
  230. data/spec/lib/oauth/password_access_token_request_spec.rb +0 -90
  231. data/spec/lib/oauth/pre_authorization_spec.rb +0 -155
  232. data/spec/lib/oauth/refresh_token_request_spec.rb +0 -154
  233. data/spec/lib/oauth/scopes_spec.rb +0 -122
  234. data/spec/lib/oauth/token_request_spec.rb +0 -98
  235. data/spec/lib/oauth/token_response_spec.rb +0 -85
  236. data/spec/lib/oauth/token_spec.rb +0 -116
  237. data/spec/lib/request/strategy_spec.rb +0 -53
  238. data/spec/lib/server_spec.rb +0 -52
  239. data/spec/models/doorkeeper/access_grant_spec.rb +0 -36
  240. data/spec/models/doorkeeper/access_token_spec.rb +0 -394
  241. data/spec/models/doorkeeper/application_spec.rb +0 -179
  242. data/spec/requests/applications/applications_request_spec.rb +0 -94
  243. data/spec/requests/applications/authorized_applications_spec.rb +0 -30
  244. data/spec/requests/endpoints/authorization_spec.rb +0 -72
  245. data/spec/requests/endpoints/token_spec.rb +0 -64
  246. data/spec/requests/flows/authorization_code_errors_spec.rb +0 -66
  247. data/spec/requests/flows/authorization_code_spec.rb +0 -156
  248. data/spec/requests/flows/client_credentials_spec.rb +0 -58
  249. data/spec/requests/flows/implicit_grant_errors_spec.rb +0 -32
  250. data/spec/requests/flows/implicit_grant_spec.rb +0 -61
  251. data/spec/requests/flows/password_spec.rb +0 -115
  252. data/spec/requests/flows/refresh_token_spec.rb +0 -174
  253. data/spec/requests/flows/revoke_token_spec.rb +0 -157
  254. data/spec/requests/flows/skip_authorization_spec.rb +0 -59
  255. data/spec/requests/protected_resources/metal_spec.rb +0 -14
  256. data/spec/requests/protected_resources/private_api_spec.rb +0 -81
  257. data/spec/routing/custom_controller_routes_spec.rb +0 -71
  258. data/spec/routing/default_routes_spec.rb +0 -35
  259. data/spec/routing/scoped_routes_spec.rb +0 -31
  260. data/spec/spec_helper.rb +0 -2
  261. data/spec/spec_helper_integration.rb +0 -59
  262. data/spec/support/dependencies/factory_girl.rb +0 -2
  263. data/spec/support/helpers/access_token_request_helper.rb +0 -11
  264. data/spec/support/helpers/authorization_request_helper.rb +0 -41
  265. data/spec/support/helpers/config_helper.rb +0 -9
  266. data/spec/support/helpers/model_helper.rb +0 -67
  267. data/spec/support/helpers/request_spec_helper.rb +0 -76
  268. data/spec/support/helpers/url_helper.rb +0 -55
  269. data/spec/support/http_method_shim.rb +0 -24
  270. data/spec/support/orm/active_record.rb +0 -3
  271. data/spec/support/shared/controllers_shared_context.rb +0 -69
  272. data/spec/support/shared/models_shared_examples.rb +0 -52
  273. data/spec/validators/redirect_uri_validator_spec.rb +0 -78
@@ -0,0 +1,204 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Doorkeeper
4
+ module OAuth
5
+ # RFC7662 OAuth 2.0 Token Introspection
6
+ #
7
+ # @see https://datatracker.ietf.org/doc/html/rfc7662
8
+ class TokenIntrospection
9
+ attr_reader :error
10
+
11
+ def initialize(server, token)
12
+ @server = server
13
+ @token = token
14
+
15
+ authorize!
16
+ end
17
+
18
+ def authorized?
19
+ @error.blank?
20
+ end
21
+
22
+ def error_response
23
+ return if @error.blank?
24
+
25
+ if @error == Errors::InvalidToken
26
+ OAuth::InvalidTokenResponse.from_access_token(authorized_token)
27
+ elsif @error == Errors::InvalidRequest
28
+ OAuth::InvalidRequestResponse.from_request(self)
29
+ else
30
+ OAuth::ErrorResponse.from_request(self)
31
+ end
32
+ end
33
+
34
+ def to_json(*)
35
+ active? ? success_response : failure_response
36
+ end
37
+
38
+ private
39
+
40
+ attr_reader :server, :token
41
+ attr_reader :invalid_request_reason
42
+
43
+ # If the protected resource uses OAuth 2.0 client credentials to
44
+ # authenticate to the introspection endpoint and its credentials are
45
+ # invalid, the authorization server responds with an HTTP 401
46
+ # (Unauthorized) as described in Section 5.2 of OAuth 2.0 [RFC6749].
47
+ #
48
+ # Endpoint must first validate the authentication.
49
+ # If the authentication is invalid, the endpoint should respond with
50
+ # an HTTP 401 status code and an invalid_client response.
51
+ #
52
+ # @see https://www.oauth.com/oauth2-servers/token-introspection-endpoint/
53
+ #
54
+ # To prevent token scanning attacks, the endpoint MUST also require
55
+ # some form of authorization to access this endpoint, such as client
56
+ # authentication as described in OAuth 2.0 [RFC6749] or a separate
57
+ # OAuth 2.0 access token such as the bearer token described in OAuth
58
+ # 2.0 Bearer Token Usage [RFC6750].
59
+ #
60
+ def authorize!
61
+ # Requested client authorization
62
+ if server.credentials
63
+ @error = Errors::InvalidClient unless authorized_client
64
+ elsif authorized_token
65
+ # Requested bearer token authorization
66
+ #
67
+ # If the protected resource uses an OAuth 2.0 bearer token to authorize
68
+ # its call to the introspection endpoint and the token used for
69
+ # authorization does not contain sufficient privileges or is otherwise
70
+ # invalid for this request, the authorization server responds with an
71
+ # HTTP 401 code as described in Section 3 of OAuth 2.0 Bearer Token
72
+ # Usage [RFC6750].
73
+ #
74
+ @error = Errors::InvalidToken unless valid_authorized_token?
75
+ else
76
+ @error = Errors::InvalidRequest
77
+ @invalid_request_reason = :request_not_authorized
78
+ end
79
+ end
80
+
81
+ # Client Authentication
82
+ def authorized_client
83
+ @authorized_client ||= server.credentials && server.client
84
+ end
85
+
86
+ # Bearer Token Authentication
87
+ def authorized_token
88
+ @authorized_token ||= Doorkeeper.authenticate(server.context.request)
89
+ end
90
+
91
+ # 2.2. Introspection Response
92
+ def success_response
93
+ customize_response(
94
+ active: true,
95
+ scope: @token.scopes_string,
96
+ client_id: @token.try(:application).try(:uid),
97
+ token_type: @token.token_type,
98
+ exp: @token.expires_at.to_i,
99
+ iat: @token.created_at.to_i,
100
+ )
101
+ end
102
+
103
+ # If the introspection call is properly authorized but the token is not
104
+ # active, does not exist on this server, or the protected resource is
105
+ # not allowed to introspect this particular token, then the
106
+ # authorization server MUST return an introspection response with the
107
+ # "active" field set to "false". Note that to avoid disclosing too
108
+ # much of the authorization server's state to a third party, the
109
+ # authorization server SHOULD NOT include any additional information
110
+ # about an inactive token, including why the token is inactive.
111
+ #
112
+ # @see https://datatracker.ietf.org/doc/html/rfc7662 2.2. Introspection Response
113
+ #
114
+ def failure_response
115
+ {
116
+ active: false,
117
+ }
118
+ end
119
+
120
+ # Boolean indicator of whether or not the presented token
121
+ # is currently active. The specifics of a token's "active" state
122
+ # will vary depending on the implementation of the authorization
123
+ # server and the information it keeps about its tokens, but a "true"
124
+ # value return for the "active" property will generally indicate
125
+ # that a given token has been issued by this authorization server,
126
+ # has not been revoked by the resource owner, and is within its
127
+ # given time window of validity (e.g., after its issuance time and
128
+ # before its expiration time).
129
+ #
130
+ # Any other error is considered an "inactive" token.
131
+ #
132
+ # * The token requested does not exist or is invalid
133
+ # * The token expired
134
+ # * The token was issued to a different client than is making this request
135
+ #
136
+ # Since resource servers using token introspection rely on the
137
+ # authorization server to determine the state of a token, the
138
+ # authorization server MUST perform all applicable checks against a
139
+ # token's state. For instance, these tests include the following:
140
+ #
141
+ # o If the token can expire, the authorization server MUST determine
142
+ # whether or not the token has expired.
143
+ # o If the token can be issued before it is able to be used, the
144
+ # authorization server MUST determine whether or not a token's valid
145
+ # period has started yet.
146
+ # o If the token can be revoked after it was issued, the authorization
147
+ # server MUST determine whether or not such a revocation has taken
148
+ # place.
149
+ # o If the token has been signed, the authorization server MUST
150
+ # validate the signature.
151
+ # o If the token can be used only at certain resource servers, the
152
+ # authorization server MUST determine whether or not the token can
153
+ # be used at the resource server making the introspection call.
154
+ #
155
+ def active?
156
+ if authorized_client
157
+ valid_token? && token_introspection_allowed?(auth_client: authorized_client.application)
158
+ else
159
+ valid_token?
160
+ end
161
+ end
162
+
163
+ # Token can be valid only if it is not expired or revoked.
164
+ def valid_token?
165
+ @token&.accessible?
166
+ end
167
+
168
+ def valid_authorized_token?
169
+ !authorized_token_matches_introspected? &&
170
+ authorized_token.accessible? &&
171
+ token_introspection_allowed?(auth_token: authorized_token)
172
+ end
173
+
174
+ # RFC7662 Section 2.1
175
+ def authorized_token_matches_introspected?
176
+ authorized_token.token == @token&.token
177
+ end
178
+
179
+ # Config constraints for introspection in Doorkeeper.config.allow_token_introspection
180
+ def token_introspection_allowed?(auth_client: nil, auth_token: nil)
181
+ allow_introspection = Doorkeeper.config.allow_token_introspection
182
+ return allow_introspection unless allow_introspection.respond_to?(:call)
183
+
184
+ allow_introspection.call(@token, auth_client, auth_token)
185
+ end
186
+
187
+ # Allows to customize introspection response.
188
+ # Provides context (controller) and token for generating developer-specific
189
+ # response.
190
+ #
191
+ # @see https://datatracker.ietf.org/doc/html/rfc7662#section-2.2
192
+ #
193
+ def customize_response(response)
194
+ customized_response = Doorkeeper.config.custom_introspection_response.call(
195
+ token,
196
+ server.context,
197
+ )
198
+ return response if customized_response.blank?
199
+
200
+ response.merge(customized_response)
201
+ end
202
+ end
203
+ end
204
+ end
@@ -1,37 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Doorkeeper
2
4
  module OAuth
3
5
  class TokenRequest
4
- attr_accessor :pre_auth, :resource_owner, :client
6
+ attr_reader :pre_auth, :resource_owner
5
7
 
6
8
  def initialize(pre_auth, resource_owner)
7
- @pre_auth = pre_auth
8
- @client = pre_auth.client
9
+ @pre_auth = pre_auth
9
10
  @resource_owner = resource_owner
10
11
  end
11
12
 
12
13
  def authorize
13
- if pre_auth.authorizable?
14
- auth = Authorization::Token.new(pre_auth, resource_owner)
15
- auth.issue_token
16
- @response = CodeResponse.new pre_auth,
17
- auth,
18
- response_on_fragment: true
19
- else
20
- @response = error_response
21
- end
14
+ auth = Authorization::Token.new(pre_auth, resource_owner)
15
+ auth.issue_token!
16
+ CodeResponse.new(pre_auth, auth, response_on_fragment: true)
22
17
  end
23
18
 
24
19
  def deny
25
- pre_auth.error = :access_denied
26
- error_response
27
- end
28
-
29
- private
30
-
31
- def error_response
32
- ErrorResponse.from_request pre_auth,
33
- redirect_uri: pre_auth.redirect_uri,
34
- response_on_fragment: true
20
+ pre_auth.error = Errors::AccessDenied
21
+ pre_auth.error_response
35
22
  end
36
23
  end
37
24
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Doorkeeper
2
4
  module OAuth
3
5
  class TokenResponse
4
- attr_accessor :token
6
+ attr_reader :token
5
7
 
6
8
  def initialize(token)
7
9
  @token = token
@@ -9,12 +11,12 @@ module Doorkeeper
9
11
 
10
12
  def body
11
13
  {
12
- 'access_token' => token.token,
13
- 'token_type' => token.token_type,
14
- 'expires_in' => token.expires_in_seconds,
15
- 'refresh_token' => token.refresh_token,
16
- 'scope' => token.scopes_string,
17
- 'created_at' => token.created_at.to_i
14
+ "access_token" => token.plaintext_token,
15
+ "token_type" => token.token_type,
16
+ "expires_in" => token.expires_in_seconds,
17
+ "refresh_token" => token.plaintext_refresh_token,
18
+ "scope" => token.scopes_string,
19
+ "created_at" => token.created_at.to_i,
18
20
  }.reject { |_, value| value.blank? }
19
21
  end
20
22
 
@@ -23,9 +25,10 @@ module Doorkeeper
23
25
  end
24
26
 
25
27
  def headers
26
- { 'Cache-Control' => 'no-store',
27
- 'Pragma' => 'no-cache',
28
- 'Content-Type' => 'application/json; charset=utf-8' }
28
+ {
29
+ "Cache-Control" => "no-store, no-cache",
30
+ "Content-Type" => "application/json; charset=utf-8",
31
+ }
29
32
  end
30
33
  end
31
34
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Doorkeeper
4
+ module OAuth
5
+ GRANT_TYPES = [
6
+ AUTHORIZATION_CODE = "authorization_code",
7
+ IMPLICIT = "implicit",
8
+ PASSWORD = "password",
9
+ CLIENT_CREDENTIALS = "client_credentials",
10
+ REFRESH_TOKEN = "refresh_token",
11
+ ].freeze
12
+ end
13
+ end
@@ -1,7 +1,9 @@
1
- module Doorkeeper
2
- class AccessGrant < ActiveRecord::Base
3
- self.table_name = "#{table_name_prefix}oauth_access_grants#{table_name_suffix}".to_sym
1
+ # frozen_string_literal: true
2
+
3
+ require "doorkeeper/orm/active_record/mixins/access_grant"
4
4
 
5
- include AccessGrantMixin
5
+ module Doorkeeper
6
+ class AccessGrant < ::ActiveRecord::Base
7
+ include Doorkeeper::Orm::ActiveRecord::Mixins::AccessGrant
6
8
  end
7
9
  end
@@ -1,29 +1,9 @@
1
- module Doorkeeper
2
- class AccessToken < ActiveRecord::Base
3
- self.table_name = "#{table_name_prefix}oauth_access_tokens#{table_name_suffix}".to_sym
4
-
5
- include AccessTokenMixin
6
-
7
- def self.delete_all_for(application_id, resource_owner)
8
- where(application_id: application_id,
9
- resource_owner_id: resource_owner.id).delete_all
10
- end
11
- private_class_method :delete_all_for
1
+ # frozen_string_literal: true
12
2
 
13
- def self.active_for(resource_owner)
14
- where(resource_owner_id: resource_owner.id, revoked_at: nil)
15
- end
3
+ require "doorkeeper/orm/active_record/mixins/access_token"
16
4
 
17
- def self.order_method
18
- :order
19
- end
20
-
21
- def self.refresh_token_revoked_on_use?
22
- column_names.include?('previous_refresh_token')
23
- end
24
-
25
- def self.created_at_desc
26
- 'created_at desc'
27
- end
5
+ module Doorkeeper
6
+ class AccessToken < ::ActiveRecord::Base
7
+ include Doorkeeper::Orm::ActiveRecord::Mixins::AccessToken
28
8
  end
29
9
  end
@@ -1,19 +1,10 @@
1
- module Doorkeeper
2
- class Application < ActiveRecord::Base
3
- self.table_name = "#{table_name_prefix}oauth_applications#{table_name_suffix}".to_sym
4
-
5
- include ApplicationMixin
1
+ # frozen_string_literal: true
6
2
 
7
- if ActiveRecord::VERSION::MAJOR >= 4
8
- has_many :authorized_tokens, -> { where(revoked_at: nil) }, class_name: 'AccessToken'
9
- else
10
- has_many :authorized_tokens, class_name: 'AccessToken', conditions: { revoked_at: nil }
11
- end
12
- has_many :authorized_applications, through: :authorized_tokens, source: :application
3
+ require "doorkeeper/orm/active_record/redirect_uri_validator"
4
+ require "doorkeeper/orm/active_record/mixins/application"
13
5
 
14
- def self.authorized_for(resource_owner)
15
- resource_access_tokens = AccessToken.active_for(resource_owner)
16
- where(id: resource_access_tokens.select(:application_id).distinct)
17
- end
6
+ module Doorkeeper
7
+ class Application < ::ActiveRecord::Base
8
+ include ::Doorkeeper::Orm::ActiveRecord::Mixins::Application
18
9
  end
19
10
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Doorkeeper::Orm::ActiveRecord::Mixins
4
+ module AccessGrant
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ self.table_name = compute_doorkeeper_table_name
9
+ self.strict_loading_by_default = false if respond_to?(:strict_loading_by_default)
10
+
11
+ include ::Doorkeeper::AccessGrantMixin
12
+
13
+ belongs_to :application, class_name: Doorkeeper.config.application_class.to_s,
14
+ optional: true,
15
+ inverse_of: :access_grants
16
+
17
+ validates :application_id,
18
+ :token,
19
+ :expires_in,
20
+ :redirect_uri,
21
+ presence: true
22
+
23
+ validates :token, uniqueness: { case_sensitive: true }
24
+
25
+ before_validation :generate_token, on: :create
26
+
27
+ # We keep a volatile copy of the raw token for initial communication
28
+ # The stored refresh_token may be mapped and not available in cleartext.
29
+ #
30
+ # Some strategies allow restoring stored secrets (e.g. symmetric encryption)
31
+ # while hashing strategies do not, so you cannot rely on this value
32
+ # returning a present value for persisted tokens.
33
+ def plaintext_token
34
+ if secret_strategy.allows_restoring_secrets?
35
+ secret_strategy.restore_secret(self, :token)
36
+ else
37
+ @raw_token
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ # Generates token value with UniqueToken class.
44
+ #
45
+ # @return [String] token value
46
+ #
47
+ def generate_token
48
+ @raw_token = Doorkeeper::OAuth::Helpers::UniqueToken.generate
49
+ secret_strategy.store_secret(self, :token, @raw_token)
50
+ end
51
+ end
52
+
53
+ module ClassMethods
54
+ private
55
+
56
+ def compute_doorkeeper_table_name
57
+ table_name = "oauth_access_grant"
58
+ table_name = table_name.pluralize if pluralize_table_names
59
+ "#{table_name_prefix}#{table_name}#{table_name_suffix}"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Doorkeeper::Orm::ActiveRecord::Mixins
4
+ module AccessToken
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ self.table_name = compute_doorkeeper_table_name
9
+ self.strict_loading_by_default = false if respond_to?(:strict_loading_by_default)
10
+
11
+ include ::Doorkeeper::AccessTokenMixin
12
+
13
+ belongs_to :application, class_name: Doorkeeper.config.application_class.to_s,
14
+ inverse_of: :access_tokens,
15
+ optional: true
16
+
17
+ validates :token, presence: true, uniqueness: { case_sensitive: true }
18
+ validates :refresh_token, uniqueness: { case_sensitive: true }, if: :use_refresh_token?
19
+
20
+ # @attr_writer [Boolean, nil] use_refresh_token
21
+ # indicates the possibility of using refresh token
22
+ attr_writer :use_refresh_token
23
+
24
+ before_validation :generate_token, on: :create
25
+ before_validation :generate_refresh_token,
26
+ on: :create, if: :use_refresh_token?
27
+ end
28
+
29
+ module ClassMethods
30
+ # Searches for not revoked Access Tokens associated with the
31
+ # specific Resource Owner.
32
+ #
33
+ # @param resource_owner [ActiveRecord::Base]
34
+ # Resource Owner model instance
35
+ #
36
+ # @return [ActiveRecord::Relation]
37
+ # active Access Tokens for Resource Owner
38
+ #
39
+ def active_for(resource_owner)
40
+ by_resource_owner(resource_owner).where(revoked_at: nil)
41
+ end
42
+
43
+ def refresh_token_revoked_on_use?
44
+ column_names.include?("previous_refresh_token")
45
+ end
46
+
47
+ # Returns non-expired and non-revoked access tokens
48
+ def not_expired
49
+ relation = where(revoked_at: nil)
50
+
51
+ if supports_expiration_time_math?
52
+ # have not reached the expiration time or it never expires
53
+ relation.where("#{expiration_time_sql} > ?", Time.now.utc).or(
54
+ relation.where(expires_in: nil)
55
+ )
56
+ else
57
+ ::Kernel.warn <<~WARNING.squish
58
+ [DOORKEEPER] Doorkeeper doesn't support expiration time math for your database adapter (#{adapter_name}).
59
+ Please add a class method `custom_expiration_time_sql` for your AccessToken class/mixin to provide a custom
60
+ SQL expression to calculate access token expiration time. See lib/doorkeeper/orm/active_record/mixins/access_token.rb
61
+ for more details.
62
+ WARNING
63
+
64
+ relation
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def compute_doorkeeper_table_name
71
+ table_name = "oauth_access_token"
72
+ table_name = table_name.pluralize if pluralize_table_names
73
+ "#{table_name_prefix}#{table_name}#{table_name_suffix}"
74
+ end
75
+ end
76
+ end
77
+ end