Skip to content

Commit 4e5b730

Browse files
recaptchaVerification (TheAlgorithms#2417)
* recaptchaVerification * recaptchaVerification * recaptchaVerification1 * recaptchaVerification2 * recaptchaVerification3 * recaptchaVerification4 * recaptchaVerificatio5 * recaptchaVerificatio5 * recaptchaVerificatio6 * drawOnVideoStreamOpenCV * matrixInverseMCAmethod * fixingImports * recaptchaVerificationfixes * recaptchaVerificationfixes * recaptchaVerificationfixes * recaptchaVerificationfixes * recaptchaVerificationfixes1 * recaptchaVerificationfixes1 * authenticate = login = render = redirect = print Co-authored-by: Christian Clauss <[email protected]>
1 parent 44b8cb0 commit 4e5b730

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Recaptcha is a free captcha service offered by Google in order to secure websites and
3+
forms. At https://www.google.com/recaptcha/admin/create you can create new recaptcha
4+
keys and see the keys that your have already created.
5+
* Keep in mind that recaptcha doesn't work with localhost
6+
When you create a recaptcha key, your will get two separate keys: ClientKey & SecretKey.
7+
ClientKey should be kept in your site's front end
8+
SecretKey should be kept in your site's back end
9+
10+
# An example HTML login form with recaptcha tag is shown below
11+
12+
<form action="" method="post">
13+
<h2 class="text-center">Log in</h2>
14+
{% csrf_token %}
15+
<div class="form-group">
16+
<input type="text" name="username" required="required">
17+
</div>
18+
<div class="form-group">
19+
<input type="password" name="password" required="required">
20+
</div>
21+
<div class="form-group">
22+
<button type="submit">Log in</button>
23+
</div>
24+
<!-- Below is the recaptcha tag of html -->
25+
<div class="g-recaptcha" data-sitekey="ClientKey"></div>
26+
</form>
27+
28+
<!-- Below is the recaptcha script to be kept inside html tag -->
29+
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
30+
31+
Below a Django function for the views.py file contains a login form for demonstrating
32+
recaptcha verification.
33+
"""
34+
import requests
35+
36+
try:
37+
from django.contrib.auth import authenticate, login
38+
from django.shortcuts import redirect, render
39+
except ImportError:
40+
authenticate = login = render = redirect = print
41+
42+
43+
def login_using_recaptcha(request):
44+
# Enter your recaptcha secret key here
45+
secret_key = "secretKey"
46+
url = "https://www.google.com/recaptcha/api/siteverify"
47+
48+
# when method is not POST, direct user to login page
49+
if request.method != "POST":
50+
return render(request, "login.html")
51+
52+
# from the frontend, get username, password, and client_key
53+
username = request.POST.get("username")
54+
password = request.POST.get("password")
55+
client_key = request.POST.get("g-recaptcha-response")
56+
57+
# post recaptcha response to Google's recaptcha api
58+
response = requests.post(url, data={"secret": secret_key, "response": client_key})
59+
# if the recaptcha api verified our keys
60+
if response.json().get("success", False):
61+
# authenticate the user
62+
user_in_database = authenticate(request, username=username, password=password)
63+
if user_in_database:
64+
login(request, user_in_database)
65+
return redirect("/your-webpage")
66+
return render(request, "login.html")

0 commit comments

Comments
 (0)