Skip to content

Commit 3afd1b5

Browse files
committed
Closed #80 - Added authentication using rubin CIlogon.
1 parent 23a85a7 commit 3afd1b5

File tree

25 files changed

+1449
-286
lines changed

25 files changed

+1449
-286
lines changed

.envs/.local/.django

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ USE_DOCKER=yes
44
IPYTHONDIR=/app/.ipython
55

66
DJANGO_DEBUG=True
7+
DJANGO_SECRET_KEY=hIIbCQezMLys5Ya2Flyx2NDdrs4ZG6DA2pLDu8kKPgtjP7xx6D1m8yccSD6zX6Br
78
DATABASE_URL=postgres://debug:debug@postgres:5432/sky_viewer
89

910
# UWSGI
10-
DJANGO_UWSGI_WORKER_PROCESSES=4
11-
DJANGO_UWSGI_WORKER_THREADS=2
11+
DJANGO_UWSGI_WORKER_PROCESSES=1
12+
DJANGO_UWSGI_WORKER_THREADS=1
1213

1314
# Redis
1415
# ------------------------------------------------------------------------------
@@ -28,5 +29,6 @@ CELERY_FLOWER_PASSWORD=debug
2829
ENVIRONMENT_NAME=development
2930
BASE_HOST=http://localhost
3031

31-
# Url de login utilizada pelo frontend
32-
LINEA_LOGIN_URL=/admin/login/?next=/
32+
# Urls de login SAML/CILogon
33+
LINEA_LOGIN_URL=$BASE_HOST/saml2/login/?idp=https://satosa.linea.org.br/linea/proxy/aHR0cHM6Ly9jaWxvZ29uLm9yZw==
34+
RUBIN_LOGIN_URL=$BASE_HOST/saml2/login/?idp=https://satosa-dev.linea.org.br/linea_saml_mirror/proxy/aHR0cHM6Ly9kYXRhLmxzc3QuY2xvdWQ=&next=/

backend/config/settings/base.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
# ------------------------------------------------------------------------------
2020
# https://docs.djangoproject.com/en/dev/ref/settings/#debug
2121
DEBUG = env.bool("DJANGO_DEBUG", False)
22+
LOG_LEVEL = env.bool("DJANGO_LOG_LEVEL", "INFO")
23+
LOG_DIR = "/logs"
2224
# Local time zone. Choices are
2325
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
2426
# though not all of them may be available with every OS.
@@ -191,6 +193,7 @@
191193
"django.template.context_processors.tz",
192194
"django.contrib.messages.context_processors.messages",
193195
"sky_viewer.users.context_processors.allauth_settings",
196+
"django_settings_export.settings_export",
194197
],
195198
},
196199
},
@@ -357,5 +360,29 @@
357360
# LINEA Settings
358361
# ------------------------------------------------------------------------------
359362
ENVIRONMENT_NAME = env("ENVIRONMENT_NAME", default="development").lower()
363+
# Complete URL of the production server with protocol and port
364+
BASE_HOST = env("BASE_HOST", default="http://localhost")
365+
# URL de login utilizada pelo frontend.
366+
# Em dev: /admin/login/?next=/
367+
# Em produção: /api/login/
368+
LOGIN_URL = "/admin/login/?next=/"
369+
# LOGIN_URL = "/api/login/"
370+
LOGOUT_URL = "/api/logout/"
360371

372+
# Urls for login with SAML2/CILogon
373+
# URL_CILOGON example: https://skyviewer.linea.org.br/saml2/login/?idp=https://satosa.linea.org.br/linea/proxy/aHR0cHM6Ly9jaWxvZ29uLm9yZw==
361374
LINEA_LOGIN_URL = env("LINEA_LOGIN_URL", default="/admin/login/?next=/")
375+
RUBIN_LOGIN_URL = env("RUBIN_LOGIN_URL", default="/admin/login/?next=/")
376+
377+
# Url de registro para os diferentes idps.
378+
LINEA_REGISTER_URL="https://register-dev.linea.org.br/Shibboleth.sso/Login?SAMLDS=1&target=https://register-dev.linea.org.br/registry/co_petitions/start/coef:155&entityID=https://satosa.linea.org.br/linea/proxy/aHR0cHM6Ly9jaWxvZ29uLm9yZw=="
379+
RUBIN_REGISTER_URL="https://register-dev.linea.org.br/Shibboleth.sso/Login?SAMLDS=1&target=https://register-dev.linea.org.br/registry/co_petitions/start/coef:231&entityID=https://satosa-dev.linea.org.br/linea_saml_mirror/proxy/aHR0cHM6Ly9kYXRhLmxzc3QuY2xvdWQ="
380+
381+
SETTINGS_EXPORT = [
382+
"BASE_HOST",
383+
"LOGOUT_URL",
384+
"LINEA_LOGIN_URL",
385+
"LINEA_REGISTER_URL",
386+
"RUBIN_LOGIN_URL",
387+
"RUBIN_REGISTER_URL"
388+
]

backend/config/settings/production.py

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,37 @@
150150
"disable_existing_loggers": True,
151151
"formatters": {
152152
"verbose": {
153-
"format": "%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s",
153+
"format": "%(asctime)s [%(levelname)s] %(message)s",
154154
},
155155
},
156156
"handlers": {
157157
"console": {
158-
"level": "DEBUG",
158+
"level": LOG_LEVEL,
159159
"class": "logging.StreamHandler",
160160
"formatter": "verbose",
161161
},
162+
"default": {
163+
"level": LOG_LEVEL,
164+
"class": "logging.handlers.RotatingFileHandler",
165+
"filename": os.path.join(LOG_DIR, "django.log"),
166+
"formatter": "verbose",
167+
},
168+
"djangosaml2": {
169+
"level": LOG_LEVEL,
170+
"class": "logging.handlers.RotatingFileHandler",
171+
"maxBytes": 1024 * 1024 * 5, # 5 MB
172+
"backupCount": 5,
173+
"filename": os.path.join(LOG_DIR, "djangosaml2.log"),
174+
"formatter": "verbose",
175+
},
162176
},
163177
"root": {"level": "INFO", "handlers": ["console"]},
164178
"loggers": {
179+
"django": {
180+
"level": LOG_LEVEL,
181+
"handlers": ["default", "console"],
182+
"propagate": True
183+
},
165184
"django.db.backends": {
166185
"level": "ERROR",
167186
"handlers": ["console"],
@@ -174,6 +193,11 @@
174193
"handlers": ["console"],
175194
"propagate": False,
176195
},
196+
"djangosaml2": {
197+
"level": LOG_LEVEL,
198+
"handlers": ["djangosaml2"],
199+
"propagate": True
200+
},
177201
},
178202
}
179203

@@ -210,14 +234,21 @@
210234
# Your stuff...
211235
# ------------------------------------------------------------------------------
212236

213-
# COmanage Autorization
214-
# ------------------------------------------------------------------------------
215-
COMANAGE_SERVER_URL = os.environ.get(
216-
"COMANAGE_SERVER_URL", "https://register.linea.org.br"
217-
)
218-
COMANAGE_USER = os.environ.get("COMANAGE_USER", "co_2.linea.apps")
219-
COMANAGE_PASSWORD = os.environ.get("COMANAGE_PASSWORD")
220-
COMANAGE_COID = os.environ.get("COMANAGE_COID")
237+
# Qualquer view que requer um usuário autenticado deve redirecionar o navegador para esta url
238+
LOGIN_URL = "/api/login"
239+
# Urls for login with SAML2/CILogon
240+
# URL_CILOGON example: https://skyviewer.linea.org.br/saml2/login/?idp=https://satosa.linea.org.br/linea/proxy/aHR0cHM6Ly9jaWxvZ29uLm9yZw==
241+
LINEA_LOGIN_URL = env("LINEA_LOGIN_URL")
242+
RUBIN_LOGIN_URL = env("RUBIN_LOGIN_URL")
243+
244+
# # COmanage Autorization
245+
# # ------------------------------------------------------------------------------
246+
# COMANAGE_SERVER_URL = os.environ.get(
247+
# "COMANAGE_SERVER_URL", "https://register.linea.org.br"
248+
# )
249+
# COMANAGE_USER = os.environ.get("COMANAGE_USER", "co_2.linea.apps")
250+
# COMANAGE_PASSWORD = os.environ.get("COMANAGE_PASSWORD")
251+
# COMANAGE_COID = os.environ.get("COMANAGE_COID")
221252

222253
# Django SAML2
223254
# ------------------------------------------------------------------------------
@@ -238,19 +269,15 @@
238269
AUTHENTICATION_BACKENDS += ("common.saml2.LineaSaml2Backend",)
239270
# Including SAML2 Middleware
240271
MIDDLEWARE += ("djangosaml2.middleware.SamlSessionMiddleware",)
241-
272+
# SAML2 Custom error handler
273+
# https://djangosaml2.readthedocs.io/contents/developer.html#custom-error-handler
274+
SAML_ACS_FAILURE_RESPONSE_FUNCTION = 'common.views.saml2_template_failure'
242275
# configurações relativas ao session cookie
243276
SAML_SESSION_COOKIE_NAME = "saml_session"
244277
SESSION_COOKIE_SECURE = True
245278

246-
# Qualquer view que requer um usuário autenticado deve redirecionar o navegador para esta url
247-
# LOGIN_URL = "/saml2/login/"
248-
LOGIN_URL = "/api/api-auth/login"
249-
# URL_CILOGON example: https://skyviewer.linea.org.br/saml2/login/?idp=https://satosa.linea.org.br/linea/proxy/aHR0cHM6Ly9jaWxvZ29uLm9yZw==
250-
AUTH_SAML2_LOGIN_URL_CILOGON = env("AUTH_SAML2_LOGIN_URL_CILOGON")
251-
252279
# Encerra a sessão quando o usuário fecha o navegador
253-
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
280+
SESSION_EXPIRE_AT_BROWSER_CLOSE = False
254281

255282
# Tipo de binding utilizado
256283
SAML_DEFAULT_BINDING = saml2.BINDING_HTTP_POST
@@ -270,6 +297,7 @@
270297
"givenName": ("first_name",),
271298
"sn": ("last_name",),
272299
"email": ("email",),
300+
"isMemberOf": ("name",),
273301
}
274302

275303
SAML_CONFIG = {
@@ -278,13 +306,13 @@
278306
"entityid": FQDN + "/saml2/metadata/",
279307
# Diretório contendo os esquemas de mapeamento de atributo
280308
"attribute_map_dir": str(ATTR_DIR),
281-
"description": "SP Target Viewer",
309+
"description": "SP Sky Viewer",
282310
"service": {
283311
"sp": {
284-
"name": "SP Target Viewer",
312+
"name": "SP Sky Viewer",
285313
"ui_info": {
286-
"display_name": {"text": "SP Target Viewer", "lang": "en"},
287-
"description": {"text": "SP Target Viewer", "lang": "en"},
314+
"display_name": {"text": "SP Sky Viewer", "lang": "en"},
315+
"description": {"text": "SP Sky Viewer", "lang": "en"},
288316
"information_url": {"text": FQDN, "lang": "en"},
289317
"privacy_statement_url": {"text": FQDN, "lang": "en"},
290318
},
@@ -324,6 +352,14 @@
324352
"url": "https://www.linea.org.br/static/metadata/satosa-prod-frontend-cilogon.xml",
325353
"cert": None,
326354
},
355+
{
356+
"url": "https://www.linea.org.br/static/metadata/satosa-dev-frontend-cilogon.xml",
357+
"cert": None,
358+
},
359+
{
360+
"url": "https://www.linea.org.br/static/metadata/satosa-dev-frontend-rubin.xml",
361+
"cert": None,
362+
},
327363
],
328364
},
329365
# Configurado como 1 para fornecer informações de debug

backend/config/urls.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@
1818
# TemplateView.as_view(template_name="pages/about.html"),
1919
# name="about",
2020
# ),
21-
# Django Admin, use {% url 'admin:index' %}
22-
path(settings.ADMIN_URL, admin.site.urls),
2321
# User management
2422
# path("/users/", include("sky_viewer.users.urls", namespace="users")),
2523
# path("/accounts/", include("allauth.urls")),
24+
25+
# Django Admin, use {% url 'admin:index' %}
26+
path(settings.ADMIN_URL, admin.site.urls),
27+
2628
# Your stuff: custom urls includes go here
2729
# Auth SAML2
2830
path("saml2/", include("djangosaml2.urls")),
31+
path(
32+
"api/login/",
33+
TemplateView.as_view(template_name="pages/linea_login.html"),
34+
name="login",
35+
),
36+
2937
# Media files
3038
*static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT),
3139
]
@@ -34,7 +42,7 @@
3442
urlpatterns += [
3543
# API base url
3644
path("api/", include("config.api_router")),
37-
path("api/logout/", CommonViews.teste, name="logout_user"),
45+
path("api/logout/", CommonViews.logout_user, name="logout_user"),
3846
path(
3947
"api/environment_settings/",
4048
CommonViews.environment_settings,

backend/requirements/base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ django-crispy-forms==2.3 # https://github.com/django-crispy-forms/django-crispy
2222
crispy-bootstrap5==2024.10 # https://github.com/django-crispy-forms/crispy-bootstrap5
2323
django-compressor==4.5.1 # https://github.com/django-compressor/django-compressor
2424
django-redis==5.4.0 # https://github.com/jazzband/django-redis
25+
django-settings-export==1.2.1 # https://github.com/jkbrzt/django-settings-export,
2526
# Django REST Framework
2627
djangorestframework==3.15.2 # https://github.com/encode/django-rest-framework
2728
django-cors-headers==4.6.0 # https://github.com/adamchainz/django-cors-headers

backend/sky_viewer/common/api/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def environment_settings(request):
3030
if enviroment in dev_environments:
3131
is_dev = True
3232

33-
login_url = settings.LINEA_LOGIN_URL
33+
login_url = settings.LOGIN_URL
3434

3535
env_settings = {
3636
"environment": enviroment,

0 commit comments

Comments
 (0)