Class: ApiWarden::Authentication
- Inherits:
-
Object
- Object
- ApiWarden::Authentication
- Defined in:
- lib/api_warden/authentication.rb,
lib/api_warden/authentication/params.rb,
lib/api_warden/authentication/header_params.rb
Defined Under Namespace
Classes: AuthenticationError, HeaderParams, Params
Instance Attribute Summary collapse
-
#key_for_access_token ⇒ Object
readonly
Returns the value of attribute key_for_access_token.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Instance Method Summary collapse
-
#authenticate ⇒ Object
Self.
-
#authenticate! ⇒ Object
This method will only authenticate once, and cache the result.
- #authenticated? ⇒ Boolean
- #id ⇒ Object
-
#initialize(scope, request) ⇒ Authentication
constructor
A new instance of Authentication.
- #refreshable? ⇒ Boolean
-
#sign_out ⇒ Object
TODO remove refresh token as well.
-
#ttl_for_access_token ⇒ Fixnum
The time to live for access token in seconds.
-
#ttl_for_access_token=(seconds) ⇒ Object
Set the ttl for access token.
- #validate_refresh_token ⇒ Object
- #validate_refresh_token! ⇒ Object
- #value_for_access_token ⇒ Object
- #value_for_refresh_token ⇒ Object
Constructor Details
#initialize(scope, request) ⇒ Authentication
Returns a new instance of Authentication.
10 11 12 13 14 |
# File 'lib/api_warden/authentication.rb', line 10 def initialize(scope, request) @scope = scope @request = request @params = scope.params_class.new(self) end |
Instance Attribute Details
#key_for_access_token ⇒ Object (readonly)
Returns the value of attribute key_for_access_token.
8 9 10 |
# File 'lib/api_warden/authentication.rb', line 8 def key_for_access_token @key_for_access_token end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/api_warden/authentication.rb', line 8 def params @params end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
8 9 10 |
# File 'lib/api_warden/authentication.rb', line 8 def request @request end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
8 9 10 |
# File 'lib/api_warden/authentication.rb', line 8 def scope @scope end |
Instance Method Details
#authenticate ⇒ Object
Returns self.
42 43 44 45 46 |
# File 'lib/api_warden/authentication.rb', line 42 def authenticate authenticate! rescue AuthenticationError => e self end |
#authenticate! ⇒ Object
This method will only authenticate once, and cache the result.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# 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? ⇒ Boolean
16 17 18 19 |
# File 'lib/api_warden/authentication.rb', line 16 def authenticated? ensure_authenticated @authenticated end |
#id ⇒ Object
26 27 28 29 |
# File 'lib/api_warden/authentication.rb', line 26 def id ensure_authenticated_or_refreshable @id end |
#refreshable? ⇒ Boolean
21 22 23 24 |
# File 'lib/api_warden/authentication.rb', line 21 def refreshable? ensure_refreshable @refreshable end |
#sign_out ⇒ Object
TODO remove refresh token as well
101 102 103 104 105 |
# 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 ⇒ Fixnum
Returns the time to live for access token in seconds.
108 109 110 111 112 |
# 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) ⇒ Object
Set the ttl for access token.
115 116 117 118 119 120 121 |
# 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 ⇒ Object
72 73 74 75 |
# File 'lib/api_warden/authentication.rb', line 72 def validate_refresh_token validate_refresh_token! rescue AuthenticationError => e end |
#validate_refresh_token! ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# 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 ⇒ Object
31 32 33 34 |
# File 'lib/api_warden/authentication.rb', line 31 def value_for_access_token ensure_authenticated @value_for_access_token end |
#value_for_refresh_token ⇒ Object
36 37 38 39 |
# File 'lib/api_warden/authentication.rb', line 36 def value_for_refresh_token ensure_refreshable @value_for_refresh_token end |