base_controller.rb 4.3 KB

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