Fix state bug

This commit is contained in:
Will Hunt 2021-12-01 11:01:37 +00:00
parent d649d872bc
commit dd84f525fc
3 changed files with 18 additions and 9 deletions

View File

@ -416,7 +416,7 @@ export class Bridge {
);
this.queue.on<OAuthRequest>("jira.oauth.response", async (msg) => {
const userId = this.tokenStore.getUserIdForOAuthState(msg.data.state);
const userId = this.tokenStore.getUserIdForOAuthState(msg.data.state, false);
await this.queue.push<boolean>({
data: !!(userId),
sender: "Bridge",
@ -425,13 +425,17 @@ export class Bridge {
});
});
this.queue.on<JiraOAuthResult>("jira.oauth.tokens", async (msg) => {
const userId = this.tokenStore.getUserIdForOAuthState(msg.data.state);
if (!userId) {
log.warn("Could not find admin room for successful tokens request. This shouldn't happen!");
this.queue.on<JiraOAuthResult>("jira.oauth.tokens", async ({data}) => {
if (!data.state) {
log.warn("Missing `state` on `jira.oauth.tokens` event. This shouldn't happen!");
return;
}
await this.tokenStore.storeUserToken("jira", userId, JSON.stringify(msg.data));
const userId = this.tokenStore.getUserIdForOAuthState(data.state);
if (!userId) {
log.warn("Could not find internal state for successful tokens request. This shouldn't happen!");
return;
}
await this.tokenStore.storeUserToken("jira", userId, JSON.stringify(data));
// Some users won't have an admin room and would have gone through provisioning.
const adminRoom = this.adminRooms.get(userId);

View File

@ -20,6 +20,9 @@ export class GitHubBotCommands extends AdminRoomCommandHandler {
if (!this.config.github) {
throw new CommandError("no-github-support", "The bridge is not configured with GitHub support");
}
if (!this.config.github.oauth) {
throw new CommandError("no-github-support", "The bridge is not configured with GitHub OAuth support");
}
const state = this.tokenStore.createStateForOAuth(this.userId);
return this.sendNotice(`To login, open ${generateGitHubOAuthUrl(this.config.github.oauth.client_id, this.config.github.oauth.redirect_uri, state)} to link your account to the bridge`);
}

View File

@ -134,13 +134,15 @@ export class UserTokenStore {
return state;
}
public getUserIdForOAuthState(state: string) {
public getUserIdForOAuthState(state: string, remove = true) {
const result = this.oauthSessionStore.get(state);
if (!result) {
return null;
}
if (remove) {
clearTimeout(result.timeout);
this.oauthSessionStore.delete(state);
}
return result.userId;
}
}