From e19cf35de50d712d005560a8e36a617d0480d773 Mon Sep 17 00:00:00 2001 From: Alan Bridgeman Date: Sat, 31 Jan 2026 17:11:57 -0600 Subject: [PATCH] Added 'inactive' state handling for OAuth apps --- package.json | 2 +- src/oauth/OAuthApp.ts | 52 +++++++++++++++++++++++++++++++++++++++++-- yarn.lock | 8 +++---- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 155c91b..7cdf613 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "create-ba-web-app": "node ./bin/create-project.js" }, "dependencies": { - "@BridgemanAccessible/ba-auth": "^1.0.24", + "@BridgemanAccessible/ba-auth": "^1.0.25", "@BridgemanAccessible/ba-logging": "^1.0.1", "express": "^4.19.2", "fs-extra": "^11.2.0", diff --git a/src/oauth/OAuthApp.ts b/src/oauth/OAuthApp.ts index f942084..46efcad 100644 --- a/src/oauth/OAuthApp.ts +++ b/src/oauth/OAuthApp.ts @@ -1,4 +1,5 @@ -import type { Application } from 'express'; +import axios from 'axios'; + import { Scopes } from '@BridgemanAccessible/ba-auth'; import Client from '@BridgemanAccessible/ba-auth/client'; import type { OnAuthCallback } from '@BridgemanAccessible/ba-auth/client'; @@ -221,7 +222,54 @@ export class OAuthApp extend auth_default_response_mode: this.options.auth_default_response_mode, client_secret: this.options.client_secret } - ); + ) + + // If the app is "inactive" (in a "pending" state etc...), we want to try to wait for it to become active + // We use a progress backoff strategy to avoid hammering the server with requests + // Because this is largely based on human intervention, exponentiation of the index by 5 (minutes), to a maximum of about 13 hours seemed reasonable + let active = this.client.isActive(); + if(!active) { + for(let j = 0;j < 5 && !active;j++) { + // Sleep for: + // 0: ! Minute + // 1: 5 Minutes + // 2: 25 Minutes + // 3: 125 Minutes (~2 hours) + // 4: 625 Minutes (~10 hours) + // Total of ~13 hours (~780 minutes) + logMessage(`Waiting for OAuth client to become active... (waited ${j > 0 ? 60 /* 1 minute */ * (5 ** (j - 1)) / 60 : 0} minutes so far). Now waiting for ${60 /* 1 minute */ * (5 ** (j)) / 60} minutes...`, LogLevel.WARN); + await new Promise(resolve => setTimeout(resolve, 1000 /* 1 second */ * 60 /* 1 minute */ + 1000 /* 1 second */ * 60 /* 1 minute */ * (5 ** j) /* Exponential backoff */)); + + await axios.get( + `https://account.bridgemanaccessible.ca/api/v1/apps/${encodeURIComponent(baseAppUrl.toString())}/status`, + { + headers: { + Accept: 'application/json' + } + } + ) + .then((response) => { + if(response.status === 200) { + const data = response.data; + + if(data.active === true) { + logMessage('OAuth client is now active!', LogLevel.INFO); + + active = true; + } + else { + logMessage('OAuth client is still not active.', LogLevel.DEBUG); + } + } + else { + logMessage(`Received non-200 response when checking OAuth client status: ${response.status}`, LogLevel.ERROR); + } + }) + .catch((error) => { + logMessage(`Error checking OAuth client status: ${error}`, LogLevel.ERROR); + }); + } + } this.client.getSetupRoutes().forEach((route) => { logMessage(`Adding outside framework route: ${route}`, LogLevel.DEBUG); diff --git a/yarn.lock b/yarn.lock index 27b8584..6e939ed 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,10 +2,10 @@ # yarn lockfile v1 -"@BridgemanAccessible/ba-auth@^1.0.24": - version "1.0.24" - resolved "https://npm.pkg.bridgemanaccessible.ca/@BridgemanAccessible/ba-auth/-/ba-auth-1.0.24.tgz#9e60d203bad6c721c03e2e2025d73d450a103e2d" - integrity sha512-AP6lFk+QWBIPfrVfGAe7P+m7CdpO4CWyqiE4JUmNeS3X93nnDsSvhFyfFBAMtF7XC3IuGQOwk0Y3Pj/rZmzwDw== +"@BridgemanAccessible/ba-auth@^1.0.25": + version "1.0.25" + resolved "https://npm.pkg.bridgemanaccessible.ca/@BridgemanAccessible/ba-auth/-/ba-auth-1.0.25.tgz#df74fa262d0ba21a5f8980396fbdb09478db52d7" + integrity sha512-ez2Z5TQGxHZZ/ABETt9qIsAKXbbRyj81Fpy7yY7i6OEHsyNyb2BYSXEwYjrXhfptAHqYqfbhXKVlQRv+/2tkrg== dependencies: "@BridgemanAccessible/ba-logging" "^1.0.1" "@azure/identity" "^4.0.1"