From d051ffb596a50be356ccbcbdbb3f582603200a6c Mon Sep 17 00:00:00 2001 From: Nigel Breslaw Date: Sat, 17 Feb 2024 11:51:47 +0200 Subject: [PATCH 1/3] Update stale access token --- native_gg/src/authentication/AuthService.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/native_gg/src/authentication/AuthService.ts b/native_gg/src/authentication/AuthService.ts index 4ab938643..dc99494a1 100644 --- a/native_gg/src/authentication/AuthService.ts +++ b/native_gg/src/authentication/AuthService.ts @@ -5,7 +5,7 @@ import * as WebBrowser from "expo-web-browser"; import * as v from "valibot"; import { clientID, redirectURL } from "../constants/env.ts"; import { Store } from "../constants/storage.ts"; -import { RefreshToken, refreshTokenSchema, getAccessToken, getRefreshToken } from "./Utilities.ts"; +import { RefreshToken, refreshTokenSchema, getAccessToken, getRefreshToken, hasAccessExpired } from "./Utilities.ts"; import { BungieUser, BungieUserSchema, @@ -93,6 +93,23 @@ class AuthService { static getTokenAsync(): Promise { return new Promise((resolve, reject) => { if (AuthService.instance.authToken) { + /// is the access token valid? + const hasExpired = hasAccessExpired(AuthService.instance.authToken); + if (hasExpired) { + console.log("Token expired"); + getAccessToken(AuthService.instance.authToken) + .then((authToken) => { + const currentUserID = AuthService.instance.currentUserID; + AsyncStorage.setItem(`${currentUserID}${Store._refresh_token}`, JSON.stringify(authToken)); + AuthService.instance.setAuthToken(authToken); + console.log("Got new token"); + return resolve(authToken); + }) + .catch((e) => { + return reject(e); + }); + } + return resolve(AuthService.instance.authToken); } reject(AuthService.instance.authToken); From 39737139f34253e21256053f6d3d950e76e8a0c7 Mon Sep 17 00:00:00 2001 From: Nigel Breslaw Date: Sat, 17 Feb 2024 11:59:32 +0200 Subject: [PATCH 2/3] hasAccessExpired -> isValidAccess --- native_gg/src/authentication/AuthService.ts | 6 +++--- native_gg/src/authentication/Utilities.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/native_gg/src/authentication/AuthService.ts b/native_gg/src/authentication/AuthService.ts index dc99494a1..ad2a3ddb2 100644 --- a/native_gg/src/authentication/AuthService.ts +++ b/native_gg/src/authentication/AuthService.ts @@ -5,7 +5,7 @@ import * as WebBrowser from "expo-web-browser"; import * as v from "valibot"; import { clientID, redirectURL } from "../constants/env.ts"; import { Store } from "../constants/storage.ts"; -import { RefreshToken, refreshTokenSchema, getAccessToken, getRefreshToken, hasAccessExpired } from "./Utilities.ts"; +import { RefreshToken, refreshTokenSchema, getAccessToken, getRefreshToken, isValidAccess } from "./Utilities.ts"; import { BungieUser, BungieUserSchema, @@ -94,8 +94,8 @@ class AuthService { return new Promise((resolve, reject) => { if (AuthService.instance.authToken) { /// is the access token valid? - const hasExpired = hasAccessExpired(AuthService.instance.authToken); - if (hasExpired) { + const isValid = isValidAccess(AuthService.instance.authToken); + if (!isValid) { console.log("Token expired"); getAccessToken(AuthService.instance.authToken) .then((authToken) => { diff --git a/native_gg/src/authentication/Utilities.ts b/native_gg/src/authentication/Utilities.ts index 5ace34c54..09b9c46f8 100644 --- a/native_gg/src/authentication/Utilities.ts +++ b/native_gg/src/authentication/Utilities.ts @@ -86,7 +86,7 @@ export function getAccessToken(token: RefreshToken): Promise { }); } -export function hasAccessExpired(token: RefreshToken): boolean { +export function isValidAccess(token: RefreshToken): boolean { // Access lasts 3600 seconds (1 hour) if (token.time_stamp) { const lifeTime = token.expires_in; @@ -94,13 +94,13 @@ export function hasAccessExpired(token: RefreshToken): boolean { const timeThen = new Date(token.time_stamp); const secondsLeft = lifeTime - (timeNow.getTime() - timeThen.getTime()) / 1000; // Count anything less than 5 mins (345 seconds) as expired - return secondsLeft < 345; + return secondsLeft > 345; } return true; } -export function hasRefreshExpired(token: RefreshToken): boolean { +export function isValidRefresh(token: RefreshToken): boolean { // Refresh lasts 7,776,000 seconds (90 days) if (token.time_stamp) { const lifeTime = token.refresh_expires_in; @@ -108,7 +108,7 @@ export function hasRefreshExpired(token: RefreshToken): boolean { const timeThen = new Date(token.time_stamp); const secondsLeft = lifeTime - (timeNow.getTime() - timeThen.getTime()) / 1000; // Count anything less than 5 mins (345 seconds) as expired - return secondsLeft < 345; + return secondsLeft > 345; } return true; From 36cab0e5fab21df1b3eca83fec37988a5e287893 Mon Sep 17 00:00:00 2001 From: Nigel Breslaw Date: Sat, 17 Feb 2024 12:00:28 +0200 Subject: [PATCH 3/3] isValidAccess -> isValidAccessToken --- native_gg/src/authentication/AuthService.ts | 4 ++-- native_gg/src/authentication/Utilities.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/native_gg/src/authentication/AuthService.ts b/native_gg/src/authentication/AuthService.ts index ad2a3ddb2..e2d913f20 100644 --- a/native_gg/src/authentication/AuthService.ts +++ b/native_gg/src/authentication/AuthService.ts @@ -5,7 +5,7 @@ import * as WebBrowser from "expo-web-browser"; import * as v from "valibot"; import { clientID, redirectURL } from "../constants/env.ts"; import { Store } from "../constants/storage.ts"; -import { RefreshToken, refreshTokenSchema, getAccessToken, getRefreshToken, isValidAccess } from "./Utilities.ts"; +import { RefreshToken, refreshTokenSchema, getAccessToken, getRefreshToken, isValidAccessToken } from "./Utilities.ts"; import { BungieUser, BungieUserSchema, @@ -94,7 +94,7 @@ class AuthService { return new Promise((resolve, reject) => { if (AuthService.instance.authToken) { /// is the access token valid? - const isValid = isValidAccess(AuthService.instance.authToken); + const isValid = isValidAccessToken(AuthService.instance.authToken); if (!isValid) { console.log("Token expired"); getAccessToken(AuthService.instance.authToken) diff --git a/native_gg/src/authentication/Utilities.ts b/native_gg/src/authentication/Utilities.ts index 09b9c46f8..261b9007e 100644 --- a/native_gg/src/authentication/Utilities.ts +++ b/native_gg/src/authentication/Utilities.ts @@ -86,7 +86,7 @@ export function getAccessToken(token: RefreshToken): Promise { }); } -export function isValidAccess(token: RefreshToken): boolean { +export function isValidAccessToken(token: RefreshToken): boolean { // Access lasts 3600 seconds (1 hour) if (token.time_stamp) { const lifeTime = token.expires_in; @@ -100,7 +100,7 @@ export function isValidAccess(token: RefreshToken): boolean { return true; } -export function isValidRefresh(token: RefreshToken): boolean { +export function isValidRefreshToken(token: RefreshToken): boolean { // Refresh lasts 7,776,000 seconds (90 days) if (token.time_stamp) { const lifeTime = token.refresh_expires_in;