2017-08-09 24 views
0

私はDjangoフレームワークの初心者です。私はログインページに何か特別な防御を追加しようとしています。confirm_login_allowedでのrecaptchaの確認方法は?

私は組み込みのDjangoの認証システムを使用しており、recaptcha検証を追加したいと考えています。

confirm_login_allowedメソッドでreCAPTCHA検証を追加することは可能ですか?

class MyAuthenticationForm(AuthenticationForm): 
def confirm_login_allowed(self, user): 
    recaptcha_response = request.POST.get('g-recaptcha-response') # Here is the stuck! 
    url = 'https://www.google.com/recaptcha/api/siteverify' 
    values = { 
     'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY, 
     'response': recaptcha_response 
    } 
    data = urllib.parse.urlencode(values).encode() 
    req = urllib.request.Request(url, data=data) 
    response = urllib.request.urlopen(req) 
    result = json.loads(response.read().decode()) 

    if not result['success']: 
     raise forms.ValidationError('Captcha Validation Failed.', code='invalid_captcha') 

をそして、私は、この行でエラーを持っていることを確認:私のforms.pyで

recaptcha_response = request.POST.get('g-recaptcha-response') 

この方法で要求を取得する方法はありますか良い方法はthrereです?

アドバイスありがとうございます!

答えて

0

conform_login_allowed(self, user):の中にreCAPTCHAを挿入することはお勧めできません。 でユーザーを制限すると、ログイン機能を担当するURLにマップされているビューが非常に始まります。何かのような:

#All the dependencies. 
from django.conf import settings 
from django.contrib.auth.forms import AuthenticationForm 
import requests 

def login(request): 
    if request.method == 'POST': 
     recaptcha_response = request.POST.get('g-recaptcha-response' , None) 
     data = { 
      'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY, 
      'response': recaptcha_response 
     } 

     r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data) 
     result = r.json() 
     if result['success']: 
      pass 
     else: 
      return HttpResponse("Error! reCAPTCHA verification failed.") 
     #Now you are done with the reCAPTCHA verification. Now bound the AuthenticationForm as described below. 
     credentials = dict() 
     credentials['username'] = request.POST['username'] 
     credentials['password'] = request.POST['password'] 

     form = AuthenticationForm(data=credentials) 
     #... Rest of your logic ... 
     #from here you can do any further validation inside your confirm_login_allowed(self, user) method in the forms. 
     #Or simply log-in the user to the django server in the form's confirm_login_allowed(self, user). 
     #Basically whatever you were doing before. 
     #Except the reCAPTCHA validation part. 

    else: 
     #... request.method == 'GET' ... 
     #Code to accept GET requests only. LOGIN FORM gets displayed from this block. 

    return render(request, 'login.html', {'form': form}) 

質問に情報が少し不完全であったので、私は自分の知る限りでそれに答えました。これがあなたが望むものでないなら、私に教えてください。私は別のアプローチを試みます。 PS:reCAPTCHA検証をデコレータ内に置くことは、DRYの原則に従うことができます。それも探してください。これが動作すれば。

これが役に立ちます。ありがとう。

+0

ありがとうございます。しかし、私はLogInViewクラスのようなクラスベースのビューに基づいてDjangoの認証システムに組み込まれているので、関数ベースのビューを避けるようにしています。最新のDjangoリリースでは、login()とlogout()関数は推奨されていません。 そして私はrecaptcha応答を得るための解決策を見つけます。 の代わりに recaptcha_response = request.POST.get( 'g-recaptcha-response') recaptcha_response = self.data ['g-recaptcha-response']を使用しています –