Implemented googleapi access token refresher
This commit is contained in:
24
src/app.d.ts
vendored
24
src/app.d.ts
vendored
@@ -49,6 +49,8 @@ declare global {
|
|||||||
connectionId: string
|
connectionId: string
|
||||||
serviceType: serviceType
|
serviceType: serviceType
|
||||||
username: string
|
username: string
|
||||||
|
serverName?: string
|
||||||
|
profilePicture?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// These Schemas should only contain general info data that is necessary for data fetching purposes.
|
// These Schemas should only contain general info data that is necessary for data fetching purposes.
|
||||||
@@ -117,23 +119,16 @@ declare global {
|
|||||||
tokens: JFTokens
|
tokens: JFTokens
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JFConnectionInfo extends ConnectionInfo {
|
|
||||||
serviceType: 'jellyfin'
|
|
||||||
servername: string
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AuthData {
|
|
||||||
User: {
|
|
||||||
Id: string
|
|
||||||
}
|
|
||||||
AccessToken: string
|
|
||||||
}
|
|
||||||
|
|
||||||
interface User {
|
interface User {
|
||||||
Name: string
|
Name: string
|
||||||
Id: string
|
Id: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface AuthData {
|
||||||
|
User: Jellyfin.User
|
||||||
|
AccessToken: string
|
||||||
|
}
|
||||||
|
|
||||||
interface System {
|
interface System {
|
||||||
ServerName: string
|
ServerName: string
|
||||||
}
|
}
|
||||||
@@ -204,11 +199,6 @@ declare global {
|
|||||||
service: YTService
|
service: YTService
|
||||||
tokens: YTTokens
|
tokens: YTTokens
|
||||||
}
|
}
|
||||||
|
|
||||||
interface YTConnectionInfo extends ConnectionInfo {
|
|
||||||
serviceType: 'youtube-music'
|
|
||||||
profilePicture?: string
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -60,10 +60,10 @@ export class Connections {
|
|||||||
static getUserConnections = (userId: string): Connection[] => {
|
static getUserConnections = (userId: string): Connection[] => {
|
||||||
const connectionRows = db.prepare('SELECT * FROM Connections WHERE userId = ?').all(userId) as ConnectionsTableSchema[]
|
const connectionRows = db.prepare('SELECT * FROM Connections WHERE userId = ?').all(userId) as ConnectionsTableSchema[]
|
||||||
const connections: Connection[] = []
|
const connections: Connection[] = []
|
||||||
connectionRows.forEach((row) => {
|
for (const row of connectionRows) {
|
||||||
const { id, service, tokens } = row
|
const { id, service, tokens } = row
|
||||||
connections.push({ id, userId, service: JSON.parse(service), tokens: JSON.parse(tokens) })
|
connections.push({ id, userId, service: JSON.parse(service), tokens: JSON.parse(tokens) })
|
||||||
})
|
}
|
||||||
return connections
|
return connections
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,4 +78,9 @@ export class Connections {
|
|||||||
const commandInfo = db.prepare('DELETE FROM Connections WHERE id = ?').run(id)
|
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`)
|
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,
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,39 +1,7 @@
|
|||||||
import type { RequestHandler } from '@sveltejs/kit'
|
import type { RequestHandler } from '@sveltejs/kit'
|
||||||
|
import { YouTubeMusic } from '$lib/service-managers/youtubeMusic'
|
||||||
|
import { Jellyfin } from '$lib/service-managers/jellyfin'
|
||||||
import { Connections } from '$lib/server/users'
|
import { Connections } from '$lib/server/users'
|
||||||
import { google } from 'googleapis'
|
|
||||||
|
|
||||||
const jellyfinInfo = async (connection: Jellyfin.JFConnection): Promise<Jellyfin.JFConnectionInfo> => {
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const youtubeInfo = async (connection: YouTubeMusic.YTConnection): Promise<YouTubeMusic.YTConnectionInfo> => {
|
|
||||||
const youtube = google.youtube('v3')
|
|
||||||
const userChannelResponse = await youtube.channels.list({ mine: true, part: ['snippet'], access_token: connection.tokens.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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const GET: RequestHandler = async ({ params, url }) => {
|
export const GET: RequestHandler = async ({ params, url }) => {
|
||||||
const userId = params.userId as string
|
const userId = params.userId as string
|
||||||
@@ -47,10 +15,10 @@ export const GET: RequestHandler = async ({ params, url }) => {
|
|||||||
let info: ConnectionInfo
|
let info: ConnectionInfo
|
||||||
switch (connection.service.type) {
|
switch (connection.service.type) {
|
||||||
case 'jellyfin':
|
case 'jellyfin':
|
||||||
info = await jellyfinInfo(connection as Jellyfin.JFConnection)
|
info = await Jellyfin.connectionInfo(connection as Jellyfin.JFConnection)
|
||||||
break
|
break
|
||||||
case 'youtube-music':
|
case 'youtube-music':
|
||||||
info = await youtubeInfo(connection as YouTubeMusic.YTConnection)
|
info = await YouTubeMusic.connectionInfo(connection as YouTubeMusic.YTConnection)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
connectionInfo.push(info)
|
connectionInfo.push(info)
|
||||||
|
|||||||
Reference in New Issue
Block a user