Skip to content

Commit

Permalink
Revert "fix: unique plex oauth login client id (#147)"
Browse files Browse the repository at this point in the history
This reverts commit 4fca10e.
  • Loading branch information
RaunoT authored Jul 18, 2024
1 parent ba338fb commit 4bc2c37
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 46 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"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 @@ -50,7 +49,6 @@
"@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: 0 additions & 17 deletions pnpm-lock.yaml

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

46 changes: 20 additions & 26 deletions src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
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_PRODUCT_NAME,
PLEX_CLIENT_IDENTIFIER,
PLEX_CLIENT_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 @@ -116,16 +109,18 @@ type PlexPinResponse = {
}

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

try {
const res = await fetch(`${PLEX_API_ENDPOINT}/pins?strong=true`, {
const res = await fetch(`${PLEX_API_ENDPOINT}/pins`, {
method: 'POST',
headers: {
Accept: 'application/json',
'X-Plex-Product': PLEX_PRODUCT_NAME,
'X-Plex-Client-Identifier': clientIdentifier,
'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,
}),
})

if (!res.ok) {
Expand All @@ -144,7 +139,6 @@ 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 @@ -153,12 +147,12 @@ export async function createPlexAuthUrl() {
const authAppUrl =
'https://app.plex.tv/auth#?' +
qs.stringify({
clientID: clientIdentifier,
clientID: PLEX_CLIENT_IDENTIFIER,
code,
forwardUrl,
context: {
device: {
product: PLEX_PRODUCT_NAME,
product: PLEX_CLIENT_NAME,
},
},
})
Expand All @@ -167,14 +161,12 @@ 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': clientIdentifier,
'X-Plex-Client-Identifier': PLEX_CLIENT_IDENTIFIER,
},
})

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

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

try {
const res = await fetch(`${PLEX_API_ENDPOINT}/user?strong=true`, {
const res = await fetch(`${PLEX_API_ENDPOINT}/user`, {
method: 'POST',
headers: {
Accept: 'application/json',
'X-Plex-Token': authToken,
'X-Plex-Product': PLEX_PRODUCT_NAME,
'X-Plex-Client-Identifier': clientIdentifier,
'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,
}),
})

if (!res.ok) {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ 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_PRODUCT_NAME = 'Plex Rewind'
export const PLEX_CLIENT_NAME = 'Plex Rewind'
export const PLEX_CLIENT_IDENTIFIER = 'plex-rewind'

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

Expand Down

0 comments on commit 4bc2c37

Please sign in to comment.