airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From machris...@apache.org
Subject [airavata-django-portal] 06/07: AIRAVATA-2859 Simple CILogon integration
Date Sat, 21 Jul 2018 20:13:00 GMT
This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git

commit da5a2f3b4e181e288c9622c9fe7e43fa273a8298
Author: Marcus Christie <machrist@iu.edu>
AuthorDate: Fri Jul 20 16:47:28 2018 -0400

    AIRAVATA-2859 Simple CILogon integration
---
 .../auth/templates/django_airavata_auth/login.html | 12 +++++
 django_airavata/apps/auth/urls.py                  |  1 +
 django_airavata/apps/auth/views.py                 | 52 +++++++++++++---------
 3 files changed, 44 insertions(+), 21 deletions(-)

diff --git a/django_airavata/apps/auth/templates/django_airavata_auth/login.html b/django_airavata/apps/auth/templates/django_airavata_auth/login.html
index 20d3e20..69ece3c 100644
--- a/django_airavata/apps/auth/templates/django_airavata_auth/login.html
+++ b/django_airavata/apps/auth/templates/django_airavata_auth/login.html
@@ -27,5 +27,17 @@
             </div>
         </div>
     </div>
+    <div class="row">
+        <div class="col-md-6 col-md-offset-3">
+            <div class="panel panel-default">
+                <div class="panel-body">
+                    <h3>CILogon</h3>
+                    <a href="{% url 'django_airavata_auth:redirect_login' %}" class="btn
btn-default">
+                        Sign in with CILogon
+                    </a>
+                </div>
+            </div>
+        </div>
+    </div>
 </div>
 {% endblock content %}
diff --git a/django_airavata/apps/auth/urls.py b/django_airavata/apps/auth/urls.py
index 4cb5b35..da9133d 100644
--- a/django_airavata/apps/auth/urls.py
+++ b/django_airavata/apps/auth/urls.py
@@ -6,6 +6,7 @@ from . import views
 app_name = 'django_airavata_auth'
 urlpatterns = [
     url(r'^login$', views.start_login, name='login'),
+    url(r'^redirect_login$', views.redirect_login, name='redirect_login'),
     url(r'^handle_login$', views.handle_login, name='handle_login'),
     url(r'^logout$', views.start_logout, name='logout'),
     url(r'^callback', views.callback, name='callback'),
diff --git a/django_airavata/apps/auth/views.py b/django_airavata/apps/auth/views.py
index 8c1c468..d4412b8 100644
--- a/django_airavata/apps/auth/views.py
+++ b/django_airavata/apps/auth/views.py
@@ -1,32 +1,35 @@
+import logging
+from urllib.parse import quote
+
 from django.conf import settings
 from django.contrib.auth import authenticate, login, logout
-from django.http import HttpResponse
-from django.shortcuts import render, redirect
+from django.shortcuts import redirect, render
 from django.urls import reverse
-
 from requests_oauthlib import OAuth2Session
 
-import logging
-from urllib.parse import quote
-
 logger = logging.getLogger(__name__)
 
-# Create your views here.
 
 def start_login(request):
-    # TODO: If the gateway is configured to not allow username password authentication, then
redirect to Keycloak
-    # client_id = settings.KEYCLOAK_CLIENT_ID
-    # base_authorize_url = settings.KEYCLOAK_AUTHORIZE_URL
-    # oauth2_session = OAuth2Session(client_id, scope='openid', redirect_uri=request.build_absolute_uri(reverse('django_airavata_auth:callback')))
-    # authorization_url, state = oauth2_session.authorization_url(base_authorize_url)
-    # logger.debug("authorization_url={}, state={}".format(authorization_url, state))
-    # # Store state in session for later validation
-    # request.session['OAUTH2_STATE'] = state
-    # return redirect(authorization_url)
     return render(request, 'django_airavata_auth/login.html', {
         'next': request.GET.get('next', None)
     })
 
+
+def redirect_login(request):
+    client_id = settings.KEYCLOAK_CLIENT_ID
+    base_authorize_url = settings.KEYCLOAK_AUTHORIZE_URL
+    oauth2_session = OAuth2Session(
+        client_id, scope='openid',
+        redirect_uri=request.build_absolute_uri(
+            reverse('django_airavata_auth:callback')))
+    authorization_url, state = oauth2_session.authorization_url(
+        base_authorize_url)
+    # Store state in session for later validation (see backends.py)
+    request.session['OAUTH2_STATE'] = state
+    return redirect(authorization_url)
+
+
 def handle_login(request):
     username = request.POST['username']
     password = request.POST['password']
@@ -43,13 +46,18 @@ def handle_login(request):
                 'username': username
             })
     except Exception as err:
-        logger.exception("An error occurred while logging in with username and password")
+        logger.exception("An error occurred while logging in with "
+                         "username and password")
         return redirect(reverse('django_airavata_auth:error'))
 
+
 def start_logout(request):
     logout(request)
-    redirect_url = request.build_absolute_uri(reverse(settings.LOGOUT_REDIRECT_URL))
-    return redirect(settings.KEYCLOAK_LOGOUT_URL + "?redirect_uri=" + quote(redirect_url))
+    redirect_url = request.build_absolute_uri(
+        reverse(settings.LOGOUT_REDIRECT_URL))
+    return redirect(settings.KEYCLOAK_LOGOUT_URL
+                    + "?redirect_uri=" + quote(redirect_url))
+
 
 def callback(request):
     try:
@@ -58,10 +66,12 @@ def callback(request):
         next_url = request.GET.get('next', settings.LOGIN_REDIRECT_URL)
         return redirect(next_url)
     except Exception as err:
-        logger.exception("An error occurred while processing OAuth2 callback: {}".format(request.build_absolute_uri()))
+        logger.exception("An error occurred while processing OAuth2 "
+                         "callback: {}".format(request.build_absolute_uri()))
         return redirect(reverse('django_airavata_auth:error'))
 
+
 def auth_error(request):
     return render(request, 'django_airavata_auth/auth_error.html', {
         'login_url': settings.LOGIN_URL
-    })
\ No newline at end of file
+    })


Mime
View raw message