class Virgil::Jwt::CachingJwtProvider

Provides an opportunity to get cached access token or renew it using callback mechanism.

Attributes

jwt[R]
renew_access_token_proc[R]

Callback, that takes an instance of [TokenContext] and returns string representation of generated instance of [AccessToken] @return [Proc]

Public Class Methods

new(obtain_token_proc) click to toggle source
# 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

Public Instance Methods

get_token(token_context) click to toggle source

Gets cached or renewed access token. @param token_context an instance of [TokenContext] @return The instance of [AccessToken]

# 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