-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflask_middleware.py
More file actions
263 lines (218 loc) · 8.01 KB
/
flask_middleware.py
File metadata and controls
263 lines (218 loc) · 8.01 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
"""
Flask Middleware and Decorators for AuthentiChip JWT Validation
Usage:
from flask import Flask
from flask_middleware import require_authentichip, authentichip_optional
app = Flask(__name__)
@app.route('/product/<id>')
@authentichip_optional
def product(id):
if request.chip_verified:
return f"Verified chip: {request.chip_id}"
else:
return "Unverified scan"
@app.route('/protected')
@require_authentichip
def protected():
return f"Access granted to chip: {request.chip_id}"
"""
from functools import wraps
from flask import request, jsonify, g
import logging
from validate_jwt import validate_authentichip_jwt
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('authentichip')
def authentichip_optional(f):
"""
Decorator that attempts to validate AuthentiChip JWT but continues
regardless of validation result.
Adds the following attributes to the request object:
- request.chip_id: Verified chip ID (SHA-256 hash) or None
- request.chip_uid: 7-byte chip UID or None
- request.chip_verified: Boolean indicating if chip was verified
- request.chip_status: 'verified', 'expired', 'invalid', 'insecure', 'error', or 'none'
- request.chip_raw_uid: Raw UID for unverified scans
Usage:
@app.route('/product/<id>')
@authentichip_optional
def product(id):
if request.chip_verified:
return f"Verified: {request.chip_id}"
return "Not verified"
"""
@wraps(f)
def decorated_function(*args, **kwargs):
vkjwt = request.args.get('vkjwt')
vkstatus = request.args.get('vkstatus')
vkuid = request.args.get('vkuid')
# Initialize request attributes
request.chip_id = None
request.chip_uid = None
request.chip_verified = False
request.chip_status = 'none'
request.chip_raw_uid = None
# Attempt JWT validation
if vkjwt:
try:
result = validate_authentichip_jwt(vkjwt)
request.chip_id = result['chipId']
request.chip_uid = result['uid']
request.chip_verified = True
request.chip_status = 'verified'
logger.info(
f'AuthentiChip verified: {result["chipId"]} (UID: {result["uid"]}) '
f'from {request.remote_addr}'
)
except Exception as e:
error_msg = str(e).lower()
if 'expired' in error_msg:
request.chip_status = 'expired'
elif 'signature' in error_msg:
request.chip_status = 'invalid'
else:
request.chip_status = 'error'
logger.warning(
f'AuthentiChip validation failed: {e} '
f'from {request.remote_addr}'
)
# Handle unverified scans
elif vkstatus and vkuid:
request.chip_raw_uid = vkuid
request.chip_status = vkstatus
request.chip_verified = False
logger.info(
f'AuthentiChip unverified scan: '
f'UID={vkuid}, status={vkstatus} '
f'from {request.remote_addr}'
)
return f(*args, **kwargs)
return decorated_function
def require_authentichip(f):
"""
Decorator that requires valid AuthentiChip JWT authentication.
Returns 401 if validation fails or no JWT is present.
Adds the following attributes to the request object:
- request.chip_id: Verified chip ID (SHA-256 hash)
- request.chip_uid: 7-byte chip UID
- request.chip_verified: Always True (or request aborted)
- request.chip_status: Always 'verified' (or request aborted)
Usage:
@app.route('/protected')
@require_authentichip
def protected():
return f"Access granted: {request.chip_id}"
"""
@wraps(f)
def decorated_function(*args, **kwargs):
vkjwt = request.args.get('vkjwt')
if not vkjwt:
logger.warning(
f'AuthentiChip required but not provided '
f'from {request.remote_addr}'
)
return jsonify({
'error': 'Authentication required',
'message': 'No chip authentication provided'
}), 401
try:
result = validate_authentichip_jwt(vkjwt)
request.chip_id = result['chipId']
request.chip_uid = result['uid']
request.chip_verified = True
request.chip_status = 'verified'
logger.info(
f'AuthentiChip verified: {result["chipId"]} (UID: {result["uid"]}) '
f'from {request.remote_addr}'
)
return f(*args, **kwargs)
except Exception as e:
error_msg = str(e).lower()
if 'expired' in error_msg:
status = 'expired'
message = 'This scan is too old. Please scan again.'
elif 'signature' in error_msg:
status = 'invalid'
message = 'This chip could not be verified.'
else:
status = 'error'
message = 'Unable to verify chip.'
logger.error(
f'AuthentiChip validation failed: {e} '
f'from {request.remote_addr}'
)
return jsonify({
'error': 'Invalid chip authentication',
'message': message,
'status': status
}), 401
return decorated_function
"""
Example Flask application
"""
if __name__ == '__main__':
from flask import Flask
app = Flask(__name__)
@app.route('/')
@authentichip_optional
def index():
return jsonify({
'chip_verified': request.chip_verified,
'chip_id': request.chip_id,
'chip_uid': request.chip_uid,
'chip_status': request.chip_status,
'message': (
f'Welcome! Verified chip: {request.chip_id} (UID: {request.chip_uid})'
if request.chip_verified
else 'No verified chip detected'
)
})
@app.route('/product/<product_id>')
@authentichip_optional
def product(product_id):
product_data = {
'id': product_id,
'name': 'Example Product',
'verified': request.chip_verified
}
if request.chip_verified:
product_data['chip_id'] = request.chip_id
product_data['uid'] = request.chip_uid
product_data['message'] = 'This is a verified authentic product'
elif request.chip_status in ('insecure', 'expired'):
product_data['message'] = 'Verification was unavailable'
product_data['uid'] = request.chip_raw_uid
product_data['status'] = request.chip_status
else:
product_data['message'] = 'No chip scan detected'
return jsonify(product_data)
@app.route('/protected')
@require_authentichip
def protected():
return jsonify({
'message': 'Access granted',
'chip_id': request.chip_id,
'uid': request.chip_uid
})
@app.route('/optional')
@authentichip_optional
def optional():
if request.chip_verified:
return jsonify({
'level': 'premium',
'chip_id': request.chip_id,
'uid': request.chip_uid,
'content': 'Full access granted'
})
else:
return jsonify({
'level': 'basic',
'content': 'Limited access'
})
PORT = 5001
print(f'Flask server running on port {PORT}')
print(f'\nTest with:')
print(f' http://localhost:{PORT}?vkjwt=<token>')
print(f' http://localhost:{PORT}/product/123?vkuid=ABC&vkstatus=insecure')
print(f' http://localhost:{PORT}/protected?vkjwt=<token>')
app.run(debug=True, port=PORT)