Class: Virgil::Jwt::CachingJwtProvider
- Inherits:
-
AccessTokenProvider
- Object
- AccessTokenProvider
- Virgil::Jwt::CachingJwtProvider
- Defined in:
- lib/virgil/jwt/caching_jwt_provider.rb
Overview
Provides an opportunity to get cached access token or renew it using callback mechanism.
Constant Summary collapse
- @@mutex =
Mutex.new
Instance Attribute Summary collapse
-
#renew_access_token_proc ⇒ Proc
readonly
Callback, that takes an instance of [TokenContext] and returns string representation of generated instance of [AccessToken].
Instance Method Summary collapse
-
#get_token(token_context) ⇒ Object
Gets cached or renewed access token.
-
#initialize(obtain_token_proc) ⇒ CachingJwtProvider
constructor
A new instance of CachingJwtProvider.
Constructor Details
#initialize(obtain_token_proc) ⇒ CachingJwtProvider
Returns a new instance of CachingJwtProvider.
49 50 51 52 |
# File 'lib/virgil/jwt/caching_jwt_provider.rb', line 49 def initialize(obtain_token_proc) Validation.check_type_argument!(Proc, obtain_token_proc) @renew_access_token_proc = obtain_token_proc end |
Instance Attribute Details
#renew_access_token_proc ⇒ Proc (readonly)
Callback, that takes an instance of [TokenContext] and returns string representation of generated instance of [AccessToken]
46 47 48 |
# File 'lib/virgil/jwt/caching_jwt_provider.rb', line 46 def renew_access_token_proc @renew_access_token_proc end |
Instance Method Details
#get_token(token_context) ⇒ Object
Gets cached or renewed access token.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/virgil/jwt/caching_jwt_provider.rb', line 57 def get_token(token_context) Validation.check_type_argument!(TokenContext, token_context) if !@jwt || (@jwt.body_content.expires_at <= Time.at(Time.now.utc.to_i + 5).utc) @@mutex.synchronize { jwt_str = @renew_access_token_proc.call(token_context) @jwt = Jwt.from(jwt_str) } end @jwt end |