class ApiWarden::Authentication
Attributes
key_for_access_token[R]
params[R]
request[R]
scope[R]
Public Class Methods
new(scope, request)
click to toggle source
# File lib/api_warden/authentication.rb, line 10 def initialize(scope, request) @scope = scope @request = request @params = scope.params_class.new(self) end
Public Instance Methods
authenticate()
click to toggle source
@return self
# File lib/api_warden/authentication.rb, line 42 def authenticate authenticate! rescue AuthenticationError => e self end
authenticate!()
click to toggle source
This method will only authenticate once, and cache the result.
@return self
# File lib/api_warden/authentication.rb, line 51 def authenticate! return unless @authenticated.nil? id, access_token = @params.retrieve_id, @params.retrieve_access_token @key_for_access_token = @scope.key_for_access_token(id, access_token) if access_token && !access_token.empty? ApiWarden.redis { |conn| @value_for_access_token = conn.get(@key_for_access_token) } end unless @value_for_access_token @authenticated = false raise AuthenticationError end @authenticated = true @id = id @access_token = access_token self end
authenticated?()
click to toggle source
# File lib/api_warden/authentication.rb, line 16 def authenticated? ensure_authenticated @authenticated end
id()
click to toggle source
# File lib/api_warden/authentication.rb, line 26 def id ensure_authenticated_or_refreshable @id end
refreshable?()
click to toggle source
# File lib/api_warden/authentication.rb, line 21 def refreshable? ensure_refreshable @refreshable end
sign_out()
click to toggle source
TODO remove refresh token as well
# File lib/api_warden/authentication.rb, line 101 def sign_out key = @scope.key_for_access_token(@id, @access_token) ApiWarden.redis { |conn| conn.del(key) } end
ttl_for_access_token()
click to toggle source
@return [Fixnum] the time to live for access token in seconds
# File lib/api_warden/authentication.rb, line 108 def ttl_for_access_token raise_if_authentication_failed! ttl_for_key(@key_for_access_token) end
ttl_for_access_token=(seconds)
click to toggle source
Set the ttl for access token.
# File lib/api_warden/authentication.rb, line 115 def ttl_for_access_token=(seconds) raise_if_authentication_failed! key = @key_for_access_token value = @value_for_access_token ApiWarden.redis { |conn| conn.set(key, value, ex: seconds) } end
validate_refresh_token()
click to toggle source
# File lib/api_warden/authentication.rb, line 72 def validate_refresh_token validate_refresh_token! rescue AuthenticationError => e end
validate_refresh_token!()
click to toggle source
# File lib/api_warden/authentication.rb, line 77 def validate_refresh_token! return unless @refreshable.nil? id, refresh_token = @params.retrieve_id, @params.retrieve_refresh_token key = @scope.key_for_refresh_token(id, refresh_token) if refresh_token && !refresh_token.empty? ApiWarden.redis do |conn| @value_for_refresh_token = conn.get(key) conn.del(key) end end unless @value_for_refresh_token @refreshable = false raise AuthenticationError end @refreshable = true @id = id self end
value_for_access_token()
click to toggle source
# File lib/api_warden/authentication.rb, line 31 def value_for_access_token ensure_authenticated @value_for_access_token end
value_for_refresh_token()
click to toggle source
# File lib/api_warden/authentication.rb, line 36 def value_for_refresh_token ensure_refreshable @value_for_refresh_token end
Private Instance Methods
ensure_authenticated()
click to toggle source
# File lib/api_warden/authentication.rb, line 124 def ensure_authenticated return unless @authenticated.nil? authenticate end
ensure_authenticated_or_refreshable()
click to toggle source
# File lib/api_warden/authentication.rb, line 134 def ensure_authenticated_or_refreshable ensure_authenticated ensure_refreshable unless @authenticated end
ensure_refreshable()
click to toggle source
# File lib/api_warden/authentication.rb, line 129 def ensure_refreshable return unless @refreshable.nil? validate_refresh_token end
raise_if_authentication_failed!()
click to toggle source
# File lib/api_warden/authentication.rb, line 139 def raise_if_authentication_failed! ensure_authenticated raise 'The authentication is not valid.' if @authenticated == false end
ttl_for_key(key)
click to toggle source
# File lib/api_warden/authentication.rb, line 144 def ttl_for_key(key) ApiWarden.redis { |conn| conn.ttl(key) } end