loading
Generated 2023-11-27T06:15:00+00:00

All Files ( 96.21% covered at 389.41 hits/line )

38 files in total.
3218 relevant lines, 3096 lines covered and 122 lines missed. ( 96.21% )
File % covered Lines Relevant Lines Lines covered Lines missed Avg. Hits / Line
lib/rodauth/features/oauth_application_management.rb 100.00 % 210 117 117 0 57.44
lib/rodauth/features/oauth_assertion_base.rb 100.00 % 92 38 38 0 53.05
lib/rodauth/features/oauth_authorization_code_grant.rb 100.00 % 165 76 76 0 599.37
lib/rodauth/features/oauth_authorize_base.rb 97.67 % 260 129 126 3 697.24
lib/rodauth/features/oauth_base.rb 96.07 % 870 433 416 17 1325.05
lib/rodauth/features/oauth_client_credentials_grant.rb 100.00 % 35 17 17 0 33.18
lib/rodauth/features/oauth_device_code_grant.rb 95.33 % 208 107 102 5 50.02
lib/rodauth/features/oauth_dynamic_client_registration.rb 95.56 % 435 225 215 10 674.76
lib/rodauth/features/oauth_grant_management.rb 100.00 % 70 33 33 0 63.27
lib/rodauth/features/oauth_implicit_grant.rb 100.00 % 95 49 49 0 366.12
lib/rodauth/features/oauth_jwt.rb 100.00 % 128 57 57 0 279.72
lib/rodauth/features/oauth_jwt_base.rb 93.12 % 510 218 203 15 197.34
lib/rodauth/features/oauth_jwt_bearer_grant.rb 100.00 % 91 47 47 0 37.28
lib/rodauth/features/oauth_jwt_jwks.rb 100.00 % 47 23 23 0 42.78
lib/rodauth/features/oauth_jwt_secured_authorization_request.rb 100.00 % 143 68 68 0 140.29
lib/rodauth/features/oauth_jwt_secured_authorization_response_mode.rb 98.53 % 126 68 67 1 70.24
lib/rodauth/features/oauth_management_base.rb 100.00 % 72 33 33 0 149.09
lib/rodauth/features/oauth_pkce.rb 100.00 % 93 49 49 0 23.27
lib/rodauth/features/oauth_pushed_authorization_request.rb 92.54 % 144 67 62 5 34.39
lib/rodauth/features/oauth_resource_indicators.rb 93.90 % 166 82 77 5 53.85
lib/rodauth/features/oauth_resource_server.rb 96.30 % 59 27 26 1 59.56
lib/rodauth/features/oauth_saml_bearer_grant.rb 96.77 % 140 62 60 2 17.61
lib/rodauth/features/oauth_tls_client_auth.rb 89.16 % 170 83 74 9 125.69
lib/rodauth/features/oauth_token_introspection.rb 98.33 % 139 60 59 1 98.60
lib/rodauth/features/oauth_token_revocation.rb 100.00 % 124 60 60 0 76.80
lib/rodauth/features/oidc.rb 93.57 % 874 389 364 25 240.25
lib/rodauth/features/oidc_backchannel_logout.rb 98.11 % 120 53 52 1 45.51
lib/rodauth/features/oidc_dynamic_client_registration.rb 90.44 % 298 136 123 13 111.97
lib/rodauth/features/oidc_frontchannel_logout.rb 100.00 % 134 67 67 0 43.28
lib/rodauth/features/oidc_logout_base.rb 100.00 % 76 38 38 0 87.79
lib/rodauth/features/oidc_rp_initiated_logout.rb 95.24 % 140 63 60 3 41.33
lib/rodauth/features/oidc_self_issued.rb 97.06 % 73 34 33 1 22.24
lib/rodauth/features/oidc_session_management.rb 97.78 % 89 45 44 1 16.27
lib/rodauth/oauth.rb 100.00 % 35 18 18 0 2630.00
lib/rodauth/oauth/database_extensions.rb 100.00 % 88 41 41 0 1670.59
lib/rodauth/oauth/http_extensions.rb 95.56 % 74 45 43 2 137.87
lib/rodauth/oauth/jwe_extensions.rb 100.00 % 64 33 33 0 34.09
lib/rodauth/oauth/ttl_store.rb 92.86 % 67 28 26 2 12.43

lib/rodauth/features/oauth_application_management.rb

100.0% lines covered

117 relevant lines. 117 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_application_management, :OauthApplicationManagement) do
  5. 12 depends :oauth_management_base, :oauth_token_revocation
  6. 12 before "create_oauth_application"
  7. 12 after "create_oauth_application"
  8. 12 error_flash "There was an error registering your oauth application", "create_oauth_application"
  9. 12 notice_flash "Your oauth application has been registered", "create_oauth_application"
  10. 12 view "oauth_applications", "Oauth Applications", "oauth_applications"
  11. 12 view "oauth_application", "Oauth Application", "oauth_application"
  12. 12 view "new_oauth_application", "New Oauth Application", "new_oauth_application"
  13. 12 view "oauth_application_oauth_grants", "Oauth Application Grants", "oauth_application_oauth_grants"
  14. # Application
  15. 12 APPLICATION_REQUIRED_PARAMS = %w[name scopes homepage_url redirect_uri client_secret].freeze
  16. 12 auth_value_method :oauth_application_required_params, APPLICATION_REQUIRED_PARAMS
  17. 12 (APPLICATION_REQUIRED_PARAMS + %w[description client_id]).each do |param|
  18. 84 auth_value_method :"oauth_application_#{param}_param", param
  19. end
  20. 12 translatable_method :oauth_applications_name_label, "Name"
  21. 12 translatable_method :oauth_applications_description_label, "Description"
  22. 12 translatable_method :oauth_applications_scopes_label, "Default scopes"
  23. 12 translatable_method :oauth_applications_contacts_label, "Contacts"
  24. 12 translatable_method :oauth_applications_tos_uri_label, "Terms of service"
  25. 12 translatable_method :oauth_applications_policy_uri_label, "Policy"
  26. 12 translatable_method :oauth_applications_jwks_label, "JSON Web Keys"
  27. 12 translatable_method :oauth_applications_jwks_uri_label, "JSON Web Keys URI"
  28. 12 translatable_method :oauth_applications_homepage_url_label, "Homepage URL"
  29. 12 translatable_method :oauth_applications_redirect_uri_label, "Redirect URI"
  30. 12 translatable_method :oauth_applications_client_secret_label, "Client Secret"
  31. 12 translatable_method :oauth_applications_client_id_label, "Client ID"
  32. 12 %w[type token refresh_token expires_in revoked_at].each do |param|
  33. 60 translatable_method :"oauth_grants_#{param}_label", param.gsub("_", " ").capitalize
  34. end
  35. 12 button "Register", "oauth_application"
  36. 12 button "Revoke", "oauth_grant_revoke"
  37. 12 auth_value_method :oauth_applications_oauth_grants_path, "oauth-grants"
  38. 12 auth_value_method :oauth_applications_route, "oauth-applications"
  39. 12 auth_value_method :oauth_applications_per_page, 20
  40. 12 auth_value_method :oauth_applications_id_pattern, Integer
  41. 12 auth_value_method :oauth_grants_per_page, 20
  42. 12 translatable_method :invalid_url_message, "Invalid URL"
  43. 12 translatable_method :null_error_message, "is not filled"
  44. 12 translatable_method :oauth_no_applications_text, "No oauth applications yet!"
  45. 12 translatable_method :oauth_no_grants_text, "No oauth grants yet!"
  46. 12 auth_methods(
  47. :oauth_application_path
  48. )
  49. 12 def oauth_applications_path(opts = {})
  50. 1032 route_path(oauth_applications_route, opts)
  51. end
  52. 12 def oauth_application_path(id)
  53. 204 "#{oauth_applications_path}/#{id}"
  54. end
  55. # /oauth-applications routes
  56. 12 def load_oauth_application_management_routes
  57. 252 request.on(oauth_applications_route) do
  58. 252 check_csrf if check_csrf?
  59. 252 require_account
  60. 252 request.get "new" do
  61. 36 new_oauth_application_view
  62. end
  63. 216 request.on(oauth_applications_id_pattern) do |id|
  64. 84 oauth_application = db[oauth_applications_table]
  65. .where(oauth_applications_id_column => id)
  66. .where(oauth_applications_account_id_column => account_id)
  67. .first
  68. 84 next unless oauth_application
  69. 72 scope.instance_variable_set(:@oauth_application, oauth_application)
  70. 72 request.is do
  71. 24 request.get do
  72. 24 oauth_application_view
  73. end
  74. end
  75. 48 request.on(oauth_applications_oauth_grants_path) do
  76. 48 page = Integer(param_or_nil("page") || 1)
  77. 48 per_page = per_page_param(oauth_grants_per_page)
  78. 48 oauth_grants = db[oauth_grants_table]
  79. .where(oauth_grants_oauth_application_id_column => id)
  80. .order(Sequel.desc(oauth_grants_id_column))
  81. 48 scope.instance_variable_set(:@oauth_grants, oauth_grants.paginate(page, per_page))
  82. 48 request.is do
  83. 48 request.get do
  84. 48 oauth_application_oauth_grants_view
  85. end
  86. end
  87. end
  88. end
  89. 132 request.is do
  90. 132 request.get do
  91. 84 page = Integer(param_or_nil("page") || 1)
  92. 84 per_page = per_page_param(oauth_applications_per_page)
  93. 84 scope.instance_variable_set(:@oauth_applications, db[oauth_applications_table]
  94. .where(oauth_applications_account_id_column => account_id)
  95. .order(Sequel.desc(oauth_applications_id_column))
  96. .paginate(page, per_page))
  97. 84 oauth_applications_view
  98. end
  99. 48 request.post do
  100. 48 catch_error do
  101. 48 validate_oauth_application_params
  102. 24 transaction do
  103. 24 before_create_oauth_application
  104. 24 id = create_oauth_application
  105. 24 after_create_oauth_application
  106. 24 set_notice_flash create_oauth_application_notice_flash
  107. 24 redirect "#{request.path}/#{id}"
  108. end
  109. end
  110. 24 set_error_flash create_oauth_application_error_flash
  111. 24 new_oauth_application_view
  112. end
  113. end
  114. end
  115. end
  116. 12 private
  117. 12 def oauth_application_params
  118. 192 @oauth_application_params ||= oauth_application_required_params.each_with_object({}) do |param, params|
  119. 240 value = request.params[__send__(:"oauth_application_#{param}_param")]
  120. 240 if value && !value.empty?
  121. 156 params[param] = value
  122. else
  123. 84 set_field_error(param, null_error_message)
  124. end
  125. end
  126. end
  127. 12 def validate_oauth_application_params
  128. 48 oauth_application_params.each do |key, value|
  129. 156 if key == oauth_application_homepage_url_param
  130. 36 set_field_error(key, invalid_url_message) unless check_valid_uri?(value)
  131. 120 elsif key == oauth_application_redirect_uri_param
  132. 36 if value.respond_to?(:each)
  133. 12 value.each do |uri|
  134. 24 next if uri.empty?
  135. 24 set_field_error(key, invalid_url_message) unless check_valid_no_fragment_uri?(uri)
  136. end
  137. else
  138. 24 set_field_error(key, invalid_url_message) unless check_valid_no_fragment_uri?(value)
  139. end
  140. 84 elsif key == oauth_application_scopes_param
  141. 24 value.each do |scope|
  142. 48 set_field_error(key, oauth_invalid_scope_message) unless oauth_application_scopes.include?(scope)
  143. end
  144. end
  145. end
  146. 48 throw :rodauth_error if @field_errors && !@field_errors.empty?
  147. end
  148. 12 def create_oauth_application
  149. 8 create_params = {
  150. 16 oauth_applications_account_id_column => account_id,
  151. oauth_applications_name_column => oauth_application_params[oauth_application_name_param],
  152. oauth_applications_description_column => oauth_application_params[oauth_application_description_param],
  153. oauth_applications_scopes_column => oauth_application_params[oauth_application_scopes_param],
  154. oauth_applications_homepage_url_column => oauth_application_params[oauth_application_homepage_url_param]
  155. }
  156. 24 redirect_uris = oauth_application_params[oauth_application_redirect_uri_param]
  157. 24 redirect_uris = redirect_uris.to_a.reject(&:empty?).join(" ") if redirect_uris.respond_to?(:each)
  158. 24 create_params[oauth_applications_redirect_uri_column] = redirect_uris unless redirect_uris.empty?
  159. # set client ID/secret pairs
  160. 24 set_client_secret(create_params, oauth_application_params[oauth_application_client_secret_param])
  161. 24 if create_params[oauth_applications_scopes_column]
  162. 24 create_params[oauth_applications_scopes_column] = create_params[oauth_applications_scopes_column].join(oauth_scope_separator)
  163. end
  164. 24 rescue_from_uniqueness_error do
  165. 24 create_params[oauth_applications_client_id_column] = oauth_unique_id_generator
  166. 24 db[oauth_applications_table].insert(create_params)
  167. end
  168. end
  169. end
  170. end

lib/rodauth/features/oauth_assertion_base.rb

100.0% lines covered

38 relevant lines. 38 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_assertion_base, :OauthAssertionBase) do
  5. 12 depends :oauth_base
  6. 12 auth_methods(
  7. :assertion_grant_type?,
  8. :client_assertion_type?,
  9. :assertion_grant_type,
  10. :client_assertion_type
  11. )
  12. 12 private
  13. 12 def validate_token_params
  14. 96 return super unless assertion_grant_type?
  15. 48 redirect_response_error("invalid_grant") unless param_or_nil("assertion")
  16. end
  17. 12 def require_oauth_application
  18. 192 if assertion_grant_type?
  19. 48 @oauth_application = __send__(:"require_oauth_application_from_#{assertion_grant_type}_assertion_issuer", param("assertion"))
  20. 144 elsif client_assertion_type?
  21. 108 @oauth_application = __send__(:"require_oauth_application_from_#{client_assertion_type}_assertion_subject",
  22. param("client_assertion"))
  23. 72 if (client_id = param_or_nil("client_id")) &&
  24. client_id != @oauth_application[oauth_applications_client_id_column]
  25. # If present, the value of the
  26. # "client_id" parameter MUST identify the same client as is
  27. # identified by the client assertion.
  28. 24 redirect_response_error("invalid_grant")
  29. end
  30. else
  31. 36 super
  32. end
  33. end
  34. 12 def account_from_bearer_assertion_subject(subject)
  35. 48 __insert_or_do_nothing_and_return__(
  36. db[accounts_table],
  37. account_id_column,
  38. [login_column],
  39. login_column => subject
  40. )
  41. end
  42. 12 def create_token(grant_type)
  43. 60 return super unless assertion_grant_type?(grant_type) && supported_grant_type?(grant_type)
  44. 48 account = __send__(:"account_from_#{assertion_grant_type}_assertion", param("assertion"))
  45. 48 redirect_response_error("invalid_grant") unless account
  46. 48 grant_scopes = if param_or_nil("scope")
  47. 24 redirect_response_error("invalid_scope") unless check_valid_scopes?
  48. 12 scopes
  49. else
  50. 24 @oauth_application[oauth_applications_scopes_column]
  51. end
  52. 12 grant_params = {
  53. 24 oauth_grants_type_column => grant_type,
  54. oauth_grants_account_id_column => account[account_id_column],
  55. oauth_grants_oauth_application_id_column => @oauth_application[oauth_applications_id_column],
  56. oauth_grants_scopes_column => grant_scopes
  57. }
  58. 36 generate_token(grant_params, false)
  59. end
  60. 12 def assertion_grant_type?(grant_type = param("grant_type"))
  61. 348 grant_type.start_with?("urn:ietf:params:oauth:grant-type:")
  62. end
  63. 12 def client_assertion_type?(client_assertion_type = param("client_assertion_type"))
  64. 144 client_assertion_type.start_with?("urn:ietf:params:oauth:client-assertion-type:")
  65. end
  66. 12 def assertion_grant_type(grant_type = param("grant_type"))
  67. 96 grant_type.delete_prefix("urn:ietf:params:oauth:grant-type:").tr("-", "_")
  68. end
  69. 12 def client_assertion_type(assertion_type = param("client_assertion_type"))
  70. 108 assertion_type.delete_prefix("urn:ietf:params:oauth:client-assertion-type:").tr("-", "_")
  71. end
  72. end
  73. end

lib/rodauth/features/oauth_authorization_code_grant.rb

100.0% lines covered

76 relevant lines. 76 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_authorization_code_grant, :OauthAuthorizationCodeGrant) do
  5. 12 depends :oauth_authorize_base
  6. 12 auth_value_method :oauth_response_mode, "form_post"
  7. 12 def oauth_grant_types_supported
  8. 4224 super | %w[authorization_code]
  9. end
  10. 12 def oauth_response_types_supported
  11. 1932 super | %w[code]
  12. end
  13. 12 def oauth_response_modes_supported
  14. 3264 super | %w[query form_post]
  15. end
  16. 12 private
  17. 12 def validate_authorize_params
  18. 2580 super
  19. 2388 response_mode = param_or_nil("response_mode")
  20. 2388 return unless response_mode
  21. 996 redirect_response_error("invalid_request") unless oauth_response_modes_supported.include?(response_mode)
  22. 996 response_type = param_or_nil("response_type")
  23. 996 return unless response_type.nil? || response_type == "code"
  24. 828 redirect_response_error("invalid_request") unless oauth_response_modes_for_code_supported.include?(response_mode)
  25. end
  26. 12 def oauth_response_modes_for_code_supported
  27. 828 %w[query form_post]
  28. end
  29. 12 def validate_token_params
  30. 1680 redirect_response_error("invalid_request") if param_or_nil("grant_type") == "authorization_code" && !param_or_nil("code")
  31. 1680 super
  32. end
  33. 12 def do_authorize(response_params = {}, response_mode = param_or_nil("response_mode"))
  34. 924 response_mode ||= oauth_response_mode
  35. 924 redirect_response_error("invalid_request") unless response_mode.nil? || supported_response_mode?(response_mode)
  36. 924 response_type = param_or_nil("response_type")
  37. 924 redirect_response_error("invalid_request") unless response_type.nil? || supported_response_type?(response_type)
  38. 924 case response_type
  39. when "code", nil
  40. 600 response_params.replace(_do_authorize_code)
  41. end
  42. 912 response_params["state"] = param("state") if param_or_nil("state")
  43. 912 [response_params, response_mode]
  44. end
  45. 12 def _do_authorize_code
  46. 240 create_params = {
  47. 480 oauth_grants_type_column => "authorization_code",
  48. **resource_owner_params
  49. }
  50. 720 { "code" => create_oauth_grant(create_params) }
  51. end
  52. 12 def authorize_response(params, mode)
  53. 528 redirect_url = URI.parse(redirect_uri)
  54. 528 case mode
  55. when "query"
  56. 504 params = [URI.encode_www_form(params)]
  57. 504 params << redirect_url.query if redirect_url.query
  58. 504 redirect_url.query = params.join("&")
  59. 504 redirect(redirect_url.to_s)
  60. when "form_post"
  61. 24 inline_html = form_post_response_html(redirect_uri) do
  62. 16 params.map do |name, value|
  63. 24 "<input type=\"hidden\" name=\"#{scope.h(name)}\" value=\"#{scope.h(value)}\" />"
  64. 8 end.join
  65. end
  66. 24 scope.view layout: false, inline: inline_html
  67. end
  68. end
  69. 12 def _redirect_response_error(redirect_url, params)
  70. 384 response_mode = param_or_nil("response_mode") || oauth_response_mode
  71. 384 case response_mode
  72. when "form_post"
  73. 12 response["Content-Type"] = "text/html"
  74. 12 error_body = form_post_error_response_html(redirect_url) do
  75. 8 params.map do |name, value|
  76. 24 "<input type=\"hidden\" name=\"#{name}\" value=\"#{scope.h(value)}\" />"
  77. 4 end.join
  78. end
  79. 12 response.write(error_body)
  80. 12 request.halt
  81. else
  82. 372 super
  83. end
  84. end
  85. 12 def form_post_response_html(url)
  86. 36 <<-FORM
  87. <html>
  88. <head><title>Authorized</title></head>
  89. <body onload="javascript:document.forms[0].submit()">
  90. <form method="post" action="#{url}">
  91. #{yield}
  92. <input type="submit" class="btn btn-outline-primary" value="#{scope.h(oauth_authorize_post_button)}"/>
  93. </form>
  94. </body>
  95. </html>
  96. FORM
  97. end
  98. 12 def form_post_error_response_html(url)
  99. 12 <<-FORM
  100. <html>
  101. <head><title></title></head>
  102. <body onload="javascript:document.forms[0].submit()">
  103. <form method="post" action="#{url}">
  104. #{yield}
  105. </form>
  106. </body>
  107. </html>
  108. FORM
  109. end
  110. 12 def create_token(grant_type)
  111. 1476 return super unless supported_grant_type?(grant_type, "authorization_code")
  112. 388 grant_params = {
  113. 776 oauth_grants_code_column => param("code"),
  114. oauth_grants_redirect_uri_column => param("redirect_uri"),
  115. oauth_grants_oauth_application_id_column => oauth_application[oauth_applications_id_column]
  116. }
  117. 1164 create_token_from_authorization_code(grant_params)
  118. end
  119. 12 def check_valid_response_type?
  120. 1584 response_type = param_or_nil("response_type")
  121. 1584 response_type == "code" || response_type == "none" || super
  122. end
  123. 12 def oauth_server_metadata_body(*)
  124. 312 super.tap do |data|
  125. 312 data[:authorization_endpoint] = authorize_url
  126. end
  127. end
  128. end
  129. end

lib/rodauth/features/oauth_authorize_base.rb

97.67% lines covered

129 relevant lines. 126 lines covered and 3 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "ipaddr"
  3. 12 require "rodauth/oauth"
  4. 12 module Rodauth
  5. 12 Feature.define(:oauth_authorize_base, :OauthAuthorizeBase) do
  6. 12 depends :oauth_base
  7. 12 before "authorize"
  8. 12 after "authorize"
  9. 12 view "authorize", "Authorize", "authorize"
  10. 12 view "authorize_error", "Authorize Error", "authorize_error"
  11. 12 button "Authorize", "oauth_authorize"
  12. 12 button "Back to Client Application", "oauth_authorize_post"
  13. 12 auth_value_method :use_oauth_access_type?, false
  14. 12 auth_value_method :oauth_grants_access_type_column, :access_type
  15. 12 translatable_method :authorize_page_lead, "The application %<name>s would like to access your data"
  16. 12 translatable_method :oauth_grants_scopes_label, "Scopes"
  17. 12 translatable_method :oauth_applications_contacts_label, "Contacts"
  18. 12 translatable_method :oauth_applications_tos_uri_label, "Terms of service URL"
  19. 12 translatable_method :oauth_applications_policy_uri_label, "Policy URL"
  20. 12 translatable_method :oauth_unsupported_response_type_message, "Unsupported response type"
  21. 12 translatable_method :oauth_authorize_parameter_required, "Invalid or missing '%<parameter>s'"
  22. 12 auth_methods(
  23. :resource_owner_params,
  24. :oauth_grants_resource_owner_columns
  25. )
  26. # /authorize
  27. 12 auth_server_route(:authorize) do |r|
  28. 2916 require_authorizable_account
  29. 2796 before_authorize_route
  30. 2796 validate_authorize_params
  31. 2196 r.get do
  32. 1224 authorize_view
  33. end
  34. 972 r.post do
  35. 972 params, mode = transaction do
  36. 972 before_authorize
  37. 972 do_authorize
  38. end
  39. 960 authorize_response(params, mode)
  40. end
  41. end
  42. 12 def check_csrf?
  43. 11040 case request.path
  44. when authorize_path
  45. 2916 only_json? ? false : super
  46. else
  47. 8124 super
  48. end
  49. end
  50. 12 def authorize_scopes
  51. 1224 scopes || begin
  52. 180 oauth_application[oauth_applications_scopes_column].split(oauth_scope_separator)
  53. end
  54. end
  55. 12 private
  56. 12 def validate_authorize_params
  57. 2544 redirect_authorize_error("client_id") unless oauth_application
  58. 2496 redirect_uris = oauth_application[oauth_applications_redirect_uri_column].split(" ")
  59. 2496 if (redirect_uri = param_or_nil("redirect_uri"))
  60. 468 normalized_redirect_uri = normalize_redirect_uri_for_comparison(redirect_uri)
  61. 468 unless redirect_uris.include?(normalized_redirect_uri) || redirect_uris.include?(redirect_uri)
  62. 12 redirect_authorize_error("redirect_uri")
  63. end
  64. 2028 elsif redirect_uris.size > 1
  65. 12 redirect_authorize_error("redirect_uri")
  66. end
  67. 2472 redirect_response_error("unsupported_response_type") unless check_valid_response_type?
  68. 2448 redirect_response_error("invalid_request") unless check_valid_access_type? && check_valid_approval_prompt?
  69. 2448 try_approval_prompt if use_oauth_access_type? && request.get?
  70. 2448 redirect_response_error("invalid_scope") if (request.post? || param_or_nil("scope")) && !check_valid_scopes?
  71. 2424 response_mode = param_or_nil("response_mode")
  72. 2424 redirect_response_error("invalid_request") unless response_mode.nil? || oauth_response_modes_supported.include?(response_mode)
  73. end
  74. 12 def check_valid_scopes?(scp = scopes)
  75. 2196 super(scp - %w[offline_access])
  76. end
  77. 12 def check_valid_response_type?
  78. 24 false
  79. end
  80. 12 ACCESS_TYPES = %w[offline online].freeze
  81. 12 def check_valid_access_type?
  82. 2448 return true unless use_oauth_access_type?
  83. 36 access_type = param_or_nil("access_type")
  84. 36 !access_type || ACCESS_TYPES.include?(access_type)
  85. end
  86. 12 APPROVAL_PROMPTS = %w[force auto].freeze
  87. 12 def check_valid_approval_prompt?
  88. 2448 return true unless use_oauth_access_type?
  89. 36 approval_prompt = param_or_nil("approval_prompt")
  90. 36 !approval_prompt || APPROVAL_PROMPTS.include?(approval_prompt)
  91. end
  92. 12 def resource_owner_params
  93. 1404 { oauth_grants_account_id_column => account_id }
  94. end
  95. 12 def oauth_grants_resource_owner_columns
  96. [oauth_grants_account_id_column]
  97. end
  98. 12 def try_approval_prompt
  99. 24 approval_prompt = param_or_nil("approval_prompt")
  100. 24 return unless approval_prompt && approval_prompt == "auto"
  101. 8 return if db[oauth_grants_table].where(resource_owner_params).where(
  102. oauth_grants_oauth_application_id_column => oauth_application[oauth_applications_id_column],
  103. oauth_grants_redirect_uri_column => redirect_uri,
  104. oauth_grants_scopes_column => scopes.join(oauth_scope_separator),
  105. oauth_grants_access_type_column => "online"
  106. 4 ).count.zero?
  107. # if there's a previous oauth grant for the params combo, it means that this user has approved before.
  108. 12 request.env["REQUEST_METHOD"] = "POST"
  109. end
  110. 12 def redirect_authorize_error(parameter, referer = request.referer || default_redirect)
  111. 96 error_message = oauth_authorize_parameter_required(parameter: parameter)
  112. 96 if accepts_json?
  113. status_code = oauth_invalid_response_status
  114. throw_json_response_error(status_code, "invalid_request", error_message)
  115. else
  116. 96 scope.instance_variable_set(:@error, error_message)
  117. 96 scope.instance_variable_set(:@back_url, referer)
  118. 96 return_response(authorize_error_view)
  119. end
  120. end
  121. 12 def authorization_required
  122. 372 if accepts_json?
  123. 360 throw_json_response_error(oauth_authorization_required_error_status, "invalid_client")
  124. else
  125. 12 set_redirect_error_flash(require_authorization_error_flash)
  126. 12 redirect(authorize_path)
  127. end
  128. end
  129. 12 def do_authorize(*args); end
  130. 12 def authorize_response(params, mode); end
  131. 12 def create_token_from_authorization_code(grant_params, should_generate_refresh_token = !use_oauth_access_type?, oauth_grant: nil)
  132. # fetch oauth grant
  133. 1128 oauth_grant ||= valid_locked_oauth_grant(grant_params)
  134. 936 should_generate_refresh_token ||= oauth_grant[oauth_grants_access_type_column] == "offline"
  135. 936 generate_token(oauth_grant, should_generate_refresh_token)
  136. end
  137. 12 def create_oauth_grant(create_params = {})
  138. 768 create_params[oauth_grants_oauth_application_id_column] ||= oauth_application[oauth_applications_id_column]
  139. 768 create_params[oauth_grants_redirect_uri_column] ||= redirect_uri
  140. 768 create_params[oauth_grants_expires_in_column] ||= Sequel.date_add(Sequel::CURRENT_TIMESTAMP, seconds: oauth_grant_expires_in)
  141. 768 create_params[oauth_grants_scopes_column] ||= scopes.join(oauth_scope_separator)
  142. 768 if use_oauth_access_type? && (access_type = param_or_nil("access_type"))
  143. 24 create_params[oauth_grants_access_type_column] = access_type
  144. end
  145. 768 ds = db[oauth_grants_table]
  146. 768 create_params[oauth_grants_code_column] = oauth_unique_id_generator
  147. 768 if oauth_reuse_access_token
  148. 384 unique_conds = Hash[oauth_grants_unique_columns.map { |column| [column, create_params[column]] }]
  149. 96 valid_grant = valid_oauth_grant_ds(unique_conds).select(oauth_grants_id_column).first
  150. 96 if valid_grant
  151. 96 create_params[oauth_grants_id_column] = valid_grant[oauth_grants_id_column]
  152. 96 rescue_from_uniqueness_error do
  153. 96 __insert_or_update_and_return__(
  154. ds,
  155. oauth_grants_id_column,
  156. [oauth_grants_id_column],
  157. create_params
  158. )
  159. end
  160. 96 return create_params[oauth_grants_code_column]
  161. end
  162. end
  163. 672 rescue_from_uniqueness_error do
  164. 708 if __one_oauth_token_per_account
  165. 354 __insert_or_update_and_return__(
  166. ds,
  167. oauth_grants_id_column,
  168. oauth_grants_unique_columns,
  169. create_params,
  170. nil,
  171. {
  172. oauth_grants_expires_in_column => Sequel.date_add(Sequel::CURRENT_TIMESTAMP, seconds: oauth_grant_expires_in),
  173. oauth_grants_revoked_at_column => nil
  174. }
  175. )
  176. else
  177. 354 __insert_and_return__(ds, oauth_grants_id_column, create_params)
  178. end
  179. end
  180. 660 create_params[oauth_grants_code_column]
  181. end
  182. 12 def normalize_redirect_uri_for_comparison(redirect_uri)
  183. 468 uri = URI(redirect_uri)
  184. 468 return redirect_uri unless uri.scheme == "http" && uri.port
  185. 48 hostname = uri.hostname
  186. # https://www.rfc-editor.org/rfc/rfc8252#section-7.3
  187. # ignore (potentially ephemeral) port number for native clients per RFC8252
  188. 16 begin
  189. 48 ip = IPAddr.new(hostname)
  190. 24 uri.port = nil if ip.loopback?
  191. rescue IPAddr::InvalidAddressError
  192. # https://www.rfc-editor.org/rfc/rfc8252#section-8.3
  193. # Although the use of localhost is NOT RECOMMENDED, it is still allowed.
  194. 24 uri.port = nil if hostname == "localhost"
  195. end
  196. 48 uri.to_s
  197. end
  198. end
  199. end

lib/rodauth/features/oauth_base.rb

96.07% lines covered

433 relevant lines. 416 lines covered and 17 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "time"
  3. 12 require "base64"
  4. 12 require "securerandom"
  5. 12 require "cgi"
  6. 12 require "digest/sha2"
  7. 12 require "rodauth/version"
  8. 12 require "rodauth/oauth"
  9. 12 require "rodauth/oauth/database_extensions"
  10. 12 require "rodauth/oauth/http_extensions"
  11. 12 module Rodauth
  12. 12 Feature.define(:oauth_base, :OauthBase) do
  13. 12 include OAuth::HTTPExtensions
  14. 12 EMPTY_HASH = {}.freeze
  15. 12 auth_value_methods(:http_request)
  16. 12 auth_value_methods(:http_request_cache)
  17. 12 before "token"
  18. 12 error_flash "Please authorize to continue", "require_authorization"
  19. 12 error_flash "You are not authorized to revoke this token", "revoke_unauthorized_account"
  20. 12 button "Cancel", "oauth_cancel"
  21. 12 auth_value_method :json_response_content_type, "application/json"
  22. 12 auth_value_method :oauth_grant_expires_in, 60 * 5 # 5 minutes
  23. 12 auth_value_method :oauth_access_token_expires_in, 60 * 60 # 60 minutes
  24. 12 auth_value_method :oauth_refresh_token_expires_in, 60 * 60 * 24 * 360 # 1 year
  25. 12 auth_value_method :oauth_unique_id_generation_retries, 3
  26. 12 auth_value_method :oauth_token_endpoint_auth_methods_supported, %w[client_secret_basic client_secret_post]
  27. 12 auth_value_method :oauth_grant_types_supported, %w[refresh_token]
  28. 12 auth_value_method :oauth_response_types_supported, []
  29. 12 auth_value_method :oauth_response_modes_supported, []
  30. 12 auth_value_method :oauth_valid_uri_schemes, %w[https]
  31. 12 auth_value_method :oauth_scope_separator, " "
  32. # OAuth Grants
  33. 12 auth_value_method :oauth_grants_table, :oauth_grants
  34. 12 auth_value_method :oauth_grants_id_column, :id
  35. 8 %i[
  36. account_id oauth_application_id type
  37. redirect_uri code scopes
  38. expires_in revoked_at
  39. token refresh_token
  40. 4 ].each do |column|
  41. 120 auth_value_method :"oauth_grants_#{column}_column", column
  42. end
  43. # Enables Token Hash
  44. 12 auth_value_method :oauth_grants_token_hash_column, :token
  45. 12 auth_value_method :oauth_grants_refresh_token_hash_column, :refresh_token
  46. # Access Token reuse
  47. 12 auth_value_method :oauth_reuse_access_token, false
  48. 12 auth_value_method :oauth_applications_table, :oauth_applications
  49. 12 auth_value_method :oauth_applications_id_column, :id
  50. 8 %i[
  51. account_id
  52. name description scopes
  53. client_id client_secret
  54. homepage_url redirect_uri
  55. token_endpoint_auth_method grant_types response_types response_modes
  56. logo_uri tos_uri policy_uri jwks jwks_uri
  57. contacts software_id software_version
  58. 4 ].each do |column|
  59. 240 auth_value_method :"oauth_applications_#{column}_column", column
  60. end
  61. # Enables client secret Hash
  62. 12 auth_value_method :oauth_applications_client_secret_hash_column, :client_secret
  63. 12 auth_value_method :oauth_authorization_required_error_status, 401
  64. 12 auth_value_method :oauth_invalid_response_status, 400
  65. 12 auth_value_method :oauth_already_in_use_response_status, 409
  66. # Feature options
  67. 12 auth_value_method :oauth_application_scopes, []
  68. 12 auth_value_method :oauth_token_type, "bearer"
  69. 12 auth_value_method :oauth_refresh_token_protection_policy, "rotation" # can be: none, sender_constrained, rotation
  70. 12 translatable_method :oauth_invalid_client_message, "Invalid client"
  71. 12 translatable_method :oauth_invalid_grant_type_message, "Invalid grant type"
  72. 12 translatable_method :oauth_invalid_grant_message, "Invalid grant"
  73. 12 translatable_method :oauth_invalid_scope_message, "Invalid scope"
  74. 12 translatable_method :oauth_unsupported_token_type_message, "Invalid token type hint"
  75. 12 translatable_method :oauth_already_in_use_message, "error generating unique token"
  76. 12 auth_value_method :oauth_already_in_use_error_code, "invalid_request"
  77. 12 auth_value_method :oauth_invalid_grant_type_error_code, "unsupported_grant_type"
  78. 12 auth_value_method :is_authorization_server?, true
  79. 12 auth_value_methods(:only_json?)
  80. 12 auth_value_method :json_request_regexp, %r{\bapplication/(?:vnd\.api\+)?json\b}i
  81. # METADATA
  82. 12 auth_value_method :oauth_metadata_service_documentation, nil
  83. 12 auth_value_method :oauth_metadata_ui_locales_supported, nil
  84. 12 auth_value_method :oauth_metadata_op_policy_uri, nil
  85. 12 auth_value_method :oauth_metadata_op_tos_uri, nil
  86. 12 auth_value_methods(
  87. :authorization_server_url,
  88. :oauth_grants_unique_columns
  89. )
  90. 12 auth_methods(
  91. :fetch_access_token,
  92. :secret_hash,
  93. :generate_token_hash,
  94. :secret_matches?,
  95. :oauth_unique_id_generator,
  96. :require_authorizable_account,
  97. :oauth_account_ds,
  98. :oauth_application_ds
  99. )
  100. # /token
  101. 12 auth_server_route(:token) do |r|
  102. 2184 require_oauth_application
  103. 1896 before_token_route
  104. 1896 r.post do
  105. 1896 catch_error do
  106. 1896 validate_token_params
  107. 1824 oauth_grant = nil
  108. 1824 transaction do
  109. 1824 before_token
  110. 1824 oauth_grant = create_token(param("grant_type"))
  111. end
  112. 1176 json_response_success(json_access_token_payload(oauth_grant))
  113. end
  114. throw_json_response_error(oauth_invalid_response_status, "invalid_request")
  115. end
  116. end
  117. 12 def load_oauth_server_metadata_route(issuer = nil)
  118. 228 request.on(".well-known") do
  119. 228 request.get("oauth-authorization-server") do
  120. 228 json_response_success(oauth_server_metadata_body(issuer), true)
  121. end
  122. end
  123. end
  124. 12 def check_csrf?
  125. 10452 case request.path
  126. when token_path
  127. 2184 false
  128. else
  129. 8268 super
  130. end
  131. end
  132. 12 def oauth_token_subject
  133. 132 return unless authorization_token
  134. 132 authorization_token[oauth_grants_account_id_column] ||
  135. db[oauth_applications_table].where(
  136. oauth_applications_id_column => authorization_token[oauth_grants_oauth_application_id_column]
  137. ).select_map(oauth_applications_client_id_column).first
  138. end
  139. 12 def current_oauth_account
  140. 132 account_id = authorization_token[oauth_grants_account_id_column]
  141. 132 return unless account_id
  142. 108 oauth_account_ds(account_id).first
  143. end
  144. 12 def current_oauth_application
  145. 156 oauth_application_ds(authorization_token[oauth_grants_oauth_application_id_column]).first
  146. end
  147. 12 def accepts_json?
  148. 1836 return true if only_json?
  149. 1824 (accept = request.env["HTTP_ACCEPT"]) && accept =~ json_request_regexp
  150. end
  151. # copied from the jwt feature
  152. 12 def json_request?
  153. 240 return super if features.include?(:jsonn)
  154. 240 return @json_request if defined?(@json_request)
  155. 240 @json_request = request.content_type =~ json_request_regexp
  156. end
  157. 12 def scopes
  158. 5940 scope = request.params["scope"]
  159. 5940 case scope
  160. when Array
  161. 2340 scope
  162. when String
  163. 3228 scope.split(" ")
  164. end
  165. end
  166. 12 def redirect_uri
  167. 4344 param_or_nil("redirect_uri") || begin
  168. 3504 return unless oauth_application
  169. 3504 redirect_uris = oauth_application[oauth_applications_redirect_uri_column].split(" ")
  170. 3504 redirect_uris.size == 1 ? redirect_uris.first : nil
  171. end
  172. end
  173. 12 def oauth_application
  174. 39810 return @oauth_application if defined?(@oauth_application)
  175. 1064 @oauth_application = begin
  176. 3192 client_id = param_or_nil("client_id")
  177. 3192 return unless client_id
  178. 3108 db[oauth_applications_table].filter(oauth_applications_client_id_column => client_id).first
  179. end
  180. end
  181. 12 def fetch_access_token
  182. 780 if (token = request.params["access_token"])
  183. 24 if request.post? && !(request.content_type.start_with?("application/x-www-form-urlencoded") &&
  184. request.params.size == 1)
  185. return
  186. end
  187. else
  188. 756 value = request.env["HTTP_AUTHORIZATION"]
  189. 756 return unless value && !value.empty?
  190. 672 scheme, token = value.split(" ", 2)
  191. 672 return unless scheme.downcase == oauth_token_type
  192. end
  193. 696 return if token.nil? || token.empty?
  194. 612 token
  195. end
  196. 12 def authorization_token
  197. 1092 return @authorization_token if defined?(@authorization_token)
  198. # check if there is a token
  199. 348 access_token = fetch_access_token
  200. 348 return unless access_token
  201. 216 @authorization_token = oauth_grant_by_token(access_token)
  202. end
  203. 12 def require_oauth_authorization(*scopes)
  204. 324 authorization_required unless authorization_token
  205. 168 token_scopes = authorization_token[oauth_grants_scopes_column].split(oauth_scope_separator)
  206. 360 authorization_required unless scopes.any? { |scope| token_scopes.include?(scope) }
  207. end
  208. 12 def use_date_arithmetic?
  209. 4986 true
  210. end
  211. # override
  212. 12 def translate(key, default, args = EMPTY_HASH)
  213. 27552 return i18n_translate(key, default, **args) if features.include?(:i18n)
  214. # do not attempt to translate by default
  215. 96 return default if args.nil?
  216. 96 default % args
  217. end
  218. 12 def post_configure
  219. 5250 super
  220. 5250 i18n_register(File.expand_path(File.join(__dir__, "..", "..", "..", "locales"))) if features.include?(:i18n)
  221. # all of the extensions below involve DB changes. Resource server mode doesn't use
  222. # database functions for OAuth though.
  223. 5250 return unless is_authorization_server?
  224. 5082 self.class.__send__(:include, Rodauth::OAuth::ExtendDatabase(db))
  225. # Check whether we can reutilize db entries for the same account / application pair
  226. 5082 one_oauth_token_per_account = db.indexes(oauth_grants_table).values.any? do |definition|
  227. 25416 definition[:unique] &&
  228. definition[:columns] == oauth_grants_unique_columns
  229. end
  230. 6786 self.class.send(:define_method, :__one_oauth_token_per_account) { one_oauth_token_per_account }
  231. end
  232. 12 private
  233. 12 def oauth_account_ds(account_id)
  234. 252 account_ds(account_id)
  235. end
  236. 12 def oauth_application_ds(oauth_application_id)
  237. 156 db[oauth_applications_table].where(oauth_applications_id_column => oauth_application_id)
  238. end
  239. 12 def require_authorizable_account
  240. 3168 require_account
  241. end
  242. 12 def rescue_from_uniqueness_error(&block)
  243. 2592 retries = oauth_unique_id_generation_retries
  244. 864 begin
  245. 2664 transaction(savepoint: :only, &block)
  246. 96 rescue Sequel::UniqueConstraintViolation
  247. 96 redirect_response_error("already_in_use") if retries.zero?
  248. 72 retries -= 1
  249. 72 retry
  250. end
  251. end
  252. # OAuth Token Unique/Reuse
  253. 12 def oauth_grants_unique_columns
  254. 8804 [
  255. 17608 oauth_grants_oauth_application_id_column,
  256. oauth_grants_account_id_column,
  257. oauth_grants_scopes_column
  258. ]
  259. end
  260. 12 def authorization_server_url
  261. 1686 base_url
  262. end
  263. 12 def template_path(page)
  264. 59802 path = File.join(File.dirname(__FILE__), "../../../templates", "#{page}.str")
  265. 59802 return super unless File.exist?(path)
  266. 2172 path
  267. end
  268. # to be used internally. Same semantics as require account, must:
  269. # fetch an authorization basic header
  270. # parse client id and secret
  271. #
  272. 12 def require_oauth_application
  273. 2100 @oauth_application = if (token = ((v = request.env["HTTP_AUTHORIZATION"]) && v[/\A *Basic (.*)\Z/, 1]))
  274. # client_secret_basic
  275. 588 require_oauth_application_from_client_secret_basic(token)
  276. 1512 elsif (client_id = param_or_nil("client_id"))
  277. 1428 if (client_secret = param_or_nil("client_secret"))
  278. # client_secret_post
  279. 960 require_oauth_application_from_client_secret_post(client_id, client_secret)
  280. else
  281. # none
  282. 468 require_oauth_application_from_none(client_id)
  283. end
  284. else
  285. 84 authorization_required
  286. end
  287. end
  288. 12 def require_oauth_application_from_client_secret_basic(token)
  289. 588 client_id, client_secret = Base64.decode64(token).split(":", 2)
  290. 588 authorization_required unless client_id
  291. 588 oauth_application = db[oauth_applications_table].where(oauth_applications_client_id_column => client_id).first
  292. 392 authorization_required unless supports_auth_method?(oauth_application,
  293. 196 "client_secret_basic") && secret_matches?(oauth_application, client_secret)
  294. 564 oauth_application
  295. end
  296. 12 def require_oauth_application_from_client_secret_post(client_id, client_secret)
  297. 960 oauth_application = db[oauth_applications_table].where(oauth_applications_client_id_column => client_id).first
  298. 640 authorization_required unless supports_auth_method?(oauth_application,
  299. 320 "client_secret_post") && secret_matches?(oauth_application, client_secret)
  300. 936 oauth_application
  301. end
  302. 12 def require_oauth_application_from_none(client_id)
  303. 468 oauth_application = db[oauth_applications_table].where(oauth_applications_client_id_column => client_id).first
  304. 468 authorization_required unless supports_auth_method?(oauth_application, "none")
  305. 360 oauth_application
  306. end
  307. 12 def supports_auth_method?(oauth_application, auth_method)
  308. 2280 return false unless oauth_application
  309. 2244 supported_auth_methods = if oauth_application[oauth_applications_token_endpoint_auth_method_column]
  310. 660 oauth_application[oauth_applications_token_endpoint_auth_method_column].split(/ +/)
  311. else
  312. 1584 oauth_token_endpoint_auth_methods_supported
  313. end
  314. 2244 supported_auth_methods.include?(auth_method)
  315. end
  316. 12 def require_oauth_application_from_account
  317. 12 ds = db[oauth_applications_table]
  318. .join(oauth_grants_table, Sequel[oauth_grants_table][oauth_grants_oauth_application_id_column] =>
  319. Sequel[oauth_applications_table][oauth_applications_id_column])
  320. .where(oauth_grant_by_token_ds(param("token")).opts.fetch(:where, true))
  321. .where(Sequel[oauth_applications_table][oauth_applications_account_id_column] => account_id)
  322. 12 @oauth_application = ds.qualify.first
  323. 12 return if @oauth_application
  324. set_redirect_error_flash revoke_unauthorized_account_error_flash
  325. redirect request.referer || "/"
  326. end
  327. 12 def secret_matches?(oauth_application, secret)
  328. 1500 if oauth_applications_client_secret_hash_column
  329. 1500 BCrypt::Password.new(oauth_application[oauth_applications_client_secret_hash_column]) == secret
  330. else
  331. oauth_application[oauth_applications_client_secret_column] == secret
  332. end
  333. end
  334. 12 def set_client_secret(params, secret)
  335. 648 if oauth_applications_client_secret_hash_column
  336. 648 params[oauth_applications_client_secret_hash_column] = secret_hash(secret)
  337. else
  338. params[oauth_applications_client_secret_column] = secret
  339. end
  340. end
  341. 12 def secret_hash(secret)
  342. 1548 password_hash(secret)
  343. end
  344. 12 def oauth_unique_id_generator
  345. 4188 SecureRandom.urlsafe_base64(32)
  346. end
  347. 12 def generate_token_hash(token)
  348. 300 Base64.urlsafe_encode64(Digest::SHA256.digest(token))
  349. end
  350. 12 def grant_from_application?(oauth_grant, oauth_application)
  351. 204 oauth_grant[oauth_grants_oauth_application_id_column] == oauth_application[oauth_applications_id_column]
  352. end
  353. 12 def password_hash(password)
  354. 1548 return super if features.include?(:login_password_requirements_base)
  355. BCrypt::Password.create(password, cost: BCrypt::Engine::DEFAULT_COST)
  356. end
  357. 12 def generate_token(grant_params = {}, should_generate_refresh_token = true)
  358. 1092 if grant_params[oauth_grants_id_column] && (oauth_reuse_access_token &&
  359. (
  360. 192 if oauth_grants_token_hash_column
  361. 96 grant_params[oauth_grants_token_hash_column]
  362. else
  363. 96 grant_params[oauth_grants_token_column]
  364. end
  365. ))
  366. 96 return grant_params
  367. end
  368. 332 update_params = {
  369. 664 oauth_grants_expires_in_column => Sequel.date_add(Sequel::CURRENT_TIMESTAMP, seconds: oauth_access_token_expires_in),
  370. oauth_grants_code_column => nil
  371. }
  372. 996 rescue_from_uniqueness_error do
  373. 996 access_token = _generate_access_token(update_params)
  374. 996 refresh_token = _generate_refresh_token(update_params) if should_generate_refresh_token
  375. 996 oauth_grant = store_token(grant_params, update_params)
  376. 996 return unless oauth_grant
  377. 996 oauth_grant[oauth_grants_token_column] = access_token
  378. 996 oauth_grant[oauth_grants_refresh_token_column] = refresh_token if refresh_token
  379. 996 oauth_grant
  380. end
  381. end
  382. 12 def _generate_access_token(params = {})
  383. 636 token = oauth_unique_id_generator
  384. 636 if oauth_grants_token_hash_column
  385. 108 params[oauth_grants_token_hash_column] = generate_token_hash(token)
  386. else
  387. 528 params[oauth_grants_token_column] = token
  388. end
  389. 636 token
  390. end
  391. 12 def _generate_refresh_token(params)
  392. 636 token = oauth_unique_id_generator
  393. 636 if oauth_grants_refresh_token_hash_column
  394. 108 params[oauth_grants_refresh_token_hash_column] = generate_token_hash(token)
  395. else
  396. 528 params[oauth_grants_refresh_token_column] = token
  397. end
  398. 636 token
  399. end
  400. 12 def _grant_with_access_token?(oauth_grant)
  401. if oauth_grants_token_hash_column
  402. oauth_grant[oauth_grants_token_hash_column]
  403. else
  404. oauth_grant[oauth_grants_token_column]
  405. end
  406. end
  407. 12 def store_token(grant_params, update_params = {})
  408. 996 ds = db[oauth_grants_table]
  409. 996 if __one_oauth_token_per_account
  410. 166 to_update_if_null = [
  411. 332 oauth_grants_token_column,
  412. oauth_grants_token_hash_column,
  413. oauth_grants_refresh_token_column,
  414. oauth_grants_refresh_token_hash_column
  415. ].compact.map do |attribute|
  416. 368 [
  417. 736 attribute,
  418. (
  419. 1104 if ds.respond_to?(:supports_insert_conflict?) && ds.supports_insert_conflict?
  420. 552 Sequel.function(:coalesce, Sequel[oauth_grants_table][attribute], Sequel[:excluded][attribute])
  421. else
  422. 552 Sequel.function(:coalesce, Sequel[oauth_grants_table][attribute], update_params[attribute])
  423. end
  424. )
  425. ]
  426. end
  427. 498 token = __insert_or_update_and_return__(
  428. ds,
  429. oauth_grants_id_column,
  430. oauth_grants_unique_columns,
  431. grant_params.merge(update_params),
  432. Sequel.expr(Sequel[oauth_grants_table][oauth_grants_expires_in_column]) > Sequel::CURRENT_TIMESTAMP,
  433. Hash[to_update_if_null]
  434. )
  435. # if the previous operation didn't return a row, it means that the conditions
  436. # invalidated the update, and the existing token is still valid.
  437. 498 token || ds.where(
  438. oauth_grants_account_id_column => update_params[oauth_grants_account_id_column],
  439. oauth_grants_oauth_application_id_column => update_params[oauth_grants_oauth_application_id_column]
  440. ).first
  441. else
  442. 498 if oauth_reuse_access_token
  443. 192 unique_conds = Hash[oauth_grants_unique_columns.map { |column| [column, update_params[column]] }]
  444. 48 valid_token_ds = valid_oauth_grant_ds(unique_conds)
  445. 48 if oauth_grants_token_hash_column
  446. 24 valid_token_ds.exclude(oauth_grants_token_hash_column => nil)
  447. else
  448. 24 valid_token_ds.exclude(oauth_grants_token_column => nil)
  449. end
  450. 48 valid_token = valid_token_ds.first
  451. 48 return valid_token if valid_token
  452. end
  453. 498 if grant_params[oauth_grants_id_column]
  454. 420 __update_and_return__(ds.where(oauth_grants_id_column => grant_params[oauth_grants_id_column]), update_params)
  455. else
  456. 78 __insert_and_return__(ds, oauth_grants_id_column, grant_params.merge(update_params))
  457. end
  458. end
  459. end
  460. 12 def valid_locked_oauth_grant(grant_params = nil)
  461. 1164 oauth_grant = valid_oauth_grant_ds(grant_params).for_update.first
  462. 1164 redirect_response_error("invalid_grant") unless oauth_grant
  463. 972 oauth_grant
  464. end
  465. 12 def valid_oauth_grant_ds(grant_params = nil)
  466. 2088 ds = db[oauth_grants_table]
  467. .where(Sequel[oauth_grants_table][oauth_grants_revoked_at_column] => nil)
  468. .where(Sequel.expr(Sequel[oauth_grants_table][oauth_grants_expires_in_column]) >= Sequel::CURRENT_TIMESTAMP)
  469. 2088 ds = ds.where(grant_params) if grant_params
  470. 2088 ds
  471. end
  472. 12 def oauth_grant_by_token_ds(token)
  473. 444 ds = valid_oauth_grant_ds
  474. 444 if oauth_grants_token_hash_column
  475. 48 ds.where(Sequel[oauth_grants_table][oauth_grants_token_hash_column] => generate_token_hash(token))
  476. else
  477. 396 ds.where(Sequel[oauth_grants_table][oauth_grants_token_column] => token)
  478. end
  479. end
  480. 12 def oauth_grant_by_token(token)
  481. 372 oauth_grant_by_token_ds(token).first
  482. end
  483. 12 def oauth_grant_by_refresh_token_ds(token, revoked: false)
  484. 396 ds = db[oauth_grants_table].where(oauth_grants_oauth_application_id_column => oauth_application[oauth_applications_id_column])
  485. #
  486. # filter expired refresh tokens out.
  487. # an expired refresh token is a token whose access token expired for a period longer than the
  488. # refresh token expiration period.
  489. #
  490. 396 ds = ds.where(Sequel.date_add(oauth_grants_expires_in_column,
  491. 396 seconds: (oauth_refresh_token_expires_in - oauth_access_token_expires_in)) >= Sequel::CURRENT_TIMESTAMP)
  492. 396 ds = if oauth_grants_refresh_token_hash_column
  493. 36 ds.where(oauth_grants_refresh_token_hash_column => generate_token_hash(token))
  494. else
  495. 360 ds.where(oauth_grants_refresh_token_column => token)
  496. end
  497. 396 ds = ds.where(oauth_grants_revoked_at_column => nil) unless revoked
  498. 396 ds
  499. end
  500. 12 def oauth_grant_by_refresh_token(token, **kwargs)
  501. 96 oauth_grant_by_refresh_token_ds(token, **kwargs).first
  502. end
  503. 12 def json_access_token_payload(oauth_grant)
  504. 420 payload = {
  505. 840 "access_token" => oauth_grant[oauth_grants_token_column],
  506. "token_type" => oauth_token_type,
  507. "expires_in" => oauth_access_token_expires_in
  508. }
  509. 1260 payload["refresh_token"] = oauth_grant[oauth_grants_refresh_token_column] if oauth_grant[oauth_grants_refresh_token_column]
  510. 1260 payload
  511. end
  512. # Access Tokens
  513. 12 def validate_token_params
  514. 1836 unless (grant_type = param_or_nil("grant_type"))
  515. 60 redirect_response_error("invalid_request")
  516. end
  517. 1776 redirect_response_error("invalid_request") if grant_type == "refresh_token" && !param_or_nil("refresh_token")
  518. end
  519. 12 def create_token(grant_type)
  520. 432 redirect_response_error("invalid_request") unless supported_grant_type?(grant_type, "refresh_token")
  521. 300 refresh_token = param("refresh_token")
  522. # fetch potentially revoked oauth token
  523. 300 oauth_grant = oauth_grant_by_refresh_token_ds(refresh_token, revoked: true).for_update.first
  524. 300 update_params = { oauth_grants_expires_in_column => Sequel.date_add(Sequel::CURRENT_TIMESTAMP,
  525. seconds: oauth_access_token_expires_in) }
  526. 300 if !oauth_grant || oauth_grant[oauth_grants_revoked_at_column]
  527. 144 redirect_response_error("invalid_grant")
  528. 156 elsif oauth_refresh_token_protection_policy == "rotation"
  529. # https://tools.ietf.org/html/draft-ietf-oauth-v2-1-00#section-6.1
  530. #
  531. # If a refresh token is compromised and subsequently used by both the attacker and the legitimate
  532. # client, one of them will present an invalidated refresh token, which will inform the authorization
  533. # server of the breach. The authorization server cannot determine which party submitted the invalid
  534. # refresh token, but it will revoke the active refresh token. This stops the attack at the cost of
  535. # forcing the legitimate client to obtain a fresh authorization grant.
  536. 72 refresh_token = _generate_refresh_token(update_params)
  537. end
  538. 156 update_params[oauth_grants_oauth_application_id_column] = oauth_grant[oauth_grants_oauth_application_id_column]
  539. 156 oauth_grant = create_token_from_token(oauth_grant, update_params)
  540. 144 oauth_grant[oauth_grants_refresh_token_column] = refresh_token
  541. 144 oauth_grant
  542. end
  543. 12 def create_token_from_token(oauth_grant, update_params)
  544. 156 redirect_response_error("invalid_grant") unless grant_from_application?(oauth_grant, oauth_application)
  545. 156 rescue_from_uniqueness_error do
  546. 192 oauth_grants_ds = db[oauth_grants_table].where(oauth_grants_id_column => oauth_grant[oauth_grants_id_column])
  547. 192 access_token = _generate_access_token(update_params)
  548. 192 oauth_grant = __update_and_return__(oauth_grants_ds, update_params)
  549. 144 oauth_grant[oauth_grants_token_column] = access_token
  550. 144 oauth_grant
  551. end
  552. end
  553. 12 def supported_grant_type?(grant_type, expected_grant_type = grant_type)
  554. 2172 return false unless grant_type == expected_grant_type
  555. 1740 grant_types_supported = if oauth_application[oauth_applications_grant_types_column]
  556. 48 oauth_application[oauth_applications_grant_types_column].split(/ +/)
  557. else
  558. 1692 oauth_grant_types_supported
  559. end
  560. 1740 grant_types_supported.include?(grant_type)
  561. end
  562. 12 def supported_response_type?(response_type, expected_response_type = response_type)
  563. 972 return false unless response_type == expected_response_type
  564. 972 response_types_supported = if oauth_application[oauth_applications_grant_types_column]
  565. 12 oauth_application[oauth_applications_response_types_column].split(/ +/)
  566. else
  567. 960 oauth_response_types_supported
  568. end
  569. 972 response_types = response_type.split(/ +/)
  570. 972 (response_types - response_types_supported).empty?
  571. end
  572. 12 def supported_response_mode?(response_mode, expected_response_mode = response_mode)
  573. 960 return false unless response_mode == expected_response_mode
  574. 960 response_modes_supported = if oauth_application[oauth_applications_response_modes_column]
  575. oauth_application[oauth_applications_response_modes_column].split(/ +/)
  576. else
  577. 960 oauth_response_modes_supported
  578. end
  579. 960 response_modes_supported.include?(response_mode)
  580. end
  581. 12 def oauth_server_metadata_body(path = nil)
  582. 312 issuer = base_url
  583. 312 issuer += "/#{path}" if path
  584. 104 {
  585. 208 issuer: issuer,
  586. token_endpoint: token_url,
  587. scopes_supported: oauth_application_scopes,
  588. response_types_supported: oauth_response_types_supported,
  589. response_modes_supported: oauth_response_modes_supported,
  590. grant_types_supported: oauth_grant_types_supported,
  591. token_endpoint_auth_methods_supported: oauth_token_endpoint_auth_methods_supported,
  592. service_documentation: oauth_metadata_service_documentation,
  593. ui_locales_supported: oauth_metadata_ui_locales_supported,
  594. op_policy_uri: oauth_metadata_op_policy_uri,
  595. op_tos_uri: oauth_metadata_op_tos_uri
  596. }
  597. end
  598. 12 def redirect_response_error(error_code, message = nil)
  599. 1320 if accepts_json?
  600. 780 status_code = if respond_to?(:"oauth_#{error_code}_response_status")
  601. 12 send(:"oauth_#{error_code}_response_status")
  602. else
  603. 768 oauth_invalid_response_status
  604. end
  605. 780 throw_json_response_error(status_code, error_code, message)
  606. else
  607. 540 redirect_url = redirect_uri || request.referer || default_redirect
  608. 540 redirect_url = URI.parse(redirect_url)
  609. 540 params = response_error_params(error_code, message)
  610. 540 state = param_or_nil("state")
  611. 540 params["state"] = state if state
  612. 540 _redirect_response_error(redirect_url, params)
  613. end
  614. end
  615. 12 def _redirect_response_error(redirect_url, params)
  616. 360 params = URI.encode_www_form(params)
  617. 360 if redirect_url.query
  618. params << "&" unless params.empty?
  619. params << redirect_url.query
  620. end
  621. 360 redirect_url.query = params
  622. 360 redirect(redirect_url.to_s)
  623. end
  624. 12 def response_error_params(error_code, message = nil)
  625. 2676 code = if respond_to?(:"oauth_#{error_code}_error_code")
  626. 60 send(:"oauth_#{error_code}_error_code")
  627. else
  628. 2616 error_code
  629. end
  630. 2676 payload = { "error" => code }
  631. 2676 error_description = message
  632. 2676 error_description ||= send(:"oauth_#{error_code}_message") if respond_to?(:"oauth_#{error_code}_message")
  633. 2676 payload["error_description"] = error_description if error_description
  634. 2676 payload
  635. end
  636. 12 def json_response_success(body, cache = false)
  637. 1908 response.status = 200
  638. 1908 response["Content-Type"] ||= json_response_content_type
  639. 1908 if cache
  640. # defaulting to 1-day for everyone, for now at least
  641. 348 max_age = 60 * 60 * 24
  642. 348 response["Cache-Control"] = "private, max-age=#{max_age}"
  643. else
  644. 1560 response["Cache-Control"] = "no-store"
  645. 1560 response["Pragma"] = "no-cache"
  646. end
  647. 1908 json_payload = _json_response_body(body)
  648. 1908 return_response(json_payload)
  649. end
  650. 12 def throw_json_response_error(status, error_code, message = nil)
  651. 2136 set_response_error_status(status)
  652. 2136 payload = response_error_params(error_code, message)
  653. 2136 json_payload = _json_response_body(payload)
  654. 2136 response["Content-Type"] ||= json_response_content_type
  655. 2136 response["WWW-Authenticate"] = oauth_token_type.upcase if status == 401
  656. 2136 return_response(json_payload)
  657. end
  658. 12 def _json_response_body(hash)
  659. 4692 return super if features.include?(:json)
  660. 4692 if request.respond_to?(:convert_to_json)
  661. request.send(:convert_to_json, hash)
  662. else
  663. 4692 JSON.dump(hash)
  664. end
  665. end
  666. 12 if Gem::Version.new(Rodauth.version) < Gem::Version.new("2.23")
  667. def return_response(body = nil)
  668. response.write(body) if body
  669. request.halt
  670. end
  671. end
  672. 12 def authorization_required
  673. 192 throw_json_response_error(oauth_authorization_required_error_status, "invalid_client")
  674. end
  675. 12 def check_valid_scopes?(scp = scopes)
  676. 2232 return false unless scp
  677. 2232 (scp - oauth_application[oauth_applications_scopes_column].split(oauth_scope_separator)).empty?
  678. end
  679. 12 def check_valid_uri?(uri)
  680. 8676 URI::DEFAULT_PARSER.make_regexp(oauth_valid_uri_schemes).match?(uri)
  681. end
  682. 12 def check_valid_no_fragment_uri?(uri)
  683. 2676 check_valid_uri?(uri) && URI.parse(uri).fragment.nil?
  684. end
  685. # Resource server mode
  686. 12 def authorization_server_metadata
  687. 48 auth_url = URI(authorization_server_url).dup
  688. 48 auth_url.path = "/.well-known/oauth-authorization-server"
  689. 48 http_request_with_cache(auth_url)
  690. end
  691. end
  692. end

lib/rodauth/features/oauth_client_credentials_grant.rb

100.0% lines covered

17 relevant lines. 17 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_client_credentials_grant, :OauthClientCredentialsGrant) do
  5. 12 depends :oauth_base
  6. 12 def oauth_grant_types_supported
  7. 96 super | %w[client_credentials]
  8. end
  9. 12 private
  10. 12 def create_token(grant_type)
  11. 72 return super unless supported_grant_type?(grant_type, "client_credentials")
  12. 60 grant_scopes = scopes
  13. 60 grant_scopes = if grant_scopes
  14. 12 redirect_response_error("invalid_scope") unless check_valid_scopes?
  15. 12 grant_scopes.join(oauth_scope_separator)
  16. else
  17. 48 oauth_application[oauth_applications_scopes_column]
  18. end
  19. 20 grant_params = {
  20. 40 oauth_grants_type_column => "client_credentials",
  21. oauth_grants_oauth_application_id_column => oauth_application[oauth_applications_id_column],
  22. oauth_grants_scopes_column => grant_scopes
  23. }
  24. 60 generate_token(grant_params, false)
  25. end
  26. end
  27. end

lib/rodauth/features/oauth_device_code_grant.rb

95.33% lines covered

107 relevant lines. 102 lines covered and 5 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_device_code_grant, :OauthDeviceCodeGrant) do
  5. 12 depends :oauth_authorize_base
  6. 12 before "device_authorization"
  7. 12 before "device_verification"
  8. 12 notice_flash "The device is verified", "device_verification"
  9. 12 error_flash "No device to authorize with the given user code", "user_code_not_found"
  10. 12 view "device_verification", "Device Verification", "device_verification"
  11. 12 view "device_search", "Device Search", "device_search"
  12. 12 button "Verify", "oauth_device_verification"
  13. 12 button "Search", "oauth_device_search"
  14. 12 auth_value_method :oauth_grants_user_code_column, :user_code
  15. 12 auth_value_method :oauth_grants_last_polled_at_column, :last_polled_at
  16. 12 translatable_method :oauth_device_search_page_lead, "Insert the user code from the device you'd like to authorize."
  17. 12 translatable_method :oauth_device_verification_page_lead, "The device with user code %<user_code>s would like to access your data."
  18. 12 translatable_method :oauth_expired_token_message, "the device code has expired"
  19. 12 translatable_method :oauth_access_denied_message, "the authorization request has been denied"
  20. 12 translatable_method :oauth_authorization_pending_message, "the authorization request is still pending"
  21. 12 translatable_method :oauth_slow_down_message, "authorization request is still pending but poll interval should be increased"
  22. 12 auth_value_method :oauth_device_code_grant_polling_interval, 5 # seconds
  23. 12 auth_value_method :oauth_device_code_grant_user_code_size, 8 # characters
  24. 12 %w[user_code].each do |param|
  25. 12 auth_value_method :"oauth_grant_#{param}_param", param
  26. end
  27. 12 translatable_method :oauth_grant_user_code_label, "User code"
  28. 12 auth_methods(
  29. :generate_user_code
  30. )
  31. # /device-authorization
  32. 12 auth_server_route(:device_authorization) do |r|
  33. 24 require_oauth_application
  34. 24 before_device_authorization_route
  35. 24 r.post do
  36. 24 user_code = generate_user_code
  37. 24 device_code = transaction do
  38. 24 before_device_authorization
  39. 24 create_oauth_grant(
  40. oauth_grants_type_column => "device_code",
  41. oauth_grants_user_code_column => user_code
  42. )
  43. end
  44. 24 json_response_success \
  45. "device_code" => device_code,
  46. "user_code" => user_code,
  47. "verification_uri" => device_url,
  48. "verification_uri_complete" => device_url(user_code: user_code),
  49. "expires_in" => oauth_grant_expires_in,
  50. "interval" => oauth_device_code_grant_polling_interval
  51. end
  52. end
  53. # /device
  54. 12 auth_server_route(:device) do |r|
  55. 252 require_authorizable_account
  56. 240 before_device_route
  57. 240 r.get do
  58. 204 if (user_code = param_or_nil("user_code"))
  59. 72 oauth_grant = valid_oauth_grant_ds(oauth_grants_user_code_column => user_code).first
  60. 72 unless oauth_grant
  61. 36 set_redirect_error_flash user_code_not_found_error_flash
  62. 36 redirect device_path
  63. end
  64. 36 scope.instance_variable_set(:@oauth_grant, oauth_grant)
  65. 36 device_verification_view
  66. else
  67. 132 device_search_view
  68. end
  69. end
  70. 36 r.post do
  71. 36 catch_error do
  72. 36 unless (user_code = param_or_nil("user_code")) && !user_code.empty?
  73. 12 set_redirect_error_flash oauth_invalid_grant_message
  74. 12 redirect device_path
  75. end
  76. 24 transaction do
  77. 24 before_device_verification
  78. 24 create_token("device_code")
  79. end
  80. end
  81. 24 set_notice_flash device_verification_notice_flash
  82. 24 redirect device_path
  83. end
  84. end
  85. 12 def check_csrf?
  86. 564 case request.path
  87. when device_authorization_path
  88. 24 false
  89. else
  90. 540 super
  91. end
  92. end
  93. 12 def oauth_grant_types_supported
  94. 132 super | %w[urn:ietf:params:oauth:grant-type:device_code]
  95. end
  96. 12 private
  97. 12 def generate_user_code
  98. 24 user_code_size = oauth_device_code_grant_user_code_size
  99. 16 SecureRandom.random_number(36**user_code_size)
  100. .to_s(36) # 0 to 9, a to z
  101. .upcase
  102. 8 .rjust(user_code_size, "0")
  103. end
  104. # TODO: think about removing this and recommend PKCE
  105. 12 def supports_auth_method?(oauth_application, auth_method)
  106. 156 return super unless auth_method == "none"
  107. 132 request.path == device_authorization_path || request.params.key?("device_code") || super
  108. end
  109. 12 def create_token(grant_type)
  110. 144 if supported_grant_type?(grant_type, "urn:ietf:params:oauth:grant-type:device_code")
  111. 120 oauth_grant = db[oauth_grants_table].where(
  112. oauth_grants_type_column => "device_code",
  113. oauth_grants_code_column => param("device_code"),
  114. oauth_grants_oauth_application_id_column => oauth_application[oauth_applications_id_column]
  115. ).for_update.first
  116. 120 throw_json_response_error(oauth_invalid_response_status, "invalid_grant") unless oauth_grant
  117. 108 now = Time.now
  118. 108 if oauth_grant[oauth_grants_user_code_column].nil?
  119. 16 return create_token_from_authorization_code(
  120. { oauth_grants_id_column => oauth_grant[oauth_grants_id_column] },
  121. oauth_grant: oauth_grant
  122. 8 )
  123. end
  124. 84 if oauth_grant[oauth_grants_revoked_at_column]
  125. 24 throw_json_response_error(oauth_invalid_response_status, "access_denied")
  126. 60 elsif oauth_grant[oauth_grants_expires_in_column] < now
  127. 12 throw_json_response_error(oauth_invalid_response_status, "expired_token")
  128. else
  129. 48 last_polled_at = oauth_grant[oauth_grants_last_polled_at_column]
  130. 48 if last_polled_at && convert_timestamp(last_polled_at) + oauth_device_code_grant_polling_interval > now
  131. 12 throw_json_response_error(oauth_invalid_response_status, "slow_down")
  132. else
  133. 36 db[oauth_grants_table].where(oauth_grants_id_column => oauth_grant[oauth_grants_id_column])
  134. 12 .update(oauth_grants_last_polled_at_column => Sequel::CURRENT_TIMESTAMP)
  135. 36 throw_json_response_error(oauth_invalid_response_status, "authorization_pending")
  136. end
  137. end
  138. 24 elsif grant_type == "device_code"
  139. # fetch oauth grant
  140. 24 rs = valid_oauth_grant_ds(
  141. oauth_grants_user_code_column => param("user_code")
  142. ).update(oauth_grants_user_code_column => nil, oauth_grants_type_column => "device_code")
  143. 24 rs if rs.positive?
  144. else
  145. super
  146. end
  147. end
  148. 12 def validate_token_params
  149. 132 grant_type = param_or_nil("grant_type")
  150. 132 if grant_type == "urn:ietf:params:oauth:grant-type:device_code" && !param_or_nil("device_code")
  151. 12 redirect_response_error("invalid_request")
  152. end
  153. 120 super
  154. end
  155. 12 def store_token(grant_params, update_params = {})
  156. 24 return super unless grant_params[oauth_grants_user_code_column]
  157. # do not clean up device code just yet
  158. update_params.delete(oauth_grants_code_column)
  159. update_params[oauth_grants_user_code_column] = nil
  160. update_params.merge!(resource_params)
  161. super(grant_params, update_params)
  162. end
  163. 12 def oauth_server_metadata_body(*)
  164. 12 super.tap do |data|
  165. 12 data[:device_authorization_endpoint] = device_authorization_url
  166. end
  167. end
  168. end
  169. end

lib/rodauth/features/oauth_dynamic_client_registration.rb

95.56% lines covered

225 relevant lines. 215 lines covered and 10 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_dynamic_client_registration, :OauthDynamicClientRegistration) do
  5. 12 depends :oauth_base
  6. 12 before "register"
  7. 12 auth_value_method :oauth_client_registration_required_params, %w[redirect_uris client_name]
  8. 12 auth_value_method :oauth_applications_registration_access_token_column, :registration_access_token
  9. 12 auth_value_method :registration_client_uri_route, "register"
  10. 12 PROTECTED_APPLICATION_ATTRIBUTES = %w[account_id client_id].freeze
  11. 12 def load_registration_client_uri_routes
  12. 48 request.on(registration_client_uri_route) do
  13. # CLIENT REGISTRATION URI
  14. 48 request.on(String) do |client_id|
  15. 48 (token = ((v = request.env["HTTP_AUTHORIZATION"]) && v[/\A *Bearer (.*)\Z/, 1]))
  16. 48 next unless token
  17. 48 oauth_application = db[oauth_applications_table]
  18. .where(oauth_applications_client_id_column => client_id)
  19. .first
  20. 48 next unless oauth_application
  21. 48 authorization_required unless password_hash_match?(oauth_application[oauth_applications_registration_access_token_column], token)
  22. 48 request.is do
  23. 48 request.get do
  24. 12 json_response_oauth_application(oauth_application)
  25. end
  26. 36 request.on method: :put do
  27. 16 %w[client_id registration_access_token registration_client_uri client_secret_expires_at
  28. 8 client_id_issued_at].each do |prohibited_param|
  29. 72 if request.params.key?(prohibited_param)
  30. 12 register_throw_json_response_error("invalid_client_metadata", register_invalid_param_message(prohibited_param))
  31. end
  32. end
  33. 12 validate_client_registration_params
  34. # if the client includes the "client_secret" field in the request, the value of this field MUST match the currently
  35. # issued client secret for that client. The client MUST NOT be allowed to overwrite its existing client secret with
  36. # its own chosen value.
  37. 12 authorization_required if request.params.key?("client_secret") && secret_matches?(oauth_application,
  38. request.params["client_secret"])
  39. 12 oauth_application = transaction do
  40. 12 applications_ds = db[oauth_applications_table]
  41. 12 __update_and_return__(applications_ds, @oauth_application_params)
  42. end
  43. 12 json_response_oauth_application(oauth_application)
  44. end
  45. 12 request.on method: :delete do
  46. 12 applications_ds = db[oauth_applications_table]
  47. 12 applications_ds.where(oauth_applications_client_id_column => client_id).delete
  48. 12 response.status = 204
  49. 12 response["Cache-Control"] = "no-store"
  50. 12 response["Pragma"] = "no-cache"
  51. 12 response.finish
  52. end
  53. end
  54. end
  55. end
  56. end
  57. # /register
  58. 12 auth_server_route(:register) do |r|
  59. 1308 before_register_route
  60. 1308 r.post do
  61. 1308 oauth_client_registration_required_params.each do |required_param|
  62. 2568 unless request.params.key?(required_param)
  63. 48 register_throw_json_response_error("invalid_client_metadata", register_required_param_message(required_param))
  64. end
  65. end
  66. 1260 validate_client_registration_params
  67. 624 response_params = transaction do
  68. 624 before_register
  69. 624 do_register
  70. end
  71. 624 response.status = 201
  72. 624 response["Content-Type"] = json_response_content_type
  73. 624 response["Cache-Control"] = "no-store"
  74. 624 response["Pragma"] = "no-cache"
  75. 624 response.write(_json_response_body(response_params))
  76. end
  77. end
  78. 12 def check_csrf?
  79. 1356 case request.path
  80. when register_path
  81. 1308 false
  82. else
  83. 48 super
  84. end
  85. end
  86. 12 private
  87. 12 def _before_register
  88. raise %{dynamic client registration requires authentication.
  89. Override ´before_register` to perform it.
  90. example:
  91. before_register do
  92. account = _account_from_login(request.env["HTTP_X_USER_EMAIL"])
  93. authorization_required unless account
  94. @oauth_application_params[:account_id] = account[:id]
  95. end
  96. }
  97. end
  98. 12 def validate_client_registration_params(request_params = request.params)
  99. 1296 @oauth_application_params = request_params.each_with_object({}) do |(key, value), params|
  100. 15804 case key
  101. when "redirect_uris"
  102. 1260 if value.is_a?(Array)
  103. 1248 value = value.each do |uri|
  104. 2376 unless check_valid_no_fragment_uri?(uri)
  105. 24 register_throw_json_response_error("invalid_redirect_uri",
  106. register_invalid_uri_message(uri))
  107. end
  108. end.join(" ")
  109. else
  110. 12 register_throw_json_response_error("invalid_redirect_uri", register_invalid_uri_message(value))
  111. end
  112. 1224 key = oauth_applications_redirect_uri_column
  113. when "token_endpoint_auth_method"
  114. 612 unless oauth_token_endpoint_auth_methods_supported.include?(value)
  115. 12 register_throw_json_response_error("invalid_client_metadata", register_invalid_client_metadata_message(key, value))
  116. end
  117. # verify if in range
  118. 600 key = oauth_applications_token_endpoint_auth_method_column
  119. when "grant_types"
  120. 672 if value.is_a?(Array)
  121. 660 value = value.each do |grant_type|
  122. 1212 unless oauth_grant_types_supported.include?(grant_type)
  123. 24 register_throw_json_response_error("invalid_client_metadata", register_invalid_client_metadata_message(grant_type, value))
  124. end
  125. end.join(" ")
  126. else
  127. 12 register_throw_json_response_error("invalid_client_metadata", register_invalid_client_metadata_message(key, value))
  128. end
  129. 636 key = oauth_applications_grant_types_column
  130. when "response_types"
  131. 696 if value.is_a?(Array)
  132. 684 grant_types = request_params["grant_types"] || %w[authorization_code]
  133. 684 value = value.each do |response_type|
  134. 696 unless oauth_response_types_supported.include?(response_type)
  135. 12 register_throw_json_response_error("invalid_client_metadata",
  136. register_invalid_response_type_message(response_type))
  137. end
  138. 684 validate_client_registration_response_type(response_type, grant_types)
  139. end.join(" ")
  140. else
  141. 12 register_throw_json_response_error("invalid_client_metadata", register_invalid_client_metadata_message(key, value))
  142. end
  143. 624 key = oauth_applications_response_types_column
  144. # verify if in range and match grant type
  145. when "client_uri", "logo_uri", "tos_uri", "policy_uri", "jwks_uri"
  146. 5808 register_throw_json_response_error("invalid_client_metadata", register_invalid_uri_message(value)) unless check_valid_uri?(value)
  147. 5748 case key
  148. when "client_uri"
  149. 1200 key = oauth_applications_homepage_url_column
  150. when "jwks_uri"
  151. 1092 if request_params.key?("jwks")
  152. 12 register_throw_json_response_error("invalid_client_metadata",
  153. register_invalid_jwks_param_message(key, "jwks"))
  154. end
  155. end
  156. 5736 key = __send__(:"oauth_applications_#{key}_column")
  157. when "jwks"
  158. 24 register_throw_json_response_error("invalid_client_metadata", register_invalid_param_message(value)) unless value.is_a?(Hash)
  159. 12 if request_params.key?("jwks_uri")
  160. register_throw_json_response_error("invalid_client_metadata",
  161. register_invalid_jwks_param_message(key, "jwks_uri"))
  162. end
  163. 12 key = oauth_applications_jwks_column
  164. 12 value = JSON.dump(value)
  165. when "scope"
  166. 1212 register_throw_json_response_error("invalid_client_metadata", register_invalid_param_message(value)) unless value.is_a?(String)
  167. 1212 scopes = value.split(" ") - oauth_application_scopes
  168. 1212 register_throw_json_response_error("invalid_client_metadata", register_invalid_scopes_message(value)) unless scopes.empty?
  169. 1188 key = oauth_applications_scopes_column
  170. # verify if in range
  171. when "contacts"
  172. 1164 register_throw_json_response_error("invalid_client_metadata", register_invalid_contacts_message(value)) unless value.is_a?(Array)
  173. 1152 value = value.join(" ")
  174. 1152 key = oauth_applications_contacts_column
  175. when "client_name"
  176. 1212 register_throw_json_response_error("invalid_client_metadata", register_invalid_param_message(value)) unless value.is_a?(String)
  177. 1212 key = oauth_applications_name_column
  178. when "require_signed_request_object"
  179. 36 unless respond_to?(:oauth_applications_require_signed_request_object_column)
  180. register_throw_json_response_error("invalid_client_metadata",
  181. register_invalid_param_message(key))
  182. end
  183. 36 request_params[key] = value = convert_to_boolean(key, value)
  184. 24 key = oauth_applications_require_signed_request_object_column
  185. when "require_pushed_authorization_requests"
  186. 36 unless respond_to?(:oauth_applications_require_pushed_authorization_requests_column)
  187. register_throw_json_response_error("invalid_client_metadata",
  188. register_invalid_param_message(key))
  189. end
  190. 36 request_params[key] = value = convert_to_boolean(key, value)
  191. 24 key = oauth_applications_require_pushed_authorization_requests_column
  192. when "tls_client_certificate_bound_access_tokens"
  193. 12 property = :oauth_applications_tls_client_certificate_bound_access_tokens_column
  194. 12 register_throw_json_response_error("invalid_client_metadata", register_invalid_param_message(key)) unless respond_to?(property)
  195. 12 request_params[key] = value = convert_to_boolean(key, value)
  196. 12 key = oauth_applications_tls_client_certificate_bound_access_tokens_column
  197. when /\Atls_client_auth_/
  198. 84 unless respond_to?(:"oauth_applications_#{key}_column")
  199. register_throw_json_response_error("invalid_client_metadata",
  200. register_invalid_param_message(key))
  201. end
  202. # client using the tls_client_auth authentication method MUST use exactly one of the below metadata
  203. # parameters to indicate the certificate subject value that the authorization server is to expect when
  204. # authenticating the respective client.
  205. 1020 if params.any? { |k, _| k.to_s.start_with?("tls_client_auth_") }
  206. 12 register_throw_json_response_error("invalid_client_metadata", register_invalid_param_message(key))
  207. end
  208. 72 key = __send__(:"oauth_applications_#{key}_column")
  209. else
  210. 2976 if respond_to?(:"oauth_applications_#{key}_column")
  211. 2916 if PROTECTED_APPLICATION_ATTRIBUTES.include?(key)
  212. 12 register_throw_json_response_error("invalid_client_metadata", register_invalid_param_message(key))
  213. end
  214. 2904 property = :"oauth_applications_#{key}_column"
  215. 2904 key = __send__(property)
  216. 60 elsif !db[oauth_applications_table].columns.include?(key.to_sym)
  217. 36 register_throw_json_response_error("invalid_client_metadata", register_invalid_param_message(key))
  218. end
  219. end
  220. 15444 params[key] = value
  221. end
  222. end
  223. 12 def validate_client_registration_response_type(response_type, grant_types)
  224. 636 case response_type
  225. when "code"
  226. 564 unless grant_types.include?("authorization_code")
  227. register_throw_json_response_error("invalid_client_metadata",
  228. register_invalid_response_type_for_grant_type_message(response_type,
  229. "authorization_code"))
  230. end
  231. when "token"
  232. 60 unless grant_types.include?("implicit")
  233. 24 register_throw_json_response_error("invalid_client_metadata",
  234. register_invalid_response_type_for_grant_type_message(response_type, "implicit"))
  235. end
  236. when "none"
  237. 12 if grant_types.include?("implicit") || grant_types.include?("authorization_code")
  238. 12 register_throw_json_response_error("invalid_client_metadata", register_invalid_response_type_message(response_type))
  239. end
  240. end
  241. end
  242. 12 def do_register(return_params = request.params.dup)
  243. 624 applications_ds = db[oauth_applications_table]
  244. 624 application_columns = applications_ds.columns
  245. # set defaults
  246. 624 create_params = @oauth_application_params
  247. # If omitted, an authorization server MAY register a client with a default set of scopes
  248. 624 create_params[oauth_applications_scopes_column] ||= return_params["scopes"] = oauth_application_scopes.join(" ")
  249. # https://datatracker.ietf.org/doc/html/rfc7591#section-2
  250. 624 if create_params[oauth_applications_grant_types_column] ||= begin
  251. # If omitted, the default behavior is that the client will use only the "authorization_code" Grant Type.
  252. 324 return_params["grant_types"] = %w[authorization_code] # rubocop:disable Lint/AssignmentInCondition
  253. 324 "authorization_code"
  254. end
  255. 624 create_params[oauth_applications_token_endpoint_auth_method_column] ||= begin
  256. # If unspecified or omitted, the default is "client_secret_basic", denoting the HTTP Basic
  257. # authentication scheme as specified in Section 2.3.1 of OAuth 2.0.
  258. 336 return_params["token_endpoint_auth_method"] = "client_secret_basic"
  259. 336 "client_secret_basic"
  260. end
  261. end
  262. 624 create_params[oauth_applications_response_types_column] ||= begin
  263. # If omitted, the default is that the client will use only the "code" response type.
  264. 324 return_params["response_types"] = %w[code]
  265. 324 "code"
  266. end
  267. 624 rescue_from_uniqueness_error do
  268. 624 initialize_register_params(create_params, return_params)
  269. 12204 create_params.delete_if { |k, _| !application_columns.include?(k) }
  270. 624 applications_ds.insert(create_params)
  271. end
  272. 624 return_params
  273. end
  274. 12 def initialize_register_params(create_params, return_params)
  275. 624 client_id = oauth_unique_id_generator
  276. 624 create_params[oauth_applications_client_id_column] = client_id
  277. 624 return_params["client_id"] = client_id
  278. 624 return_params["client_id_issued_at"] = Time.now.utc.iso8601
  279. 624 registration_access_token = oauth_unique_id_generator
  280. 624 create_params[oauth_applications_registration_access_token_column] = secret_hash(registration_access_token)
  281. 624 return_params["registration_access_token"] = registration_access_token
  282. 624 return_params["registration_client_uri"] = "#{base_url}/#{registration_client_uri_route}/#{return_params['client_id']}"
  283. 624 if create_params.key?(oauth_applications_client_secret_column)
  284. 12 set_client_secret(create_params, create_params[oauth_applications_client_secret_column])
  285. 12 return_params.delete("client_secret")
  286. else
  287. 612 client_secret = oauth_unique_id_generator
  288. 612 set_client_secret(create_params, client_secret)
  289. 612 return_params["client_secret"] = client_secret
  290. 612 return_params["client_secret_expires_at"] = 0
  291. end
  292. end
  293. 12 def register_throw_json_response_error(code, message)
  294. 696 throw_json_response_error(oauth_invalid_response_status, code, message)
  295. end
  296. 12 def register_required_param_message(key)
  297. 60 "The param '#{key}' is required by this server."
  298. end
  299. 12 def register_invalid_param_message(key)
  300. 120 "The param '#{key}' is not supported by this server."
  301. end
  302. 12 def register_invalid_client_metadata_message(key, value)
  303. 192 "The value '#{value}' is not supported by this server for param '#{key}'."
  304. end
  305. 12 def register_invalid_contacts_message(contacts)
  306. 12 "The contacts '#{contacts}' are not allowed by this server."
  307. end
  308. 12 def register_invalid_uri_message(uri)
  309. 216 "The '#{uri}' URL is not allowed by this server."
  310. end
  311. 12 def register_invalid_jwks_param_message(key1, key2)
  312. 12 "The param '#{key1}' cannot be accepted together with param '#{key2}'."
  313. end
  314. 12 def register_invalid_scopes_message(scopes)
  315. 24 "The given scopes (#{scopes}) are not allowed by this server."
  316. end
  317. 12 def register_oauth_invalid_grant_type_message(grant_type)
  318. "The grant type #{grant_type} is not allowed by this server."
  319. end
  320. 12 def register_invalid_response_type_message(response_type)
  321. 24 "The response type #{response_type} is not allowed by this server."
  322. end
  323. 12 def register_invalid_response_type_for_grant_type_message(response_type, grant_type)
  324. 36 "The grant type '#{grant_type}' must be registered for the response " \
  325. "type '#{response_type}' to be allowed."
  326. end
  327. 12 def convert_to_boolean(key, value)
  328. 108 case value
  329. when true, false then value
  330. 60 when "true" then true
  331. 24 when "false" then false
  332. else
  333. 24 register_throw_json_response_error(
  334. "invalid_client_metadata",
  335. register_invalid_param_message(key)
  336. )
  337. end
  338. end
  339. 12 def json_response_oauth_application(oauth_application)
  340. 10304 params = methods.map { |k| k.to_s[/\Aoauth_applications_(\w+)_column\z/, 1] }.compact
  341. 24 body = params.each_with_object({}) do |k, hash|
  342. 552 next if %w[id account_id client_id client_secret cliennt_secret_hash].include?(k)
  343. 456 value = oauth_application[__send__(:"oauth_applications_#{k}_column")]
  344. 456 next unless value
  345. 168 case k
  346. when "redirect_uri"
  347. 24 hash["redirect_uris"] = value.split(" ")
  348. when "token_endpoint_auth_method", "grant_types", "response_types", "request_uris", "post_logout_redirect_uris"
  349. hash[k] = value.split(" ")
  350. when "scopes"
  351. 24 hash["scope"] = value
  352. when "jwks"
  353. hash[k] = value.is_a?(String) ? JSON.parse(value) : value
  354. when "homepage_url"
  355. 24 hash["client_uri"] = value
  356. when "name"
  357. 24 hash["client_name"] = value
  358. else
  359. 72 hash[k] = value
  360. end
  361. end
  362. 24 response.status = 200
  363. 24 response["Content-Type"] ||= json_response_content_type
  364. 24 response["Cache-Control"] = "no-store"
  365. 24 response["Pragma"] = "no-cache"
  366. 24 json_payload = _json_response_body(body)
  367. 24 return_response(json_payload)
  368. end
  369. 12 def oauth_server_metadata_body(*)
  370. 24 super.tap do |data|
  371. 24 data[:registration_endpoint] = register_url
  372. end
  373. end
  374. end
  375. end

lib/rodauth/features/oauth_grant_management.rb

100.0% lines covered

33 relevant lines. 33 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_grant_management, :OauthTokenManagement) do
  5. 12 depends :oauth_management_base, :oauth_token_revocation
  6. 12 view "oauth_grants", "My Oauth Grants", "oauth_grants"
  7. 12 button "Revoke", "oauth_grant_revoke"
  8. 12 auth_value_method :oauth_grants_path, "oauth-grants"
  9. 12 %w[type token refresh_token expires_in revoked_at].each do |param|
  10. 60 translatable_method :"oauth_grants_#{param}_label", param.gsub("_", " ").capitalize
  11. end
  12. 12 translatable_method :oauth_no_grants_text, "No oauth grants yet!"
  13. 12 auth_value_method :oauth_grants_route, "oauth-grants"
  14. 12 auth_value_method :oauth_grants_id_pattern, Integer
  15. 12 auth_value_method :oauth_grants_per_page, 20
  16. 12 auth_methods(
  17. :oauth_grant_path
  18. )
  19. 12 def oauth_grants_path(opts = {})
  20. 660 route_path(oauth_grants_route, opts)
  21. end
  22. 12 def oauth_grant_path(id)
  23. 252 "#{oauth_grants_path}/#{id}"
  24. end
  25. 12 def load_oauth_grant_management_routes
  26. 96 request.on(oauth_grants_route) do
  27. 96 check_csrf if check_csrf?
  28. 96 require_account
  29. 96 request.post(oauth_grants_id_pattern) do |id|
  30. 8 db[oauth_grants_table]
  31. .where(oauth_grants_id_column => id)
  32. .where(oauth_grants_account_id_column => account_id)
  33. 4 .update(oauth_grants_revoked_at_column => Sequel::CURRENT_TIMESTAMP)
  34. 12 set_notice_flash revoke_oauth_grant_notice_flash
  35. 12 redirect oauth_grants_path || "/"
  36. end
  37. 84 request.is do
  38. 84 request.get do
  39. 84 page = Integer(param_or_nil("page") || 1)
  40. 84 per_page = per_page_param(oauth_grants_per_page)
  41. 84 scope.instance_variable_set(:@oauth_grants, db[oauth_grants_table]
  42. .select(Sequel[oauth_grants_table].*, Sequel[oauth_applications_table][oauth_applications_name_column])
  43. .join(oauth_applications_table, Sequel[oauth_grants_table][oauth_grants_oauth_application_id_column] =>
  44. Sequel[oauth_applications_table][oauth_applications_id_column])
  45. .where(Sequel[oauth_grants_table][oauth_grants_account_id_column] => account_id)
  46. .where(oauth_grants_revoked_at_column => nil)
  47. .order(Sequel.desc(oauth_grants_id_column))
  48. .paginate(page, per_page))
  49. 84 oauth_grants_view
  50. end
  51. end
  52. end
  53. end
  54. end
  55. end

lib/rodauth/features/oauth_implicit_grant.rb

100.0% lines covered

49 relevant lines. 49 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_implicit_grant, :OauthImplicitGrant) do
  5. 12 depends :oauth_authorize_base
  6. 12 def oauth_grant_types_supported
  7. 2760 super | %w[implicit]
  8. end
  9. 12 def oauth_response_types_supported
  10. 1404 super | %w[token]
  11. end
  12. 12 def oauth_response_modes_supported
  13. 1560 super | %w[fragment]
  14. end
  15. 12 private
  16. 12 def validate_authorize_params
  17. 1620 super
  18. 1524 response_mode = param_or_nil("response_mode")
  19. 1524 return unless response_mode
  20. 396 response_type = param_or_nil("response_type")
  21. 396 return unless response_type == "token"
  22. 72 redirect_response_error("invalid_request") unless oauth_response_modes_for_token_supported.include?(response_mode)
  23. end
  24. 12 def oauth_response_modes_for_token_supported
  25. 72 %w[fragment]
  26. end
  27. 12 def do_authorize(response_params = {}, response_mode = param_or_nil("response_mode"))
  28. 636 response_type = param("response_type")
  29. 636 return super unless response_type == "token" && supported_response_type?(response_type)
  30. 48 response_mode ||= "fragment"
  31. 48 redirect_response_error("invalid_request") unless supported_response_mode?(response_mode)
  32. 48 oauth_grant = _do_authorize_token
  33. 48 response_params.replace(json_access_token_payload(oauth_grant))
  34. 48 response_params["state"] = param("state") if param_or_nil("state")
  35. 48 [response_params, response_mode]
  36. end
  37. 12 def _do_authorize_token(grant_params = {})
  38. 20 grant_params = {
  39. 40 oauth_grants_type_column => "implicit",
  40. oauth_grants_oauth_application_id_column => oauth_application[oauth_applications_id_column],
  41. oauth_grants_scopes_column => scopes,
  42. **resource_owner_params
  43. }.merge(grant_params)
  44. 60 generate_token(grant_params, false)
  45. end
  46. 12 def _redirect_response_error(redirect_url, params)
  47. 240 response_types = param("response_type").split(/ +/)
  48. 240 return super if response_types.empty? || response_types == %w[code]
  49. 336 params = params.map { |k, v| "#{k}=#{v}" }
  50. 132 redirect_url.fragment = params.join("&")
  51. 132 redirect(redirect_url.to_s)
  52. end
  53. 12 def authorize_response(params, mode)
  54. 540 return super unless mode == "fragment"
  55. 336 redirect_url = URI.parse(redirect_uri)
  56. 336 params = [URI.encode_www_form(params)]
  57. 336 params << redirect_url.query if redirect_url.query
  58. 336 redirect_url.fragment = params.join("&")
  59. 336 redirect(redirect_url.to_s)
  60. end
  61. 12 def check_valid_response_type?
  62. 792 return true if param_or_nil("response_type") == "token"
  63. 660 super
  64. end
  65. end
  66. end

lib/rodauth/features/oauth_jwt.rb

100.0% lines covered

57 relevant lines. 57 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 require "rodauth/oauth/http_extensions"
  4. 12 module Rodauth
  5. 12 Feature.define(:oauth_jwt, :OauthJwt) do
  6. 12 depends :oauth_jwt_base, :oauth_jwt_jwks
  7. 12 auth_value_method :oauth_jwt_access_tokens, true
  8. 12 auth_methods(:jwt_claims)
  9. 12 def require_oauth_authorization(*scopes)
  10. 228 return super unless oauth_jwt_access_tokens
  11. 228 authorization_required unless authorization_token
  12. 216 token_scopes = authorization_token["scope"].split(" ")
  13. 432 authorization_required unless scopes.any? { |scope| token_scopes.include?(scope) }
  14. end
  15. 12 def oauth_token_subject
  16. 324 return super unless oauth_jwt_access_tokens
  17. 324 return unless authorization_token
  18. 324 authorization_token["sub"]
  19. end
  20. 12 def current_oauth_account
  21. 156 subject = oauth_token_subject
  22. 156 return if subject == authorization_token["client_id"]
  23. 144 oauth_account_ds(subject).first
  24. end
  25. 12 def current_oauth_application
  26. 128 db[oauth_applications_table].where(
  27. oauth_applications_client_id_column => authorization_token["client_id"]
  28. 64 ).first
  29. end
  30. 12 private
  31. 12 def authorization_token
  32. 1572 return super unless oauth_jwt_access_tokens
  33. 1572 return @authorization_token if defined?(@authorization_token)
  34. 116 @authorization_token = begin
  35. 348 access_token = fetch_access_token
  36. 348 return unless access_token
  37. 336 jwt_claims = jwt_decode(access_token)
  38. 336 return unless jwt_claims
  39. 336 return unless jwt_claims["sub"]
  40. 336 return unless jwt_claims["aud"]
  41. 336 jwt_claims
  42. end
  43. end
  44. # /token
  45. 12 def create_token_from_token(_grant, update_params)
  46. 96 oauth_grant = super
  47. 96 if oauth_jwt_access_tokens
  48. 96 access_token = _generate_jwt_access_token(oauth_grant)
  49. 96 oauth_grant[oauth_grants_token_column] = access_token
  50. end
  51. 96 oauth_grant
  52. end
  53. 12 def generate_token(_grant_params = {}, should_generate_refresh_token = true)
  54. 468 oauth_grant = super
  55. 468 if oauth_jwt_access_tokens
  56. 456 access_token = _generate_jwt_access_token(oauth_grant)
  57. 456 oauth_grant[oauth_grants_token_column] = access_token
  58. end
  59. 468 oauth_grant
  60. end
  61. 12 def _generate_jwt_access_token(oauth_grant)
  62. 576 claims = jwt_claims(oauth_grant)
  63. # one of the points of using jwt is avoiding database lookups, so we put here all relevant
  64. # token data.
  65. 576 claims[:scope] = oauth_grant[oauth_grants_scopes_column]
  66. 576 jwt_encode(claims)
  67. end
  68. 12 def _generate_access_token(*)
  69. 564 super unless oauth_jwt_access_tokens
  70. end
  71. 12 def jwt_claims(oauth_grant)
  72. 1140 issued_at = Time.now.to_i
  73. 380 {
  74. 760 iss: oauth_jwt_issuer, # issuer
  75. iat: issued_at, # issued at
  76. #
  77. # sub REQUIRED - as defined in section 4.1.2 of [RFC7519]. In case of
  78. # access tokens obtained through grants where a resource owner is
  79. # involved, such as the authorization code grant, the value of "sub"
  80. # SHOULD correspond to the subject identifier of the resource owner.
  81. # In case of access tokens obtained through grants where no resource
  82. # owner is involved, such as the client credentials grant, the value
  83. # of "sub" SHOULD correspond to an identifier the authorization
  84. # server uses to indicate the client application.
  85. sub: jwt_subject(oauth_grant[oauth_grants_account_id_column]),
  86. client_id: oauth_application[oauth_applications_client_id_column],
  87. exp: issued_at + oauth_access_token_expires_in,
  88. aud: oauth_jwt_audience
  89. }
  90. end
  91. end
  92. end

lib/rodauth/features/oauth_jwt_base.rb

93.12% lines covered

218 relevant lines. 203 lines covered and 15 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 require "rodauth/oauth/http_extensions"
  4. 12 module Rodauth
  5. 12 Feature.define(:oauth_jwt_base, :OauthJwtBase) do
  6. 12 depends :oauth_base
  7. 12 auth_value_method :oauth_application_jwt_public_key_param, "jwt_public_key"
  8. 12 auth_value_method :oauth_application_jwks_param, "jwks"
  9. 12 auth_value_method :oauth_jwt_keys, {}
  10. 12 auth_value_method :oauth_jwt_public_keys, {}
  11. 12 auth_value_method :oauth_jwt_jwe_keys, {}
  12. 12 auth_value_method :oauth_jwt_jwe_public_keys, {}
  13. 12 auth_value_method :oauth_jwt_jwe_copyright, nil
  14. 12 auth_methods(
  15. :jwt_encode,
  16. :jwt_decode,
  17. :jwt_decode_no_key,
  18. :generate_jti,
  19. :oauth_jwt_issuer,
  20. :oauth_jwt_audience,
  21. :resource_owner_params_from_jwt_claims
  22. )
  23. 12 private
  24. 12 def oauth_jwt_issuer
  25. # The JWT MUST contain an "iss" (issuer) claim that contains a
  26. # unique identifier for the entity that issued the JWT.
  27. 2463 @oauth_jwt_issuer ||= authorization_server_url
  28. end
  29. 12 def oauth_jwt_audience
  30. # The JWT MUST contain an "aud" (audience) claim containing a
  31. # value that identifies the authorization server as an intended
  32. # audience. The token endpoint URL of the authorization server
  33. # MAY be used as a value for an "aud" element to identify the
  34. # authorization server as an intended audience of the JWT.
  35. 1140 @oauth_jwt_audience ||= if is_authorization_server?
  36. 852 oauth_application[oauth_applications_client_id_column]
  37. else
  38. metadata = authorization_server_metadata
  39. return unless metadata
  40. metadata[:token_endpoint]
  41. end
  42. end
  43. 12 def grant_from_application?(grant_or_claims, oauth_application)
  44. 108 return super if grant_or_claims[oauth_grants_id_column]
  45. if grant_or_claims["client_id"]
  46. grant_or_claims["client_id"] == oauth_application[oauth_applications_client_id_column]
  47. else
  48. Array(grant_or_claims["aud"]).include?(oauth_application[oauth_applications_client_id_column])
  49. end
  50. end
  51. 12 def jwt_subject(account_unique_id, client_application = oauth_application)
  52. 1176 (account_unique_id || client_application[oauth_applications_client_id_column]).to_s
  53. end
  54. 12 def resource_owner_params_from_jwt_claims(claims)
  55. 108 { oauth_grants_account_id_column => claims["sub"] }
  56. end
  57. 12 def oauth_server_metadata_body(path = nil)
  58. 180 metadata = super
  59. 180 metadata.merge! \
  60. token_endpoint_auth_signing_alg_values_supported: oauth_jwt_keys.keys.uniq
  61. 180 metadata
  62. end
  63. 12 def _jwt_key
  64. 267 @_jwt_key ||= (oauth_application_jwks(oauth_application) if oauth_application)
  65. end
  66. # Resource Server only!
  67. #
  68. # returns the jwks set from the authorization server.
  69. 12 def auth_server_jwks_set
  70. 48 metadata = authorization_server_metadata
  71. 48 return unless metadata && (jwks_uri = metadata[:jwks_uri])
  72. 48 jwks_uri = URI(jwks_uri)
  73. 48 http_request_with_cache(jwks_uri)
  74. end
  75. 12 def generate_jti(payload)
  76. # Use the key and iat to create a unique key per request to prevent replay attacks
  77. 545 jti_raw = [
  78. 1090 payload[:aud] || payload["aud"],
  79. payload[:iat] || payload["iat"]
  80. ].join(":").to_s
  81. 1635 Digest::SHA256.hexdigest(jti_raw)
  82. end
  83. 12 def verify_jti(jti, claims)
  84. 279 generate_jti(claims) == jti
  85. end
  86. 12 def verify_aud(expected_aud, aud)
  87. 618 expected_aud == aud
  88. end
  89. 12 def oauth_application_jwks(oauth_application)
  90. 1359 jwks = oauth_application[oauth_applications_jwks_column]
  91. 1359 if jwks
  92. 627 jwks = JSON.parse(jwks, symbolize_names: true) if jwks.is_a?(String)
  93. 627 return jwks
  94. end
  95. 732 jwks_uri = oauth_application[oauth_applications_jwks_uri_column]
  96. 732 return unless jwks_uri
  97. 24 jwks_uri = URI(jwks_uri)
  98. 24 http_request_with_cache(jwks_uri)
  99. end
  100. 12 if defined?(JSON::JWT)
  101. # json-jwt
  102. 3 auth_value_method :oauth_jwt_jws_algorithms_supported, %w[
  103. HS256 HS384 HS512
  104. RS256 RS384 RS512
  105. PS256 PS384 PS512
  106. ES256 ES384 ES512 ES256K
  107. ]
  108. 3 auth_value_method :oauth_jwt_jwe_algorithms_supported, %w[
  109. RSA1_5 RSA-OAEP dir A128KW A256KW
  110. ]
  111. 3 auth_value_method :oauth_jwt_jwe_encryption_methods_supported, %w[
  112. A128GCM A256GCM A128CBC-HS256 A256CBC-HS512
  113. ]
  114. 3 def key_to_jwk(key)
  115. 15 JSON::JWK.new(key)
  116. end
  117. 3 def jwk_export(key)
  118. 12 key_to_jwk(key)
  119. end
  120. 3 def jwk_import(jwk)
  121. 6 JSON::JWK.new(jwk)
  122. end
  123. 3 def jwk_key(jwk)
  124. jwk = jwk_import(jwk) unless jwk.is_a?(JSON::JWK)
  125. jwk.to_key
  126. end
  127. 3 def jwk_thumbprint(jwk)
  128. 6 jwk = jwk_import(jwk) if jwk.is_a?(Hash)
  129. 6 jwk.thumbprint
  130. end
  131. 3 def jwt_encode(payload,
  132. jwks: nil,
  133. headers: {},
  134. encryption_algorithm: oauth_jwt_jwe_keys.keys.dig(0, 0),
  135. encryption_method: oauth_jwt_jwe_keys.keys.dig(0, 1),
  136. jwe_key: oauth_jwt_jwe_keys[[encryption_algorithm,
  137. encryption_method]],
  138. signing_algorithm: oauth_jwt_keys.keys.first)
  139. 339 payload[:jti] = generate_jti(payload)
  140. 339 jwt = JSON::JWT.new(payload)
  141. 339 key = oauth_jwt_keys[signing_algorithm] || _jwt_key
  142. 339 key = key.first if key.is_a?(Array)
  143. 339 jwk = JSON::JWK.new(key || "")
  144. # update headers
  145. 339 headers.each_key do |k|
  146. 18 if jwt.respond_to?(:"#{k}=")
  147. 18 jwt.send(:"#{k}=", headers[k])
  148. 18 headers.delete(k)
  149. end
  150. end
  151. 339 jwt.header.merge(headers) unless headers.empty?
  152. 339 jwt = jwt.sign(jwk, signing_algorithm)
  153. 339 return jwt.to_s unless encryption_algorithm && encryption_method
  154. 57 if jwks && (jwk = jwks.find { |k| k[:use] == "enc" && k[:alg] == encryption_algorithm && k[:enc] == encryption_method })
  155. 18 jwk = JSON::JWK.new(jwk)
  156. 18 jwe = jwt.encrypt(jwk, encryption_algorithm.to_sym, encryption_method.to_sym)
  157. 18 jwe.to_s
  158. 3 elsif jwe_key
  159. 3 jwe_key = jwe_key.first if jwe_key.is_a?(Array)
  160. 3 algorithm = encryption_algorithm.to_sym
  161. 3 meth = encryption_method.to_sym
  162. 3 jwt.encrypt(jwe_key, algorithm, meth)
  163. else
  164. jwt.to_s
  165. end
  166. end
  167. 3 def jwt_decode(
  168. token,
  169. jwks: nil,
  170. jws_algorithm: oauth_jwt_public_keys.keys.first || oauth_jwt_keys.keys.first,
  171. jws_key: oauth_jwt_keys[jws_algorithm] || _jwt_key,
  172. jws_encryption_algorithm: oauth_jwt_jwe_keys.keys.dig(0, 0),
  173. jws_encryption_method: oauth_jwt_jwe_keys.keys.dig(0, 1),
  174. jwe_key: oauth_jwt_jwe_keys[[jws_encryption_algorithm, jws_encryption_method]] || oauth_jwt_jwe_keys.values.first,
  175. verify_claims: true,
  176. verify_jti: true,
  177. verify_iss: true,
  178. verify_aud: true,
  179. **
  180. )
  181. 210 jws_key = jws_key.first if jws_key.is_a?(Array)
  182. 210 if jwe_key
  183. 9 jwe_key = jwe_key.first if jwe_key.is_a?(Array)
  184. 9 token = JSON::JWT.decode(token, jwe_key).plain_text
  185. end
  186. 210 claims = if is_authorization_server?
  187. 198 if jwks
  188. 72 jwks = jwks[:keys] if jwks.is_a?(Hash)
  189. 72 enc_algs = [jws_encryption_algorithm].compact
  190. 72 enc_meths = [jws_encryption_method].compact
  191. 150 sig_algs = jws_algorithm ? [jws_algorithm] : jwks.select { |k| k[:use] == "sig" }.map { |k| k[:alg] }
  192. 72 sig_algs = sig_algs.compact.map(&:to_sym)
  193. # JWKs may be set up without a KID, when there's a single one
  194. 72 if jwks.size == 1 && !jwks[0][:kid]
  195. 3 key = jwks[0]
  196. 3 jwk_key = JSON::JWK.new(key)
  197. 3 jws = JSON::JWT.decode(token, jwk_key)
  198. else
  199. 69 jws = JSON::JWT.decode(token, JSON::JWK::Set.new({ keys: jwks }), enc_algs + sig_algs, enc_meths)
  200. 63 jws = JSON::JWT.decode(jws.plain_text, JSON::JWK::Set.new({ keys: jwks }), sig_algs) if jws.is_a?(JSON::JWE)
  201. end
  202. 66 jws
  203. 126 elsif jws_key
  204. 123 JSON::JWT.decode(token, jws_key)
  205. else
  206. 3 JSON::JWT.decode(token, nil, jws_algorithm)
  207. end
  208. 12 elsif (jwks = auth_server_jwks_set)
  209. 12 JSON::JWT.decode(token, JSON::JWK::Set.new(jwks))
  210. end
  211. 204 now = Time.now
  212. 204 if verify_claims && (
  213. (!claims[:exp] || Time.at(claims[:exp]) < now) &&
  214. (claims[:nbf] && Time.at(claims[:nbf]) < now) &&
  215. (claims[:iat] && Time.at(claims[:iat]) < now) &&
  216. (verify_iss && claims[:iss] != oauth_jwt_issuer) &&
  217. (verify_aud && !verify_aud(claims[:aud], claims[:client_id])) &&
  218. (verify_jti && !verify_jti(claims[:jti], claims))
  219. )
  220. return
  221. end
  222. 204 claims
  223. rescue JSON::JWT::Exception
  224. 6 nil
  225. end
  226. 3 def jwt_decode_no_key(token)
  227. 24 jws = JSON::JWT.decode(token, :skip_verification)
  228. 24 [jws.to_h, jws.header]
  229. end
  230. 9 elsif defined?(JWT)
  231. # ruby-jwt
  232. 9 require "rodauth/oauth/jwe_extensions" if defined?(JWE)
  233. 9 auth_value_method :oauth_jwt_jws_algorithms_supported, %w[
  234. HS256 HS384 HS512 HS512256
  235. RS256 RS384 RS512
  236. ED25519
  237. ES256 ES384 ES512
  238. PS256 PS384 PS512
  239. ]
  240. 9 if defined?(JWE)
  241. 9 auth_value_methods(
  242. :oauth_jwt_jwe_algorithms_supported,
  243. :oauth_jwt_jwe_encryption_methods_supported
  244. )
  245. 9 def oauth_jwt_jwe_algorithms_supported
  246. 270 JWE::VALID_ALG
  247. end
  248. 9 def oauth_jwt_jwe_encryption_methods_supported
  249. 261 JWE::VALID_ENC
  250. end
  251. else
  252. auth_value_method :oauth_jwt_jwe_algorithms_supported, []
  253. auth_value_method :oauth_jwt_jwe_encryption_methods_supported, []
  254. end
  255. 9 def key_to_jwk(key)
  256. 45 JWT::JWK.new(key)
  257. end
  258. 9 def jwk_export(key)
  259. 36 key_to_jwk(key).export
  260. end
  261. 9 def jwk_import(jwk)
  262. 9 JWT::JWK.import(jwk)
  263. end
  264. 9 def jwk_key(jwk)
  265. jwk = jwk_import(jwk) unless jwk.is_a?(JWT::JWK)
  266. jwk.keypair
  267. end
  268. 9 def jwk_thumbprint(jwk)
  269. 18 jwk = jwk_import(jwk) if jwk.is_a?(Hash)
  270. 18 JWT::JWK::Thumbprint.new(jwk).generate
  271. end
  272. 9 def jwt_encode(payload,
  273. signing_algorithm: oauth_jwt_keys.keys.first,
  274. headers: {}, **)
  275. 1017 key = oauth_jwt_keys[signing_algorithm] || _jwt_key
  276. 1017 key = key.first if key.is_a?(Array)
  277. 1017 case key
  278. when OpenSSL::PKey::PKey
  279. 801 jwk = JWT::JWK.new(key)
  280. 801 headers[:kid] = jwk.kid
  281. 801 key = jwk.keypair
  282. end
  283. # @see JWT reserved claims - https://tools.ietf.org/html/draft-jones-json-web-token-07#page-7
  284. 1017 payload[:jti] = generate_jti(payload)
  285. 1017 JWT.encode(payload, key, signing_algorithm, headers)
  286. end
  287. 9 if defined?(JWE)
  288. 9 def jwt_encode_with_jwe(
  289. payload,
  290. jwks: nil,
  291. encryption_algorithm: oauth_jwt_jwe_keys.keys.dig(0, 0),
  292. encryption_method: oauth_jwt_jwe_keys.keys.dig(0, 1),
  293. jwe_key: oauth_jwt_jwe_keys[[encryption_algorithm, encryption_method]],
  294. **args
  295. )
  296. 1017 token = jwt_encode_without_jwe(payload, **args)
  297. 1017 return token unless encryption_algorithm && encryption_method
  298. 153 if jwks && jwks.any? { |k| k[:use] == "enc" }
  299. 54 JWE.__rodauth_oauth_encrypt_from_jwks(token, jwks, alg: encryption_algorithm, enc: encryption_method)
  300. 9 elsif jwe_key
  301. 9 jwe_key = jwe_key.first if jwe_key.is_a?(Array)
  302. 3 params = {
  303. 6 zip: "DEF",
  304. copyright: oauth_jwt_jwe_copyright
  305. }
  306. 9 params[:enc] = encryption_method if encryption_method
  307. 9 params[:alg] = encryption_algorithm if encryption_algorithm
  308. 9 JWE.encrypt(token, jwe_key, **params)
  309. else
  310. token
  311. end
  312. end
  313. 9 alias_method :jwt_encode_without_jwe, :jwt_encode
  314. 9 alias_method :jwt_encode, :jwt_encode_with_jwe
  315. end
  316. 9 def jwt_decode(
  317. token,
  318. jwks: nil,
  319. jws_algorithm: oauth_jwt_public_keys.keys.first || oauth_jwt_keys.keys.first,
  320. jws_key: oauth_jwt_keys[jws_algorithm] || _jwt_key,
  321. verify_claims: true,
  322. verify_jti: true,
  323. verify_iss: true,
  324. verify_aud: true
  325. )
  326. 621 jws_key = jws_key.first if jws_key.is_a?(Array)
  327. # verifying the JWT implies verifying:
  328. #
  329. # issuer: check that server generated the token
  330. # aud: check the audience field (client is who he says he is)
  331. # iat: check that the token didn't expire
  332. #
  333. # subject can't be verified automatically without having access to the account id,
  334. # which we don't because that's the whole point.
  335. #
  336. 621 verify_claims_params = if verify_claims
  337. 189 {
  338. 378 verify_iss: verify_iss,
  339. iss: oauth_jwt_issuer,
  340. # can't use stock aud verification, as it's dependent on the client application id
  341. verify_aud: false,
  342. 567 verify_jti: (verify_jti ? method(:verify_jti) : false),
  343. verify_iat: true
  344. }
  345. else
  346. 54 {}
  347. end
  348. # decode jwt
  349. 621 claims = if is_authorization_server?
  350. 585 if jwks
  351. 207 jwks = jwks[:keys] if jwks.is_a?(Hash)
  352. # JWKs may be set up without a KID, when there's a single one
  353. 207 if jwks.size == 1 && !jwks[0][:kid]
  354. 9 key = jwks[0]
  355. 9 algo = key[:alg]
  356. 9 key = JWT::JWK.import(key).keypair
  357. 9 JWT.decode(token, key, true, algorithms: [algo], **verify_claims_params).first
  358. else
  359. 432 algorithms = jws_algorithm ? [jws_algorithm] : jwks.select { |k| k[:use] == "sig" }.map { |k| k[:alg] }
  360. 198 JWT.decode(token, nil, true, algorithms: algorithms, jwks: { keys: jwks }, **verify_claims_params).first
  361. end
  362. 378 elsif jws_key
  363. 369 JWT.decode(token, jws_key, true, algorithms: [jws_algorithm], **verify_claims_params).first
  364. else
  365. 9 JWT.decode(token, jws_key, false, **verify_claims_params).first
  366. end
  367. 36 elsif (jwks = auth_server_jwks_set)
  368. 108 algorithms = jwks[:keys].select { |k| k[:use] == "sig" }.map { |k| k[:alg] }
  369. 36 JWT.decode(token, nil, true, jwks: jwks, algorithms: algorithms, **verify_claims_params).first
  370. end
  371. 612 return if verify_claims && verify_aud && !verify_aud(claims["aud"], claims["client_id"])
  372. 612 claims
  373. rescue JWT::DecodeError, JWT::JWKError
  374. 9 nil
  375. end
  376. 9 if defined?(JWE)
  377. 9 def jwt_decode_with_jwe(
  378. token,
  379. jwks: nil,
  380. jws_encryption_algorithm: oauth_jwt_jwe_keys.keys.dig(0, 0),
  381. jws_encryption_method: oauth_jwt_jwe_keys.keys.dig(0, 1),
  382. jwe_key: oauth_jwt_jwe_keys[[jws_encryption_algorithm, jws_encryption_method]] || oauth_jwt_jwe_keys.values.first,
  383. **args
  384. )
  385. 891 token = if jwks && jwks.any? { |k| k[:use] == "enc" }
  386. 27 JWE.__rodauth_oauth_decrypt_from_jwks(token, jwks, alg: jws_encryption_algorithm, enc: jws_encryption_method)
  387. 603 elsif jwe_key
  388. 27 jwe_key = jwe_key.first if jwe_key.is_a?(Array)
  389. 27 JWE.decrypt(token, jwe_key)
  390. else
  391. 576 token
  392. end
  393. 612 jwt_decode_without_jwe(token, jwks: jwks, **args)
  394. rescue JWE::DecodeError => e
  395. 18 jwt_decode_without_jwe(token, jwks: jwks, **args) if e.message.include?("Not enough or too many segments")
  396. end
  397. 9 alias_method :jwt_decode_without_jwe, :jwt_decode
  398. 9 alias_method :jwt_decode, :jwt_decode_with_jwe
  399. end
  400. 9 def jwt_decode_no_key(token)
  401. 72 JWT.decode(token, nil, false)
  402. end
  403. else
  404. skipped # :nocov:
  405. skipped def jwk_export(_key)
  406. skipped raise "#{__method__} is undefined, redefine it or require either \"jwt\" or \"json-jwt\""
  407. skipped end
  408. skipped
  409. skipped def jwk_import(_jwk)
  410. skipped raise "#{__method__} is undefined, redefine it or require either \"jwt\" or \"json-jwt\""
  411. skipped end
  412. skipped
  413. skipped def jwk_thumbprint(_jwk)
  414. skipped raise "#{__method__} is undefined, redefine it or require either \"jwt\" or \"json-jwt\""
  415. skipped end
  416. skipped
  417. skipped def jwt_encode(_token)
  418. skipped raise "#{__method__} is undefined, redefine it or require either \"jwt\" or \"json-jwt\""
  419. skipped end
  420. skipped
  421. skipped def jwt_decode(_token, **)
  422. skipped raise "#{__method__} is undefined, redefine it or require either \"jwt\" or \"json-jwt\""
  423. skipped end
  424. skipped # :nocov:
  425. end
  426. end
  427. end

lib/rodauth/features/oauth_jwt_bearer_grant.rb

100.0% lines covered

47 relevant lines. 47 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_jwt_bearer_grant, :OauthJwtBearerGrant) do
  5. 12 depends :oauth_assertion_base, :oauth_jwt
  6. 12 auth_value_method :max_param_bytesize, nil if Rodauth::VERSION >= "2.26.0"
  7. 12 auth_methods(
  8. :require_oauth_application_from_jwt_bearer_assertion_issuer,
  9. :require_oauth_application_from_jwt_bearer_assertion_subject,
  10. :account_from_jwt_bearer_assertion
  11. )
  12. 12 def oauth_token_endpoint_auth_methods_supported
  13. 36 if oauth_applications_client_secret_hash_column.nil?
  14. 12 super | %w[client_secret_jwt private_key_jwt urn:ietf:params:oauth:client-assertion-type:jwt-bearer]
  15. else
  16. 24 super | %w[private_key_jwt]
  17. end
  18. end
  19. 12 def oauth_grant_types_supported
  20. 96 super | %w[urn:ietf:params:oauth:grant-type:jwt-bearer]
  21. end
  22. 12 private
  23. 12 def require_oauth_application_from_jwt_bearer_assertion_issuer(assertion)
  24. 36 claims = jwt_assertion(assertion)
  25. 36 return unless claims
  26. 24 db[oauth_applications_table].where(
  27. oauth_applications_client_id_column => claims["iss"]
  28. 12 ).first
  29. end
  30. 12 def require_oauth_application_from_jwt_bearer_assertion_subject(assertion)
  31. 96 claims, header = jwt_decode_no_key(assertion)
  32. 96 client_id = claims["sub"]
  33. 96 case header["alg"]
  34. when "none"
  35. # do not accept jwts with no alg set
  36. 12 authorization_required
  37. when /\AHS/
  38. 36 require_oauth_application_from_client_secret_jwt(client_id, assertion, header["alg"])
  39. else
  40. 48 require_oauth_application_from_private_key_jwt(client_id, assertion)
  41. end
  42. end
  43. 12 def require_oauth_application_from_client_secret_jwt(client_id, assertion, alg)
  44. 36 oauth_application = db[oauth_applications_table].where(oauth_applications_client_id_column => client_id).first
  45. 36 authorization_required unless oauth_application && supports_auth_method?(oauth_application, "client_secret_jwt")
  46. 24 client_secret = oauth_application[oauth_applications_client_secret_column]
  47. 24 claims = jwt_assertion(assertion, jws_key: client_secret, jws_algorithm: alg)
  48. 24 authorization_required unless claims && claims["iss"] == client_id
  49. 24 oauth_application
  50. end
  51. 12 def require_oauth_application_from_private_key_jwt(client_id, assertion)
  52. 48 oauth_application = db[oauth_applications_table].where(oauth_applications_client_id_column => client_id).first
  53. 48 authorization_required unless oauth_application && supports_auth_method?(oauth_application, "private_key_jwt")
  54. 36 jwks = oauth_application_jwks(oauth_application)
  55. 36 claims = jwt_assertion(assertion, jwks: jwks)
  56. 36 authorization_required unless claims
  57. 36 oauth_application
  58. end
  59. 12 def account_from_jwt_bearer_assertion(assertion)
  60. 36 claims = jwt_assertion(assertion)
  61. 36 return unless claims
  62. 36 account_from_bearer_assertion_subject(claims["sub"])
  63. end
  64. 12 def jwt_assertion(assertion, **kwargs)
  65. 132 claims = jwt_decode(assertion, verify_iss: false, verify_aud: false, verify_jti: false, **kwargs)
  66. 132 return unless claims && verify_aud(request.url, claims["aud"])
  67. 132 claims
  68. end
  69. end
  70. end

lib/rodauth/features/oauth_jwt_jwks.rb

100.0% lines covered

23 relevant lines. 23 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 require "rodauth/oauth/http_extensions"
  4. 12 module Rodauth
  5. 12 Feature.define(:oauth_jwt_jwks, :OauthJwtJwks) do
  6. 12 depends :oauth_jwt_base
  7. 12 auth_methods(:jwks_set)
  8. 12 auth_server_route(:jwks) do |r|
  9. 36 before_jwks_route
  10. 36 r.get do
  11. 36 json_response_success({ keys: jwks_set }, true)
  12. end
  13. end
  14. 12 private
  15. 12 def oauth_server_metadata_body(path = nil)
  16. 180 metadata = super
  17. 180 metadata.merge!(jwks_uri: jwks_url)
  18. 180 metadata
  19. end
  20. 12 def jwks_set
  21. 36 @jwks_set ||= [
  22. *(
  23. 36 unless oauth_jwt_public_keys.empty?
  24. 72 oauth_jwt_public_keys.flat_map { |algo, pkeys| Array(pkeys).map { |pkey| jwk_export(pkey).merge(use: "sig", alg: algo) } }
  25. end
  26. ),
  27. *(
  28. 36 unless oauth_jwt_jwe_public_keys.empty?
  29. 12 oauth_jwt_jwe_public_keys.flat_map do |(algo, _enc), pkeys|
  30. 12 Array(pkeys).map do |pkey|
  31. 12 jwk_export(pkey).merge(use: "enc", alg: algo)
  32. end
  33. end
  34. end
  35. )
  36. ].compact
  37. end
  38. end
  39. end

lib/rodauth/features/oauth_jwt_secured_authorization_request.rb

100.0% lines covered

68 relevant lines. 68 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_jwt_secured_authorization_request, :OauthJwtSecuredAuthorizationRequest) do
  5. 12 ALLOWED_REQUEST_URI_CONTENT_TYPES = %w[application/jose application/oauth-authz-req+jwt].freeze
  6. 12 depends :oauth_authorize_base, :oauth_jwt_base
  7. 12 auth_value_method :oauth_require_request_uri_registration, false
  8. 12 auth_value_method :oauth_require_signed_request_object, false
  9. 12 auth_value_method :oauth_request_object_signing_alg_allow_none, false
  10. 8 %i[
  11. request_uris require_signed_request_object request_object_signing_alg
  12. request_object_encryption_alg request_object_encryption_enc
  13. 4 ].each do |column|
  14. 60 auth_value_method :"oauth_applications_#{column}_column", column
  15. end
  16. 12 translatable_method :oauth_invalid_request_object_message, "request object is invalid"
  17. 12 auth_value_method :max_param_bytesize, nil if Rodauth::VERSION >= "2.26.0"
  18. 12 private
  19. # /authorize
  20. 12 def validate_authorize_params
  21. 540 request_object = param_or_nil("request")
  22. 540 request_uri = param_or_nil("request_uri")
  23. 540 unless (request_object || request_uri) && oauth_application
  24. 132 if request.path == authorize_path && request.get? && require_signed_request_object?
  25. 12 redirect_response_error("invalid_request_object")
  26. end
  27. 120 return super
  28. end
  29. 408 if request_uri
  30. 108 request_uri = CGI.unescape(request_uri)
  31. 108 redirect_response_error("invalid_request_uri") unless supported_request_uri?(request_uri, oauth_application)
  32. 60 response = http_request(request_uri)
  33. 60 unless response.code.to_i == 200 && ALLOWED_REQUEST_URI_CONTENT_TYPES.include?(response["content-type"])
  34. 12 redirect_response_error("invalid_request_uri")
  35. end
  36. 48 request_object = response.body
  37. end
  38. 348 claims = decode_request_object(request_object)
  39. 228 redirect_response_error("invalid_request_object") unless claims
  40. 228 if (iss = claims["iss"]) && (iss != oauth_application[oauth_applications_client_id_column])
  41. 12 redirect_response_error("invalid_request_object")
  42. end
  43. 216 if (aud = claims["aud"]) && !verify_aud(aud, oauth_jwt_issuer)
  44. 12 redirect_response_error("invalid_request_object")
  45. end
  46. # If signed, the Authorization Request
  47. # Object SHOULD contain the Claims "iss" (issuer) and "aud" (audience)
  48. # as members, with their semantics being the same as defined in the JWT
  49. # [RFC7519] specification. The value of "aud" should be the value of
  50. # the Authorization Server (AS) "issuer" as defined in RFC8414
  51. # [RFC8414].
  52. 204 claims.delete("iss")
  53. 204 audience = claims.delete("aud")
  54. 204 redirect_response_error("invalid_request_object") if audience && audience != oauth_jwt_issuer
  55. 204 claims.each do |k, v|
  56. 1260 request.params[k.to_s] = v
  57. end
  58. 204 super
  59. end
  60. 12 def supported_request_uri?(request_uri, oauth_application)
  61. 108 return false unless check_valid_uri?(request_uri)
  62. 84 request_uris = oauth_application[oauth_applications_request_uris_column]
  63. 144 request_uris.nil? || request_uris.split(oauth_scope_separator).one? { |uri| request_uri.start_with?(uri) }
  64. end
  65. 12 def require_signed_request_object?
  66. 60 return @require_signed_request_object if defined?(@require_signed_request_object)
  67. 48 @require_signed_request_object = (oauth_application[oauth_applications_require_signed_request_object_column] if oauth_application)
  68. 48 @require_signed_request_object = oauth_require_signed_request_object if @require_signed_request_object.nil?
  69. 48 @require_signed_request_object
  70. end
  71. 12 def decode_request_object(request_object)
  72. 120 request_sig_enc_opts = {
  73. 240 jws_algorithm: oauth_application[oauth_applications_request_object_signing_alg_column],
  74. jws_encryption_algorithm: oauth_application[oauth_applications_request_object_encryption_alg_column],
  75. jws_encryption_method: oauth_application[oauth_applications_request_object_encryption_enc_column]
  76. }.compact
  77. 360 request_sig_enc_opts[:jws_algorithm] ||= "none" if oauth_request_object_signing_alg_allow_none
  78. 360 if request_sig_enc_opts[:jws_algorithm] == "none"
  79. 36 redirect_response_error("invalid_request_object") if require_signed_request_object?
  80. 12 jwks = nil
  81. 324 elsif (jwks = oauth_application_jwks(oauth_application))
  82. 252 jwks = JSON.parse(jwks, symbolize_names: true) if jwks.is_a?(String)
  83. else
  84. 72 redirect_response_error("invalid_request_object")
  85. end
  86. 264 claims = jwt_decode(request_object,
  87. jwks: jwks,
  88. verify_jti: false,
  89. verify_iss: false,
  90. verify_aud: false,
  91. **request_sig_enc_opts)
  92. 264 redirect_response_error("invalid_request_object") unless claims
  93. 240 claims
  94. end
  95. 12 def oauth_server_metadata_body(*)
  96. 36 super.tap do |data|
  97. 36 data[:request_parameter_supported] = true
  98. 36 data[:request_uri_parameter_supported] = true
  99. 36 data[:require_request_uri_registration] = oauth_require_request_uri_registration
  100. 36 data[:require_signed_request_object] = oauth_require_signed_request_object
  101. end
  102. end
  103. end
  104. end

lib/rodauth/features/oauth_jwt_secured_authorization_response_mode.rb

98.53% lines covered

68 relevant lines. 67 lines covered and 1 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_jwt_secured_authorization_response_mode, :OauthJwtSecuredAuthorizationResponseMode) do
  5. 12 depends :oauth_authorize_base, :oauth_jwt_base
  6. 12 auth_value_method :oauth_authorization_response_mode_expires_in, 60 * 5 # 5 minutes
  7. 12 auth_value_method :oauth_applications_authorization_signed_response_alg_column, :authorization_signed_response_alg
  8. 12 auth_value_method :oauth_applications_authorization_encrypted_response_alg_column, :authorization_encrypted_response_alg
  9. 12 auth_value_method :oauth_applications_authorization_encrypted_response_enc_column, :authorization_encrypted_response_enc
  10. 12 auth_value_methods(
  11. :authorization_signing_alg_values_supported,
  12. :authorization_encryption_alg_values_supported,
  13. :authorization_encryption_enc_values_supported
  14. )
  15. 12 def oauth_response_modes_supported
  16. 516 jwt_response_modes = %w[jwt]
  17. 516 jwt_response_modes.push("query.jwt", "form_post.jwt") if features.include?(:oauth_authorization_code_grant)
  18. 516 jwt_response_modes << "fragment.jwt" if features.include?(:oauth_implicit_grant)
  19. 516 super | jwt_response_modes
  20. end
  21. 12 def authorization_signing_alg_values_supported
  22. 12 oauth_jwt_jws_algorithms_supported
  23. end
  24. 12 def authorization_encryption_alg_values_supported
  25. 24 oauth_jwt_jwe_algorithms_supported
  26. end
  27. 12 def authorization_encryption_enc_values_supported
  28. 24 oauth_jwt_jwe_encryption_methods_supported
  29. end
  30. 12 private
  31. 12 def oauth_response_modes_for_code_supported
  32. 144 return [] unless features.include?(:oauth_authorization_code_grant)
  33. 144 super | %w[query.jwt form_post.jwt jwt]
  34. end
  35. 12 def oauth_response_modes_for_token_supported
  36. 60 return [] unless features.include?(:oauth_implicit_grant)
  37. 60 super | %w[fragment.jwt jwt]
  38. end
  39. 12 def authorize_response(params, mode)
  40. 120 return super unless mode.end_with?("jwt")
  41. 120 response_type = param_or_nil("response_type")
  42. 120 redirect_url = URI.parse(redirect_uri)
  43. 120 jwt = jwt_encode_authorization_response_mode(params)
  44. 120 if mode == "query.jwt" || (mode == "jwt" && response_type == "code")
  45. 60 return super unless features.include?(:oauth_authorization_code_grant)
  46. 60 params = ["response=#{CGI.escape(jwt)}"]
  47. 60 params << redirect_url.query if redirect_url.query
  48. 60 redirect_url.query = params.join("&")
  49. 60 redirect(redirect_url.to_s)
  50. 60 elsif mode == "form_post.jwt"
  51. 12 return super unless features.include?(:oauth_authorization_code_grant)
  52. 12 response["Content-Type"] = "text/html"
  53. 12 body = form_post_response_html(redirect_url) do
  54. 12 "<input type=\"hidden\" name=\"response\" value=\"#{scope.h(jwt)}\" />"
  55. end
  56. 12 response.write(body)
  57. 12 request.halt
  58. 48 elsif mode == "fragment.jwt" || (mode == "jwt" && response_type == "token")
  59. 48 return super unless features.include?(:oauth_implicit_grant)
  60. 48 params = ["response=#{CGI.escape(jwt)}"]
  61. 48 params << redirect_url.query if redirect_url.query
  62. 48 redirect_url.fragment = params.join("&")
  63. 48 redirect(redirect_url.to_s)
  64. else
  65. super
  66. end
  67. end
  68. 12 def _redirect_response_error(redirect_url, params)
  69. 36 response_mode = param_or_nil("response_mode")
  70. 36 return super unless response_mode.end_with?("jwt")
  71. 36 authorize_response(Hash[params], response_mode)
  72. end
  73. 12 def jwt_encode_authorization_response_mode(params)
  74. 120 now = Time.now.to_i
  75. 40 claims = {
  76. 80 iss: oauth_jwt_issuer,
  77. aud: oauth_application[oauth_applications_client_id_column],
  78. exp: now + oauth_authorization_response_mode_expires_in,
  79. iat: now
  80. }.merge(params)
  81. 40 encode_params = {
  82. 80 jwks: oauth_application_jwks(oauth_application),
  83. signing_algorithm: oauth_application[oauth_applications_authorization_signed_response_alg_column],
  84. encryption_algorithm: oauth_application[oauth_applications_authorization_encrypted_response_alg_column],
  85. encryption_method: oauth_application[oauth_applications_authorization_encrypted_response_enc_column]
  86. }.compact
  87. 120 jwt_encode(claims, **encode_params)
  88. end
  89. 12 def oauth_server_metadata_body(*)
  90. 24 super.tap do |data|
  91. 24 data[:authorization_signing_alg_values_supported] = authorization_signing_alg_values_supported
  92. 24 data[:authorization_encryption_alg_values_supported] = authorization_encryption_alg_values_supported
  93. 24 data[:authorization_encryption_enc_values_supported] = authorization_encryption_enc_values_supported
  94. end
  95. end
  96. end
  97. end

lib/rodauth/features/oauth_management_base.rb

100.0% lines covered

33 relevant lines. 33 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_management_base, :OauthManagementBase) do
  5. 12 depends :oauth_authorize_base
  6. 12 button "Previous", "oauth_management_pagination_previous"
  7. 12 button "Next", "oauth_management_pagination_next"
  8. 12 def oauth_management_pagination_links(paginated_ds)
  9. 168 html = +'<nav aria-label="Pagination"><ul class="pagination">'
  10. 168 html << oauth_management_pagination_link(paginated_ds.prev_page, label: oauth_management_pagination_previous_button)
  11. 168 html << oauth_management_pagination_link(paginated_ds.current_page - 1) unless paginated_ds.first_page?
  12. 168 html << oauth_management_pagination_link(paginated_ds.current_page, label: paginated_ds.current_page, current: true)
  13. 168 html << oauth_management_pagination_link(paginated_ds.current_page + 1) unless paginated_ds.last_page?
  14. 168 html << oauth_management_pagination_link(paginated_ds.next_page, label: oauth_management_pagination_next_button)
  15. 168 html << "</ul></nav>"
  16. end
  17. 12 def oauth_management_pagination_link(page, label: page, current: false, classes: "")
  18. 558 classes += " disabled" if current || !page
  19. 558 classes += " active" if current
  20. 558 if page
  21. 276 params = URI.encode_www_form(request.GET.merge("page" => page))
  22. 276 href = "#{request.path}?#{params}"
  23. 276 <<-HTML
  24. <li class="page-item #{classes}" #{'aria-current="page"' if current}>
  25. <a class="page-link" href="#{href}" tabindex="-1" aria-disabled="#{current || !page}">
  26. #{label}
  27. </a>
  28. </li>
  29. HTML
  30. else
  31. 282 <<-HTML
  32. <li class="page-item #{classes}">
  33. <span class="page-link">
  34. #{label}
  35. #{'<span class="sr-only">(current)</span>' if current}
  36. </span>
  37. </li>
  38. HTML
  39. end
  40. end
  41. 12 def post_configure
  42. 78 super
  43. # TODO: remove this in v1, when resource-server mode does not load all of the provider features.
  44. 78 return unless db
  45. 78 db.extension :pagination
  46. end
  47. 12 private
  48. 12 def per_page_param(default_per_page)
  49. 216 per_page = param_or_nil("per_page")
  50. 216 return default_per_page unless per_page
  51. 54 per_page = per_page.to_i
  52. 54 return default_per_page if per_page <= 0
  53. 54 [per_page, default_per_page].min
  54. end
  55. end
  56. end

lib/rodauth/features/oauth_pkce.rb

100.0% lines covered

49 relevant lines. 49 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_pkce, :OauthPkce) do
  5. 12 depends :oauth_authorization_code_grant
  6. 12 auth_value_method :oauth_require_pkce, true
  7. 12 auth_value_method :oauth_pkce_challenge_method, "S256"
  8. 12 auth_value_method :oauth_grants_code_challenge_column, :code_challenge
  9. 12 auth_value_method :oauth_grants_code_challenge_method_column, :code_challenge_method
  10. 12 auth_value_method :oauth_code_challenge_required_error_code, "invalid_request"
  11. 12 translatable_method :oauth_code_challenge_required_message, "code challenge required"
  12. 12 auth_value_method :oauth_unsupported_transform_algorithm_error_code, "invalid_request"
  13. 12 translatable_method :oauth_unsupported_transform_algorithm_message, "transform algorithm not supported"
  14. 12 private
  15. 12 def supports_auth_method?(oauth_application, auth_method)
  16. 72 return super unless auth_method == "none"
  17. 48 request.params.key?("code_verifier") || super
  18. end
  19. 12 def validate_authorize_params
  20. 48 validate_pkce_challenge_params
  21. 36 super
  22. end
  23. 12 def create_oauth_grant(create_params = {})
  24. # PKCE flow
  25. 12 if (code_challenge = param_or_nil("code_challenge"))
  26. 12 code_challenge_method = param_or_nil("code_challenge_method") || oauth_pkce_challenge_method
  27. 12 create_params[oauth_grants_code_challenge_column] = code_challenge
  28. 12 create_params[oauth_grants_code_challenge_method_column] = code_challenge_method
  29. end
  30. 12 super
  31. end
  32. 12 def create_token_from_authorization_code(grant_params, *args, oauth_grant: nil)
  33. 72 oauth_grant ||= valid_locked_oauth_grant(grant_params)
  34. 72 if oauth_grant[oauth_grants_code_challenge_column]
  35. 60 code_verifier = param_or_nil("code_verifier")
  36. 60 redirect_response_error("invalid_request") unless code_verifier && check_valid_grant_challenge?(oauth_grant, code_verifier)
  37. 12 elsif oauth_require_pkce
  38. 12 redirect_response_error("code_challenge_required")
  39. end
  40. 24 super({ oauth_grants_id_column => oauth_grant[oauth_grants_id_column] }, *args, oauth_grant: oauth_grant)
  41. end
  42. 12 def validate_pkce_challenge_params
  43. 48 if param_or_nil("code_challenge")
  44. 24 challenge_method = param_or_nil("code_challenge_method")
  45. 24 redirect_response_error("code_challenge_required") unless oauth_pkce_challenge_method == challenge_method
  46. else
  47. 24 return unless oauth_require_pkce
  48. 12 redirect_response_error("code_challenge_required")
  49. end
  50. end
  51. 12 def check_valid_grant_challenge?(grant, verifier)
  52. 48 challenge = grant[oauth_grants_code_challenge_column]
  53. 48 case grant[oauth_grants_code_challenge_method_column]
  54. when "plain"
  55. 12 challenge == verifier
  56. when "S256"
  57. 24 generated_challenge = Base64.urlsafe_encode64(Digest::SHA256.digest(verifier), padding: false)
  58. 24 challenge == generated_challenge
  59. else
  60. 12 redirect_response_error("unsupported_transform_algorithm")
  61. end
  62. end
  63. 12 def oauth_server_metadata_body(*)
  64. 12 super.tap do |data|
  65. 12 data[:code_challenge_methods_supported] = oauth_pkce_challenge_method
  66. end
  67. end
  68. end
  69. end

lib/rodauth/features/oauth_pushed_authorization_request.rb

92.54% lines covered

67 relevant lines. 62 lines covered and 5 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_pushed_authorization_request, :OauthJwtPushedAuthorizationRequest) do
  5. 12 depends :oauth_authorize_base
  6. 12 auth_value_method :oauth_require_pushed_authorization_requests, false
  7. 12 auth_value_method :oauth_applications_require_pushed_authorization_requests_column, :require_pushed_authorization_requests
  8. 12 auth_value_method :oauth_pushed_authorization_request_expires_in, 90 # 90 seconds
  9. 12 auth_value_method :oauth_require_pushed_authorization_request_iss_request_object, true
  10. 12 auth_value_method :oauth_pushed_authorization_requests_table, :oauth_pushed_requests
  11. 8 %i[
  12. oauth_application_id params code expires_in
  13. 4 ].each do |column|
  14. 48 auth_value_method :"oauth_pushed_authorization_requests_#{column}_column", column
  15. end
  16. # /par
  17. 12 auth_server_route(:par) do |r|
  18. 48 require_oauth_application
  19. 36 before_par_route
  20. 36 r.post do
  21. 36 validate_par_params
  22. 24 ds = db[oauth_pushed_authorization_requests_table]
  23. 24 code = oauth_unique_id_generator
  24. 8 push_request_params = {
  25. 16 oauth_pushed_authorization_requests_oauth_application_id_column => oauth_application[oauth_applications_id_column],
  26. oauth_pushed_authorization_requests_code_column => code,
  27. oauth_pushed_authorization_requests_params_column => URI.encode_www_form(request.params),
  28. oauth_pushed_authorization_requests_expires_in_column => Sequel.date_add(Sequel::CURRENT_TIMESTAMP,
  29. seconds: oauth_pushed_authorization_request_expires_in)
  30. }
  31. 24 rescue_from_uniqueness_error do
  32. 24 ds.insert(push_request_params)
  33. end
  34. 24 json_response_success(
  35. "request_uri" => "urn:ietf:params:oauth:request_uri:#{code}",
  36. "expires_in" => oauth_pushed_authorization_request_expires_in
  37. )
  38. end
  39. end
  40. 12 def check_csrf?
  41. 384 case request.path
  42. when par_path
  43. 48 false
  44. else
  45. 336 super
  46. end
  47. end
  48. 12 private
  49. 12 def validate_par_params
  50. # https://datatracker.ietf.org/doc/html/rfc9126#section-2.1
  51. # The request_uri authorization request parameter is one exception, and it MUST NOT be provided.
  52. 36 redirect_response_error("invalid_request") if param_or_nil("request_uri")
  53. 24 if features.include?(:oauth_jwt_secured_authorization_request)
  54. 12 if (request_object = param_or_nil("request"))
  55. 12 claims = decode_request_object(request_object)
  56. # https://datatracker.ietf.org/doc/html/rfc9126#section-3-5.3
  57. # reject the request if the authenticated client_id does not match the client_id claim in the Request Object
  58. 12 if (client_id = claims["client_id"]) && (client_id != oauth_application[oauth_applications_client_id_column])
  59. redirect_response_error("invalid_request_object")
  60. end
  61. # requiring the iss claim to match the client_id is at the discretion of the authorization server
  62. 12 if oauth_require_pushed_authorization_request_iss_request_object &&
  63. 12 (iss = claims.delete("iss")) &&
  64. iss != oauth_application[oauth_applications_client_id_column]
  65. redirect_response_error("invalid_request_object")
  66. end
  67. 12 if (aud = claims.delete("aud")) && !verify_aud(aud, oauth_jwt_issuer)
  68. redirect_response_error("invalid_request_object")
  69. end
  70. 12 claims.delete("exp")
  71. 12 request.params.delete("request")
  72. 12 claims.each do |k, v|
  73. 72 request.params[k.to_s] = v
  74. end
  75. elsif require_signed_request_object?
  76. redirect_response_error("invalid_request_object")
  77. end
  78. end
  79. 24 validate_authorize_params
  80. end
  81. 12 def validate_authorize_params
  82. 132 return super unless request.get? && request.path == authorize_path
  83. 84 if (request_uri = param_or_nil("request_uri"))
  84. 36 code = request_uri.delete_prefix("urn:ietf:params:oauth:request_uri:")
  85. 36 table = oauth_pushed_authorization_requests_table
  86. 36 ds = db[table]
  87. 36 pushed_request = ds.where(
  88. oauth_pushed_authorization_requests_oauth_application_id_column => oauth_application[oauth_applications_id_column],
  89. oauth_pushed_authorization_requests_code_column => code
  90. ).where(
  91. Sequel.expr(Sequel[table][oauth_pushed_authorization_requests_expires_in_column]) >= Sequel::CURRENT_TIMESTAMP
  92. ).first
  93. 36 redirect_response_error("invalid_request") unless pushed_request
  94. 24 URI.decode_www_form(pushed_request[oauth_pushed_authorization_requests_params_column]).each do |k, v|
  95. 108 request.params[k.to_s] = v
  96. end
  97. 24 request.params.delete("request_uri")
  98. # we're removing the request_uri here, so the checkup for signed reqest has to be invalidated.
  99. 24 @require_signed_request_object = false
  100. 48 elsif oauth_require_pushed_authorization_requests ||
  101. (oauth_application && oauth_application[oauth_applications_require_pushed_authorization_requests_column])
  102. 24 redirect_authorize_error("request_uri")
  103. end
  104. 48 super
  105. end
  106. 12 def oauth_server_metadata_body(*)
  107. 12 super.tap do |data|
  108. 12 data[:require_pushed_authorization_requests] = oauth_require_pushed_authorization_requests
  109. 12 data[:pushed_authorization_request_endpoint] = par_url
  110. end
  111. end
  112. end
  113. end

lib/rodauth/features/oauth_resource_indicators.rb

93.9% lines covered

82 relevant lines. 77 lines covered and 5 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_resource_indicators, :OauthResourceIndicators) do
  5. 12 depends :oauth_authorize_base
  6. 12 auth_value_method :oauth_grants_resource_column, :resource
  7. 12 def resource_indicators
  8. 480 return @resource_indicators if defined?(@resource_indicators)
  9. 120 resources = param_or_nil("resource")
  10. 120 return unless resources
  11. 120 if json_request? || param_or_nil("request") # signed request
  12. 24 resources = Array(resources)
  13. else
  14. 96 query = if request.form_data?
  15. 60 request.body.rewind
  16. 60 request.body.read
  17. else
  18. 36 request.query_string
  19. end
  20. # resource query param does not conform to rack parsing rules
  21. 96 resources = URI.decode_www_form(query).each_with_object([]) do |(k, v), memo|
  22. 504 memo << v if k == "resource"
  23. end
  24. end
  25. 120 @resource_indicators = resources
  26. end
  27. 12 def require_oauth_authorization(*)
  28. 84 super
  29. # done so to support token-in-grant-db, jwt, and resource-server mode
  30. 72 token_indicators = authorization_token[oauth_grants_resource_column] || authorization_token["resource"]
  31. 72 return unless token_indicators
  32. 60 token_indicators = token_indicators.split(" ") if token_indicators.is_a?(String)
  33. 120 authorization_required unless token_indicators.any? { |resource| base_url.start_with?(resource) }
  34. end
  35. 12 private
  36. 12 def validate_token_params
  37. 48 super
  38. 48 return unless resource_indicators
  39. 48 resource_indicators.each do |resource|
  40. 48 redirect_response_error("invalid_target") unless check_valid_no_fragment_uri?(resource)
  41. end
  42. end
  43. 12 def create_token_from_token(oauth_grant, update_params)
  44. return super unless resource_indicators
  45. grant_indicators = oauth_grant[oauth_grants_resource_column]
  46. grant_indicators = grant_indicators.split(" ") if grant_indicators.is_a?(String)
  47. redirect_response_error("invalid_target") unless (grant_indicators - resource_indicators) != grant_indicators
  48. super(oauth_grant, update_params.merge(oauth_grants_resource_column => resource_indicators))
  49. end
  50. 12 module IndicatorAuthorizationCodeGrant
  51. 12 private
  52. 12 def validate_authorize_params
  53. 72 super
  54. 72 return unless resource_indicators
  55. 72 resource_indicators.each do |resource|
  56. 72 redirect_response_error("invalid_target") unless check_valid_no_fragment_uri?(resource)
  57. end
  58. end
  59. 12 def create_token_from_authorization_code(grant_params, *args, oauth_grant: nil)
  60. 48 return super unless resource_indicators
  61. 48 oauth_grant ||= valid_locked_oauth_grant(grant_params)
  62. 48 redirect_response_error("invalid_target") unless oauth_grant[oauth_grants_resource_column]
  63. 48 grant_indicators = oauth_grant[oauth_grants_resource_column]
  64. 48 grant_indicators = grant_indicators.split(" ") if grant_indicators.is_a?(String)
  65. 48 redirect_response_error("invalid_target") unless (grant_indicators - resource_indicators) != grant_indicators
  66. # update ownership
  67. 36 if grant_indicators != resource_indicators
  68. 12 oauth_grant = __update_and_return__(
  69. db[oauth_grants_table].where(oauth_grants_id_column => oauth_grant[oauth_grants_id_column]),
  70. oauth_grants_resource_column => resource_indicators
  71. )
  72. end
  73. 36 super({ oauth_grants_id_column => oauth_grant[oauth_grants_id_column] }, *args, oauth_grant: oauth_grant)
  74. end
  75. 12 def create_oauth_grant(create_params = {})
  76. 12 create_params[oauth_grants_resource_column] = resource_indicators.join(" ") if resource_indicators
  77. 12 super
  78. end
  79. end
  80. 12 module IndicatorIntrospection
  81. 12 def json_token_introspect_payload(grant)
  82. 12 return super unless grant[oauth_grants_id_column]
  83. 12 payload = super
  84. 12 token_indicators = grant[oauth_grants_resource_column]
  85. 12 token_indicators = token_indicators.split(" ") if token_indicators.is_a?(String)
  86. 12 payload[:aud] = token_indicators
  87. 12 payload
  88. end
  89. 12 def introspection_request(*)
  90. 36 payload = super
  91. 36 payload[oauth_grants_resource_column] = payload["aud"] if payload["aud"]
  92. 36 payload
  93. end
  94. end
  95. 12 module IndicatorJwt
  96. 12 def jwt_claims(*)
  97. 12 return super unless resource_indicators
  98. 12 super.merge(aud: resource_indicators)
  99. end
  100. 12 def jwt_decode(token, verify_aud: true, **args)
  101. 36 claims = super(token, verify_aud: false, **args)
  102. 36 return claims unless verify_aud
  103. 24 return unless claims["aud"] && claims["aud"].one? { |aud| request.url.starts_with?(aud) }
  104. 12 claims
  105. end
  106. end
  107. 12 def self.included(rodauth)
  108. 180 super
  109. 180 rodauth.send(:include, IndicatorAuthorizationCodeGrant) if rodauth.features.include?(:oauth_authorization_code_grant)
  110. 180 rodauth.send(:include, IndicatorIntrospection) if rodauth.features.include?(:oauth_token_introspection)
  111. 180 rodauth.send(:include, IndicatorJwt) if rodauth.features.include?(:oauth_jwt)
  112. end
  113. end
  114. end

lib/rodauth/features/oauth_resource_server.rb

96.3% lines covered

27 relevant lines. 26 lines covered and 1 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "rodauth/oauth"
  3. 12 module Rodauth
  4. 12 Feature.define(:oauth_resource_server, :OauthResourceServer) do
  5. 12 depends :oauth_token_introspection
  6. 12 auth_value_method :is_authorization_server?, false
  7. 12 auth_methods(
  8. :before_introspection_request
  9. )
  10. 12 def authorization_token
  11. 216 return @authorization_token if defined?(@authorization_token)
  12. # check if there is a token
  13. 108 access_token = fetch_access_token
  14. 108 return unless access_token
  15. # where in resource server, NOT the authorization server.
  16. 84 payload = introspection_request("access_token", access_token)
  17. 84 return unless payload["active"]
  18. 72 @authorization_token = payload
  19. end
  20. 12 def require_oauth_authorization(*scopes)
  21. 108 authorization_required unless authorization_token
  22. 72 aux_scopes = authorization_token["scope"]
  23. 72 token_scopes = if aux_scopes
  24. 72 aux_scopes.split(oauth_scope_separator)
  25. else
  26. []
  27. end
  28. 144 authorization_required unless scopes.any? { |scope| token_scopes.include?(scope) }
  29. end
  30. 12 private
  31. 12 def introspection_request(token_type_hint, token)
  32. 84 introspect_url = URI("#{authorization_server_url}#{introspect_path}")
  33. 84 response = http_request(introspect_url, { "token_type_hint" => token_type_hint, "token" => token }) do |request|
  34. 84 before_introspection_request(request)
  35. end
  36. 84 JSON.parse(response.body)
  37. end
  38. 12 def before_introspection_request(request); end
  39. end
  40. end

lib/rodauth/features/oauth_saml_bearer_grant.rb

96.77% lines covered

62 relevant lines. 60 lines covered and 2 lines missed.
    
  1. # frozen_string_literal: true
  2. 12 require "onelogin/ruby-saml"
  3. 12 require "rodauth/oauth"
  4. 12 module Rodauth
  5. 12 Feature.define(:oauth_saml_bearer_grant, :OauthSamlBearerGrant) do
  6. 12 depends :oauth_assertion_base
  7. 12 auth_value_method :oauth_saml_name_identifier_format, "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
  8. 12 auth_value_method :oauth_saml_idp_cert_check_expiration, true
  9. 12 auth_value_method :max_param_bytesize, nil if Rodauth::VERSION >= "2.26.0"
  10. 12 auth_value_method :oauth_saml_settings_table, :oauth_saml_settings
  11. 8 %i[
  12. id oauth_application_id
  13. idp_cert idp_cert_fingerprint idp_cert_fingerprint_algorithm
  14. name_identifier_format
  15. issuer
  16. audience
  17. idp_cert_check_expiration
  18. 4 ].each do |column|
  19. 108 auth_value_method :"oauth_saml_settings_#{column}_column", column
  20. end
  21. 12 translatable_method :oauth_saml_assertion_not_base64_message, "SAML assertion must be in base64 format"
  22. 12 translatable_method :oauth_saml_assertion_single_issuer_message, "SAML assertion must have a single issuer"
  23. 12 translatable_method :oauth_saml_settings_not_found_message, "No SAML settings found for issuer"
  24. 12 auth_methods(
  25. :require_oauth_application_from_saml2_bearer_assertion_issuer,
  26. :require_oauth_application_from_saml2_bearer_assertion_subject,
  27. :account_from_saml2_bearer_assertion
  28. )
  29. 12 def oauth_grant_types_supported
  30. 24 super | %w[urn:ietf:params:oauth:grant-type:saml2-bearer]
  31. end
  32. 12 private
  33. 12 def require_oauth_application_from_saml2_bearer_assertion_issuer(assertion)
  34. 12 parse_saml_assertion(assertion)
  35. 12 return unless @saml_settings
  36. 8 db[oauth_applications_table].where(
  37. oauth_applications_id_column => @saml_settings[oauth_saml_settings_oauth_application_id_column]
  38. 4 ).first
  39. end
  40. 12 def require_oauth_application_from_saml2_bearer_assertion_subject(assertion)
  41. 12 parse_saml_assertion(assertion)
  42. 12 return unless @assertion
  43. # 3.3.8 - For client authentication, the Subject MUST be the "client_id" of the OAuth client.
  44. 8 db[oauth_applications_table].where(
  45. oauth_applications_client_id_column => @assertion.nameid
  46. 4 ).first
  47. end
  48. 12 def account_from_saml2_bearer_assertion(assertion)
  49. 12 parse_saml_assertion(assertion)
  50. 12 return unless @assertion