hussein2000 commited on
Commit
79e247b
·
verified ·
1 Parent(s): c8e724a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -79
app.py CHANGED
@@ -6,7 +6,7 @@ import base64
6
  app = Flask(__name__)
7
 
8
  # GitHub credentials from environment variables
9
- GITHUB_TOKEN = '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' # Set your token in environment variables
10
  REPO_OWNER = 'hussein2000-oo'
11
  REPO_NAME = 'dbailloolloloolollhrthlnewrgnk'
12
  USER_FILE_NAME = 'user.json'
@@ -20,18 +20,10 @@ def fetch_user_data():
20
  if response.status_code == 200:
21
  content = response.json()
22
  user_data = json.loads(base64.b64decode(content['content']).decode('utf-8'))
23
-
24
- # Ensure user_data is a dictionary
25
- if not isinstance(user_data, dict):
26
- print("User data is not in the expected format. Initializing empty user data.")
27
- user_data = {}
28
-
29
  return user_data, content['sha'] # Return the SHA for updating the file
30
  else:
31
- print("Failed to fetch user data:", response.status_code, response.json())
32
  return {}, None # Return an empty dict if fetching fails
33
 
34
-
35
  # Function to update user data on GitHub
36
  def update_user_data(user_data, sha):
37
  url = f'https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/contents/{USER_FILE_NAME}'
@@ -40,93 +32,41 @@ def update_user_data(user_data, sha):
40
  updated_content = base64.b64encode(json.dumps(user_data).encode('utf-8')).decode('utf-8')
41
 
42
  payload = {
43
- "message": "Update user.json with new user",
44
  "content": updated_content,
45
  "sha": sha
46
  }
47
 
48
  response = requests.put(url, headers=headers, json=payload)
49
-
50
- if response.status_code == 200:
51
- print("User data updated successfully.")
52
- else:
53
- print("Failed to update user data:", response.status_code, response.json())
54
-
55
-
56
- # API endpoint to create a user account
57
- @app.route('/api/create_user', methods=['POST'])
58
- def create_user():
59
- data = request.json
60
- username = data.get('username')
61
- password = data.get('password')
62
- first_name = data.get('first_name')
63
- last_name = data.get('last_name')
64
- birthday = data.get('birthday')
65
- security_questions = data.get('security_questions')
66
-
67
- user_data, sha = fetch_user_data()
68
-
69
- if user_data is not None:
70
- if username in user_data:
71
- return jsonify({"message": "User already exists."}), 400
72
- else:
73
- user_data[username] = {
74
- "password": password,
75
- "first_name": first_name,
76
- "last_name": last_name,
77
- "birthday": birthday,
78
- "security_questions": security_questions # Store security questions
79
- }
80
- update_user_data(user_data, sha)
81
- return jsonify({"message": f"User {username} created successfully."}), 201
82
- else:
83
- return jsonify({"message": "Could not create user. User data fetch failed."}), 500
84
 
85
-
86
- # API endpoint to sign in
87
- @app.route('/api/sign_in', methods=['POST'])
88
- def sign_in():
89
- data = request.json
90
- username = data.get('username')
91
- password = data.get('password')
92
-
93
- user_data, _ = fetch_user_data()
94
-
95
- if user_data is not None and isinstance(user_data, dict):
96
- if username in user_data:
97
- if user_data[username]['password'] == password: # Corrected password check
98
- return jsonify({"message": "Signed in successfully!"}), 200
99
- else:
100
- return jsonify({"message": "Sign in failed."}), 401
101
- else:
102
- return jsonify({"message": "User not found."}), 404
103
- else:
104
- return jsonify({"message": "Unexpected data format in user.json."}), 500
105
-
106
-
107
- # API endpoint to reset password using security questions
108
- @app.route('/api/reset_password', methods=['POST'])
109
  def reset_password():
110
  data = request.json
111
  username = data.get('username')
112
- answers = data.get('answers')
113
-
114
- user_data, _ = fetch_user_data()
115
 
116
  if username in user_data:
117
- questions = user_data[username]['security_questions ']
 
118
 
 
 
 
 
119
  # Check if answers match
120
  if all(user_data[username]['security_questions'][q] == answers[q] for q in questions):
121
- new_password = data.get('new_password')
122
  user_data[username]['password'] = new_password
123
- update_user_data(user_data, _)
124
- return jsonify({"message": "Password reset successfully."}), 200
 
 
125
  else:
126
- return jsonify({"message": "Security answers do not match."}), 401
127
  else:
128
- return jsonify({"message": "User not found."}), 404
129
-
130
 
131
 
132
  if __name__ == '__main__':
 
6
  app = Flask(__name__)
7
 
8
  # GitHub credentials from environment variables
9
+ GITHUB_TOKEN = 'your_github_token' # Replace with your GitHub token
10
  REPO_OWNER = 'hussein2000-oo'
11
  REPO_NAME = 'dbailloolloloolollhrthlnewrgnk'
12
  USER_FILE_NAME = 'user.json'
 
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, content['sha'] # Return the SHA for updating the file
24
  else:
 
25
  return {}, None # Return an empty dict if fetching fails
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}'
 
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('/reset_password', methods=['POST'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def reset_password():
45
  data = request.json
46
  username = data.get('username')
47
+
48
+ user_data, sha = fetch_user_data()
 
49
 
50
  if username in user_data:
51
+ questions = user_data[username]['security_questions']
52
+ answers = {}
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({"message": "User not found."}), 404
 
70
 
71
 
72
  if __name__ == '__main__':