Class: RestClient::Resource

Inherits:
Object
  • Object
show all
Includes:
Conjur::Cast, Conjur::Escape, Conjur::LogSource
Defined in:
lib/conjur/api.rb

Instance Method Summary collapse

Methods included from Conjur::LogSource

#log

Methods included from Conjur::Escape

#fully_escape, #path_escape, #query_escape

Constructor Details

#initialize(url, options = nil, &block) ⇒ Resource

Returns a new instance of Resource.



53
54
55
# File 'lib/conjur/api.rb', line 53

def initialize url, options = nil, &block
  initialize_without_defaults url, default_options.merge(options || {}), &block
end

Instance Method Details

#conjur_apiConjur::API

Creates a Conjur API from this resource's authorization header.

The new API is created using the token, so it will not be able to refresh when the token expires (after about 8 minutes). This is equivalent to creating an Conjur::API instance with Conjur::API.new_from_token.

Returns:



87
88
89
# File 'lib/conjur/api.rb', line 87

def conjur_api
  Conjur::API.new_from_token token
end

#default_optionsObject



57
58
59
60
61
# File 'lib/conjur/api.rb', line 57

def default_options
  {
    ssl_cert_store: OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE
  }
end

#initialize_without_defaultsObject



51
# File 'lib/conjur/api.rb', line 51

alias_method :initialize_without_defaults, :initialize

#tokenHash

Get an authentication token from the clients Authorization header.

Useful fields in the token include "data", which holds the username for which the token was issued, and "timestamp", which contains the time at which the token was issued. The token will expire 8 minutes after timestamp, but we recommend you treat the lifespan as about 5 minutes to account for time differences.

Returns:

  • (Hash)

    the parsed authentication token



99
100
101
102
103
104
105
106
# File 'lib/conjur/api.rb', line 99

def token
  authorization = options[:headers][:authorization]
  if authorization && authorization.to_s[/^Token token="(.*)"/]
    JSON.parse(Base64.decode64($1))
  else
    raise AuthorizationError.new("Authorization missing")
  end
end

#usernameString

The username this resource authenticates as.

Returns:

  • (String)

    the username



111
112
113
# File 'lib/conjur/api.rb', line 111

def username
  options[:user] || options[:username]
end