base_controller.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # frozen_string_literal: true
  2. class Api::BaseController < ApplicationController
  3. DEFAULT_STATUSES_LIMIT = 20
  4. DEFAULT_ACCOUNTS_LIMIT = 40
  5. include Api::RateLimitHeaders
  6. include Api::AccessTokenTrackingConcern
  7. include Api::CachingConcern
  8. include Api::ContentSecurityPolicy
  9. skip_before_action :require_functional!, unless: :limited_federation_mode?
  10. before_action :require_authenticated_user!, if: :disallow_unauthenticated_api_access?
  11. before_action :require_not_suspended!
  12. vary_by 'Authorization'
  13. protect_from_forgery with: :null_session
  14. rescue_from ActiveRecord::RecordInvalid, Mastodon::ValidationError do |e|
  15. render json: { error: e.to_s }, status: 422
  16. end
  17. rescue_from ActiveRecord::RecordNotUnique do
  18. render json: { error: 'Duplicate record' }, status: 422
  19. end
  20. rescue_from Date::Error do
  21. render json: { error: 'Invalid date supplied' }, status: 422
  22. end
  23. rescue_from ActiveRecord::RecordNotFound do
  24. render json: { error: 'Record not found' }, status: 404
  25. end
  26. rescue_from HTTP::Error, Mastodon::UnexpectedResponseError do
  27. render json: { error: 'Remote data could not be fetched' }, status: 503
  28. end
  29. rescue_from OpenSSL::SSL::SSLError do
  30. render json: { error: 'Remote SSL certificate could not be verified' }, status: 503
  31. end
  32. rescue_from Mastodon::NotPermittedError do
  33. render json: { error: 'This action is not allowed' }, status: 403
  34. end
  35. rescue_from Seahorse::Client::NetworkingError do |e|
  36. Rails.logger.warn "Storage server error: #{e}"
  37. render json: { error: 'There was a temporary problem serving your request, please try again' }, status: 503
  38. end
  39. rescue_from Mastodon::RaceConditionError, Stoplight::Error::RedLight do
  40. render json: { error: 'There was a temporary problem serving your request, please try again' }, status: 503
  41. end
  42. rescue_from Mastodon::RateLimitExceededError do
  43. render json: { error: I18n.t('errors.429') }, status: 429
  44. end
  45. rescue_from ActionController::ParameterMissing, Mastodon::InvalidParameterError do |e|
  46. render json: { error: e.to_s }, status: 400
  47. end
  48. def doorkeeper_unauthorized_render_options(error: nil)
  49. { json: { error: error.try(:description) || 'Not authorized' } }
  50. end
  51. def doorkeeper_forbidden_render_options(*)
  52. { json: { error: 'This action is outside the authorized scopes' } }
  53. end
  54. protected
  55. def set_pagination_headers(next_path = nil, prev_path = nil)
  56. links = []
  57. links << [next_path, [%w(rel next)]] if next_path
  58. links << [prev_path, [%w(rel prev)]] if prev_path
  59. response.headers['Link'] = LinkHeader.new(links) unless links.empty?
  60. end
  61. def limit_param(default_limit)
  62. return default_limit unless params[:limit]
  63. [params[:limit].to_i.abs, default_limit * 2].min
  64. end
  65. def params_slice(*keys)
  66. params.slice(*keys).permit(*keys)
  67. end
  68. def current_resource_owner
  69. @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
  70. end
  71. def current_user
  72. current_resource_owner || super
  73. rescue ActiveRecord::RecordNotFound
  74. nil
  75. end
  76. def require_authenticated_user!
  77. render json: { error: 'This method requires an authenticated user' }, status: 401 unless current_user
  78. end
  79. def require_not_suspended!
  80. render json: { error: 'Your login is currently disabled' }, status: 403 if current_user&.account&.unavailable?
  81. end
  82. def require_valid_pagination_options!
  83. render json: { error: 'Pagination values for `offset` and `limit` must be positive' }, status: 400 if pagination_options_invalid?
  84. end
  85. def require_user!
  86. if !current_user
  87. render json: { error: 'This method requires an authenticated user' }, status: 422
  88. elsif !current_user.confirmed?
  89. render json: { error: 'Your login is missing a confirmed e-mail address' }, status: 403
  90. elsif !current_user.approved?
  91. render json: { error: 'Your login is currently pending approval' }, status: 403
  92. elsif !current_user.functional?
  93. render json: { error: 'Your login is currently disabled' }, status: 403
  94. else
  95. update_user_sign_in
  96. end
  97. end
  98. def render_empty
  99. render json: {}, status: 200
  100. end
  101. def authorize_if_got_token!(*scopes)
  102. doorkeeper_authorize!(*scopes) if doorkeeper_token
  103. end
  104. def disallow_unauthenticated_api_access?
  105. ENV['DISALLOW_UNAUTHENTICATED_API_ACCESS'] == 'true' || Rails.configuration.x.limited_federation_mode
  106. end
  107. private
  108. def pagination_options_invalid?
  109. params.slice(:limit, :offset).values.map(&:to_i).any?(&:negative?)
  110. end
  111. def respond_with_error(code)
  112. render json: { error: Rack::Utils::HTTP_STATUS_CODES[code] }, status: code
  113. end
  114. end