Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update stale auth tokens #397

Merged
merged 3 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion native_gg/src/authentication/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, isValidAccessToken } from "./Utilities.ts";
import {
BungieUser,
BungieUserSchema,
Expand Down Expand Up @@ -93,6 +93,23 @@ class AuthService {
static getTokenAsync(): Promise<RefreshToken | null> {
return new Promise((resolve, reject) => {
if (AuthService.instance.authToken) {
/// is the access token valid?
const isValid = isValidAccessToken(AuthService.instance.authToken);
if (!isValid) {
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);
Expand Down
8 changes: 4 additions & 4 deletions native_gg/src/authentication/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,29 @@ export function getAccessToken(token: RefreshToken): Promise<RefreshToken> {
});
}

export function hasAccessExpired(token: RefreshToken): boolean {
export function isValidAccessToken(token: RefreshToken): boolean {
// Access lasts 3600 seconds (1 hour)
if (token.time_stamp) {
const lifeTime = token.expires_in;
const timeNow = new Date();
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 isValidRefreshToken(token: RefreshToken): boolean {
// Refresh lasts 7,776,000 seconds (90 days)
if (token.time_stamp) {
const lifeTime = token.refresh_expires_in;
const timeNow = new Date();
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;
Expand Down
Loading