-
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.
Browse files
Browse the repository at this point in the history
* [#951] create new route for Virtual Agent JWT proof of concept [original issue 951](https://github.com/department-of-veterans-affairs/va-virtual-agent/issues/951) Co-authored-by: Maurice Okumu <maurice.okumu@thoughtworks.com>
- Loading branch information
1 parent
15d696c
commit 04d681e
Showing
2 changed files
with
57 additions
and
0 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,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'erb' | ||
|
||
module V0 | ||
class VirtualAgentJwtTokenController < ApplicationController | ||
rescue_from 'V0::VirtualAgentJwtTokenController::ServiceException', with: :service_exception_handler | ||
rescue_from Net::HTTPError, with: :service_exception_handler | ||
|
||
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) | ||
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 |
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