Refreshable connections, refactoring, access-token endpoint
- Aded /users/@me/connections/:connection_name/:connection_id/access-token - Replaced `access_token` property on ConnectedAccount with `token_data` object for refreshing tokens - Made a common interface for connection things like ComonOAuthTokenResponse - Added `RefreshableConnection` class - Added token refresh to Spotify connection (disabled)
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import crypto from "crypto";
|
||||
import { ConnectedAccount } from "../entities";
|
||||
import { OrmUtils } from "../imports";
|
||||
import { ConnectedAccountSchema, ConnectionCallbackSchema } from "../schemas";
|
||||
import { DiscordApiErrors } from "../util";
|
||||
|
||||
/**
|
||||
* A connection that can be used to connect to an external service.
|
||||
*/
|
||||
export default abstract class Connection {
|
||||
id: string;
|
||||
settings: { enabled: boolean };
|
||||
@@ -21,7 +23,9 @@ export default abstract class Connection {
|
||||
* Processes the callback
|
||||
* @param args Callback arguments
|
||||
*/
|
||||
abstract handleCallback(params: ConnectionCallbackSchema): Promise<ConnectedAccount | null>;
|
||||
abstract handleCallback(
|
||||
params: ConnectionCallbackSchema,
|
||||
): Promise<ConnectedAccount | null>;
|
||||
|
||||
/**
|
||||
* Gets a user id from state
|
||||
@@ -54,12 +58,25 @@ export default abstract class Connection {
|
||||
this.states.delete(state);
|
||||
}
|
||||
|
||||
async createConnection(data: ConnectedAccountSchema): Promise<ConnectedAccount> {
|
||||
const ca = OrmUtils.mergeDeep(new ConnectedAccount(), data) as ConnectedAccount;
|
||||
/**
|
||||
* Creates a Connected Account in the database.
|
||||
* @param data connected account data
|
||||
* @returns the new connected account
|
||||
*/
|
||||
async createConnection(
|
||||
data: ConnectedAccountSchema,
|
||||
): Promise<ConnectedAccount> {
|
||||
const ca = ConnectedAccount.create({ ...data });
|
||||
await ca.save();
|
||||
return ca;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a user has an exist connected account for the given extenal id.
|
||||
* @param userId the user id
|
||||
* @param externalId the connection id to find
|
||||
* @returns
|
||||
*/
|
||||
async hasConnection(userId: string, externalId: string): Promise<boolean> {
|
||||
const existing = await ConnectedAccount.findOne({
|
||||
where: {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import Connection from "./Connection";
|
||||
import RefreshableConnection from "./RefreshableConnection";
|
||||
|
||||
export class ConnectionStore {
|
||||
public static connections: Map<string, Connection> = new Map();
|
||||
public static connections: Map<string, Connection | RefreshableConnection> =
|
||||
new Map();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { ConnectedAccount } from "../entities";
|
||||
import { ConnectedAccountCommonOAuthTokenResponse } from "../interfaces";
|
||||
import Connection from "./Connection";
|
||||
|
||||
/**
|
||||
* A connection that can refresh its token.
|
||||
*/
|
||||
export default abstract class RefreshableConnection extends Connection {
|
||||
refreshEnabled = true;
|
||||
/**
|
||||
* Refreshes the token for a connected account.
|
||||
* @param connectedAccount The connected account to refresh
|
||||
*/
|
||||
abstract refreshToken(
|
||||
connectedAccount: ConnectedAccount,
|
||||
): Promise<ConnectedAccountCommonOAuthTokenResponse>;
|
||||
|
||||
/**
|
||||
* Refreshes the token for a connected account and saves it to the database.
|
||||
* @param connectedAccount The connected account to refresh
|
||||
*/
|
||||
async refresh(
|
||||
connectedAccount: ConnectedAccount,
|
||||
): Promise<ConnectedAccountCommonOAuthTokenResponse> {
|
||||
const tokenData = await this.refreshToken(connectedAccount);
|
||||
connectedAccount.token_data = tokenData;
|
||||
await connectedAccount.save();
|
||||
return tokenData;
|
||||
}
|
||||
}
|
||||
@@ -2,3 +2,4 @@ export * from "./Connection";
|
||||
export * from "./ConnectionConfig";
|
||||
export * from "./ConnectionLoader";
|
||||
export * from "./ConnectionStore";
|
||||
export * from "./RefreshableConnection";
|
||||
|
||||
@@ -23,8 +23,8 @@ export class ConnectedAccountDTO {
|
||||
this.id = connectedAccount.external_id;
|
||||
this.user_id = connectedAccount.user_id;
|
||||
this.access_token =
|
||||
connectedAccount.access_token && with_token
|
||||
? connectedAccount.access_token
|
||||
connectedAccount.token_data && with_token
|
||||
? connectedAccount.token_data.access_token
|
||||
: undefined;
|
||||
this.friend_sync = connectedAccount.friend_sync;
|
||||
this.name = connectedAccount.name;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
*/
|
||||
|
||||
import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm";
|
||||
import { ConnectedAccountTokenData } from "../interfaces";
|
||||
import { BaseClass } from "./BaseClass";
|
||||
import { User } from "./User";
|
||||
|
||||
@@ -40,9 +41,6 @@ export class ConnectedAccount extends BaseClass {
|
||||
})
|
||||
user: User;
|
||||
|
||||
@Column({ select: false, nullable: true })
|
||||
access_token?: string;
|
||||
|
||||
@Column({ select: false })
|
||||
friend_sync?: boolean = false;
|
||||
|
||||
@@ -75,4 +73,7 @@ export class ConnectedAccount extends BaseClass {
|
||||
|
||||
@Column()
|
||||
two_way_link?: boolean = false;
|
||||
|
||||
@Column({ select: false, nullable: true, type: "simple-json" })
|
||||
token_data?: ConnectedAccountTokenData;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
export interface ConnectedAccountCommonOAuthTokenResponse {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
scope: string;
|
||||
refresh_token?: string;
|
||||
expires_in?: number;
|
||||
}
|
||||
|
||||
export interface ConnectedAccountTokenData {
|
||||
access_token: string;
|
||||
token_type?: string;
|
||||
scope?: string;
|
||||
refresh_token?: string;
|
||||
expires_in?: number;
|
||||
expires_at?: number;
|
||||
}
|
||||
@@ -17,7 +17,8 @@
|
||||
*/
|
||||
|
||||
export * from "./Activity";
|
||||
export * from "./Presence";
|
||||
export * from "./Interaction";
|
||||
export * from "./ConnectedAccount";
|
||||
export * from "./Event";
|
||||
export * from "./Interaction";
|
||||
export * from "./Presence";
|
||||
export * from "./Status";
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { ConnectedAccountTokenData } from "../interfaces";
|
||||
|
||||
export interface ConnectedAccountSchema {
|
||||
external_id: string;
|
||||
user_id: string;
|
||||
access_token?: string;
|
||||
token_data?: ConnectedAccountTokenData;
|
||||
friend_sync?: boolean;
|
||||
name: string;
|
||||
revoked?: boolean;
|
||||
|
||||
Reference in New Issue
Block a user