Class: Twilio::REST::TokenAuthStrategy
- Inherits:
-
AuthStrategy
- Object
- AuthStrategy
- Twilio::REST::TokenAuthStrategy
- Defined in:
- lib/twilio-ruby/auth_strategy/token_auth_strategy.rb
Instance Attribute Summary collapse
-
#lock ⇒ Object
Returns the value of attribute lock.
-
#token ⇒ Object
Returns the value of attribute token.
-
#token_manager ⇒ Object
Returns the value of attribute token_manager.
Attributes inherited from AuthStrategy
Instance Method Summary collapse
- #auth_string ⇒ Object
- #fetch_token ⇒ Object
-
#initialize(token_manager) ⇒ TokenAuthStrategy
constructor
A new instance of TokenAuthStrategy.
- #requires_authentication ⇒ Object
- #token_expired? ⇒ Boolean
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
#lock ⇒ Object
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 |
#token ⇒ Object
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_manager ⇒ Object
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_string ⇒ Object
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_token ⇒ Object
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_authentication ⇒ Object
34 35 36 |
# File 'lib/twilio-ruby/auth_strategy/token_auth_strategy.rb', line 34 def requires_authentication true end |
#token_expired? ⇒ 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 |