Class: Rails::Vault::JWT::TokenProvider::BaseProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/vault/jwt/token_provider/base_provider.rb

Direct Known Subclasses

RoleID

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_args, bearer_role_name: nil, **_kwargs) ⇒ BaseProvider

Returns a new instance of BaseProvider.



12
13
14
15
# File 'lib/rails/vault/jwt/token_provider/base_provider.rb', line 12

def initialize(*_args, bearer_role_name: nil, **_kwargs)
  @expire_time = DateTime.now
  @bearer_role_name = bearer_role_name || ENV.fetch('VAULT_BEARER_ROLE', '')
end

Instance Attribute Details

#expire_timeObject (readonly)

Returns the value of attribute expire_time.



10
11
12
# File 'lib/rails/vault/jwt/token_provider/base_provider.rb', line 10

def expire_time
  @expire_time
end

Instance Method Details

#authObject



17
# File 'lib/rails/vault/jwt/token_provider/base_provider.rb', line 17

def auth; end

#bearer_tokenObject



19
20
21
22
# File 'lib/rails/vault/jwt/token_provider/base_provider.rb', line 19

def bearer_token
  token
  client.logical.read("identity/oidc/token/#{@bearer_role_name}")&.data[:token]
end

#clientObject



47
48
49
# File 'lib/rails/vault/jwt/token_provider/base_provider.rb', line 47

def client
  @client ||= ::Vault::Client.new
end

#tokenObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/rails/vault/jwt/token_provider/base_provider.rb', line 24

def token
  unless token_valid?
    auth_data = auth
    @token = auth_data.client_token
    @expire_time = DateTime.now + (auth_data.lease_duration / 86_400.0)
    client.token = @token
  end

  @token
end

#token_expired?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rails/vault/jwt/token_provider/base_provider.rb', line 35

def token_expired?
  DateTime.now > @expire_time
end

#token_life_remainingObject



39
40
41
# File 'lib/rails/vault/jwt/token_provider/base_provider.rb', line 39

def token_life_remaining
  (@expire_time - DateTime.now) * 86_400.0
end

#token_valid?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rails/vault/jwt/token_provider/base_provider.rb', line 43

def token_valid?
  !@token.nil? && !token_expired?
end