60 lines
2.2 KiB
Python
60 lines
2.2 KiB
Python
# backend/routes/credentials.py
|
|
from flask import Blueprint, request, jsonify
|
|
from datetime import datetime
|
|
import json
|
|
|
|
from ..db import get_db_connection, release_db_connection
|
|
|
|
credentials_blueprint = Blueprint('credentials', __name__)
|
|
|
|
@credentials_blueprint.route('/credentials', methods=['GET'])
|
|
def list_credentials():
|
|
"""Return configured platforms (without exposing secrets)."""
|
|
conn = get_db_connection()
|
|
cur = conn.cursor()
|
|
cur.execute("SELECT platform FROM credentials")
|
|
rows = cur.fetchall()
|
|
release_db_connection(conn)
|
|
|
|
platforms = [r[0] for r in rows]
|
|
return jsonify({"configured_platforms": platforms})
|
|
|
|
@credentials_blueprint.route('/credentials/<string:platform>', methods=['POST'])
|
|
def save_credentials(platform):
|
|
"""Save or update credentials for a specific platform."""
|
|
data = request.json
|
|
api_key = data.get('api_key')
|
|
client_id = data.get('client_id')
|
|
client_secret = data.get('client_secret')
|
|
other_data = data.get('other_data', {})
|
|
|
|
conn = get_db_connection()
|
|
cur = conn.cursor()
|
|
|
|
cur.execute(
|
|
"""INSERT INTO credentials (platform, api_key, client_id, client_secret, other_data, updated_at)
|
|
VALUES (%s, %s, %s, %s, %s, %s)
|
|
ON CONFLICT (platform) DO UPDATE SET
|
|
api_key = EXCLUDED.api_key,
|
|
client_id = EXCLUDED.client_id,
|
|
client_secret = EXCLUDED.client_secret,
|
|
other_data = EXCLUDED.other_data,
|
|
updated_at = EXCLUDED.updated_at""",
|
|
(platform, api_key, client_id, client_secret, json.dumps(other_data), datetime.utcnow())
|
|
)
|
|
conn.commit()
|
|
release_db_connection(conn)
|
|
|
|
return jsonify({"message": f"Credentials for {platform} saved successfully."}), 200
|
|
|
|
@credentials_blueprint.route('/credentials/<string:platform>', methods=['DELETE'])
|
|
def delete_credentials(platform):
|
|
"""Delete credentials for a specific platform."""
|
|
conn = get_db_connection()
|
|
cur = conn.cursor()
|
|
cur.execute("DELETE FROM credentials WHERE platform = %s", (platform,))
|
|
conn.commit()
|
|
release_db_connection(conn)
|
|
|
|
return jsonify({"message": f"Credentials for {platform} deleted."}), 200
|