Skip to content

Commit

Permalink
[#951] Refactor the Virtual Agent JWT fetch code to use a helper clas…
Browse files Browse the repository at this point in the history
…s. (#12859)

* [#951] Refactor the Virtual Agent JWT fetch code to use a helper class.
[department-of-veterans-affairs/va-virtual-agent#951]

   Co-authored-by: Maurice Okumu <maurice.okumu@thoughtworks.com>

We refactored the code to allow for the extending of the
common::client::base
  • Loading branch information
aaronyoung-tw authored Jun 1, 2023
1 parent b2376d3 commit 4825af0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
46 changes: 46 additions & 0 deletions app/controllers/v0/virtual_agent/jwt_token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

module V0
module VirtualAgent
class JwtToken < Common::Client::Base
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.account_uuid, jti: decoded_token(token)['jti'] })
response.body
end

def config
VAOS::Configuration.instance
end

private

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

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

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

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
end
end
end
37 changes: 1 addition & 36 deletions app/controllers/v0/virtual_agent_jwt_token_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,11 @@ def create
# on a post request, if the flipper is enabled
if Flipper.enabled?(:virtual_agent_fetch_jwt_token, current_user)
# create a new jwt token
jwt_token = new_jwt_token(current_user)
jwt_token = VirtualAgent::JwtToken.new.new_jwt_token(current_user)
render json: { token: jwt_token }
else
render json: { token: 'disabled' }
end
end

private

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.account_uuid, jti: decoded_token(token)['jti'] })
response.body
end

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

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

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

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
end
end

0 comments on commit 4825af0

Please sign in to comment.