From d68f9eebdf80ed787162b9b7f29ca5dc65ff8bca Mon Sep 17 00:00:00 2001 From: Matt Gardner Date: Thu, 2 May 2024 20:37:29 -0400 Subject: [PATCH 1/3] Refresh token when it expires --- .../api/argyle/tokens_controller.rb | 25 +++++++++++++++++++ .../controllers/cbv_flows_controller.js | 4 +-- app/javascript/utilities/argyle.js | 8 ++++++ config/routes.rb | 6 +++++ spec/requests/api/argyle/tokens_spec.rb | 7 ++++++ 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 app/controllers/api/argyle/tokens_controller.rb create mode 100644 spec/requests/api/argyle/tokens_spec.rb diff --git a/app/controllers/api/argyle/tokens_controller.rb b/app/controllers/api/argyle/tokens_controller.rb new file mode 100644 index 000000000..b7a350e9a --- /dev/null +++ b/app/controllers/api/argyle/tokens_controller.rb @@ -0,0 +1,25 @@ +class Api::Argyle::TokensController < ApplicationController + skip_before_action :verify_authenticity_token + + USER_TOKEN_ENDPOINT = 'https://api-sandbox.argyle.com/v2/user-tokens'; + + def create + cbv_flow = CbvFlow.find(session[:cbv_flow_id]) + new_token = refresh_token(cbv_flow.argyle_user_id) + + render json: { status: :ok, token: new_token['user_token'] } + end + + def refresh_token(argyle_user_id) + res = Net::HTTP.post( + URI.parse(USER_TOKEN_ENDPOINT), + { "user": argyle_user_id }.to_json, + { + "Authorization" => "Basic #{Rails.application.credentials.argyle[:api_key]}", + "Content-Type" => "application/json" + } + ) + + JSON.parse(res.body) + end +end diff --git a/app/javascript/controllers/cbv_flows_controller.js b/app/javascript/controllers/cbv_flows_controller.js index cfa3f4003..3c7f65cbf 100644 --- a/app/javascript/controllers/cbv_flows_controller.js +++ b/app/javascript/controllers/cbv_flows_controller.js @@ -2,7 +2,7 @@ import { Controller } from "@hotwired/stimulus" import * as ActionCable from '@rails/actioncable' import metaContent from "../utilities/meta"; -import { loadArgyle, initializeArgyle } from "../utilities/argyle" +import { loadArgyle, initializeArgyle, updateToken } from "../utilities/argyle" function toOptionHTML({ value }) { return ``; @@ -76,7 +76,7 @@ export default class extends Controller { // Unsure what these are for! onDDSSuccess: () => { console.log('onDDSSuccess') }, onDDSError: () => { console.log('onDDSSuccess') }, - onTokenExpired: updateToken => { console.log('onTokenExpired') } + onTokenExpired: updateToken, })) .then(argyle => this.argyle = argyle) .then(() => this.argyle.open()); diff --git a/app/javascript/utilities/argyle.js b/app/javascript/utilities/argyle.js index f0835886c..02e13cbf7 100644 --- a/app/javascript/utilities/argyle.js +++ b/app/javascript/utilities/argyle.js @@ -1,6 +1,8 @@ import loadScript from 'load-script'; import metaContent from "./meta"; +const ARGYLE_TOKENS_REFRESH = '/api/argyle/tokens'; + export function loadArgyle() { return new Promise((resolve, reject) => { loadScript('https://plugin.argyle.com/argyle.web.v5.js', (err, script) => { @@ -20,3 +22,9 @@ export function initializeArgyle(Argyle, userToken, callbacks) { ...callbacks }); } + +export const updateToken = async updateToken => { + const response = await fetch(ARGYLE_TOKENS_REFRESH).then(response => response.json()); + + updateToken(response.token); +} diff --git a/config/routes.rb b/config/routes.rb index 99fe445dc..2547cc6e9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -27,4 +27,10 @@ resources :events, only: :create end end + + namespace :api do + namespace :argyle do + resources :tokens, only: :create + end + end end diff --git a/spec/requests/api/argyle/tokens_spec.rb b/spec/requests/api/argyle/tokens_spec.rb new file mode 100644 index 000000000..7d773c9fc --- /dev/null +++ b/spec/requests/api/argyle/tokens_spec.rb @@ -0,0 +1,7 @@ +require 'rails_helper' + +RSpec.describe "Api::Argyle::Tokens", type: :request do + describe "GET /index" do + pending "add some examples (or delete) #{__FILE__}" + end +end From 96734c2e546ddef89fd094fefa0727bd64bd95c9 Mon Sep 17 00:00:00 2001 From: Matt Gardner Date: Thu, 2 May 2024 20:58:19 -0400 Subject: [PATCH 2/3] Simple controller --- .../{argyle/tokens_controller.rb => argyle_controller.rb} | 6 ++++-- config/routes.rb | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) rename app/controllers/api/{argyle/tokens_controller.rb => argyle_controller.rb} (88%) diff --git a/app/controllers/api/argyle/tokens_controller.rb b/app/controllers/api/argyle_controller.rb similarity index 88% rename from app/controllers/api/argyle/tokens_controller.rb rename to app/controllers/api/argyle_controller.rb index b7a350e9a..795f7e375 100644 --- a/app/controllers/api/argyle/tokens_controller.rb +++ b/app/controllers/api/argyle_controller.rb @@ -1,15 +1,17 @@ -class Api::Argyle::TokensController < ApplicationController +class Api::ArgyleController < ApplicationController skip_before_action :verify_authenticity_token USER_TOKEN_ENDPOINT = 'https://api-sandbox.argyle.com/v2/user-tokens'; - def create + def update_token cbv_flow = CbvFlow.find(session[:cbv_flow_id]) new_token = refresh_token(cbv_flow.argyle_user_id) render json: { status: :ok, token: new_token['user_token'] } end + private + def refresh_token(argyle_user_id) res = Net::HTTP.post( URI.parse(USER_TOKEN_ENDPOINT), diff --git a/config/routes.rb b/config/routes.rb index 2547cc6e9..6e6b07b92 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,8 +29,8 @@ end namespace :api do - namespace :argyle do - resources :tokens, only: :create + scope :argyle do + post '/tokens' => 'argyle#update_token' end end end From f2eda70e4e115ef589ba448a888aaf787c990574 Mon Sep 17 00:00:00 2001 From: Matt Gardner Date: Thu, 2 May 2024 21:08:17 -0400 Subject: [PATCH 3/3] Post it --- app/javascript/utilities/argyle.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/javascript/utilities/argyle.js b/app/javascript/utilities/argyle.js index 02e13cbf7..7a2347208 100644 --- a/app/javascript/utilities/argyle.js +++ b/app/javascript/utilities/argyle.js @@ -24,7 +24,7 @@ export function initializeArgyle(Argyle, userToken, callbacks) { } export const updateToken = async updateToken => { - const response = await fetch(ARGYLE_TOKENS_REFRESH).then(response => response.json()); + const response = await fetch(ARGYLE_TOKENS_REFRESH, { method: 'post' }).then(response => response.json()); updateToken(response.token); }