summary refs log tree commit diff
path: root/src/util/connections/RefreshableConnection.ts
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@proton.me>2022-12-23 18:34:36 -0500
committerPuyodead1 <puyodead@proton.me>2023-03-18 19:27:39 -0400
commit0db1fa5f0b2b9b357c1f96178c0e5df7858a99ab (patch)
treeb045eee5f984a40ae47413bad2458cf65eff3c8e /src/util/connections/RefreshableConnection.ts
parentDon't try to upload entire config for each connection loaded (diff)
downloadserver-0db1fa5f0b2b9b357c1f96178c0e5df7858a99ab.tar.xz
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)
Diffstat (limited to 'src/util/connections/RefreshableConnection.ts')
-rw-r--r--src/util/connections/RefreshableConnection.ts30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/util/connections/RefreshableConnection.ts b/src/util/connections/RefreshableConnection.ts
new file mode 100644
index 00000000..0008cbc0
--- /dev/null
+++ b/src/util/connections/RefreshableConnection.ts
@@ -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;
+	}
+}