Skip to content

Commit

Permalink
New action values
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Nov 22, 2019
1 parent a85f452 commit 39de023
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/appier_extras/parts/recaptcha/part.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ def load(self):

self.owner.context["recaptcha"] = self.recaptcha

def recaptcha(self, scope = "homepage", name = "recaptcha_token"):
def recaptcha(self, action = "homepage", name = "recaptcha_token"):
recaptcha_key = appier.conf("RECAPTCHA_KEY", None)
appier.verify(recaptcha_key, message = "No reCAPTCHA site key provided")
return self.owner.escape_template(
"<input type=\"hidden\" id=\"recaptcha-token\" name=\"%s\" />" % name +
"<script src=\"https://www.google.com/recaptcha/api.js?render=%s\"></script>" % recaptcha_key +
"<script>grecaptcha.ready(function() {" +
"grecaptcha.execute(\"%s\", {action: \"%s\"}).then(function(token) {" % (recaptcha_key, scope) +
"grecaptcha.execute(\"%s\", {action: \"%s\"}).then(function(token) {" % (recaptcha_key, action) +
"document.getElementById(\"recaptcha-token\").value = token;"
"});" +
"});" +
Expand Down
9 changes: 5 additions & 4 deletions src/appier_extras/parts/recaptcha/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@

import appier

def recaptcha_protect(scope = "homepage"):
def recaptcha_protect(action = "homepage"):

def decorator(function):

@functools.wraps(function)
def interceptor(self, *args, **kwargs):
token = self.field("recaptcha_token", None)
recaptcha_ensure(self, token, scope = scope)
recaptcha_ensure(self, token, action = action)
return appier.call_safe(function, self, *args, **kwargs)
return interceptor

return decorator

def recaptcha_ensure(self, token, scope = "homepage"):
def recaptcha_ensure(self, token, action = "homepage"):
secret = appier.conf("RECAPTCHA_SECRET", None)
min_score = appier.conf("RECAPTCHA_MIN", 0.5)
result = appier.post(
Expand All @@ -64,7 +64,8 @@ def recaptcha_ensure(self, token, scope = "homepage"):
response = token
)
)
if result["score"] >= min_score: return token
if result["score"] >= min_score and\
result["action"] == action: return token
raise appier.AppierException(
message = "Invalid reCAPTCHA score",
code = 403
Expand Down

0 comments on commit 39de023

Please sign in to comment.