Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,43 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
-
import requests
|
| 3 |
import json
|
| 4 |
import base64
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
# GitHub credentials
|
| 9 |
-
GITHUB_TOKEN = '
|
| 10 |
REPO_OWNER = 'hussein2000-oo'
|
| 11 |
REPO_NAME = 'dbailloolloloolollhrthlnewrgnk'
|
| 12 |
USER_FILE_NAME = 'user.json'
|
| 13 |
|
|
|
|
| 14 |
# Function to fetch user data from GitHub
|
| 15 |
def fetch_user_data():
|
| 16 |
url = f'https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/contents/{USER_FILE_NAME}'
|
| 17 |
headers = {'Authorization': f'token {GITHUB_TOKEN}'}
|
| 18 |
response = requests.get(url, headers=headers)
|
| 19 |
-
|
| 20 |
if response.status_code == 200:
|
| 21 |
content = response.json()
|
| 22 |
user_data = json.loads(base64.b64decode(content['content']).decode('utf-8'))
|
| 23 |
-
return user_data
|
| 24 |
else:
|
| 25 |
-
return
|
| 26 |
|
| 27 |
-
# Function to update user data on GitHub
|
| 28 |
-
def update_user_data(user_data, sha):
|
| 29 |
-
url = f'https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/contents/{USER_FILE_NAME}'
|
| 30 |
-
headers = {'Authorization': f'token {GITHUB_TOKEN}'}
|
| 31 |
-
|
| 32 |
-
updated_content = base64.b64encode(json.dumps(user_data).encode('utf-8')).decode('utf-8')
|
| 33 |
-
|
| 34 |
-
payload = {
|
| 35 |
-
"message": "Update user.json with new password",
|
| 36 |
-
"content": updated_content,
|
| 37 |
-
"sha": sha
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
response = requests.put(url, headers=headers, json=payload)
|
| 41 |
-
return response.status_code == 200
|
| 42 |
|
| 43 |
-
@app.route('/
|
| 44 |
-
def
|
| 45 |
data = request.json
|
| 46 |
username = data.get('username')
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
user_data
|
| 49 |
-
|
| 50 |
-
if username in user_data:
|
| 51 |
questions = user_data[username]['security_questions']
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
for question in questions:
|
| 55 |
-
answer = input(f"{question}: ")
|
| 56 |
-
answers[question] = answer
|
| 57 |
-
|
| 58 |
-
# Check if answers match
|
| 59 |
-
if all(user_data[username]['security_questions'][q] == answers[q] for q in questions):
|
| 60 |
-
new_password = input("Enter your new password: ")
|
| 61 |
-
user_data[username]['password'] = new_password
|
| 62 |
-
if update_user_data(user_data, sha):
|
| 63 |
-
return jsonify({"message": "Password reset successfully."}), 200
|
| 64 |
-
else:
|
| 65 |
-
return jsonify({"message": "Failed to update user data."}), 500
|
| 66 |
-
else:
|
| 67 |
-
return jsonify({"message": "Security answers do not match."}), 403
|
| 68 |
else:
|
| 69 |
-
return jsonify({"
|
| 70 |
|
| 71 |
|
| 72 |
if __name__ == '__main__':
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
|
|
|
| 2 |
import json
|
| 3 |
import base64
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
# GitHub credentials
|
| 9 |
+
GITHUB_TOKEN = 'your_github_token' # Set your token in environment variables
|
| 10 |
REPO_OWNER = 'hussein2000-oo'
|
| 11 |
REPO_NAME = 'dbailloolloloolollhrthlnewrgnk'
|
| 12 |
USER_FILE_NAME = 'user.json'
|
| 13 |
|
| 14 |
+
|
| 15 |
# Function to fetch user data from GitHub
|
| 16 |
def fetch_user_data():
|
| 17 |
url = f'https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/contents/{USER_FILE_NAME}'
|
| 18 |
headers = {'Authorization': f'token {GITHUB_TOKEN}'}
|
| 19 |
response = requests.get(url, headers=headers)
|
| 20 |
+
|
| 21 |
if response.status_code == 200:
|
| 22 |
content = response.json()
|
| 23 |
user_data = json.loads(base64.b64decode(content['content']).decode('utf-8'))
|
| 24 |
+
return user_data
|
| 25 |
else:
|
| 26 |
+
return None # Return None if fetching fails
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
@app.route('/get_security_questions', methods=['POST'])
|
| 30 |
+
def get_security_questions():
|
| 31 |
data = request.json
|
| 32 |
username = data.get('username')
|
| 33 |
+
|
| 34 |
+
user_data = fetch_user_data()
|
| 35 |
|
| 36 |
+
if user_data and username in user_data:
|
|
|
|
|
|
|
| 37 |
questions = user_data[username]['security_questions']
|
| 38 |
+
return jsonify({"security_questions": list(questions.keys())}), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
else:
|
| 40 |
+
return jsonify({"error": "User not found or data fetch failed."}), 404
|
| 41 |
|
| 42 |
|
| 43 |
if __name__ == '__main__':
|