Support erroring when the token has expired

This commit is contained in:
Will Hunt 2022-04-14 11:47:31 +01:00
parent a766967a62
commit 4bc9bbbe74
3 changed files with 27 additions and 10 deletions

View File

@ -1,7 +1,7 @@
import qs from "querystring";
import { AdminRoomCommandHandler } from "../AdminRoomCommandHandler"
import { botCommand } from "../BotCommands";
import { CommandError } from "../errors";
import { CommandError, TokenError, TokenErrorCode } from "../errors";
import { GithubInstance } from "./GithubInstance";
import { GitHubOAuthToken } from "./Types";
import LogWrapper from "../LogWrapper";
@ -50,16 +50,23 @@ export class GitHubBotCommands extends AdminRoomCommandHandler {
await this.tokenStore.storeUserToken("github", this.userId, JSON.stringify({access_token: accessToken, token_type: 'pat'} as GitHubOAuthToken));
}
@botCommand("github hastoken", {help: "Check if you have a token stored for GitHub", category: "github"})
public async hasPersonalToken() {
@botCommand("github status", {help: "Check the status of your GitHub authentication", category: "github"})
public async getTokenStatus() {
if (!this.config.github) {
throw new CommandError("no-github-support", "The bridge is not configured with GitHub support.");
}
const result = await this.tokenStore.getUserToken("github", this.userId);
if (result === null) {
await this.sendNotice("You do not currently have a token stored.");
try {
const octokit = await this.tokenStore.getOctokitForUser(this.userId);
if (octokit === null) {
await this.sendNotice("You are not authenticated, please login.");
return;
}
await this.sendNotice("A token is stored for your GitHub account.");
const me = await octokit.users.getAuthenticated();
this.sendNotice(`You are logged in as ${me.data.login}`);
} catch (ex) {
if (ex instanceof TokenError && ex.code === TokenErrorCode.EXPIRED) {
await this.sendNotice("Your authentication is no longer valid, please login again.");
}
}
}
}

View File

@ -155,7 +155,7 @@ export class UserTokenStore {
if (senderToken.expires_in && senderToken.expires_in < date) {
log.info(`GitHub access token for ${userId} has expired ${senderToken.expires_in} < ${date}, attempting refresh`);
if (!this.config.github?.oauth) {
throw Error('GitHub oauth not configured, cannot refresh token');
throw new TokenError(TokenErrorCode.EXPIRED, "GitHub oauth not configured, cannot refresh token");
}
if (senderToken.refresh_token && senderToken.refresh_token_expires_in && senderToken?.refresh_token_expires_in > date) {
// Needs a refresh.
@ -175,7 +175,7 @@ export class UserTokenStore {
} else {
log.error(`GitHub access token for ${userId} has expired, and the refresh token is stale or not given`);
throw Error('Token is expired, cannot refresh');
throw new TokenError(TokenErrorCode.EXPIRED, `GitHub access token for ${userId} has expired, and the refresh token is stale or not given`);
}
}
return senderToken.access_token;

View File

@ -15,3 +15,13 @@ export class ConfigError extends Error {
super(`There was an error in the config (${configPath}): ${msg}`);
}
}
export enum TokenErrorCode {
EXPIRED = "The token has expired."
}
export class TokenError extends Error {
constructor(public readonly code: TokenErrorCode, public readonly innerError: string) {
super(code);
}
}