From 0793022e37fb0d73e46bcdc0b74a673709508603 Mon Sep 17 00:00:00 2001 From: lombardofr Date: Tue, 19 Jun 2018 16:59:45 +0200 Subject: [PATCH] working on client v2 Change-Id: I3ea66bd2d34b07e06de0f79aa2968ecc95ead795 Signed-off-by: lombardofr --- authosm/backend.py | 29 +++++++++++++++---------- authosm/views.py | 3 +-- lib/osm/osmclient/client.py | 2 +- lib/osm/osmclient/clientv2.py | 41 +++++++++++++++++++++++++++++++++++ projecthandler/views.py | 2 +- sf_t3d/settings.py | 4 ++-- sf_t3d/urls.py | 2 +- sf_user/sessions.py | 32 +++++++++++++-------------- template/login.html | 4 ++-- 9 files changed, 83 insertions(+), 36 deletions(-) create mode 100644 lib/osm/osmclient/clientv2.py diff --git a/authosm/backend.py b/authosm/backend.py index 1b92156..4bf9e72 100644 --- a/authosm/backend.py +++ b/authosm/backend.py @@ -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 diff --git a/authosm/views.py b/authosm/views.py index f97e8e1..a662ed0 100644 --- a/authosm/views.py +++ b/authosm/views.py @@ -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}) diff --git a/lib/osm/osmclient/client.py b/lib/osm/osmclient/client.py index e9248ec..a8a5fd0 100644 --- a/lib/osm/osmclient/client.py +++ b/lib/osm/osmclient/client.py @@ -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 index 0000000..ad58de3 --- /dev/null +++ b/lib/osm/osmclient/clientv2.py @@ -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 + diff --git a/projecthandler/views.py b/projecthandler/views.py index 16fb457..de21e9a 100644 --- a/projecthandler/views.py +++ b/projecthandler/views.py @@ -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 diff --git a/sf_t3d/settings.py b/sf_t3d/settings.py index 36de1b6..dedc230 100644 --- a/sf_t3d/settings.py +++ b/sf_t3d/settings.py @@ -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 diff --git a/sf_t3d/urls.py b/sf_t3d/urls.py index a213202..d0bbf01 100644 --- a/sf_t3d/urls.py +++ b/sf_t3d/urls.py @@ -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 diff --git a/sf_user/sessions.py b/sf_user/sessions.py index df4ed71..a2e1136 100644 --- a/sf_user/sessions.py +++ b/sf_user/sessions.py @@ -14,19 +14,19 @@ # 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 diff --git a/template/login.html b/template/login.html index 2323200..47dd512 100644 --- a/template/login.html +++ b/template/login.html @@ -13,11 +13,11 @@ {% csrf_token %}
- +
- +
-- 2.17.1