base_controller.rb 3.6 KB

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