# RUG website template and single signon for django Template and for creating a website in the University of Groningen *style*, which also helps you to get single sign-on to work. ## Getting started ### Dependencies Make sure these packages are installed on the webhost serving your website. * **centos** `yum install libxml2-devel libxslt-devel python34-devel xmlsec1-devel libmcrypt libmcrypt-devel xmlsec1-openssl` * **ubuntu** `sudo apt-get install libxml2-dev libxslt1-dev libxmlsec1-dev python3-dev` ## Virtualenv I would suggest you use a virtualenv, this is a directory which contains the python executables and all python packages, like django, python3-saml, etc. mkdir rugwebsite && cd rugwebsite virtualenv --python=/usr/bin/python3.5 venv source venv/bin/activate pip3 install -U gunicorn git+ssh://git@git.webhosting.rug.nl:222/p253591/rug-website.git ## Settings and X509 certificate Create a file `rugwebsite/settings.py`, with settings like these: import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = '....' DEBUG = True LOGIN_REDIRECT_URL = '/' INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'bootstrap4', 'rugwebsite', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', ) ROOT_URLCONF = 'urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, '..', 'db.sqlite3'), } } LANGUAGE_CODE = 'en-us' TIME_ZONE = 'Europe/Amsterdam' USE_I18N = True USE_L10N = True USE_TZ = True STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') SAML_ROUTE = 'sso/saml/' SAML_REDIRECT = '/' SAML_USERS_MAP = [] SAML_PROVIDERS = [] Make sure to fill in something random for `SECRET_KEY`, consult the dango documentation to make sure you're super secure. Then you can use django-admin to complete the settings with SAML2 metadata, inclusing a private key and x509 certificate. Run the following and **append** the output to `rugwebsite/settings.py` PYTHONPATH=. DJANGO_SETTINGS_MODULE=settings django-admin init-saml2-settings --country NL --city Groningen \ --organisation 'University of Groningen' --organisation-unit 'Research and Innovation Support' \ --common-name 'cosmo.service.rug.nl' --state Groningen \ --support-name 'Research and Innovation Support' --support-email 'ris@list.rug.nl' \ --technical-name 'Research and Innovation Support' --technical-email 'ris@list.rug.nl' \ --base-url 'https://cosmo.service.rug.nl' --entity-id 'https://www.rug.nl/cosmo' You can change these settings if you like. Make sure to choose these correctly for your situation: * entity-id * common-name * base-url, including http or https and a prefix to all paths, make sure base-url + "/sso/saml/metadata?provider=RuG" is a working url. * support-name, support-email, technical-name, technical-email ### Database and static files Create the database PYTHONPATH=. DJANGO_SETTINGS_MODULE=settings django-admin migrate And collect all static files (javascript, css, etc). Recall what the absolute path of rugwebsite/static is. PYTHONPATH=. DJANGO_SETTINGS_MODULE=settings django-admin collectstatic ### Host the website using Gunicorn Create a `rugwebsite/wsgi.py` import os, sys sys.path.insert(0, 'absolute-path-to/rugwebsite/venv/lib/python3.5/site-packages/') sys.path.insert(0, 'absolute-path-to/rugwebsite/venv/lib64/python3.5/site-packages/') sys.path.insert(0, 'absolute-path-to/rugwebsite/') from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") application = get_wsgi_application() You can test-run the website with gunicorn like, but it won't host the static files (javascript, css, etc). Decide on what port it should run, for example `8890`. absolute-path-to/rugwebsite/venv/bin/gunicorn --bind 127.0.0.1:8890 wsgi you can run this in a `sceen`, but it might be better to create a service like, in centos you create a `/usr/lib/systemd/system/yourwebsite.service` like this [Unit] Description=your website gunicorn deamon description After=network.target [Service] PIDFile=/run/gunicorn/yourwebsitepid User=cosmo Group=cosmo RuntimeDirectory=gunicorn WorkingDirectory=absolute-path-to/rugwebsite/ ExecStart=absolute-path-to/rugwebsite/venv/bin/gunicorn --pid /run/gunicorn/yourwebsitepid --bind 127.0.0.1:8890 wsgi ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target You can start your service using `sudo service yourwebsite start` (or `restart`, `status`, `stop`, ...) ### nginx proxy You can use nginx to proxy your website, and add https-support (instead of plain http), but also to server the static files efficiently. Make sure nginx is installed and configure the website, for example by creating the `/etc/nginx/conf.d/yourwebsite.conf` file on centos: # Make sure the port matches upstream yourwebsiteupstream { server 127.0.0.1:8890; } # Forward http to https server { listen 80; server_name cosmo.service.rug.nl; return 301 https://$host$request_uri; } server { client_max_body_size 64M; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300; send_timeout 300; listen 443 ssl; server_name cosmo.service.rug.nl; ssl_certificate /etc/nginx/conf.d/cosmo.service.rug.nl.pem; ssl_certificate_key /etc/nginx/conf.d/cosmo.service.rug.nl.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA'; ssl_prefer_server_ciphers on; ssl_dhparam /etc/nginx/conf.d/dhparams.pem; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { alias absolute-path-to/rugwebsite/static/; } location / { proxy_pass http://yourwebsiteupstream/; proxy_set_header Host cosmo.service.rug.nl; proxy_set_header X-Forwarded-For $remote_addr; # This setting is important, it allows SAML2 to verify the provider url, whithout getting into trouble because # the https-part is handled by nginx, and hence not seen by the django SAML2 code. proxy_set_header X_FORWARDED_PROTO https; } } Restart nginx to load the settings `sudo service nginx restart` (or `reload`, to only reload the settings instaed of a full restart)