handleCallback returns connection if created for USER_CONNECTIONS_UPDATE

This commit is contained in:
Madeline 2022-12-23 12:44:04 +11:00 committed by Puyodead1
parent 5c682137b2
commit a4961800d7
No known key found for this signature in database
GPG Key ID: A4FA4FEC0DD353FC
5 changed files with 25 additions and 26 deletions

1
package-lock.json generated
View File

@ -46,7 +46,6 @@
"probe-image-size": "^7.2.3", "probe-image-size": "^7.2.3",
"proxy-agent": "^5.0.0", "proxy-agent": "^5.0.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"sqlite3": "^5.1.5",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"tslib": "^2.4.1", "tslib": "^2.4.1",
"typeorm": "^0.3.10", "typeorm": "^0.3.10",

View File

@ -1,11 +1,11 @@
import { Request, Response, Router } from "express"; import { route } from "@fosscord/api";
import { import {
ConnectionCallbackSchema, ConnectionCallbackSchema,
ConnectionStore,
emitEvent, emitEvent,
FieldErrors, FieldErrors,
} from "../../../../util"; } from "@fosscord/util";
import { ConnectionStore } from "../../../../util/connections"; import { Request, Response, Router } from "express";
import { route } from "../../../util";
const router = Router(); const router = Router();
@ -36,15 +36,16 @@ router.post(
const body = req.body as ConnectionCallbackSchema; const body = req.body as ConnectionCallbackSchema;
const userId = connection.getUserId(body.state); const userId = connection.getUserId(body.state);
const emit = await connection.handleCallback(body); const connectedAccnt = await connection.handleCallback(body);
// whether we should emit a connections update event, only used when a connection doesnt already exist // whether we should emit a connections update event, only used when a connection doesnt already exist
if (emit) if (connectedAccnt)
emitEvent({ emitEvent({
event: "USER_CONNECTIONS_UPDATE", event: "USER_CONNECTIONS_UPDATE",
data: {}, data: connectedAccnt,
user_id: userId, user_id: userId,
}); });
res.sendStatus(204); res.sendStatus(204);
}, },
); );

View File

@ -1,5 +1,5 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import { Config, ConnectionCallbackSchema, DiscordApiErrors } from "../../util"; import { Config, ConnectedAccount, ConnectionCallbackSchema, DiscordApiErrors } from "../../util";
import Connection from "../../util/connections/Connection"; import Connection from "../../util/connections/Connection";
import { ConnectionLoader } from "../../util/connections/ConnectionLoader"; import { ConnectionLoader } from "../../util/connections/ConnectionLoader";
import { BattleNetSettings } from "./BattleNetSettings"; import { BattleNetSettings } from "./BattleNetSettings";
@ -46,8 +46,7 @@ export default class BattleNetConnection extends Connection {
// TODO: probably shouldn't rely on cdn as this could be different from what we actually want. we should have an api endpoint setting. // TODO: probably shouldn't rely on cdn as this could be different from what we actually want. we should have an api endpoint setting.
url.searchParams.append( url.searchParams.append(
"redirect_uri", "redirect_uri",
`${ `${Config.get().cdn.endpointPrivate || "http://localhost:3001"
Config.get().cdn.endpointPrivate || "http://localhost:3001"
}/connections/${this.id}/callback`, }/connections/${this.id}/callback`,
); );
url.searchParams.append("scope", this.scopes.join(" ")); url.searchParams.append("scope", this.scopes.join(" "));
@ -75,9 +74,8 @@ export default class BattleNetConnection extends Connection {
code: code, code: code,
client_id: this.settings.clientId!, client_id: this.settings.clientId!,
client_secret: this.settings.clientSecret!, client_secret: this.settings.clientSecret!,
redirect_uri: `${ redirect_uri: `${Config.get().cdn.endpointPrivate || "http://localhost:3001"
Config.get().cdn.endpointPrivate || "http://localhost:3001" }/connections/${this.id}/callback`,
}/connections/${this.id}/callback`,
}), }),
}) })
.then((res) => res.json()) .then((res) => res.json())
@ -108,21 +106,21 @@ export default class BattleNetConnection extends Connection {
}); });
} }
async handleCallback(params: ConnectionCallbackSchema): Promise<boolean> { async handleCallback(params: ConnectionCallbackSchema): Promise<ConnectedAccount | null> {
const userId = this.getUserId(params.state); const userId = this.getUserId(params.state);
const token = await this.exchangeCode(params.state, params.code!); const token = await this.exchangeCode(params.state, params.code!);
const userInfo = await this.getUser(token); const userInfo = await this.getUser(token);
const exists = await this.hasConnection(userId, userInfo.id.toString()); const exists = await this.hasConnection(userId, userInfo.id.toString());
if (exists) return false; if (exists) return null;
await this.createConnection({
return await this.createConnection({
user_id: userId, user_id: userId,
external_id: userInfo.id.toString(), external_id: userInfo.id.toString(),
friend_sync: params.friend_sync, friend_sync: params.friend_sync,
name: userInfo.battletag, name: userInfo.battletag,
type: this.id, type: this.id,
}); });
return true;
} }
} }

View File

@ -1,5 +1,5 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import { Config, ConnectionCallbackSchema, DiscordApiErrors } from "../../util"; import { Config, ConnectedAccount, ConnectionCallbackSchema, DiscordApiErrors } from "../../util";
import Connection from "../../util/connections/Connection"; import Connection from "../../util/connections/Connection";
import { ConnectionLoader } from "../../util/connections/ConnectionLoader"; import { ConnectionLoader } from "../../util/connections/ConnectionLoader";
import { GitHubSettings } from "./GitHubSettings"; import { GitHubSettings } from "./GitHubSettings";
@ -89,21 +89,21 @@ export default class GitHubConnection extends Connection {
}).then((res) => res.json()); }).then((res) => res.json());
} }
async handleCallback(params: ConnectionCallbackSchema): Promise<boolean> { async handleCallback(params: ConnectionCallbackSchema): Promise<ConnectedAccount | null> {
const userId = this.getUserId(params.state); const userId = this.getUserId(params.state);
const token = await this.exchangeCode(params.state, params.code!); const token = await this.exchangeCode(params.state, params.code!);
const userInfo = await this.getUser(token); const userInfo = await this.getUser(token);
const exists = await this.hasConnection(userId, userInfo.id.toString()); const exists = await this.hasConnection(userId, userInfo.id.toString());
if (exists) return false; if (exists) return null;
await this.createConnection({
return await this.createConnection({
user_id: userId, user_id: userId,
external_id: userInfo.id.toString(), external_id: userInfo.id.toString(),
friend_sync: params.friend_sync, friend_sync: params.friend_sync,
name: userInfo.name, name: userInfo.name,
type: this.id, type: this.id,
}); });
return true;
} }
} }

View File

@ -21,7 +21,7 @@ export default abstract class Connection {
* Processes the callback * Processes the callback
* @param args Callback arguments * @param args Callback arguments
*/ */
abstract handleCallback(params: ConnectionCallbackSchema): Promise<boolean>; abstract handleCallback(params: ConnectionCallbackSchema): Promise<ConnectedAccount | null>;
/** /**
* Gets a user id from state * Gets a user id from state
@ -54,9 +54,10 @@ export default abstract class Connection {
this.states.delete(state); this.states.delete(state);
} }
async createConnection(data: ConnectedAccountSchema): Promise<void> { async createConnection(data: ConnectedAccountSchema): Promise<ConnectedAccount> {
const ca = OrmUtils.mergeDeep(new ConnectedAccount(), data); const ca = OrmUtils.mergeDeep(new ConnectedAccount(), data) as ConnectedAccount;
await ca.save(); await ca.save();
return ca;
} }
async hasConnection(userId: string, externalId: string): Promise<boolean> { async hasConnection(userId: string, externalId: string): Promise<boolean> {