-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdjango_middleware.py
More file actions
274 lines (226 loc) · 8.69 KB
/
django_middleware.py
File metadata and controls
274 lines (226 loc) · 8.69 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
"""
Django Middleware for AuthentiChip JWT Validation
Installation:
1. Copy this file to your Django project (e.g., myapp/middleware/authentichip.py)
2. Add to MIDDLEWARE in settings.py:
MIDDLEWARE = [
# ... other middleware
'myapp.middleware.authentichip.AuthentiChipMiddleware',
]
Usage in views:
from django.http import JsonResponse
def product_view(request, product_id):
chip_id = getattr(request, 'chip_id', None)
chip_verified = getattr(request, 'chip_verified', False)
if chip_verified:
return JsonResponse({
'verified': True,
'chip_id': chip_id
})
else:
return JsonResponse({
'verified': False
})
Decorator for required authentication:
from myapp.middleware.authentichip import require_authentichip
@require_authentichip
def protected_view(request):
# Only executes if chip is verified
return JsonResponse({
'chip_id': request.chip_id
})
"""
import logging
from django.http import JsonResponse
from django.utils.deprecation import MiddlewareMixin
from functools import wraps
# Import the validation function from validate_jwt.py
# Make sure validate_jwt.py is in the same directory or in PYTHONPATH
try:
from .validate_jwt import validate_authentichip_jwt
except ImportError:
from validate_jwt import validate_authentichip_jwt
logger = logging.getLogger('authentichip')
class AuthentiChipMiddleware(MiddlewareMixin):
"""
Django middleware for AuthentiChip JWT validation
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
"""
def process_request(self, request):
"""Process incoming request and validate AuthentiChip parameters"""
vkjwt = request.GET.get('vkjwt')
vkstatus = request.GET.get('vkstatus')
vkuid = request.GET.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 {self._get_client_ip(request)}'
)
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 {self._get_client_ip(request)}'
)
# 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 {self._get_client_ip(request)}'
)
# No return value means continue processing
return None
def _get_client_ip(self, request):
"""Get client IP address from request"""
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
return ip
def require_authentichip(view_func):
"""
Decorator that requires valid AuthentiChip JWT authentication.
Returns 401 if validation fails or no JWT is present.
Usage:
@require_authentichip
def protected_view(request):
return JsonResponse({'chip_id': request.chip_id})
"""
@wraps(view_func)
def wrapper(request, *args, **kwargs):
# Check if middleware has run
if not hasattr(request, 'chip_verified'):
logger.error('AuthentiChipMiddleware not configured')
return JsonResponse({
'error': 'Server configuration error'
}, status=500)
# Check if chip is verified
if not request.chip_verified:
chip_status = getattr(request, 'chip_status', 'none')
if chip_status == 'expired':
message = 'This scan is too old. Please scan again.'
elif chip_status == 'invalid':
message = 'This chip could not be verified.'
elif chip_status == 'none':
message = 'No chip authentication provided.'
else:
message = 'Unable to verify chip.'
logger.warning(
f'AuthentiChip required but not verified '
f'(status: {chip_status}) '
f'from {request.META.get("REMOTE_ADDR")}'
)
return JsonResponse({
'error': 'Authentication required',
'message': message,
'status': chip_status
}, status=401)
# Chip is verified, proceed with view
return view_func(request, *args, **kwargs)
return wrapper
"""
Example Django view usage
"""
if __name__ == '__main__':
# Example views showing usage patterns
def example_optional_view(request):
"""Example view with optional authentication"""
chip_id = getattr(request, 'chip_id', None)
chip_uid = getattr(request, 'chip_uid', None)
chip_verified = getattr(request, 'chip_verified', False)
chip_status = getattr(request, 'chip_status', 'none')
return JsonResponse({
'chip_verified': chip_verified,
'chip_id': chip_id,
'chip_uid': chip_uid,
'chip_status': chip_status,
'message': (
f'Welcome! Verified chip: {chip_id} (UID: {chip_uid})'
if chip_verified
else 'No verified chip detected'
)
})
@require_authentichip
def example_required_view(request):
"""Example view with required authentication"""
return JsonResponse({
'message': 'Access granted',
'chip_id': request.chip_id,
'uid': request.chip_uid
})
def example_product_view(request, product_id):
"""Example product view"""
chip_verified = getattr(request, 'chip_verified', False)
chip_id = getattr(request, 'chip_id', None)
chip_uid = getattr(request, 'chip_uid', None)
chip_status = getattr(request, 'chip_status', 'none')
chip_raw_uid = getattr(request, 'chip_raw_uid', None)
product_data = {
'id': product_id,
'name': 'Example Product',
'verified': chip_verified
}
if chip_verified:
product_data['chip_id'] = chip_id
product_data['uid'] = chip_uid
product_data['message'] = 'This is a verified authentic product'
elif chip_status in ('insecure', 'expired'):
product_data['message'] = 'Verification was unavailable'
product_data['uid'] = chip_raw_uid
product_data['status'] = chip_status
else:
product_data['message'] = 'No chip scan detected'
return JsonResponse(product_data)
print("""
Django Middleware Example
Add to settings.py:
MIDDLEWARE = [
# ... other middleware
'myapp.middleware.authentichip.AuthentiChipMiddleware',
]
Use in views:
from django.http import JsonResponse
from myapp.middleware.authentichip import require_authentichip
def product_view(request, product_id):
if request.chip_verified:
return JsonResponse({
'chip_id': request.chip_id,
'uid': request.chip_uid
})
return JsonResponse({'verified': False})
@require_authentichip
def protected_view(request):
return JsonResponse({
'chip_id': request.chip_id,
'uid': request.chip_uid
})
""")