Skip to content

Commit

Permalink
fix(isTokenValid): added try catch to validation method
Browse files Browse the repository at this point in the history
  • Loading branch information
LennardZieglerCONPORT committed Jun 21, 2024
1 parent a4906c9 commit d3b6ccd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"build": "webpack --mode production",
"build:dev": "webpack --mode development",
"dev-server": "webpack serve --mode development",
"dev-server-local": "webpack serve --mode development --host localhost --allowed-hosts all",
"lint": "office-addin-lint check",
"lint:fix": "office-addin-lint fix",
"prettier": "office-addin-lint prettier",
Expand Down
34 changes: 26 additions & 8 deletions src/wireAuthorize/wireAuthorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function fetchWithAuthorizeDialog(url: string | URL, options: Reque
"Authorization failed.",
Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage
);

throw new Error("Authorization failed");
}
} else if (!response.ok) {
Expand Down Expand Up @@ -161,7 +161,7 @@ export async function revokeOauthToken(): Promise<boolean> {
const payload = {
refresh_token: refreshToken,
client_id: config.clientId,
}
};

const response = await fetch(new URL("/oauth/revoke", config.apiBaseUrl), {
method: "POST",
Expand All @@ -180,14 +180,32 @@ export async function revokeOauthToken(): Promise<boolean> {
}

export function isTokenValid(token: string): boolean {
if (token) {
const decodedToken = jwt_decode<DecodedToken>(token);
const currentDate = new Date();
const currentTime = currentDate.getTime();
return decodedToken.exp * 1000 > currentTime;
// null-check
if (!token) {
console.error("isTokenValid: token was null", token);
return false;
}

// decode token
let decodedToken: DecodedToken;
try {
decodedToken = jwt_decode<DecodedToken>(token);
} catch (err) {
console.error("isTokenValid: error decoding token", err);
return false;
}

// check token
let result: boolean;
try {
result = decodedToken.exp * 1000 > new Date().getTime();
} catch (err) {
console.error("isTokenValid: error checking token validity");
return false;
}

return false;
// return if token is valid
return result;
}

function isLoggedIn(): boolean {
Expand Down

0 comments on commit d3b6ccd

Please sign in to comment.