AiC and REST login onelab
authorLoic Baron <loic.baron@lip6.fr>
Wed, 24 May 2017 12:38:42 +0000 (14:38 +0200)
committerLoic Baron <loic.baron@lip6.fr>
Wed, 24 May 2017 12:38:42 +0000 (14:38 +0200)
myslice/urls.py
portal/templates/loginwidget.html
portal/templates/onelab/onelab_home-view.html
rest/login.py [new file with mode: 0644]

index 6b1c9fa..954c7ec 100644 (file)
@@ -86,6 +86,7 @@ urls = [
     #
     #
     # RESTful interface
+    (r'^rest/login/$','rest.login.dispatch'),
     (r'^rest/(?P<object_type>[^/]+)/(?P<object_name>[^/]+)?/?$', 'rest.get.dispatch'),
     (r'^sfa/(?P<method>[^/]+)/?$', 'rest.sfa_api.dispatch'),
     (r'^table/(?P<object_type>[^/]+)/(?P<object_name>[^/]+)?/?$', 'rest.get.dispatch'),
index 763fab6..b738d46 100644 (file)
@@ -14,4 +14,4 @@
     You don't have an account yet? 
     <br /><a href="http://portal.onelab.eu/register">Sign Up!</a>
 </div>
-</form>
\ No newline at end of file
+</form>
index c98ee91..d7689fc 100644 (file)
                     </p>
                     {% include theme|add:"_dashboard_links.html" %}
                 </div>
-            </div>            
+            </div>
+                       <br>
+            <div class="row">
+                <div class="col-sm-12">
+                    <h3>
+                        <span class="glyphicon glyphicon-cloud" style="font-size:40px; color:#ca47c3; display:inline-block;"></span>
+                                               <span style="vertical-align:15px; display:inline-block; margin-left:20px;">SERVICES</span>
+                    </h3>
+                    <p>
+                                               <a href="https://aic.onelab.eu" target="_blank"><img src="{{ STATIC_URL }}img/aic-logo.png" alt="" style="width:100px;margin-left:40px;" /></a>
+                                               <br>
+                                               <span class="glyphicon glyphicon-phone"></span>
+                                               <a href="https://aic.onelab.eu" target="_blank">
+                                               Connect to AiC
+                                               </a>
+                                               <br>
+                                               <span class="glyphicon glyphicon-book"></span>
+                                               <a href="https://aic-project.github.io/" target="_blank">Documentation</a>
+                    </p>
+                </div>
+            </div>
         </div>
     </div>
 </div>
diff --git a/rest/login.py b/rest/login.py
new file mode 100644 (file)
index 0000000..ca9b784
--- /dev/null
@@ -0,0 +1,63 @@
+import json
+from django.shortcuts               import render_to_response
+from django.views.decorators.csrf   import csrf_exempt
+from django.http                    import HttpResponse, HttpResponseNotFound, HttpResponseForbidden, HttpResponseServerError, HttpResponseBadRequest
+from django.contrib.auth            import authenticate, login
+from manifoldapi.manifoldresult     import ManifoldResult
+
+import activity.user
+
+@csrf_exempt
+def dispatch(request):
+    if request.method == 'POST':
+       data = json.loads(request.body)
+    else:
+        return HttpResponseBadRequest(json.dumps({"error":"Bad request use POST"}), content_type="application/json")
+
+    result = None
+    username = None
+    password = None
+    if 'email' in data:
+        username = data['email']
+    if 'password' in data:
+        password = data['password']
+
+    if not username or not password:
+        return HttpResponseBadRequest(json.dumps({"error":"Bad request"}), content_type="application/json")
+    else:
+        token = {'username': username, 'password': password, 'request': request}
+        auth_result = authenticate(token=token)
+        # our authenticate function returns either
+        # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
+        # . a django User in case of success
+        # . or None if the backend could be reached but the authentication failed
+        if isinstance (auth_result, ManifoldResult):
+            manifoldresult = auth_result
+            # let's use ManifoldResult.__repr__
+            msg="%s"%manifoldresult
+            return HttpResponseServerError(json.dumps({"error":msg}), content_type="application/json")
+        # user was authenticated at the backend
+        elif auth_result is not None:
+            user=auth_result
+            if user is not None and user.is_active:
+                login(request, user)
+
+                if request.user.is_authenticated():
+                    try:
+                        result = {'email':username}
+                        # log user activity
+                        activity.user.login(request)
+                    except Exception as e:
+                        import traceback
+                        traceback.print_exc()
+                        msg = "Your session has expired"
+                        return HttpResponseServerError(json.dumps({"error":msg}), content_type="application/json")
+            else:
+                msg = "Your account is not active, please contact the site admin."
+                return HttpResponseForbidden(json.dumps({"error":msg}), content_type="application/json")
+        # otherwise
+        else:
+            msg = "Your username and/or password were incorrect."
+            return HttpResponseNotFound(json.dumps({"error":msg}), content_type="application/json")
+    return HttpResponse(json.dumps(result))
+