base_controller.rb 3.0 KB

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