Class: V0::VirtualAgent::JwtToken

Inherits:
Common::Client::Base show all
Defined in:
app/controllers/v0/virtual_agent/jwt_token.rb

Instance Method Summary collapse

Methods inherited from Common::Client::Base

configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Instance Method Details

#body?(response) ⇒ Boolean (private)

Returns:

  • (Boolean)


33
34
35
# File 'app/controllers/v0/virtual_agent/jwt_token.rb', line 33

def body?(response)
  response&.body && response.body.present?
end

#configObject



19
20
21
# File 'app/controllers/v0/virtual_agent/jwt_token.rb', line 19

def config
  VAOS::Configuration.instance
end

#decoded_token(token) ⇒ Object (private)



29
30
31
# File 'app/controllers/v0/virtual_agent/jwt_token.rb', line 29

def decoded_token(token)
  JWT.decode(token, nil, false).first
end

#headersObject (private)



25
26
27
# File 'app/controllers/v0/virtual_agent/jwt_token.rb', line 25

def headers
  { 'Accept' => 'text/plain', 'Content-Type' => 'text/plain', 'Referer' => referrer }
end

#new_jwt_token(user) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/v0/virtual_agent/jwt_token.rb', line 6

def new_jwt_token(user)
  url = '/users/v2/session?processRules=true'
  # get the basic unsigned JWT token
  token = VAOS::JwtWrapper.new(user).token
  # request a signed JWT token
  response = perform(:post, url, token, headers)
  # raise Common::Exceptions::BackendServiceException.new('VAOS_502', source: self.class) unless body?(response)

  Rails.logger.info('Chatbot JWT session created',
                    { account_uuid: user., jti: decoded_token(token)['jti'] })
  response.body
end

#referrerObject (private)



37
38
39
40
41
42
43
# File 'app/controllers/v0/virtual_agent/jwt_token.rb', line 37

def referrer
  if Settings.hostname.ends_with?('.gov')
    "https://#{Settings.hostname}".gsub('vets', 'va')
  else
    'https://review-instance.va.gov' # VAMF rejects Referer that is not valid; such as those of review instances
  end
end