-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2.4-sample-solution.py
More file actions
299 lines (242 loc) · 7.97 KB
/
Copy path2.4-sample-solution.py
File metadata and controls
299 lines (242 loc) · 7.97 KB
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
from flask import Flask, request, jsonify, abort, current_app, g, make_response
import secrets
import psutil
import socket
import time
from functools import wraps
import hmac
import hashlib
from datetime import datetime, timedelta
import jwt
"""
Flask
psutil
PyJWT
"""
# should use env variables
HASH_SECRET = b"dev-secret-change-me"
JWT_SECRET = b"dev-secret-change-me"
def hash_key(api_key: str) -> str:
"""
hash_key("secretapikey123")
'a9be16b5989c1cffc7e91a81143c6053362340717cbfc98b3a07ffcbe931f396'
hash_key('secretapikey1')
'deda7fdcf493cae490ea6b7889bc032799d5c1459085cd59bcb6f38ff6f4045a'
hash_key('abc123')
'8e024929eb9be0f39c3fb4e0f58bb5f2e8c9ccf81d1723e4c78729d3d0b135f0'
hash_key("password1")
'6cbfeac955cd5296ec7394a3d845c0b2f53603fb6fd49629b2b6371bf39ab4f7'
hash_key("password2")
'a3a232a44f8017ae2d673ae57b5b132f5153d1f117e89008e2f6098f2880a2f2'
"""
return hmac.new(HASH_SECRET, api_key.encode(), hashlib.sha256).hexdigest()
def keys_match(api_key: str, stored_hash: str) -> bool:
return hmac.compare_digest(hash_key(api_key), stored_hash)
app = Flask(__name__)
# -----------------------
# In-memory data store
# -----------------------
data = {
"interfaces": {},
"notes": {}
}
# In-memory user/key database
user_db = {
"alice": {
"api_key": ["deda7fdcf493cae490ea6b7889bc032799d5c1459085cd59bcb6f38ff6f4045a"], # secretapikey1
"role": "admin",
"password" : "6cbfeac955cd5296ec7394a3d845c0b2f53603fb6fd49629b2b6371bf39ab4f7" # password1
},
"bob": {
"api_key": ["8e024929eb9be0f39c3fb4e0f58bb5f2e8c9ccf81d1723e4c78729d3d0b135f0"], # abc123
"role": "user",
"password" : "a3a232a44f8017ae2d673ae57b5b132f5153d1f117e89008e2f6098f2880a2f2" # password2
}
}
def jwt_protected(func):
@wraps(func)
def wrapper(*args, **kwargs):
token = request.cookies.get('jwt')
if not token:
return jsonify({'error': 'Missing token'}), 401
try:
g.jwt_payload = jwt.decode(token, JWT_SECRET, algorithms=['HS256'])
except jwt.ExpiredSignatureError:
abort(401, description="Token Expired")
except jwt.InvalidTokenError:
abort(401, description="Invalid Token")
return func(*args, **kwargs)
return wrapper
@app.errorhandler(401)
def unauthorized(e):
return jsonify({
"error": e.description
}), 401
def api_protected(func):
@wraps(func)
def wrapper(*args, **kwargs):
current_app.logger.info("api_protected running")
auth = request.headers.get("Authorization", "")
username = request.headers.get("Username", "")
if not username:
abort(401, description="Missing Username header")
if auth.startswith("Bearer "):
key = auth.split(" ", 1)[1]
info = user_db.get(username)
for v in info.get('api_key', []):
if keys_match(key, v):
return func(*args, **kwargs)
abort(401, description="Invalid or missing API key or username")
return wrapper
def load_data():
data["interfaces"] = {}
for iface, addrs in psutil.net_if_addrs().items():
data["interfaces"][iface] = []
for a in addrs:
fam = "MAC"
if a.family == socket.AF_INET:
fam = "IPv4"
elif a.family == socket.AF_INET6:
fam = "IPv6"
data["interfaces"][iface].append({
"family": fam,
"address": a.address
})
load_data()
@app.route('/login', methods=['POST'])
def login():
"""
curl -i -X POST http://localhost:8000/login \
-H "Content-Type: application/json" \
-d '{"username":"alice","password":"password1"}' \
-c cookies.txt
"""
data = request.get_json()
username = data.get('username')
password = data.get('password')
if user_db.get(username)["password"] == hash_key(password):
payload = {
'username': username,
'exp': datetime.utcnow() + timedelta(minutes=5)
}
token = jwt.encode(payload, JWT_SECRET, algorithm='HS256')
resp = make_response({'message': 'Logged in'})
resp.set_cookie('jwt', token, httponly=True, samesite='Lax')
return resp
abort(401, description='Invalid credentials')
@app.route("/chpasswd", methods=["POST"])
@jwt_protected
def chpasswd():
"""
Updates a user password
curl -i -X POST http://localhost:8000/chpasswd \
-H "Content-Type: application/json" \
-d '{"new_password":"newsecret123"}' \
-b cookies.txt
"""
data = request.get_json()
new_password = hash_key(data.get('new_password'))
current_user = g.jwt_payload.get("username")
user_db[current_user]["password"] = new_password
return jsonify({"message": "Password Updatted"})
@app.route("/api/newkey", methods=["POST"])
@jwt_protected
def api_new_key():
"""
Updates a user password
curl -i -X POST http://localhost:8000/api/newkey \
-H "Content-Type: application/json" \
-b cookies.txt
"""
api_key = secrets.token_urlsafe(32)
api_hash = hash_key(api_key)
current_user = g.jwt_payload.get("username")
user_db[current_user]["api_key"].append(api_hash)
return jsonify({"new_api_key": api_key})
@app.route("/<resource>", methods=["GET"])
@api_protected
def get_all(resource):
"""
Get all entries for a resource.
Example:
curl -i \
-H "Username: alice" \
-H "Authorization: Bearer secretapikey1" \
http://localhost:8000/interfaces
"""
if resource not in data:
return jsonify({"error": "not found"}), 404
return jsonify(data[resource])
@app.route("/<resource>", methods=["POST"])
@api_protected
def create(resource):
"""
Create a new entry in a resource (requires JSON body with 'id').
Example:
curl -i -X POST \
-H "Content-Type: application/json" \
-H "Username: alice" \
-H "Authorization: Bearer secretapikey1" \
-d '{"id":"test1","value":"hello"}' \
http://localhost:8000/notes
"""
body = request.json
if not body or "id" not in body:
return jsonify({"error": "id required"}), 400
data.setdefault(resource, {})
data[resource][body["id"]] = body
return jsonify(body), 201
@app.route("/<resource>/<id>", methods=["PUT"])
@api_protected
def update(resource, id):
"""
Update an existing entry by id.
Example:
curl -i -X PUT \
-H "Content-Type: application/json" \
-H "Username: alice" \
-H "Authorization: Bearer secretapikey1" \
-d '{"value":"updated"}' \
http://localhost:8000/notes/test1
"""
if resource not in data or id not in data[resource]:
return jsonify({"error": "not found"}), 404
data[resource][id].update(request.json)
return jsonify(data[resource][id])
@app.route("/<resource>/<id>", methods=["DELETE"])
@api_protected
def delete(resource, id):
"""
Delete an entry by id.
Example:
curl -i -X DELETE \
-H "Username: alice" \
-H "Authorization: Bearer secretapikey1" \
http://localhost:8000/notes/test1
"""
if resource not in data or id not in data[resource]:
return jsonify({"error": "not found"}), 404
del data[resource][id]
return "", 204
@app.route("/search")
@api_protected
def search():
"""
Search across all resources for a term.
Example:
curl -i \
-H "Username: alice" \
-H "Authorization: Bearer secretapikey1" \
"http://localhost:8000/search?q=ipv4"
"""
q = request.args.get("q", "").lower()
results = []
for res, items in data.items():
for k, v in items.items():
blob = str(k).lower() + str(v).lower()
if q in blob:
results.append({res: {k: v}})
return jsonify(results)
if __name__ == "__main__":
context = ('cert.pem', 'key.pem')
app.run(debug=True, host="0.0.0.0", port=8000)