Repository navigation
Expand file tree
/
Copy pathauth.py
More file actions
221 lines (179 loc) · 6.9 KB
/
Copy pathauth.py
File metadata and controls
221 lines (179 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from flask import Blueprint, jsonify, request, redirect, url_for, session, current_app
from flask_login import login_required, login_user, logout_user, current_user
import requests
import os
import json
import uuid
from urllib.parse import urlencode
auth_bp = Blueprint('auth', __name__)
# Overseerr and Plex configuration
# These would be stored in environment variables or a config file in production
OVERSEERR_URL = os.environ.get('OVERSEERR_URL', 'http://localhost:5055')
OVERSEERR_API_KEY = os.environ.get('OVERSEERR_API_KEY', '')
PLEX_CLIENT_ID = os.environ.get('PLEX_CLIENT_ID', 'PlexPortal')
PLEX_OAUTH_URL = 'https://app.plex.tv/auth'
PLEX_API_URL = 'https://plex.tv/api/v2'
@auth_bp.route('/login', methods=['GET'])
def login():
"""
Initiate the Plex OAuth login flow.
Redirects the user to the Plex authentication page.
"""
# Generate a random state token to prevent CSRF
state = str(uuid.uuid4())
session['oauth_state'] = state
# Redirect to Plex OAuth
params = {
'clientID': PLEX_CLIENT_ID,
'code': state,
'context[device][product]': 'Plex Portal',
'context[device][environment]': 'Web',
'context[device][layout]': 'desktop',
'context[device][platform]': 'Web',
'forwardUrl': url_for('auth.plex_callback', _external=True),
}
auth_url = f"{PLEX_OAUTH_URL}#!{urlencode(params)}"
return redirect(auth_url)
@auth_bp.route('/callback', methods=['GET'])
def plex_callback():
"""
Handle the callback from Plex OAuth.
Exchanges the auth code for a token and logs the user in.
"""
auth_code = request.args.get('code')
state = request.args.get('state')
# Verify state to prevent CSRF
if not state or state != session.get('oauth_state'):
return jsonify({'error': 'Invalid state parameter'}), 400
# Exchange auth code for token
try:
# This is a simplified example - in production, you'd make a proper token exchange
# For Plex, the auth flow is a bit different than standard OAuth
headers = {
'Accept': 'application/json',
'X-Plex-Client-Identifier': PLEX_CLIENT_ID,
'X-Plex-Product': 'Plex Portal',
}
# Get user info from Plex
response = requests.get(
f"{PLEX_API_URL}/user",
headers=headers,
params={'X-Plex-Token': auth_code}
)
if response.status_code != 200:
return jsonify({'error': 'Failed to authenticate with Plex'}), 401
user_data = response.json()
# Here you would typically:
# 1. Check if the user exists in your database
# 2. If not, create a new user
# 3. Log the user in
# For this example, we'll just store the user info in the session
session['user'] = {
'id': user_data.get('id'),
'username': user_data.get('username'),
'email': user_data.get('email'),
'plex_token': auth_code,
}
# Redirect to the frontend dashboard
return redirect('/')
except Exception as e:
return jsonify({'error': f'Authentication error: {str(e)}'}), 500
@auth_bp.route('/logout', methods=['GET'])
def logout():
"""
Log the user out by clearing their session.
"""
session.clear()
return redirect('/')
@auth_bp.route('/user', methods=['GET'])
def get_user():
"""
Get the current user's information.
"""
if 'user' not in session:
return jsonify({'authenticated': False})
return jsonify({
'authenticated': True,
'user': session['user']
})
# Overseerr integration routes
@auth_bp.route('/overseerr/search', methods=['GET'])
def search_overseerr():
"""
Search for media on Overseerr.
"""
if 'user' not in session:
return jsonify({'error': 'Authentication required'}), 401
query = request.args.get('query', '')
media_type = request.args.get('type', 'movie') # movie, tv, person
if not query:
return jsonify({'error': 'Query parameter is required'}), 400
try:
headers = {
'X-Api-Key': OVERSEERR_API_KEY,
'Content-Type': 'application/json'
}
response = requests.get(
f"{OVERSEERR_URL}/api/v1/search",
headers=headers,
params={'query': query, 'mediaType': media_type}
)
if response.status_code != 200:
return jsonify({'error': f'Overseerr API error: {response.status_code}'}), response.status_code
return jsonify(response.json())
except Exception as e:
return jsonify({'error': f'Error searching Overseerr: {str(e)}'}), 500
@auth_bp.route('/overseerr/request', methods=['POST'])
def request_media():
"""
Submit a media request to Overseerr.
"""
if 'user' not in session:
return jsonify({'error': 'Authentication required'}), 401
data = request.json
if not data or 'mediaId' not in data or 'mediaType' not in data:
return jsonify({'error': 'Missing required parameters'}), 400
try:
headers = {
'X-Api-Key': OVERSEERR_API_KEY,
'Content-Type': 'application/json'
}
# Prepare request payload
payload = {
'mediaId': data['mediaId'],
'mediaType': data['mediaType'],
'userId': session['user']['id'],
'seasons': data.get('seasons', []), # For TV shows
'is4k': data.get('is4k', False)
}
response = requests.post(
f"{OVERSEERR_URL}/api/v1/request",
headers=headers,
json=payload
)
if response.status_code not in [200, 201]:
return jsonify({'error': f'Overseerr API error: {response.status_code}'}), response.status_code
return jsonify(response.json())
except Exception as e:
return jsonify({'error': f'Error submitting request to Overseerr: {str(e)}'}), 500
@auth_bp.route('/overseerr/requests', methods=['GET'])
def get_requests():
"""
Get the current user's requests from Overseerr.
"""
if 'user' not in session:
return jsonify({'error': 'Authentication required'}), 401
try:
headers = {
'X-Api-Key': OVERSEERR_API_KEY,
'Content-Type': 'application/json'
}
response = requests.get(
f"{OVERSEERR_URL}/api/v1/request?take=20&skip=0&sort=created&filter=all",
headers=headers
)
if response.status_code != 200:
return jsonify({'error': f'Overseerr API error: {response.status_code}'}), response.status_code
return jsonify(response.json())
except Exception as e:
return jsonify({'error': f'Error getting requests from Overseerr: {str(e)}'}), 500