Class: Shutl::Auth::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/shutl/auth/authenticator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Authenticator

Returns a new instance of Authenticator.



6
7
8
9
10
11
12
# File 'lib/shutl/auth/authenticator.rb', line 6

def initialize(options = {})
  Shutl::Auth.logger.debug "building a new authenticator"
  self.cache_key      = options.fetch(:cache_key) { :access_token }
  self.client_id      = options.fetch(:client_id)
  self.client_secret  = options.fetch(:client_secret)
  self.url            = options.fetch(:url)
end

Instance Method Details

#access_tokenObject



14
15
16
17
# File 'lib/shutl/auth/authenticator.rb', line 14

def access_token
  return read_token if read_token
  set_token request_access_token
end

#authenticated_request(&blk) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/shutl/auth/authenticator.rb', line 19

def authenticated_request &blk
  begin
    yield
  rescue Shutl::UnauthorizedAccess => e
    Shutl::Auth.logger.debug "Shutl::UnauthorizedAccess - resetting access token"
    set_token nil
    access_token
    yield
  end
end

#request_access_tokenObject



30
31
32
33
34
35
# File 'lib/shutl/auth/authenticator.rb', line 30

def request_access_token
  return read_token if read_token
  Shutl::Auth.logger.debug "request_access_token: cached? #{!!read_token}"

  AccessTokenRequest.new(opts).access_token
end