Class: Hadley::TokenStore

Inherits:
Object
  • Object
show all
Defined in:
lib/hadley/token_store.rb

Overview

This class handles the storage, retrieval and removal of OAuth 2 bearer tokens sent from the AFID authorization server. The TokenStore delegates most of the work to the delegate store, which must support the api set forth by ActiveSupport::Cache::Store.

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ TokenStore

This method initializes the TokenStore with the delegate store.

Parameters:

  • store (ActiveSupport::Cache::Store)

    The TokenStore instance will delegate the heavy lifting to the provided store.



10
11
12
# File 'lib/hadley/token_store.rb', line 10

def initialize(store)
  @store = store
end

Instance Method Details

#delete(token) ⇒ Boolean?

This method removes the AFID identity information associated with the provided token.

Parameters:

  • token (String)

    The token provisioned by the AFID resource server.

Returns:

  • (Boolean, nil)

    True if an only if the identity information was removed successfully.



47
48
49
# File 'lib/hadley/token_store.rb', line 47

def delete(token)
  @store.delete(key_for(token))
end

#get(token) ⇒ Hash?

This method retrieves the AFID identity information associated with the provided token. If no such identity is found the result will be nil.

Parameters:

  • token (String)

    The unique token provisioned by the AFID resource server.

Returns:

  • (Hash, nil)

    A Hash representation of the identity associated with the provided token or nil if no such identity exists.



21
22
23
24
25
26
27
# File 'lib/hadley/token_store.rb', line 21

def get(token)
  access = @store.read(key_for(token))
  if access
    access[:anonymous] = access[:identity] == Hadley::ANONYMOUS_IDENTITY
  end
  access
end

#key_for(token) ⇒ Symbol (protected)

This method derives the appropriate datastore key from the given AFID token.

Parameters:

  • token (String)

    The unique token provisioned by the AFID resource server.

Returns:

  • (Symbol)

    The appropriate datastore key for the given AFID token.



58
59
60
# File 'lib/hadley/token_store.rb', line 58

def key_for(token)
  "afid-access-token:#{token}".to_sym
end

#put(token, expires_in, data = {}) ⇒ Boolean?

This method stores the provided AFID identity information under the given AFID token for the duration of time specified by the expires_in argument.

Parameters:

  • token (String)

    The unique token provisioned by the AFID resource server.

  • expires_in (Integer)

    The duration of time (in seconds) that the provided AFID identity information should be stored.

  • data (Hash) (defaults to: {})

    The identity information to be assiciated with the given AFID token.

Returns:

  • (Boolean, nil)

    True if and only if the identity information was stored successfully.



38
39
40
# File 'lib/hadley/token_store.rb', line 38

def put(token, expires_in, data={})
  @store.write(key_for(token), data, expires_in: expires_in)
end