mirror of
https://github.com/matrix-org/matrix-hookshot.git
synced 2025-03-10 21:19:13 +00:00
Support erroring when the token has expired
This commit is contained in:
parent
a766967a62
commit
4bc9bbbe74
@ -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.");
|
||||
return;
|
||||
try {
|
||||
const octokit = await this.tokenStore.getOctokitForUser(this.userId);
|
||||
if (octokit === null) {
|
||||
await this.sendNotice("You are not authenticated, please login.");
|
||||
return;
|
||||
}
|
||||
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.");
|
||||
}
|
||||
}
|
||||
await this.sendNotice("A token is stored for your GitHub account.");
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -14,4 +14,14 @@ export class ConfigError extends Error {
|
||||
constructor(public readonly configPath: string, public readonly msg?: string) {
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user