forked from MasoniteFramework/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CSRF protection MasoniteFramework#27
- Loading branch information
Showing
7 changed files
with
138 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import binascii | ||
import os | ||
|
||
|
||
class Csrf(object): | ||
""" | ||
Class for csrf protection | ||
""" | ||
|
||
def __init__(self, request): | ||
self.request = request | ||
|
||
def generate_csrf_token(self): | ||
""" | ||
Generate token for csrf protection | ||
""" | ||
|
||
token = binascii.b2a_hex(os.urandom(15)) | ||
self.request.cookie('csrftoken', token) | ||
|
||
def verify_csrf_token(self, token): | ||
""" | ||
Verify if csrf token is valid | ||
""" | ||
|
||
if self.request.get_cookie('csrftoken') == token: | ||
return True | ||
else: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from masonite.exceptions import InvalidCSRFToken | ||
|
||
|
||
class CsrfMiddleware(object): | ||
""" | ||
Verify csrf token middleware | ||
""" | ||
|
||
exempt = [] | ||
|
||
def __init__(self, Request, CSRF): | ||
self.request = Request | ||
self.csrf = CSRF | ||
|
||
def before(self): | ||
if self.request.is_post(): | ||
token = self.request.input('csrftoken') | ||
if (not self.csrf.verify_csrf_token(token) | ||
and not self.__in_except()): | ||
raise InvalidCSRFToken("Invalid CSRF token.") | ||
else: | ||
self.csrf.generate_token() | ||
|
||
def after(self): | ||
pass | ||
|
||
def __in_except(self): | ||
""" | ||
Determine if the request has a URI that should pass | ||
through CSRF verification. | ||
""" | ||
|
||
if self.request.path in self.exempt: | ||
return True | ||
else: | ||
return False |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" A Csrf Service Provider """ | ||
from masonite.provider import ServiceProvider | ||
from masonite.auth.Csrf import Csrf | ||
|
||
|
||
class CsrfProvider(ServiceProvider): | ||
|
||
wsgi = True | ||
|
||
def register(self): | ||
request = self.app.make('Request') | ||
self.app.bind('Request', request) | ||
self.app.bind('CSRF', Csrf(request)) | ||
|
||
def boot(self, View, ViewClass, Request): | ||
# Share token csrf | ||
token = Request.get_cookie('csrftoken') | ||
|
||
ViewClass.share({'csrf_field': "<input type='hidden' name='csrf_token' value='{0}' />".format(token)}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters