Added new method to verify
This commit is contained in:
parent
2beca59775
commit
adddfb2737
3
.gitignore
vendored
3
.gitignore
vendored
@ -55,8 +55,7 @@ coverage.xml
|
|||||||
|
|
||||||
# Node (if using JS build tools)
|
# Node (if using JS build tools)
|
||||||
node_modules/
|
node_modules/
|
||||||
|
build/
|
||||||
|
|
||||||
|
|
||||||
# Docker
|
# Docker
|
||||||
*.pid
|
*.pid
|
||||||
|
@ -39,19 +39,25 @@ from .settings import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Built-in key functions
|
# Built-in key functions
|
||||||
def get_ip(request):
|
def get_ip(group, request):
|
||||||
return request.META.get('REMOTE_ADDR', 'unknown-ip')
|
return request.META.get('REMOTE_ADDR', 'unknown-ip')
|
||||||
|
|
||||||
def get_user(request):
|
def user_or_session(group, request):
|
||||||
|
if request.user.is_authenticated:
|
||||||
|
return str(request.user.pk)
|
||||||
|
return request.session.session_key or request.META.get("REMOTE_ADDR")
|
||||||
|
|
||||||
|
def get_user(group, request):
|
||||||
return str(request.user.id) if request.user.is_authenticated else None
|
return str(request.user.id) if request.user.is_authenticated else None
|
||||||
|
|
||||||
def get_user_or_ip(request):
|
def get_user_or_ip(group, request):
|
||||||
return get_user(request) or get_ip(request)
|
return get_user(request) or get_ip(request)
|
||||||
|
|
||||||
BUILTIN_KEYS = {
|
BUILTIN_KEYS = {
|
||||||
'ip': get_ip,
|
'ip': get_ip,
|
||||||
'user': get_user,
|
'user': get_user,
|
||||||
'user_or_ip': get_user_or_ip,
|
'user_or_ip': get_user_or_ip,
|
||||||
|
'user_or_session': user_or_session
|
||||||
}
|
}
|
||||||
|
|
||||||
def hoptcha_protected(
|
def hoptcha_protected(
|
||||||
@ -107,7 +113,7 @@ def hoptcha_protected(
|
|||||||
if request.method not in methods:
|
if request.method not in methods:
|
||||||
return view_func(request, *args, **kwargs)
|
return view_func(request, *args, **kwargs)
|
||||||
|
|
||||||
user_key = key_func(request)
|
user_key = key_func(None, request)
|
||||||
if not shared:
|
if not shared:
|
||||||
user_key = f"{user_key}:{request.path}"
|
user_key = f"{user_key}:{request.path}"
|
||||||
|
|
||||||
@ -129,6 +135,7 @@ def hoptcha_protected(
|
|||||||
pass # Malformed or empty JSON
|
pass # Malformed or empty JSON
|
||||||
|
|
||||||
if not token or not verify_token(token):
|
if not token or not verify_token(token):
|
||||||
|
print(token)
|
||||||
return response(request) if response else JsonResponse({
|
return response(request) if response else JsonResponse({
|
||||||
"captcha": True,
|
"captcha": True,
|
||||||
"url": f"{HOPTCHA_URL}?{urlencode({'client_key': HOPTCHA_CLIENT_ID, 'timestamp': int(time.time() * 1000), 'type': type})}"
|
"url": f"{HOPTCHA_URL}?{urlencode({'client_key': HOPTCHA_CLIENT_ID, 'timestamp': int(time.time() * 1000), 'type': type})}"
|
||||||
|
@ -28,7 +28,7 @@ def get(key, default=None):
|
|||||||
return getattr(settings, key, default)
|
return getattr(settings, key, default)
|
||||||
|
|
||||||
HOPTCHA_URL = get('HOPTCHA_URL', 'https://hoptcha.com/captcha/')
|
HOPTCHA_URL = get('HOPTCHA_URL', 'https://hoptcha.com/captcha/')
|
||||||
HOPTCHA_VERIFY_URL = get('CAPTCHA_VERIFY_URL', 'https://hoptcha.com/captcha/validate/')
|
HOPTCHA_VERIFY_URL = get('HOPTCHA_VERIFY_URL', 'https://hoptcha.com/captcha/validate/')
|
||||||
|
|
||||||
HOPTCHA_CLIENT_ID = get('HOPTCHA_CLIENT_ID', '')
|
HOPTCHA_CLIENT_ID = get('HOPTCHA_CLIENT_ID', '')
|
||||||
HOPTCHA_CLIENT_SECRET = get('HOPTCHA_CLIENT_SECRET', '')
|
HOPTCHA_CLIENT_SECRET = get('HOPTCHA_CLIENT_SECRET', '')
|
||||||
|
@ -41,7 +41,7 @@ def captcha_placeholder():
|
|||||||
@register.simple_tag
|
@register.simple_tag
|
||||||
def captcha_iframe():
|
def captcha_iframe():
|
||||||
context = {
|
context = {
|
||||||
'captcha_url': getattr(settings, 'HOPTCHA_URL', '#'),
|
'captcha_url': HOPTCHA_URL,
|
||||||
'public_key': getattr(settings, 'HOPTCHA_CLIENT_ID', ''),
|
'public_key': HOPTCHA_CLIENT_ID,
|
||||||
}
|
}
|
||||||
return render_to_string('django_hoptcha/captcha_iframe.html', context)
|
return render_to_string('django_hoptcha/captcha_iframe.html', context)
|
||||||
|
@ -42,6 +42,8 @@ def verify_token(token):
|
|||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
response = requests.post(HOPTCHA_VERIFY_URL, data=json.dumps(payload), headers=headers, timeout=5)
|
response = requests.post(HOPTCHA_VERIFY_URL, data=json.dumps(payload), headers=headers, timeout=5)
|
||||||
|
|
||||||
|
print(HOPTCHA_VERIFY_URL, HOPTCHA_CLIENT_SECRET)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
return response.json().get("success", False)
|
return response.json().get("success", False)
|
||||||
return False
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user