blob: f71eddda4654e992cc72ae838a6aad60385ed3a6 [file] [log] [blame]
lombardofr099364f2018-06-12 11:21:02 +02001#
2# Copyright 2018 EveryUP Srl
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
lombardofr99f922f2018-07-17 17:27:36 +020016from django.core.exceptions import PermissionDenied
17
lombardofr099364f2018-06-12 11:21:02 +020018from .models import OsmUser
lombardofr07930222018-06-19 16:59:45 +020019from lib.osm.osmclient.clientv2 import Client
lombardofr99f922f2018-07-17 17:27:36 +020020from .exceptions import OSMAuthException
lombardofr099364f2018-06-12 11:21:02 +020021
lombardofr099364f2018-06-12 11:21:02 +020022
lombardofr835a0b72018-07-18 15:26:16 +020023class OsmBackend(object):
lombardofr4908f382018-09-10 11:36:06 +020024
lombardofr099364f2018-06-12 11:21:02 +020025 def authenticate(self, **kwargs):
26 '''
27 kwargs will receive the python dict that may contain
28 {username, password, project-id} to authenticate
29 '''
30 if all(k in kwargs for k in ('username', 'password', 'project_id')):
31 username = kwargs['username']
32 password = kwargs['password']
lombardofr099364f2018-06-12 11:21:02 +020033
lombardofr07930222018-06-19 16:59:45 +020034 client = Client()
35 result = client.auth(kwargs)
lombardofr099364f2018-06-12 11:21:02 +020036
lombardofr99f922f2018-07-17 17:27:36 +020037 if 'error' in result and result['error'] is True:
38 raise OSMAuthException(result['data'])
lombardofr07930222018-06-19 16:59:45 +020039 else:
lombardofr099364f2018-06-12 11:21:02 +020040
lombardofr07930222018-06-19 16:59:45 +020041 try:
42 user = OsmUser.objects.get(username=username)
lombardofr99f922f2018-07-17 17:27:36 +020043 user.psw = password
lombardofr835a0b72018-07-18 15:26:16 +020044 user.token = result['data']['id']
45 user.project_id = result['data']['project_id']
lombardofre5a130a2019-07-15 09:17:59 +020046 user.project_name = result['data']['project_name']
lombardofr835a0b72018-07-18 15:26:16 +020047 user.token_expires = result['data']['expires']
48 user.is_admin = bool(result['data']['admin'])
lombardofr99f922f2018-07-17 17:27:36 +020049 user.save()
lombardofr07930222018-06-19 16:59:45 +020050 except OsmUser.DoesNotExist:
lombardofr99f922f2018-07-17 17:27:36 +020051 user = OsmUser(username=username, psw=password, token=result['data']['id'],
lombardofr835a0b72018-07-18 15:26:16 +020052 project_id=result['data']['project_id'],
53 token_expires=result['data']['expires'], is_admin=result['data']['admin'])
lombardofr07930222018-06-19 16:59:45 +020054 user.save()
lombardofr99f922f2018-07-17 17:27:36 +020055
lombardofr099364f2018-06-12 11:21:02 +020056 return user
57
58 return None
59
60 def get_user(self, user_id):
61 try:
62 return OsmUser.objects.get(pk=user_id)
63 except OsmUser.DoesNotExist:
lombardofr835a0b72018-07-18 15:26:16 +020064 return None
lombardofr3ec25da2019-04-04 17:29:20 +020065