Skip to content

Commit

Permalink
add csrf_protection app with someviews with protection
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardo.leano committed Nov 11, 2020
1 parent 0a74526 commit ee9caf9
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 2 deletions.
Empty file added csrf_protection/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions csrf_protection/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions csrf_protection/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class CsrfProtectionConfig(AppConfig):
name = 'csrf_protection'
Empty file.
3 changes: 3 additions & 0 deletions csrf_protection/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
27 changes: 27 additions & 0 deletions csrf_protection/templates/csrf_protection/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<header>
CSRF INITIAL PAGE
</header>
<p>The project is configured with the Middleware:</p>
<p>django.middleware.csrf.CsrfViewMiddleware</p>
<div>
Submit Without CSRFTOKEN - to view without protection
<form action="{% url 'csrf_protection:without_protection' %}" method="post" >
<input type="submit" value="Submit">
</form>
<div>

<div>
Submit Without CSRFTOKEN - to view with standard protection (should popup a 403)
<form action="{% url 'csrf_protection:with_standard_protection' %}" method="post" >
<input type="submit" value="Submit">
</form>
<div>


<div>
Submit With CSRFTOKEN - to view with standard protection
<form action="{% url 'csrf_protection:with_standard_protection' %}" method="post" >
{% csrf_token %}
<input type="submit" value="Submit">
</form>
<div>
3 changes: 3 additions & 0 deletions csrf_protection/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
10 changes: 10 additions & 0 deletions csrf_protection/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path
from csrf_protection import views

app_name = 'csrf_protection'

urlpatterns = [
path("", views.csrf_index, name='csrf_index'),
path("without_protection/", views.without_protection, name='without_protection'),
path("with_standard_protection/", views.with_standard_protection, name='with_standard_protection'),
]
47 changes: 47 additions & 0 deletions csrf_protection/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

def csrf_index(request):
html_data={}
return render(request, "csrf_protection/index.html",html_data)

@csrf_exempt
def without_protection(request):
"""
without_protection
url: csrf/without_protection/
url-shortcut: "csrf_protection:without_protection"
Parameters
----------
request : [type]
[description]
Returns
-------
[type]
[description]
"""
html_data={}
return render(request, "csrf_protection/index.html", html_data)

def with_standard_protection(request):
"""
without_protection
url: csrf/without_protection/
url-shortcut: "csrf_protection:with_standard_protection"
Parameters
----------
request : [type]
[description]
Returns
-------
[type]
[description]
"""
html_data={}
return render(request, "csrf_protection/index.html", html_data)
1 change: 1 addition & 0 deletions django_sandbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
INSTALLED_APPS = [
'http_response.apps.HttpResponseConfig', # add to register http_reponse app
'forms_examples.apps.FormsExamplesConfig', # add to register forms_examples
'csrf_protection.apps.CsrfProtectionConfig', # add to register csrf_protection
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
Expand Down
3 changes: 2 additions & 1 deletion django_sandbox/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
urlpatterns = [
path('admin/', admin.site.urls),
path('http_response/', include('http_response.urls') ),
path('forms/', include('forms_examples.urls') )
path('forms/', include('forms_examples.urls') ),
path('csrf/',include('csrf_protection.urls') )
]
2 changes: 1 addition & 1 deletion forms_examples/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def form_choicefield_flexible_render(request):
def form_choice_cascade(request):
"""
url: /forms/form_choice_cascade
url shortcut: 'forms_examples:form_choice_cascade'
url-shortcut: 'forms_examples:form_choice_cascade'
Parameters
----------
Expand Down

0 comments on commit ee9caf9

Please sign in to comment.