-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathapplication_controller.rb
67 lines (55 loc) · 1.61 KB
/
application_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class ApplicationController < ActionController::API
include ActionController::Serialization
include ActionController::HttpAuthentication::Token::ControllerMethods
before_action :authenticate, except: [:index_public]
before_filter :throttle_token
protected
def authenticate
authenticate_token || render_unauthorized
end
def authenticate_token
authenticate_with_http_token do |token, options|
@current_user = User.find_by(api_key: token)
@token = token
end
end
def render_unauthorized(realm = "Application")
self.headers["WWW-Authenticate"] = %(Token realm="#{realm.gsub(/"/, "")}")
render json: {message: 'Bad credentials'}, status: :unauthorized
end
def throttle_ip
client_ip = request.env["REMOTE_ADDR"]
key = "count:#{client_ip}"
count = REDIS.get(key)
unless count
REDIS.set(key, 0)
REDIS.expire(key, THROTTLE_TIME_WINDOW)
return true
end
if count.to_i >= THROTTLE_MAX_REQUESTS
render :json => {:message => "You have fired too many requests. Please wait for some time."}, :status => 429
return
end
REDIS.incr(key)
true
end
def throttle_token
if @token.present?
key = "count:#{@token}"
count = REDIS.get(key)
unless count
REDIS.set(key, 0)
REDIS.expire(key, THROTTLE_TIME_WINDOW)
return true
end
if count.to_i >= THROTTLE_MAX_REQUESTS
render :json => {:message => "You have fired too many requests. Please wait for some time."}, :status => 429
return
end
REDIS.incr(key)
true
else
false
end
end
end