Skip to content

Commit 5818eeb

Browse files
committed
Add an example django project, one for django integrated, forms based authentication and one for ADFS integration
1 parent eea1e84 commit 5818eeb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2082
-0
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,6 @@ target/
6969

7070
# Virtual env
7171
.venv/
72+
73+
# Vagrant
74+
.vagrant/

Diff for: Vagrantfile

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
Vagrant.configure("2") do |config|
2+
config.vm.define "adfs2016", autostart: false do |adfs2016|
3+
4+
# adfs2016.vm.box = "adamrushuk/win2016-std-dev"
5+
adfs2016.vm.box = "cdaf/WindowsServer"
6+
7+
adfs2016.vm.provider "virtualbox" do |v|
8+
v.memory = 2048
9+
v.gui = true
10+
v.customize ["modifyvm", :id, "--clipboard", "bidirectional"]
11+
end
12+
13+
# If you change this IP, also change the DNS server for the "web" VM.
14+
adfs2016.vm.network "private_network", ip: "10.0.0.2"
15+
16+
# Some winrm hacking
17+
# It prevents the connection with the VM from dropping
18+
# after promoting it to a domain controller
19+
adfs2016.winrm.timeout = 180
20+
adfs2016.winrm.retry_limit = 20
21+
adfs2016.winrm.retry_delay = 10
22+
adfs2016.winrm.transport = :plaintext
23+
adfs2016.winrm.basic_auth_only = true
24+
adfs2016.winrm.username = "administrator"
25+
adfs2016.winrm.password = "vagrant"
26+
27+
# Setup the domain controller
28+
adfs2016.vm.provision "shell", privileged: true, path: "vagrant\\01-setup-domain.ps1"
29+
adfs2016.vm.provision :reload
30+
# Setup ADFS
31+
adfs2016.vm.provision "shell", privileged: true, path: "vagrant\\02-setup-adfs.ps1"
32+
adfs2016.vm.provision :reload
33+
# Configure ADFS for use with the example project
34+
adfs2016.vm.provision "shell", privileged: true, path: "vagrant\\03-example-adfs-config.ps1"
35+
end
36+
37+
config.vm.define "adfs2012", autostart: false do |adfs2012|
38+
39+
# adfs2012.vm.box = "adamrushuk/win2012r2-std-wmf5-dev"
40+
adfs2012.vm.box = "cdaf/WindowsServer2012R2"
41+
42+
adfs2012.vm.provider "virtualbox" do |v|
43+
v.memory = 2048
44+
v.gui = true
45+
v.customize ["modifyvm", :id, "--clipboard", "bidirectional"]
46+
end
47+
48+
# If you change this IP, also change the DNS server for the "web" VM.
49+
adfs2012.vm.network "private_network", ip: "10.0.0.2"
50+
51+
# Some winrm hacking
52+
# It prevents the connection with the VM from dropping
53+
# after promoting it to a domain controller
54+
adfs2012.winrm.timeout = 180
55+
adfs2012.winrm.retry_limit = 20
56+
adfs2012.winrm.retry_delay = 10
57+
adfs2012.winrm.transport = :plaintext
58+
adfs2012.winrm.basic_auth_only = true
59+
adfs2012.winrm.username = "administrator"
60+
adfs2012.winrm.password = "vagrant"
61+
62+
# Setup the domain controller
63+
adfs2012.vm.provision "shell", privileged: true, path: "vagrant\\01-setup-domain.ps1"
64+
adfs2012.vm.provision :reload
65+
# Setup ADFS
66+
adfs2012.vm.provision "shell", privileged: true, path: "vagrant\\02-setup-adfs.ps1"
67+
adfs2012.vm.provision :reload
68+
# Configure ADFS for use with the example project
69+
adfs2012.vm.provision "shell", privileged: true, path: "vagrant\\03-example-adfs-config.ps1"
70+
end
71+
72+
config.vm.define "web" do |web|
73+
web.vm.hostname = "web"
74+
web.vm.box = "bento/debian-9.4"
75+
76+
# If you change this IP, you also have to change it in the file 03-example-adfs-config.ps1
77+
web.vm.network "private_network", ip: "10.0.0.10"
78+
79+
# Install all needed tools and migrate the 2 example django projects
80+
web.vm.provision "shell", privileged: true, inline: <<-SHELL
81+
set -x
82+
apt-get update
83+
apt-get install -y python3-pip
84+
# Install django-auth-adfs in editable mode
85+
pip3 install -e /vagrant
86+
# run migrate command for both example projects
87+
python3 /vagrant/example/adfs/manage.py makemigrations
88+
python3 /vagrant/example/adfs/manage.py migrate
89+
python3 /vagrant/example/formsbased/manage.py makemigrations
90+
python3 /vagrant/example/formsbased/manage.py migrate
91+
SHELL
92+
93+
# Point the DNS server to the domain controller
94+
# It's ran every "up" because it's overwritten by DHCP
95+
web.vm.provision "shell", privileged: true, run: 'always', inline: <<-SHELL
96+
set -x
97+
echo "domain example.com" > /etc/resolv.conf
98+
echo "domain example.com" >> /etc/resolv.conf
99+
echo "nameserver 10.0.0.2" >> /etc/resolv.conf
100+
SHELL
101+
102+
end
103+
end

Diff for: example/adfs/manage.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
7+
try:
8+
from django.core.management import execute_from_command_line
9+
except ImportError as exc:
10+
raise ImportError(
11+
"Couldn't import Django. Are you sure it's installed and "
12+
"available on your PYTHONPATH environment variable? Did you "
13+
"forget to activate a virtual environment?"
14+
) from exc
15+
execute_from_command_line(sys.argv)

Diff for: example/adfs/mysite/__init__.py

Whitespace-only changes.

Diff for: example/adfs/mysite/settings.py

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
"""
2+
Django settings for mysite project.
3+
4+
Generated by 'django-admin startproject' using Django 2.0.5.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.0/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/2.0/ref/settings/
11+
"""
12+
13+
import os
14+
15+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = '72zx9=7byz54=z-@oyuv^h)nse=qljty65zj$nj*$z42j353sa'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = ['*']
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'django_auth_adfs',
35+
'polls',
36+
'django.contrib.admin',
37+
'django.contrib.auth',
38+
'django.contrib.contenttypes',
39+
'django.contrib.sessions',
40+
'django.contrib.messages',
41+
'django.contrib.staticfiles',
42+
]
43+
44+
MIDDLEWARE = [
45+
'django.middleware.security.SecurityMiddleware',
46+
'django.contrib.sessions.middleware.SessionMiddleware',
47+
'django.middleware.common.CommonMiddleware',
48+
'django.middleware.csrf.CsrfViewMiddleware',
49+
'django.contrib.auth.middleware.AuthenticationMiddleware',
50+
'django.contrib.messages.middleware.MessageMiddleware',
51+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
52+
]
53+
54+
ROOT_URLCONF = 'mysite.urls'
55+
56+
TEMPLATES = [
57+
{
58+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
59+
'DIRS': [os.path.join(BASE_DIR, 'templates')],
60+
'APP_DIRS': True,
61+
'OPTIONS': {
62+
'context_processors': [
63+
'django.template.context_processors.debug',
64+
'django.template.context_processors.request',
65+
'django.contrib.auth.context_processors.auth',
66+
'django.contrib.messages.context_processors.messages',
67+
],
68+
},
69+
},
70+
]
71+
72+
WSGI_APPLICATION = 'mysite.wsgi.application'
73+
74+
75+
# Database
76+
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
77+
78+
DATABASES = {
79+
'default': {
80+
'ENGINE': 'django.db.backends.sqlite3',
81+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
82+
}
83+
}
84+
85+
86+
# Password validation
87+
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
88+
89+
AUTH_PASSWORD_VALIDATORS = [
90+
{
91+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
92+
},
93+
{
94+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
95+
},
96+
{
97+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
98+
},
99+
{
100+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
101+
},
102+
]
103+
104+
AUTHENTICATION_BACKENDS = (
105+
'django_auth_adfs.backend.AdfsBackend',
106+
)
107+
108+
AUTH_ADFS = {
109+
"SERVER": "adfs.example.com",
110+
"CLIENT_ID": "django_website.adfs.client_id",
111+
"RESOURCE": "django_website.adfs.relying_party_id",
112+
# Make sure to read the documentation about the AUDIENCE setting
113+
# when you configured the identifier as a URL!
114+
"AUDIENCE": "microsoft:identityserver:django_website.adfs.relying_party_id",
115+
"ISSUER": "http://adfs.example.com/adfs/services/trust",
116+
"CA_BUNDLE": False, # !!! DON'T DO THIS IN A PRODUCTION SETUP !!!
117+
"CLAIM_MAPPING": {"first_name": "given_name",
118+
"last_name": "family_name",
119+
"email": "email"},
120+
"BOOLEAN_CLAIM_MAPPING": {"is_staff": "is_staff"},
121+
"REDIR_URI": "http://web.example.com:8000/oauth2/login",
122+
}
123+
124+
# Internationalization
125+
# https://docs.djangoproject.com/en/2.0/topics/i18n/
126+
127+
LANGUAGE_CODE = 'en-us'
128+
129+
TIME_ZONE = 'UTC'
130+
131+
USE_I18N = True
132+
133+
USE_L10N = True
134+
135+
USE_TZ = True
136+
137+
138+
# Static files (CSS, JavaScript, Images)
139+
# https://docs.djangoproject.com/en/2.0/howto/static-files/
140+
141+
STATIC_URL = '/static/'
142+
STATIC_ROOT = os.path.join(BASE_DIR, "static")
143+
144+
LOGIN_URL = "https://adfs.example.com/adfs/oauth2/authorize" \
145+
"?response_type=code" \
146+
"&client_id=django_website.adfs.client_id" \
147+
"&resource=django_website.adfs.relying_party_id" \
148+
"&redirect_uri=http://web.example.com:8000/oauth2/login"
149+
LOGIN_REDIRECT_URL = "home"
150+
151+
LOGGING = {
152+
'version': 1,
153+
'disable_existing_loggers': False,
154+
'formatters': {
155+
'verbose': {
156+
'format': '%(levelname)s %(asctime)s %(name)s %(message)s'
157+
},
158+
},
159+
'handlers': {
160+
'console': {
161+
'class': 'logging.StreamHandler',
162+
'formatter': 'verbose'
163+
},
164+
},
165+
'loggers': {
166+
'django_auth_adfs': {
167+
'handlers': ['console'],
168+
'level': 'DEBUG',
169+
},
170+
},
171+
}

Diff for: example/adfs/mysite/urls.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""mysite URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/2.0/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.conf import settings
17+
from django.conf.urls.static import static
18+
from django.contrib import admin
19+
from django.urls import include, path
20+
from django.views.generic.base import TemplateView
21+
22+
urlpatterns = [
23+
path('', TemplateView.as_view(template_name='home.html'), name='home'),
24+
path('polls/', include('polls.urls')),
25+
path('admin/', admin.site.urls, name='admin'),
26+
path('oauth2/', include('django_auth_adfs.urls')),
27+
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
28+
29+
print(settings.STATIC_ROOT)

Diff for: example/adfs/mysite/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for mysite project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
15+
16+
application = get_wsgi_application()

Diff for: example/adfs/polls/__init__.py

Whitespace-only changes.

Diff for: example/adfs/polls/admin.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from django.contrib import admin
2+
3+
from .models import Choice, Question
4+
5+
6+
class ChoiceInline(admin.TabularInline):
7+
model = Choice
8+
extra = 3
9+
10+
11+
class QuestionAdmin(admin.ModelAdmin):
12+
fieldsets = [
13+
(None, {'fields': ['question_text']}),
14+
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
15+
]
16+
inlines = [ChoiceInline]
17+
list_display = ('question_text', 'pub_date', 'was_published_recently')
18+
list_filter = ['pub_date']
19+
search_fields = ['question_text']
20+
21+
22+
admin.site.register(Question, QuestionAdmin)

Diff for: example/adfs/polls/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class PollsConfig(AppConfig):
5+
name = 'polls'

0 commit comments

Comments
 (0)