"""
Django settings for seleniumRobotServer project.

Generated by 'django-admin startproject' using Django 1.10.5.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
# Login/ mdp: admin / adminServer
import os
import ldap
from django_auth_ldap.config import LDAPSearch
from seleniumRobotServer.settings_base import *


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# allow files up to 10 MB to be uploaded
FILE_UPLOAD_MAX_MEMORY_SIZE = 10000000

TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'
TEST_OUTPUT_FILE_NAME = 'unittest.xml'

AUTHENTICATION_BACKENDS = (
    #"seleniumRobotServer.ldapbackends.LDAPBackend1", "seleniumRobotServer.ldapbackends.LDAPBackend2", "seleniumRobotServer.ldapbackends.LDAPBackend3",
    'django.contrib.auth.backends.ModelBackend',
#     'seleniumRobotServer.openidbackend.NameOIDCAB',
#     'mozilla_django_oidc.auth.OIDCAuthenticationBackend'
)

DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.sqlite3',
         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
     }
#     'default': {
#         'ENGINE': 'django.db.backends.postgresql_psycopg2',
#         'NAME': 'seleniumServer',
#         'USER': 'seleniumRobot',
#         'PASSWORD': 'robotDb',
#         'HOST': '',
#         'PORT': '',
#     }
}

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# whether to enable security of API / GUI
SECURITY_WEB_ENABLED = "True"
SECURITY_API_ENABLED = "True" 

if (SECURITY_API_ENABLED):
    REST_FRAMEWORK = {
        # Use Django's standard `django.contrib.auth` permissions,
        'DEFAULT_PERMISSION_CLASSES': [
            'rest_framework.permissions.DjangoModelPermissions'
        ],
        'DEFAULT_AUTHENTICATION_CLASSES': [
            'rest_framework.authentication.TokenAuthentication',   # for API calls
            'rest_framework.authentication.SessionAuthentication', # for Ajax requests from GUI
        ]
    }
else:
    REST_FRAMEWORK = {
        # allow read-only access for unauthenticated users.
        'DEFAULT_PERMISSION_CLASSES': [
            'rest_framework.permissions.AllowAny'
        ]
    }

DRAMATIQ_BROKER = {
    "BROKER": "dramatiq.brokers.stub.StubBroker",
    "OPTIONS": {},
    "MIDDLEWARE": [
        "dramatiq.middleware.AgeLimit",
        "dramatiq.middleware.TimeLimit",
        "dramatiq.middleware.Callbacks",
        "dramatiq.middleware.Pipelines",
        "dramatiq.middleware.Retries",
        "django_dramatiq.middleware.DbConnectionsMiddleware",
        #"django_dramatiq.middleware.AdminMiddleware",
    ]
}
#DRAMATIQ_RESULT_BACKEND = None
#DRAMATIQ_BROKER['OPTIONS'] = 'redis://redis_server.fr:6379/0'
FIELD_DETECTOR_ENABLED = 'True'
DRAMATIQ_RESULT_BACKEND['BACKEND'] = "dramatiq.results.backends.stub.StubBackend"
DRAMATIQ_RESULT_BACKEND['BACKEND_OPTIONS'] = {}

# -------- Application specific flags ------------
# whether we restrict the view/change/delete/add to the user, in admin view to only applications he has rights for (issue #28)
RESTRICT_ACCESS_TO_APPLICATION_IN_ADMIN = False
 

# -------- OpenID Authentication -----------------
OIDC_RP_CLIENT_ID = 'seleniumserver'
OIDC_RP_CLIENT_SECRET = 'secret'
OIDC_OP_AUTHORIZATION_ENDPOINT = "https://endpoint/oauth2/authorization"
OIDC_OP_TOKEN_ENDPOINT = "https://endpoint/oauth2/token"
OIDC_OP_USER_ENDPOINT = "https://endpoint/oauth2/userInfo"
OIDC_RP_SIGN_ALGO = "RS256"
OIDC_RP_SCOPES = "sub" # may not be necessary if email is provided
OIDC_USE_NONCE = False # may be set to True if needed
OIDC_RP_IDP_KEYFILE = 'key.pem' # pem file to read public key from. Only necessary for RS256 algo
# if os.path.isfile(OIDC_RP_IDP_KEYFILE):
#     with(open(OIDC_RP_IDP_KEYFILE)) as keyfile:
#         OIDC_RP_IDP_SIGN_KEY = keyfile.read()

OIDC_OP_JWKS_ENDPOINT = 'https://endpoint/json-web-key-set' # either set this or OIDC_RP_IDP_SIGN_KEY, not both
LOGOUT_REDIRECT_URL = "/accounts/login/?next=/snapshot/"
LOGIN_REDIRECT_URL = "/snapshot/"
LOGIN_REDIRECT_URL_FAILURE = "/accounts/login/?next=/snapshot/"

# to use in conjunction with 'NameOIDCAB' backend
OIDC_IS_STAFF_GROUP_NAMES = ['User']
OIDC_IS_SUPERUSER_GROUP_NAMES = []

# -------- LDAP Authentication ------------------- 
AUTH_LDAP_GLOBAL_OPTIONS = {
    ldap.OPT_REFERRALS: 0
}
 
# first LDAP server configuration
AUTH_LDAP_1_SERVER_URI = "ldap://mycompany.com:389"
AUTH_LDAP_1_BIND_DN = 'CN=user,OU=ou,DC=company,DC=com'
AUTH_LDAP_1_BIND_PASSWORD = 'pwd'
AUTH_LDAP_1_USER_SEARCH = LDAPSearch("OU=ou,DC=company,DC=com", ldap.SCOPE_SUBTREE, "(uid=%(user)s)")

# second LDAP server configuration (uncomment "seleniumRobotServer.ldapbackends.LDAPBackend2" in AUTHENTICATION_BACKENDS to use it)
AUTH_LDAP_2_SERVER_URI = "ldap://mycompany.com:389"
AUTH_LDAP_2_BIND_DN = 'CN=user,OU=ou,DC=company,DC=com'
AUTH_LDAP_2_BIND_PASSWORD = 'pwd'
AUTH_LDAP_2_USER_SEARCH = LDAPSearch("OU=ou,DC=company,DC=com", ldap.SCOPE_SUBTREE, "(uid=%(user)s)")

# third LDAP server configuration (uncomment "seleniumRobotServer.ldapbackends.LDAPBackend3" in AUTHENTICATION_BACKENDS to use it)
AUTH_LDAP_3_SERVER_URI = "ldap://mycompany.com:389"
AUTH_LDAP_3_BIND_DN = 'CN=user,OU=ou,DC=company,DC=com'
AUTH_LDAP_3_BIND_PASSWORD = 'pwd'
AUTH_LDAP_3_USER_SEARCH = LDAPSearch("OU=ou,DC=company,DC=com", ldap.SCOPE_SUBTREE, "(uid=%(user)s)")

