Guest User

Untitled

a guest
Oct 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. # The secure redirect I found in the snippets library did not satisfy me.
  2. # Here is my version which satisfied my needs more. The next argument/
  3. # referrer will only be used if your application knows how to route it.
  4. # This won't work very well if you have multiple applications.
  5. # Licensed under the Unlicense, go nuts.
  6.  
  7. ## Create the base form ##
  8. from flask import request, url_for, render_template
  9. from flaskext.wtf import Form, HiddenField
  10. from werkzeug.exceptions import NotFound, HTTPException
  11.  
  12. class RedirectForm(Form):
  13. next = HiddenField()
  14.  
  15. def __init__(self, *args, **kwargs):
  16. super(RedirectForm, self).__init__(*args, **kwargs)
  17.  
  18. if not self.next.data:
  19. self.next.data = request.args.get("next") or request.referrer
  20.  
  21. try:
  22. # Will raise an exception if no endpoint exists for the url
  23. app.create_url_adapter(request).match(self.next.data)
  24. except NotFound:
  25. self.next.data = ""
  26. except HTTPException:
  27. # Any other exceptions are harmless (I think)
  28. pass
  29.  
  30. @property
  31. def redirect_target(self):
  32. return self.next.data
Add Comment
Please, Sign In to add comment