You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.3 KiB

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