Skip to content

Commit

Permalink
RefreshToken -> AuthToken
Browse files Browse the repository at this point in the history
  • Loading branch information
NigelBreslaw committed Feb 17, 2024
1 parent 47d8db7 commit 1771fda
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions native_gg/src/account/Account.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down Expand Up @@ -80,7 +80,7 @@ export const BungieUserSchema = v.object({

export type BungieUser = v.Output<typeof BungieUserSchema>;

export async function getLinkedProfiles(authToken: RefreshToken, getAllAccounts = false): Promise<JSON> {
export async function getLinkedProfiles(authToken: AuthToken, getAllAccounts = false): Promise<JSON> {
const headers = new Headers();
headers.append("Authorization", `Bearer ${authToken.access_token}`);
headers.append("X-API-Key", apiKey);
Expand Down
19 changes: 9 additions & 10 deletions native_gg/src/authentication/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<AuthAction> | null;
private stateID: string;
private usedAuthCodes: Array<string>;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<RefreshToken | null> {
// TODO: This also needs an async queue to handle multiple requests for the token.
static getTokenAsync(): Promise<AuthToken | null> {
return new Promise((resolve, reject) => {
if (AuthService.instance.authToken) {
/// is the access token valid?
Expand Down Expand Up @@ -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() });
Expand Down Expand Up @@ -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) {
Expand All @@ -246,7 +245,7 @@ class AuthService {
}
}

async buildBungieAccount(authToken: RefreshToken) {
async buildBungieAccount(authToken: AuthToken) {
if (authToken) {
try {
let rawLinkedProfiles = await getLinkedProfiles(authToken);
Expand Down
12 changes: 6 additions & 6 deletions native_gg/src/authentication/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -12,7 +12,7 @@ export const refreshTokenSchema = v.object({
token_type: v.string(),
});

export type RefreshToken = v.Output<typeof refreshTokenSchema>;
export type AuthToken = v.Output<typeof authTokenSchema>;

export function getRefreshToken(bungieCode: string): Promise<JSON> {
const headers = new Headers();
Expand Down Expand Up @@ -45,7 +45,7 @@ export function getRefreshToken(bungieCode: string): Promise<JSON> {
});
}

export function getAccessToken(token: RefreshToken): Promise<RefreshToken> {
export function getAccessToken(token: AuthToken): Promise<AuthToken> {
const headers = new Headers();
headers.append("Content-Type", "application/x-www-form-urlencoded");
headers.append("X-API-Key", apiKey);
Expand All @@ -71,7 +71,7 @@ export function getAccessToken(token: RefreshToken): Promise<RefreshToken> {

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) {
Expand All @@ -86,7 +86,7 @@ export function getAccessToken(token: RefreshToken): Promise<RefreshToken> {
});
}

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;
Expand All @@ -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;
Expand Down

0 comments on commit 1771fda

Please sign in to comment.