Update auth URL handling

This commit is contained in:
Travis Ralston 2021-12-03 14:49:31 -07:00
parent ba51f0db1b
commit 61bae5d1cb
8 changed files with 64 additions and 14 deletions

View File

@ -2,7 +2,12 @@ import { Context, DELETE, GET, Path, PathParam, POST, Security, ServiceContext }
import { ApiError } from "../ApiError";
import { LogService } from "matrix-bot-sdk";
import { ROLE_USER } from "../security/MatrixSecurity";
import { HookshotGithubOrg, HookshotGithubRepo, HookshotGithubRoomConfig } from "../../bridges/models/hookshot";
import {
HookshotGithubAuthUrls,
HookshotGithubOrg,
HookshotGithubRepo,
HookshotGithubRoomConfig
} from "../../bridges/models/hookshot";
import { HookshotGithubBridge } from "../../bridges/HookshotGithubBridge";
interface BridgeRoomRequest {
@ -22,13 +27,13 @@ export class DimensionHookshotGithubService {
@GET
@Path("auth")
@Security(ROLE_USER)
public async getAuthUrl(): Promise<{ authUrl: string }> {
public async getAuthUrl(): Promise<HookshotGithubAuthUrls> {
const userId = this.context.request.user.userId;
try {
const hookshot = new HookshotGithubBridge(userId);
const authUrl = await hookshot.getAuthUrl();
return {authUrl};
const authUrls = await hookshot.getAuthUrls();
return authUrls;
} catch (e) {
LogService.error("DimensionHookshotGithubService", e);
throw new ApiError(400, "Error getting auth info");

View File

@ -1,5 +1,11 @@
import HookshotGithubBridgeRecord from "../db/models/HookshotGithubBridgeRecord";
import { HookshotGithubRepo, HookshotGithubRoomConfig, HookshotGithubUserInfo, HookshotTypes } from "./models/hookshot";
import {
HookshotGithubAuthUrls,
HookshotGithubRepo,
HookshotGithubRoomConfig,
HookshotGithubUserInfo,
HookshotTypes
} from "./models/hookshot";
import { HookshotBridge } from "./HookshotBridge";
export class HookshotGithubBridge extends HookshotBridge {
@ -16,9 +22,9 @@ export class HookshotGithubBridge extends HookshotBridge {
return bridges[0];
}
public async getAuthUrl(): Promise<string> {
public async getAuthUrls(): Promise<HookshotGithubAuthUrls> {
const bridge = await this.getDefaultBridge();
return this.doProvisionRequest(bridge, "GET", `/v1/github/oauth`).then(r => r['url']);
return this.doProvisionRequest(bridge, "GET", `/v1/github/oauth`).then(r => ({userUrl: r['user_url'], orgUrl: r['org_url']}));
}
public async getBotUserId(): Promise<string> {

View File

@ -49,6 +49,11 @@ export interface HookshotGithubUserInfo {
organisations?: HookshotGithubOrg[];
}
export interface HookshotGithubAuthUrls {
userUrl: string;
orgUrl: string;
}
export enum SupportedGithubRepoEventType {
IssueCreated = "issue.created",
IssueChanged = "issue.changed",

View File

@ -22,7 +22,15 @@
<img src="/assets/img/avatars/github.png" width="35" /> {{'Sign in with GitHub' | translate}}
</a>
</div>
<div *ngIf="!isBridged && !authUrl">
<div *ngIf="!isBridged && orgAuthUrl">
<p>
{{'Almost there! Just need to add the bot to your organizations now.' | translate}}
</p>
<a [href]="authUrl" rel="noopener" target="_blank" class="btn btn-lg btn-link">
<img src="/assets/img/avatars/github.png" width="35" /> {{'Add to GitHub' | translate}}
</a>
</div>
<div *ngIf="!isBridged && !authUrl && !orgAuthUrl">
<label class="label-block">
{{'Organization' | translate}}
<select class="form-control form-control-sm" [(ngModel)]="orgId"

View File

@ -19,6 +19,7 @@ export class HookshotGithubBridgeConfigComponent extends BridgeComponent<Hooksho
public isBusy: boolean;
public authUrl: SafeUrl;
public orgAuthUrl: SafeUrl;
public loadingConnections = true;
public bridgedRepoSlug: string;
@ -44,9 +45,23 @@ export class HookshotGithubBridgeConfigComponent extends BridgeComponent<Hooksho
private tryLoadOrgs() {
this.hookshot.getOrgs().then(r => {
console.log(r);
this.authUrl = null;
if (r.length <= 0) {
this.hookshot.getAuthUrls().then(urls => {
console.log(urls);
this.orgAuthUrl = this.sanitizer.bypassSecurityTrustResourceUrl(urls.orgUrl);
this.loadingConnections = false;
this.timerId = setTimeout(() => {
this.tryLoadOrgs();
}, 1000);
});
return;
}
this.orgs = r.map(o => o.name);
this.orgId = this.orgs[0];
this.orgAuthUrl = null;
this.loadRepos();
if (this.timerId) {
@ -54,8 +69,8 @@ export class HookshotGithubBridgeConfigComponent extends BridgeComponent<Hooksho
}
}).catch(e => {
if (e.status === 403 && e.error.dim_errcode === "T2B_NOT_LOGGED_IN") {
this.hookshot.getAuthUrl().then(url => {
this.authUrl = this.sanitizer.bypassSecurityTrustResourceUrl(url);
this.hookshot.getAuthUrls().then(urls => {
this.authUrl = this.sanitizer.bypassSecurityTrustResourceUrl(urls.userUrl);
this.loadingConnections = false;
this.timerId = setTimeout(() => {
this.tryLoadOrgs();

View File

@ -50,6 +50,7 @@ export class HookshotJiraBridgeConfigComponent extends BridgeComponent<HookshotC
private tryLoadInstances() {
this.hookshot.getInstances().then(r => {
this.authUrl = null;
this.instances = r;
this.instance = this.instances[0];
this.loadProjects();

View File

@ -26,3 +26,8 @@ export interface FE_HookshotGithubRepo {
avatarUrl: string;
description: string;
}
export interface FE_HookshotGithubAuthUrls {
userUrl: string;
orgUrl: string;
}

View File

@ -1,7 +1,12 @@
import { Injectable } from "@angular/core";
import { AuthedApi } from "../authed-api";
import { HttpClient } from "@angular/common/http";
import { FE_HookshotGithubConnection, FE_HookshotGithubOrg, FE_HookshotGithubRepo } from "../../models/hookshot_github";
import {
FE_HookshotGithubAuthUrls,
FE_HookshotGithubConnection,
FE_HookshotGithubOrg,
FE_HookshotGithubRepo
} from "../../models/hookshot_github";
@Injectable()
export class HookshotGithubApiService extends AuthedApi {
@ -19,8 +24,8 @@ export class HookshotGithubApiService extends AuthedApi {
return this.authedDelete("/api/v1/dimension/hookshot/github/room/" + roomId + "/connections/all").toPromise();
}
public getAuthUrl(): Promise<string> {
return this.authedGet("/api/v1/dimension/hookshot/github/auth").toPromise().then(r => r['authUrl']);
public getAuthUrls(): Promise<FE_HookshotGithubAuthUrls> {
return this.authedGet<FE_HookshotGithubAuthUrls>("/api/v1/dimension/hookshot/github/auth").toPromise();
}
public getOrgs(): Promise<FE_HookshotGithubOrg[]> {