base_controller.rb 4.1 KB

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