Implemented googleapi access token refresher
This commit is contained in:
Binary file not shown.
@@ -60,10 +60,10 @@ export class Connections {
|
||||
static getUserConnections = (userId: string): Connection[] => {
|
||||
const connectionRows = db.prepare('SELECT * FROM Connections WHERE userId = ?').all(userId) as ConnectionsTableSchema[]
|
||||
const connections: Connection[] = []
|
||||
connectionRows.forEach((row) => {
|
||||
for (const row of connectionRows) {
|
||||
const { id, service, tokens } = row
|
||||
connections.push({ id, userId, service: JSON.parse(service), tokens: JSON.parse(tokens) })
|
||||
})
|
||||
}
|
||||
return connections
|
||||
}
|
||||
|
||||
@@ -78,4 +78,9 @@ export class Connections {
|
||||
const commandInfo = db.prepare('DELETE FROM Connections WHERE id = ?').run(id)
|
||||
if (commandInfo.changes === 0) throw new Error(`Connection with id: ${id} does not exist`)
|
||||
}
|
||||
|
||||
static updateTokens = (id: string, tokens: Tokens): void => {
|
||||
const commandInfo = db.prepare('UPDATE Connections SET tokens = ? WHERE id = ?').run(JSON.stringify(tokens), id)
|
||||
if (commandInfo.changes === 0) throw new Error('Failed to update tokens')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,4 +100,24 @@ export class Jellyfin {
|
||||
thumbnail,
|
||||
}
|
||||
}
|
||||
|
||||
static connectionInfo = async (connection: Jellyfin.JFConnection): Promise<ConnectionInfo> => {
|
||||
const reqHeaders = new Headers({ Authorization: `MediaBrowser Token="${connection.tokens.accessToken}"` })
|
||||
|
||||
const userUrl = new URL(`Users/${connection.service.userId}`, connection.service.urlOrigin).href
|
||||
const systemUrl = new URL('System/Info', connection.service.urlOrigin).href
|
||||
|
||||
const userResponse = await fetch(userUrl, { headers: reqHeaders })
|
||||
const systemResponse = await fetch(systemUrl, { headers: reqHeaders })
|
||||
|
||||
const userData: Jellyfin.User = await userResponse.json()
|
||||
const systemData: Jellyfin.System = await systemResponse.json()
|
||||
|
||||
return {
|
||||
connectionId: connection.id,
|
||||
serviceType: 'jellyfin',
|
||||
username: userData.Name,
|
||||
serverName: systemData.ServerName,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
46
src/lib/service-managers/youtubeMusic.ts
Normal file
46
src/lib/service-managers/youtubeMusic.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { YOUTUBE_API_CLIENT_SECRET } from '$env/static/private'
|
||||
import { PUBLIC_YOUTUBE_API_CLIENT_ID } from '$env/static/public'
|
||||
import { Connections } from '$lib/server/users'
|
||||
import { google } from 'googleapis'
|
||||
|
||||
export class YouTubeMusic {
|
||||
static refreshAccessToken = async (connectionId: string, refreshToken: string): Promise<Tokens> => {
|
||||
// Again DON'T SHIP THIS, CLIENT SECRET SHOULD NOT BE EXPOSED TO USERS
|
||||
const response = await fetch('https://oauth2.googleapis.com/token', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
client_id: PUBLIC_YOUTUBE_API_CLIENT_ID,
|
||||
client_secret: YOUTUBE_API_CLIENT_SECRET,
|
||||
refresh_token: refreshToken,
|
||||
grant_type: 'refresh_token',
|
||||
}),
|
||||
})
|
||||
const { access_token, expires_in } = await response.json()
|
||||
const newTokens: Tokens = {
|
||||
accessToken: access_token,
|
||||
refreshToken,
|
||||
expiry: Date.now() + expires_in * 1000,
|
||||
}
|
||||
Connections.updateTokens(connectionId, newTokens)
|
||||
return newTokens
|
||||
}
|
||||
|
||||
static connectionInfo = async (connection: YouTubeMusic.YTConnection): Promise<ConnectionInfo> => {
|
||||
let accessToken = connection.tokens.accessToken
|
||||
if (Date.now() > connection.tokens.expiry) {
|
||||
const newTokenData = await this.refreshAccessToken(connection.id, connection.tokens.refreshToken)
|
||||
accessToken = newTokenData.accessToken
|
||||
}
|
||||
|
||||
const youtube = google.youtube('v3')
|
||||
const userChannelResponse = await youtube.channels.list({ mine: true, part: ['snippet'], access_token: accessToken })
|
||||
const userChannel = userChannelResponse.data.items![0]
|
||||
|
||||
return {
|
||||
connectionId: connection.id,
|
||||
serviceType: connection.service.type,
|
||||
username: userChannel.snippet?.title as string,
|
||||
profilePicture: userChannel.snippet?.thumbnails?.default?.url as string | undefined,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user