X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=authosm%2Fbackend.py;h=f71eddda4654e992cc72ae838a6aad60385ed3a6;hb=c1e3d672556a859371f2586d6e512bfb708b7561;hp=1b9215659209632b13f339a982a914159aeee3ed;hpb=099364f3465712ac0232f9535ee15b3b5f902fa9;p=osm%2FLW-UI.git diff --git a/authosm/backend.py b/authosm/backend.py index 1b92156..f71eddd 100644 --- a/authosm/backend.py +++ b/authosm/backend.py @@ -13,7 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from django.core.exceptions import PermissionDenied + from .models import OsmUser +from lib.osm.osmclient.clientv2 import Client +from .exceptions import OSMAuthException class OsmBackend(object): @@ -26,20 +30,29 @@ class OsmBackend(object): if all(k in kwargs for k in ('username', 'password', 'project_id')): username = kwargs['username'] password = kwargs['password'] - project_id = kwargs['project_id'] - print username - print password - print project_id + client = Client() + result = client.auth(kwargs) + + if 'error' in result and result['error'] is True: + raise OSMAuthException(result['data']) + else: - try: + try: + user = OsmUser.objects.get(username=username) + user.psw = password + user.token = result['data']['id'] + user.project_id = result['data']['project_id'] + user.project_name = result['data']['project_name'] + user.token_expires = result['data']['expires'] + user.is_admin = bool(result['data']['admin']) + user.save() + except OsmUser.DoesNotExist: + user = OsmUser(username=username, psw=password, token=result['data']['id'], + project_id=result['data']['project_id'], + token_expires=result['data']['expires'], is_admin=result['data']['admin']) + user.save() - 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() return user return None @@ -48,4 +61,5 @@ class OsmBackend(object): try: return OsmUser.objects.get(pk=user_id) except OsmUser.DoesNotExist: - return None \ No newline at end of file + return None +