Skip to content

Commit

Permalink
[#951] create new route for Virtual Agent JWT proof of concept (#12844)
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
aaronyoung-tw authored May 31, 2023
1 parent 15d696c commit 04d681e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
56 changes: 56 additions & 0 deletions app/controllers/v0/virtual_agent_jwt_token_controller.rb
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
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
resources :education_career_counseling_claims, only: :create
resources :veteran_readiness_employment_claims, only: :create
resource :virtual_agent_token, only: [:create], controller: :virtual_agent_token
resource :virtual_agent_jwt_token, only: [:create], controller: :virtual_agent_jwt_token

get 'form1095_bs/download_pdf/:tax_year', to: 'form1095_bs#download_pdf'
get 'form1095_bs/download_txt/:tax_year', to: 'form1095_bs#download_txt'
Expand Down

0 comments on commit 04d681e

Please sign in to comment.