Class: Twilio::REST::TokenAuthStrategy

Inherits:
AuthStrategy show all
Defined in:
lib/twilio-ruby/auth_strategy/token_auth_strategy.rb

Instance Attribute Summary collapse

Attributes inherited from AuthStrategy

#auth_type

Instance Method Summary collapse

Constructor Details

#initialize(token_manager) ⇒ TokenAuthStrategy

Returns a new instance of TokenAuthStrategy.



9
10
11
12
13
14
# File 'lib/twilio-ruby/auth_strategy/token_auth_strategy.rb', line 9

def initialize(token_manager)
  super(AuthType::ORGS_TOKEN)
  @token = nil
  @token_manager = token_manager
  @lock = Mutex.new
end

Instance Attribute Details

#lockObject

Returns the value of attribute lock.



7
8
9
# File 'lib/twilio-ruby/auth_strategy/token_auth_strategy.rb', line 7

def lock
  @lock
end

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/twilio-ruby/auth_strategy/token_auth_strategy.rb', line 7

def token
  @token
end

#token_managerObject

Returns the value of attribute token_manager.



7
8
9
# File 'lib/twilio-ruby/auth_strategy/token_auth_strategy.rb', line 7

def token_manager
  @token_manager
end

Instance Method Details

#auth_stringObject



16
17
18
19
# File 'lib/twilio-ruby/auth_strategy/token_auth_strategy.rb', line 16

def auth_string
  token = fetch_token
  "Bearer #{token}"
end

#fetch_tokenObject



21
22
23
24
25
26
# File 'lib/twilio-ruby/auth_strategy/token_auth_strategy.rb', line 21

def fetch_token
  @lock.synchronize do
    @token = @token_manager.fetch_access_token if @token.nil? || token_expired? || @token == ''
    return @token
  end
end

#requires_authenticationObject



34
35
36
# File 'lib/twilio-ruby/auth_strategy/token_auth_strategy.rb', line 34

def requires_authentication
  true
end

#token_expired?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/twilio-ruby/auth_strategy/token_auth_strategy.rb', line 28

def token_expired?
  decoded_token = ::JWT.decode(@token, nil, false)
  exp = decoded_token[0]['exp']
  Time.at(exp) < Time.now
end