-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#951] Refactor the Virtual Agent JWT fetch code to use a helper clas…
…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
1 parent
b2376d3
commit 4825af0
Showing
2 changed files
with
47 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters