[refs #796] small refactoring
This commit is contained in:
parent
2a5a955bfb
commit
5fa08cc393
|
@ -40,8 +40,8 @@ class ApiDefender < Rack::Throttle::Hourly
|
||||||
count = cache.incr(key)
|
count = cache.incr(key)
|
||||||
cache.expire(key, 1.day) if count == 1
|
cache.expire(key, 1.day) if count == 1
|
||||||
|
|
||||||
if authorized? request
|
if @user
|
||||||
count = cache.incr(choice_key(request, :only_user => true))
|
count = cache.incr(choice_key(request))
|
||||||
cache.expire(key, 1.day) if count == 1
|
cache.expire(key, 1.day) if count == 1
|
||||||
end
|
end
|
||||||
count
|
count
|
||||||
|
@ -55,18 +55,16 @@ class ApiDefender < Rack::Throttle::Hourly
|
||||||
end
|
end
|
||||||
|
|
||||||
def authorized?(request)
|
def authorized?(request)
|
||||||
return @authorized unless @authorized.nil?
|
return @authorized if @authorized
|
||||||
auth = Rack::Auth::Basic::Request.new(request.env)
|
auth = Rack::Auth::Basic::Request.new(request.env)
|
||||||
if auth.provided? and auth.basic?
|
if auth.provided? and auth.basic?
|
||||||
@authorized = (@user = User.auth_by_token_or_login_pass(*auth.credentials))
|
@user = User.auth_by_token_or_login_pass(*auth.credentials)
|
||||||
end
|
end
|
||||||
@user = nil unless @authorized
|
@authorized = true # cache
|
||||||
@authorized
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def choice_key request, opts = {}
|
def choice_key request
|
||||||
raise 'user not authorized for key' if opts[:only_user] && !authorized?(request) # Debug
|
return cache_key(request) unless @user
|
||||||
return cache_key(request) if opts[:only_ip] || !authorized?(request)
|
|
||||||
[@options[:key_prefix], @user.uname, Time.now.strftime('%Y-%m-%dT%H')].join(':')
|
[@options[:key_prefix], @user.uname, Time.now.strftime('%Y-%m-%dT%H')].join(':')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue