working on client v2 60/6360/1
authorlombardofr <lombardo@everyup.it>
Tue, 19 Jun 2018 14:59:45 +0000 (16:59 +0200)
committerlombardofr <lombardo@everyup.it>
Thu, 19 Jul 2018 12:40:12 +0000 (14:40 +0200)
Change-Id: I3ea66bd2d34b07e06de0f79aa2968ecc95ead795
Signed-off-by: lombardofr <lombardo@everyup.it>
authosm/backend.py
authosm/views.py
lib/osm/osmclient/client.py
lib/osm/osmclient/clientv2.py [new file with mode: 0644]
projecthandler/views.py
sf_t3d/settings.py
sf_t3d/urls.py
sf_user/sessions.py
template/login.html

index 1b92156..4bf9e72 100644 (file)
@@ -14,7 +14,7 @@
 #   limitations under the License.
 #
 from .models import OsmUser
-
+from lib.osm.osmclient.clientv2 import Client
 
 class OsmBackend(object):
 
@@ -28,18 +28,25 @@ class OsmBackend(object):
             password = kwargs['password']
             project_id = kwargs['project_id']
 
-            print username
-            print password
-            print project_id
+            client = Client()
+            result = client.auth(kwargs)
+            print "######"
+            print result
+
+            if 'error' in result and result['error'] == True:
+                return None
+            else:
+
+                try:
+                    user = OsmUser.objects.get(username=username)
 
-            try:
+                except OsmUser.DoesNotExist:
+                    # Create a new user. There's no need to set a password
+                    # we will keep just some preferences
+                    user = OsmUser(username=username)
 
-                return OsmUser.objects.get(username=username)
-            except OsmUser.DoesNotExist:
-                # Create a new user. There's no need to set a password
-                # we will keep just some preferences
-                user = OsmUser(username=username)
-                user.save()
+                    user.save()
+                user.session = result['data']
                 return user
 
         return None
index f97e8e1..a662ed0 100644 (file)
@@ -33,7 +33,6 @@ def user_login(request):
         user = authenticate(username=request.POST.get('username'),
                             password=request.POST.get('password'),
                             project_id=request.POST.get('project_id'))
-
         if user and user.is_active:
             if user.is_authenticated():
                 login(request, user)
@@ -44,4 +43,4 @@ def user_login(request):
                     return HttpResponseRedirect(next_page)
         else:
             error_message = 'Login failed!'
-    return render(request, 'login.html', {'error_message':error_message, 'collapsed_sidebar': False})
+    return render(request, 'login.html', {'error_message': error_message, 'collapsed_sidebar': False})
index e9248ec..a8a5fd0 100644 (file)
@@ -14,7 +14,7 @@ log = logging.getLogger('helper.py')
 
 
 class Client(object):
-    def __init__(self, host=os.getenv('OSM_SERVER', "localhost"), so_port=9999, so_project='admin', ro_host=None, ro_port=9090, **kwargs):
+    def __init__(self, host=os.getenv('OSM_SERVER', "192.168.100.199"), so_port=9999, so_project='admin', ro_host=None, ro_port=9090, **kwargs):
 
         self._user = 'admin'
         self._password = 'admin'
diff --git a/lib/osm/osmclient/clientv2.py b/lib/osm/osmclient/clientv2.py
new file mode 100644 (file)
index 0000000..ad58de3
--- /dev/null
@@ -0,0 +1,41 @@
+import requests
+import logging
+import json
+import tarfile
+import yaml
+import pyaml
+import StringIO
+from lib.util import Util
+import hashlib
+import os
+
+logging.basicConfig(level=logging.DEBUG)
+log = logging.getLogger('helper.py')
+
+
+class Client(object):
+    def __init__(self):
+        self._token_endpoint = 'admin/v1/tokens'
+        self._user_endpoint = 'admin/v1/users'
+        self._host = os.getenv('OSM_SERVER', "192.168.1.73")
+        self._so_port = 9999
+        self._base_path = "https://{0}:{1}/osm".format(self._host, self._so_port)
+
+    def auth(self, args):
+        result = {'error': True, 'data': ''}
+        token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
+        headers = {"Content-Type": "application/yaml", "accept": "application/json"}
+        try:
+            r = requests.post(token_url, json=args, verify=False, headers=headers)
+        except Exception as e:
+            print "saltata"
+            log.exception(e)
+            result['data'] = str(e)
+            return result
+        if r.status_code == requests.codes.ok:
+            result['error'] = False
+
+        result['data'] = Util.json_loads_byteified(r.text)
+
+        return result
+
index 16fb457..de21e9a 100644 (file)
@@ -22,7 +22,7 @@ from django.middleware.csrf import get_token
 from django.shortcuts import render, redirect
 from lib.util import Util
 from projecthandler.osm_model import OsmProject
-from lib.osm.osmclient.client import Client
+from lib.osm.osmclient.clientv2 import Client
 
 
 
index 36de1b6..dedc230 100644 (file)
@@ -45,7 +45,7 @@ INSTALLED_APPS = [
     'django.contrib.contenttypes',
     'django.contrib.messages',
     'django.contrib.staticfiles',
-    'sf_user',
+    'django.contrib.sessions',
     'authosm',
     'projecthandler',
     'vimhandler',
@@ -66,7 +66,7 @@ MIDDLEWARE_CLASSES = [
 
 ]
 
-SESSION_ENGINE='sf_user.sessions'
+SESSION_ENGINE ='django.contrib.sessions.backends.db'
 SESSION_COOKIE_AGE = 3500 #25 min
 SESSION_EXPIRE_AT_BROWSER_CLOSE = True
 SESSION_SAVE_EVERY_REQUEST = True
index a213202..d0bbf01 100644 (file)
@@ -15,7 +15,7 @@ Including another URLconf
     3. Add a URL to urlpatterns:  url(r'^blog/', include(blog_urls))
 """
 from django.conf.urls import url, include
-from sf_user import views as user_views
+#from sf_user import views as user_views
 from authosm import views as user_views
 from sf_t3d import views
 
index df4ed71..a2e1136 100644 (file)
 #   limitations under the License.
 #
 
-from django.contrib.sessions.backends.db import SessionStore as DBStore
-import sf_user
-
-class SessionStore(DBStore):
-    @classmethod
-    def get_model_class(cls):
-        return sf_user.models.CustomSession
-
-    def create_model_instance(self, data):
-        obj = super(SessionStore, self).create_model_instance(data)
-        try:
-            account_id = int(data.get('_auth_user_id'))
-        except (ValueError, TypeError):
-            account_id = None
-        obj.account_id = account_id
-        return obj
\ No newline at end of file
+from django.contrib.sessions.backends.db import SessionStore as DBStore
+import sf_user
+#
+class SessionStore(DBStore):
+    @classmethod
+    def get_model_class(cls):
+        return sf_user.models.CustomSession
+#
+    def create_model_instance(self, data):
+        obj = super(SessionStore, self).create_model_instance(data)
+        try:
+            account_id = int(data.get('_auth_user_id'))
+        except (ValueError, TypeError):
+            account_id = None
+        obj.account_id = account_id
+        return obj
\ No newline at end of file
index 2323200..47dd512 100644 (file)
             <input id="next_input" type="hidden" name="next" class="form-control">
             {% csrf_token %}
             <div class="form-group has-feedback">
-                <input type="username" name="username" class="form-control" placeholder="Username">
+                <input type="username" name="username" class="form-control" placeholder="Username" required>
                 <span class="glyphicon glyphicon-user form-control-feedback"></span>
             </div>
             <div class="form-group has-feedback">
-                <input type="password" name="password" class="form-control" placeholder="Password">
+                <input type="password" name="password" class="form-control" placeholder="Password" required>
                 <span class="glyphicon glyphicon-lock form-control-feedback"></span>
             </div>
             <div class="row">