Adding OAuth access scopes, fixing OAuth authorization UI, adding rate limiting
to the API
This commit is contained in:
		@@ -50,8 +50,8 @@ Doorkeeper.configure do
 | 
			
		||||
  # Define access token scopes for your provider
 | 
			
		||||
  # For more information go to
 | 
			
		||||
  # https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes
 | 
			
		||||
  # default_scopes  :public
 | 
			
		||||
  # optional_scopes :write, :follow
 | 
			
		||||
  default_scopes  :read
 | 
			
		||||
  optional_scopes :write, :follow
 | 
			
		||||
 | 
			
		||||
  # Change the way client credentials are retrieved from the request object.
 | 
			
		||||
  # By default it retrieves first from the `HTTP_AUTHORIZATION` header, then
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
Rabl.configure do |config|
 | 
			
		||||
  config.cache_all_output  = true
 | 
			
		||||
  config.cache_all_output  = false
 | 
			
		||||
  config.cache_sources     = !!Rails.env.production?
 | 
			
		||||
  config.include_json_root = false
 | 
			
		||||
  config.view_paths        = [Rails.root.join('app/views')]
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,19 @@
 | 
			
		||||
class Rack::Attack
 | 
			
		||||
  throttle('get-req/ip', limit: 300, period: 5.minutes) do |req|
 | 
			
		||||
    req.ip if req.get?
 | 
			
		||||
  # Rate limits for the API
 | 
			
		||||
  throttle('api', limit: 150, period: 5.minutes) do |req|
 | 
			
		||||
    req.ip if req.path.match(/\A\/api\//)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  throttle('post-req/ip', limit: 100, period: 5.minutes) do |req|
 | 
			
		||||
    req.ip if req.post?
 | 
			
		||||
  self.throttled_response = lambda do |env|
 | 
			
		||||
    now        = Time.now.utc
 | 
			
		||||
    match_data = env['rack.attack.match_data']
 | 
			
		||||
 | 
			
		||||
    headers = {
 | 
			
		||||
      'X-RateLimit-Limit'     => match_data[:limit].to_s,
 | 
			
		||||
      'X-RateLimit-Remaining' => '0',
 | 
			
		||||
      'X-RateLimit-Reset'     => (now + (match_data[:period] - now.to_i % match_data[:period])).to_s
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    [429, headers, [{ error: 'Throttled' }.to_json]]
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user