blob: 9c316a93152b5c6ead7a99525056e17cb30c2bbb [file] [log] [blame]
lombardofr84d0a012018-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#
lombardofr236e3e32018-07-17 17:27:36 +020016from django.core.exceptions import PermissionDenied
17
lombardofr84d0a012018-06-12 11:21:02 +020018from .models import OsmUser
lombardofr672969e2018-06-19 16:59:45 +020019from lib.osm.osmclient.clientv2 import Client
lombardofr236e3e32018-07-17 17:27:36 +020020from .exceptions import OSMAuthException
lombardofr84d0a012018-06-12 11:21:02 +020021
lombardofr84d0a012018-06-12 11:21:02 +020022
lombardofrb4d6cec2018-07-18 15:26:16 +020023class OsmBackend(object):
lombardofr84d0a012018-06-12 11:21:02 +020024 def authenticate(self, **kwargs):
25 '''
26 kwargs will receive the python dict that may contain
27 {username, password, project-id} to authenticate
28 '''
29 if all(k in kwargs for k in ('username', 'password', 'project_id')):
30 username = kwargs['username']
31 password = kwargs['password']
lombardofr84d0a012018-06-12 11:21:02 +020032
lombardofr672969e2018-06-19 16:59:45 +020033 client = Client()
34 result = client.auth(kwargs)
lombardofr84d0a012018-06-12 11:21:02 +020035
lombardofr236e3e32018-07-17 17:27:36 +020036 if 'error' in result and result['error'] is True:
37 raise OSMAuthException(result['data'])
lombardofr672969e2018-06-19 16:59:45 +020038 else:
lombardofr84d0a012018-06-12 11:21:02 +020039
lombardofr672969e2018-06-19 16:59:45 +020040 try:
41 user = OsmUser.objects.get(username=username)
lombardofr236e3e32018-07-17 17:27:36 +020042 user.psw = password
lombardofrb4d6cec2018-07-18 15:26:16 +020043 user.token = result['data']['id']
44 user.project_id = result['data']['project_id']
45 user.token_expires = result['data']['expires']
46 user.is_admin = bool(result['data']['admin'])
lombardofr236e3e32018-07-17 17:27:36 +020047 user.save()
lombardofr672969e2018-06-19 16:59:45 +020048 except OsmUser.DoesNotExist:
lombardofr236e3e32018-07-17 17:27:36 +020049 user = OsmUser(username=username, psw=password, token=result['data']['id'],
lombardofrb4d6cec2018-07-18 15:26:16 +020050 project_id=result['data']['project_id'],
51 token_expires=result['data']['expires'], is_admin=result['data']['admin'])
lombardofr672969e2018-06-19 16:59:45 +020052 user.save()
lombardofr236e3e32018-07-17 17:27:36 +020053
lombardofr84d0a012018-06-12 11:21:02 +020054 return user
55
56 return None
57
58 def get_user(self, user_id):
59 try:
60 return OsmUser.objects.get(pk=user_id)
61 except OsmUser.DoesNotExist:
lombardofrb4d6cec2018-07-18 15:26:16 +020062 return None