forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.py
More file actions
273 lines (238 loc) · 12.4 KB
/
Copy pathoauth.py
File metadata and controls
273 lines (238 loc) · 12.4 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import hmac
import logging
import os
import secrets
from typing import Optional
from fastapi import APIRouter, Cookie, Request, HTTPException, Form
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel, Field
import firebase_admin.auth
import httpx
from database.apps import get_app_by_id_db
from utils.executors import critical_executor, db_executor, run_blocking
from utils.other.endpoints import enforce_account_deletion_http_access
from utils.http_client import safe_request_target, get_auth_client, UnsafeWebhookURLError
from database.redis_db import enable_app, increase_app_installs_count
from utils.apps import is_user_app_enabled, get_is_user_paid_app, is_tester
from utils.jit_qa_admission import JITQAAdmissionError, enforce_jit_qa_uid
from models.app import App as AppModel, ActionType
logger = logging.getLogger(__name__)
router = APIRouter(
tags=["oauth"],
)
class OAuthTokenResponse(BaseModel):
"""OAuth token-exchange response for app integrations."""
uid: str = Field(description='Authenticated user UID.')
redirect_url: str = Field(description='URL to redirect the user back to the app.')
state: Optional[str] = Field(
default=None, description='Opaque state value passed through from the authorize request.'
)
# Ensure the templates directory exists
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
templates = Jinja2Templates(directory=os.path.join(BASE_DIR, "templates"))
# Double-submit CSRF protection for the authorize -> token exchange: /authorize
# hands the browser the same random value two ways (an HttpOnly SameSite=Strict
# cookie it never has to read, and a value embedded directly in the page it
# loaded), and /token requires both to match. A cross-site page cannot forge
# this — it has no way to read or set the cookie for our origin, and
# SameSite=Strict keeps the cookie from being attached to a cross-site request
# in the first place. This closes the "state accepted but never validated
# server-side" gap without depending on the third-party app's own `state`
# handling, which Omi's server has no way to verify.
OAUTH_CSRF_COOKIE_NAME = 'omi_oauth_csrf'
@router.get("/v1/oauth/authorize", response_class=HTMLResponse)
def oauth_authorize(
request: Request,
app_id: str,
state: Optional[str] = None,
):
app_data = get_app_by_id_db(app_id)
if not app_data:
raise HTTPException(status_code=404, detail="App not found")
app = AppModel(**app_data)
if not app.external_integration:
raise HTTPException(status_code=400, detail="App does not support external integration")
if not app.external_integration.app_home_url:
raise HTTPException(status_code=400, detail="App home URL not configured for this app.")
# Prepare permission strings
permissions = []
if app.capabilities:
if "chat" in app.capabilities:
permissions.append({"icon": "💬", "text": "Engage in chat conversations with Omi."})
if "memories" in app.capabilities:
permissions.append({"icon": "📝", "text": "Access and manage your conversations."})
if "external_integration" in app.capabilities and app.external_integration:
if app.external_integration.triggers_on == 'audio_bytes':
permissions.append({"icon": "🎤", "text": "Process audio data in real-time."})
elif app.external_integration.triggers_on == 'memory_creation':
permissions.append({"icon": "🔔", "text": "Trigger actions when new conversations are created."})
elif app.external_integration.triggers_on == 'transcript_processed':
permissions.append({"icon": "🎧", "text": "Trigger actions when live transcripts are processed."})
if app.external_integration.actions:
for action_item in app.external_integration.actions:
action_type_value = action_item.action.value
if action_type_value == ActionType.CREATE_MEMORY.value:
permissions.append({"icon": "✍️", "text": "Create new conversations on your behalf."})
elif action_type_value == ActionType.CREATE_FACTS.value:
permissions.append({"icon": "➕", "text": "Create new memories for you."})
elif action_type_value == ActionType.READ_CONVERSATIONS.value:
permissions.append({"icon": "📖", "text": "Access and read your conversation history."})
elif action_type_value == ActionType.READ_MEMORIES.value:
permissions.append({"icon": "🔍", "text": "Access and read your stored memories."})
elif action_type_value == ActionType.READ_TASKS.value:
permissions.append({"icon": "📋", "text": "Access and read your stored tasks."})
if (
"proactive_notification" in app.capabilities
and app.proactive_notification
and app.proactive_notification.scopes
):
if "user_name" in app.proactive_notification.scopes:
permissions.append({"icon": "📛", "text": "Access your user name for notifications."})
if "user_facts" in app.proactive_notification.scopes:
permissions.append({"icon": "💡", "text": "Access your facts for notifications."})
if "user_context" in app.proactive_notification.scopes:
permissions.append({"icon": "📜", "text": "Access your conversation context for notifications."})
if "user_chat" in app.proactive_notification.scopes:
permissions.append({"icon": "🗣️", "text": "Access your chat history for notifications."})
if not permissions:
permissions.append({"icon": "✅", "text": "Access your basic Omi profile information."})
# Remove duplicate permissions (based on text)
unique_permissions = []
seen_texts = set()
for perm in permissions:
if perm["text"] not in seen_texts:
unique_permissions.append(perm)
seen_texts.add(perm["text"])
permissions = unique_permissions
csrf_token = secrets.token_urlsafe(32)
response = templates.TemplateResponse(
request,
"oauth_authenticate.html",
{
"app_id": app_id,
"app_name": app.name,
"app_image": app.image,
"state": state,
"csrf_token": csrf_token,
"permissions": permissions,
"firebase_api_key": os.getenv("FIREBASE_API_KEY"),
"firebase_auth_domain": os.getenv("FIREBASE_AUTH_DOMAIN"),
"firebase_project_id": os.getenv("FIREBASE_PROJECT_ID"),
},
)
response.set_cookie(
OAUTH_CSRF_COOKIE_NAME,
csrf_token,
max_age=600,
httponly=True,
secure=True,
samesite='strict',
)
return response
def _setup_completed_from_payload(payload: object) -> bool:
"""Read `is_setup_completed` from a third-party setup_completed_url body.
Mirrors `routers.apps._setup_completed_from_response`: the body is
developer-controlled, so a JSON scalar or array is as likely as the documented
object. Anything that is not an object carrying a truthy `is_setup_completed`
means setup is not completed. A non-JSON body still raises ValueError out of
`res.json()` and is answered with the 503 below.
"""
return isinstance(payload, dict) and bool(payload.get('is_setup_completed', False))
@router.post("/v1/oauth/token", response_model=OAuthTokenResponse)
async def oauth_token(
firebase_id_token: str = Form(...),
app_id: str = Form(...),
state: Optional[str] = Form(None),
csrf_token: str = Form(...),
oauth_csrf_cookie: Optional[str] = Cookie(default=None, alias=OAUTH_CSRF_COOKIE_NAME),
):
if not oauth_csrf_cookie or not hmac.compare_digest(csrf_token, oauth_csrf_cookie):
raise HTTPException(
status_code=403,
detail='This authorization request is invalid or expired. Please restart the connection from the app.',
)
try:
decoded_token = await run_blocking(
critical_executor,
firebase_admin.auth.verify_id_token,
firebase_id_token,
)
uid = decoded_token['uid']
except firebase_admin.auth.InvalidIdTokenError as e:
raise HTTPException(status_code=401, detail=f"Invalid Firebase ID token: {e}")
except Exception as e:
raise HTTPException(status_code=401, detail=f"Error verifying Firebase ID token: {e}")
try:
enforce_jit_qa_uid(uid)
except JITQAAdmissionError as error:
raise HTTPException(status_code=403, detail="account is not admitted to the isolated JIT QA plane") from error
await run_blocking(db_executor, enforce_account_deletion_http_access, uid)
app_data = await run_blocking(db_executor, get_app_by_id_db, app_id)
if not app_data:
raise HTTPException(status_code=404, detail="App not found")
app = AppModel(**app_data)
if not app.external_integration or not app.external_integration.app_home_url:
raise HTTPException(status_code=400, detail="App not configured for OAuth or app home URL not set")
# Validate if the user has enabled this app, if not, try to enable it automatically
if not await run_blocking(db_executor, is_user_app_enabled, uid, app_id):
if app.private is not None:
if app.private and app.uid != uid and not await run_blocking(db_executor, is_tester, uid):
raise HTTPException(
status_code=403, detail="This app is private and you are not authorized to enable it."
)
# Check Setup completes
if app.works_externally() and app.external_integration.setup_completed_url:
try:
pinned_url, pin_kwargs = await run_blocking(
db_executor, safe_request_target, app.external_integration.setup_completed_url
)
client = get_auth_client()
res = await client.get(
pinned_url + f'?uid={uid}',
headers=pin_kwargs['headers'],
extensions=pin_kwargs['extensions'],
follow_redirects=False,
)
res.raise_for_status()
if not _setup_completed_from_payload(res.json()):
raise HTTPException(
status_code=400,
detail='App setup is not completed. Please complete app setup before authorizing.',
)
except UnsafeWebhookURLError as e:
logger.warning(f'Rejected setup_completed_url for app {app_id}: {e}')
raise HTTPException(
status_code=400,
detail='This app is misconfigured (setup URL is not a public address). Please contact the app developer.',
)
except (httpx.HTTPStatusError, httpx.RequestError) as e:
raise HTTPException(
status_code=503,
detail=f'Failed to verify app setup completion. Please try again later or contact support.',
)
except ValueError:
raise HTTPException(
status_code=503,
detail='Could not verify app setup due to an invalid response from the app. Please contact app developer or support.',
)
# Check payment status
if app.is_paid and not await run_blocking(db_executor, get_is_user_paid_app, app.id, uid):
raise HTTPException(
status_code=403, detail='This is a paid app. Please purchase the app before authorizing.'
)
try:
await run_blocking(db_executor, enable_app, uid, app_id)
if (
(app.private is None or not app.private)
and (app.uid is None or app.uid != uid)
and not await run_blocking(db_executor, is_tester, uid)
):
await run_blocking(db_executor, increase_app_installs_count, app_id)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Could not automatically enable the app. Please try again or enable it manually.",
)
redirect_url = app.external_integration.app_home_url
return {"uid": uid, "redirect_url": redirect_url, "state": state}