File size: 11,496 Bytes
383cb38 | 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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | #!/usr/bin/env python3
"""
IMPROVED OAUTH SERVER - Emergency Fix
Complete OAuth server with all required endpoints
"""
from datetime import datetime
import json
import os
import secrets
import urllib.parse
from flask import Flask, jsonify, request
def create_improved_oauth_server():
"""Create improved OAuth server with all endpoints"""
app = Flask(__name__)
app.secret_key = os.getenv("FLASK_SECRET_KEY", "emergency-oauth-secret")
# Enhanced services configuration
services_config = {
"github": {
"status": "configured"
if os.getenv("GITHUB_CLIENT_ID")
else "needs_credentials",
"client_id": os.getenv("GITHUB_CLIENT_ID", "github_placeholder_client_id"),
"auth_url": "https://github.com/login/oauth/authorize",
"scopes": ["repo", "user:email"],
},
"google": {
"status": "configured"
if os.getenv("GOOGLE_CLIENT_ID")
else "needs_credentials",
"client_id": os.getenv("GOOGLE_CLIENT_ID", "google_placeholder_client_id"),
"auth_url": "https://accounts.google.com/o/oauth2/v2/auth",
"scopes": ["email", "profile", "https://www.googleapis.com/auth/calendar"],
},
"slack": {
"status": "configured"
if os.getenv("SLACK_CLIENT_ID")
else "needs_credentials",
"client_id": os.getenv("SLACK_CLIENT_ID", "slack_placeholder_client_id"),
"auth_url": "https://slack.com/oauth/v2/authorize",
"scopes": ["chat:read", "chat:write", "channels:read"],
},
"dropbox": {
"status": "configured"
if os.getenv("DROPBOX_CLIENT_ID")
else "needs_credentials",
"client_id": os.getenv("DROPBOX_APP_KEY", "dropbox_placeholder_client_id"),
"auth_url": "https://www.dropbox.com/oauth2/authorize",
"scopes": [
"files.metadata.read",
"files.content.read",
"files.content.write",
],
},
"trello": {
"status": "configured"
if os.getenv("TRELLO_CLIENT_ID")
else "needs_credentials",
"client_id": os.getenv("TRELLO_API_KEY", "trello_placeholder_client_id"),
"auth_url": "https://trello.com/1/authorize",
"scopes": ["read", "write"],
},
}
# Health endpoint
@app.route("/healthz")
def health():
return jsonify(
{
"status": "ok",
"service": "atom-oauth-emergency-fix",
"version": "2.0.0-emergency",
"timestamp": datetime.now().isoformat(),
}
)
# Root endpoint
@app.route("/")
def root():
return jsonify(
{
"service": "ATOM OAuth Server (Emergency Fix)",
"status": "running",
"endpoints": [
"/healthz",
"/api/auth/oauth-status",
"/api/auth/services",
"/api/auth/{service}/authorize",
"/api/auth/{service}/status",
"/api/auth/{service}/callback",
],
}
)
# OAuth status endpoint
@app.route("/api/auth/oauth-status", methods=["GET"])
def oauth_status():
user_id = request.args.get("user_id", "emergency_test_user")
results = {}
connected_count = 0
needs_credentials_count = 0
for service, config in services_config.items():
status_info = {
"ok": True,
"service": service,
"user_id": user_id,
"status": config["status"],
"client_id": config["client_id"],
"message": f"{service.title()} OAuth is {config['status'].replace('_', ' ')}",
}
results[service] = status_info
if config["status"] == "configured":
connected_count += 1
elif "placeholder" in config["client_id"]:
needs_credentials_count += 1
return jsonify(
{
"ok": True,
"user_id": user_id,
"total_services": len(services_config),
"connected_services": connected_count,
"services_needing_credentials": needs_credentials_count,
"success_rate": f"{connected_count / len(services_config) * 100:.1f}%",
"results": results,
"timestamp": datetime.now().isoformat(),
}
)
# Services list endpoint
@app.route("/api/auth/services", methods=["GET"])
def oauth_services_list():
return jsonify(
{
"ok": True,
"services": list(services_config.keys()),
"total_services": len(services_config),
"services_with_real_credentials": len(
[
s
for s, c in services_config.items()
if c.get("client_id")
and "placeholder" not in c.get("client_id", "")
]
),
"services_needing_credentials": len(
[
s
for s, c in services_config.items()
if "placeholder" in c.get("client_id", "")
]
),
"timestamp": datetime.now().isoformat(),
}
)
# OAuth authorize endpoint (works for all services)
@app.route("/api/auth/<service>/authorize", methods=["GET"])
def oauth_authorize(service):
user_id = request.args.get("user_id")
redirect_uri = request.args.get(
"redirect_uri", "http://localhost:3000/api/auth/callback"
)
if not user_id:
return jsonify({"error": "user_id parameter is required"}), 400
if service not in services_config:
return jsonify({"error": f"Service {service} not supported"}), 404
config = services_config[service]
if "placeholder" in config["client_id"]:
return jsonify(
{
"ok": True,
"service": service,
"user_id": user_id,
"status": "needs_credentials",
"message": f"{service.title()} OAuth needs real credentials",
"setup_guide": f"Set {service.upper()}_CLIENT_ID and {service.upper()}_CLIENT_SECRET in .env",
"credentials": "placeholder",
"available_services": list(services_config.keys()),
"auth_url": config["auth_url"],
"timestamp": datetime.now().isoformat(),
}
), 200
# Generate authorization URL for real credentials
csrf_token = secrets.token_urlsafe(32)
state = f"csrf_token={csrf_token}&service={service}&user_id={user_id}"
auth_params = {
"client_id": config["client_id"],
"redirect_uri": redirect_uri,
"response_type": "code",
"state": state,
"scope": " ".join(config.get("scopes", [])),
}
if service in ["github"]:
auth_params["scope"] = " ".join(config["scopes"])
elif service in ["google", "gmail"]:
auth_params.update({"access_type": "offline", "prompt": "consent"})
elif service == "slack":
auth_params["scope"] = " ".join(config["scopes"])
auth_url = f"{config['auth_url']}?{urllib.parse.urlencode(auth_params)}"
return jsonify(
{
"ok": True,
"service": service,
"user_id": user_id,
"auth_url": auth_url,
"csrf_token": csrf_token,
"client_id": config["client_id"],
"credentials": "real",
"scopes": config.get("scopes", []),
"redirect_uri": redirect_uri,
"message": f"{service.title()} OAuth authorization URL generated successfully",
"timestamp": datetime.now().isoformat(),
}
)
# OAuth status endpoint (specific service)
@app.route("/api/auth/<service>/status", methods=["GET"])
def oauth_status_service(service):
if service not in services_config:
return jsonify({"error": f"Service {service} not supported"}), 404
config = services_config[service]
return jsonify(
{
"ok": True,
"service": service,
"user_id": request.args.get("user_id", "emergency_test_user"),
"status": config["status"],
"client_id": config["client_id"],
"scopes": config.get("scopes", []),
"auth_url": config["auth_url"],
"last_check": datetime.now().isoformat(),
"message": f"{service.title()} OAuth is {config['status'].replace('_', ' ')}",
"timestamp": datetime.now().isoformat(),
}
)
# OAuth callback endpoint
@app.route("/api/auth/<service>/callback", methods=["GET", "POST"])
def oauth_callback(service):
if service not in services_config:
return jsonify({"error": f"Service {service} not supported"}), 404
code = request.args.get("code")
state = request.args.get("state")
error = request.args.get("error")
if error:
return jsonify(
{
"ok": True,
"service": service,
"error": error,
"message": f"{service.title()} OAuth failed with error: {error}",
"redirect": f"/settings?service={service}&status=error&error={error}",
"timestamp": datetime.now().isoformat(),
}
)
return jsonify(
{
"ok": True,
"service": service,
"code": code,
"state": state,
"message": f"{service.title()} OAuth callback received successfully",
"redirect": f"/settings?service={service}&status=connected",
"token_exchange": "Use this code to exchange for access tokens",
"timestamp": datetime.now().isoformat(),
}
)
return app
if __name__ == "__main__":
app = create_improved_oauth_server()
print("🚨 ATOM EMERGENCY OAUTH SERVER")
print("=" * 45)
print("🌐 Server starting on http://localhost:5058")
print("📋 Available OAuth Services:")
print(" - github")
print(" - google")
print(" - slack")
print("📋 Available Endpoints:")
print(" - GET /healthz")
print(" - GET /api/auth/oauth-status")
print(" - GET /api/auth/services")
print(" - GET /api/auth/{service}/authorize")
print(" - GET /api/auth/{service}/status")
print(" - GET/POST /api/auth/{service}/callback")
print("=" * 45)
try:
app.run(host="0.0.0.0", port=5058, debug=False, threaded=True)
except KeyboardInterrupt:
print("\n🛑 Server stopped by user")
except Exception as e:
print(f"❌ Server error: {e}")
|