Skip to content

Commit

Permalink
fix: unique plex oauth login client id (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaunoT authored Jul 18, 2024
1 parent 550e94a commit 0a7b220
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"sharp": "^0.33.4",
"stories-react": "^1.1.2",
"three": "^0.166.1",
"uuid": "^10.0.0",
"vanta": "^0.5.24",
"xml2js": "^0.6.2",
"zod": "^3.23.8"
Expand All @@ -49,6 +50,7 @@
"@types/qs": "^6.9.15",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/uuid": "^10.0.0",
"@types/xml2js": "^0.4.14",
"@typescript-eslint/eslint-plugin": "^7.16.1",
"@typescript-eslint/parser": "^7.16.1",
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 26 additions & 20 deletions src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { AuthOptions } from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
import qs from 'qs'
import { v4 as uuidv4 } from 'uuid'
import { parseStringPromise } from 'xml2js'
import {
APP_URL,
PLEX_API_ENDPOINT,
PLEX_CLIENT_IDENTIFIER,
PLEX_CLIENT_NAME,
PLEX_PRODUCT_NAME,
} from '../utils/constants'
import fetchTautulli from '../utils/fetchTautulli'

function getClientIdentifier(): string {
const clientId = localStorage.getItem('plexClientId') || uuidv4()
localStorage.setItem('plexClientId', clientId)

return clientId
}

export const authOptions: AuthOptions = {
providers: [
CredentialsProvider({
Expand Down Expand Up @@ -109,18 +116,16 @@ type PlexPinResponse = {
}

async function fetchPlexPins(): Promise<PlexPinResponse> {
const clientIdentifier = getClientIdentifier()

try {
const res = await fetch(`${PLEX_API_ENDPOINT}/pins`, {
const res = await fetch(`${PLEX_API_ENDPOINT}/pins?strong=true`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'X-Plex-Product': PLEX_PRODUCT_NAME,
'X-Plex-Client-Identifier': clientIdentifier,
},
body: new URLSearchParams({
strong: 'true',
'X-Plex-Product': PLEX_CLIENT_NAME,
'X-Plex-Client-Identifier': PLEX_CLIENT_IDENTIFIER,
}),
})

if (!res.ok) {
Expand All @@ -139,6 +144,7 @@ async function fetchPlexPins(): Promise<PlexPinResponse> {
export async function createPlexAuthUrl() {
const { id, code } = await fetchPlexPins()
const forwardUrl = `${APP_URL}?plexPinId=${id}`
const clientIdentifier = getClientIdentifier()

if (!forwardUrl) {
console.error('Base url is not configured!')
Expand All @@ -147,12 +153,12 @@ export async function createPlexAuthUrl() {
const authAppUrl =
'https://app.plex.tv/auth#?' +
qs.stringify({
clientID: PLEX_CLIENT_IDENTIFIER,
clientID: clientIdentifier,
code,
forwardUrl,
context: {
device: {
product: PLEX_CLIENT_NAME,
product: PLEX_PRODUCT_NAME,
},
},
})
Expand All @@ -161,12 +167,14 @@ export async function createPlexAuthUrl() {
}

export async function getPlexAuthToken(pinId: string) {
const clientIdentifier = getClientIdentifier()

try {
const res = await fetch(`${PLEX_API_ENDPOINT}/pins/${pinId}`, {
method: 'GET',
headers: {
Accept: 'application/json',
'X-Plex-Client-Identifier': PLEX_CLIENT_IDENTIFIER,
'X-Plex-Client-Identifier': clientIdentifier,
},
})

Expand All @@ -185,19 +193,17 @@ export async function getPlexAuthToken(pinId: string) {
}

export async function verifyPlexAuthToken(authToken: string) {
const clientIdentifier = getClientIdentifier()

try {
const res = await fetch(`${PLEX_API_ENDPOINT}/user`, {
const res = await fetch(`${PLEX_API_ENDPOINT}/user?strong=true`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
strong: 'true',
'X-Plex-Product': PLEX_CLIENT_NAME,
'X-Plex-Client-Identifier': PLEX_CLIENT_IDENTIFIER,
'X-Plex-Token': authToken,
}),
'X-Plex-Product': PLEX_PRODUCT_NAME,
'X-Plex-Client-Identifier': clientIdentifier,
},
})

if (!res.ok) {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ export const META_TITLE: string = 'Plex Rewind'
export const META_TITLE_TEMPLATE: string = '%s | Plex Rewind'

export const PLEX_API_ENDPOINT = 'https://plex.tv/api/v2'
export const PLEX_CLIENT_NAME = 'Plex Rewind'
export const PLEX_CLIENT_IDENTIFIER = 'plex-rewind'
export const PLEX_PRODUCT_NAME = 'Plex Rewind'

export const APP_URL = env('NEXT_PUBLIC_SITE_URL') || 'http://localhost:8383'

Expand Down

0 comments on commit 0a7b220

Please sign in to comment.