#!/usr/bin/env python3
"""
Quick script to check OAuth token scopes
"""
import json
from pathlib import Path
TOKEN_FILE = Path("/tmp/iris_oauth_tokens.json")
if not TOKEN_FILE.exists():
print("β Token file not found. Please login first at:")
print(" https://trustypa.brainaihub.tech/")
exit(1)
with open(TOKEN_FILE) as f:
tokens = json.load(f)
print("=" * 70)
print("π OAUTH TOKEN ANALYSIS")
print("=" * 70)
for user_email, data in tokens.items():
print(f"\nπ§ User: {user_email}")
# Extract scopes
scopes = data.get('scope', '').split()
print(f"π Total scopes: {len(scopes)}")
# Check critical scopes for user search
critical_scopes = {
'User.Read': 'β
' if 'User.Read' in scopes else 'β',
'User.Read.All': 'β
' if 'User.Read.All' in scopes else 'β',
'User.ReadBasic.All': 'β
' if 'User.ReadBasic.All' in scopes else 'β
' if 'User.ReadBasic.All' in scopes else 'β',
}
# Check Bookings scopes
bookings_scopes = {
'Bookings.Read.All': 'β
' if 'Bookings.Read.All' in scopes else 'β',
'Bookings.ReadWrite.All': 'β
' if 'Bookings.ReadWrite.All' in scopes else 'β',
'Bookings.Manage.All': 'β
' if 'Bookings.Manage.All' in scopes else 'β',
}
print("\nπ Critical Scopes (User Operations):")
for scope, status in critical_scopes.items():
print(f" {status} {scope}")
print("\nπ
Bookings Scopes:")
for scope, status in bookings_scopes.items():
print(f" {status} {scope}")
print("\nπ All Scopes:")
for i, scope in enumerate(sorted(scopes), 1):
print(f" {i:2}. {scope}")
# Token expiry
expires_at = data.get('expires_at', 0)
if expires_at:
from datetime import datetime
expires_dt = datetime.fromtimestamp(expires_at)
now = datetime.now()
remaining = expires_dt - now
minutes = int(remaining.total_seconds() / 60)
print(f"\nβ° Token expires in: {minutes} minutes")
print("\n" + "=" * 70)
# Check if user search will work
all_tokens = list(tokens.values())
if all_tokens:
first_token = all_tokens[0]
scopes = first_token.get('scope', '').split()
has_user_read_all = 'User.Read.All' in scopes
has_user_readbasic = 'User.ReadBasic.All' in scopes
if has_user_read_all or has_user_readbasic:
print("β
USER SEARCH SHOULD WORK!")
print(" Token has sufficient permissions for /users endpoint")
else:
print("β οΈ USER SEARCH MAY FAIL")
print(" Missing User.Read.All or User.ReadBasic.All scope")
print(" Current user-related scopes:")
for scope in scopes:
if 'User' in scope or 'People' in scope:
print(f" - {scope}")
print("=" * 70)