From 1771fdaaee096987a9cf0d06bdcf8a889a79eec0 Mon Sep 17 00:00:00 2001 From: Nigel Breslaw Date: Sat, 17 Feb 2024 12:30:01 +0200 Subject: [PATCH] RefreshToken -> AuthToken --- native_gg/src/account/Account.ts | 4 ++-- native_gg/src/authentication/AuthService.ts | 19 +++++++++---------- native_gg/src/authentication/Utilities.ts | 12 ++++++------ 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/native_gg/src/account/Account.ts b/native_gg/src/account/Account.ts index 4e38fd57c..3f033fac1 100644 --- a/native_gg/src/account/Account.ts +++ b/native_gg/src/account/Account.ts @@ -1,6 +1,6 @@ import * as v from "valibot"; import { apiKey } from "../constants/env.ts"; -import { RefreshToken } from "../authentication/Utilities.ts"; +import { AuthToken } from "../authentication/Utilities.ts"; const PlatformSilverSchema = v.object({ itemHash: v.number(), @@ -80,7 +80,7 @@ export const BungieUserSchema = v.object({ export type BungieUser = v.Output; -export async function getLinkedProfiles(authToken: RefreshToken, getAllAccounts = false): Promise { +export async function getLinkedProfiles(authToken: AuthToken, getAllAccounts = false): Promise { const headers = new Headers(); headers.append("Authorization", `Bearer ${authToken.access_token}`); headers.append("X-API-Key", apiKey); diff --git a/native_gg/src/authentication/AuthService.ts b/native_gg/src/authentication/AuthService.ts index 780688e47..67db513e3 100644 --- a/native_gg/src/authentication/AuthService.ts +++ b/native_gg/src/authentication/AuthService.ts @@ -6,8 +6,8 @@ import * as v from "valibot"; import { clientID, redirectURL } from "../constants/env.ts"; import { Store } from "../constants/storage.ts"; import { - RefreshToken, - refreshTokenSchema, + AuthToken, + authTokenSchema, getAccessToken, getRefreshToken, isValidAccessToken, @@ -24,7 +24,7 @@ import { AuthAction } from "../state/Actions.ts"; class AuthService { private static instance: AuthService; - private authToken: RefreshToken | null; + private authToken: AuthToken | null; private dispatch: React.Dispatch | null; private stateID: string; private usedAuthCodes: Array; @@ -75,7 +75,7 @@ class AuthService { .then((token) => { try { const stringToken = v.parse(v.string(), token); - const validatedToken = v.parse(refreshTokenSchema, JSON.parse(stringToken)); + const validatedToken = v.parse(authTokenSchema, JSON.parse(stringToken)); const isValidRefresh = isValidRefreshToken(validatedToken); if (!isValidRefresh) { @@ -119,9 +119,8 @@ class AuthService { }); } - // TODO: Make this check the current token if valid and if not get a new one and return that. - // This also needs an async queue to handle multiple requests for the token. - static getTokenAsync(): Promise { + // TODO: This also needs an async queue to handle multiple requests for the token. + static getTokenAsync(): Promise { return new Promise((resolve, reject) => { if (AuthService.instance.authToken) { /// is the access token valid? @@ -184,7 +183,7 @@ class AuthService { } } - setAuthToken(token: RefreshToken | null) { + setAuthToken(token: AuthToken | null) { this.authToken = token; if (this.dispatch) { this.dispatch({ type: "setAuthenticated", payload: this.isAuthenticated() }); @@ -232,7 +231,7 @@ class AuthService { // If this fails the user needs to auth again. It isn't safe to retry as it can result in 'invalid_grand'. const initialJSONToken = await getRefreshToken(code); try { - const validatedToken = v.parse(refreshTokenSchema, initialJSONToken); + const validatedToken = v.parse(authTokenSchema, initialJSONToken); const fullToken = await getAccessToken(validatedToken); this.buildBungieAccount(fullToken); } catch (e) { @@ -246,7 +245,7 @@ class AuthService { } } - async buildBungieAccount(authToken: RefreshToken) { + async buildBungieAccount(authToken: AuthToken) { if (authToken) { try { let rawLinkedProfiles = await getLinkedProfiles(authToken); diff --git a/native_gg/src/authentication/Utilities.ts b/native_gg/src/authentication/Utilities.ts index 261b9007e..349a7a1cf 100644 --- a/native_gg/src/authentication/Utilities.ts +++ b/native_gg/src/authentication/Utilities.ts @@ -2,7 +2,7 @@ import * as base64 from "base-64"; import * as v from "valibot"; import { apiKey, clientID, clientSecret } from "../constants/env.ts"; -export const refreshTokenSchema = v.object({ +export const authTokenSchema = v.object({ access_token: v.string(), expires_in: v.number(), membership_id: v.string(), @@ -12,7 +12,7 @@ export const refreshTokenSchema = v.object({ token_type: v.string(), }); -export type RefreshToken = v.Output; +export type AuthToken = v.Output; export function getRefreshToken(bungieCode: string): Promise { const headers = new Headers(); @@ -45,7 +45,7 @@ export function getRefreshToken(bungieCode: string): Promise { }); } -export function getAccessToken(token: RefreshToken): Promise { +export function getAccessToken(token: AuthToken): Promise { const headers = new Headers(); headers.append("Content-Type", "application/x-www-form-urlencoded"); headers.append("X-API-Key", apiKey); @@ -71,7 +71,7 @@ export function getAccessToken(token: RefreshToken): Promise { response.json().then((rawToken) => { try { - const validatedToken = v.parse(refreshTokenSchema, rawToken); + const validatedToken = v.parse(authTokenSchema, rawToken); validatedToken.time_stamp = new Date().toISOString(); resolve(validatedToken); } catch (error) { @@ -86,7 +86,7 @@ export function getAccessToken(token: RefreshToken): Promise { }); } -export function isValidAccessToken(token: RefreshToken): boolean { +export function isValidAccessToken(token: AuthToken): boolean { // Access lasts 3600 seconds (1 hour) if (token.time_stamp) { const lifeTime = token.expires_in; @@ -100,7 +100,7 @@ export function isValidAccessToken(token: RefreshToken): boolean { return true; } -export function isValidRefreshToken(token: RefreshToken): boolean { +export function isValidRefreshToken(token: AuthToken): boolean { // Refresh lasts 7,776,000 seconds (90 days) if (token.time_stamp) { const lifeTime = token.refresh_expires_in;