2 # Copyright 2018 EveryUP Srl
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from django
.core
.exceptions
import PermissionDenied
18 from .models
import OsmUser
19 from lib
.osm
.osmclient
.clientv2
import Client
20 from .exceptions
import OSMAuthException
23 class OsmBackend(object):
25 def authenticate(self
, **kwargs
):
27 kwargs will receive the python dict that may contain
28 {username, password, project-id} to authenticate
30 if all(k
in kwargs
for k
in ('username', 'password', 'project_id')):
31 username
= kwargs
['username']
32 password
= kwargs
['password']
35 result
= client
.auth(kwargs
)
37 if 'error' in result
and result
['error'] is True:
38 raise OSMAuthException(result
['data'])
42 user
= OsmUser
.objects
.get(username
=username
)
44 user
.token
= result
['data']['id']
45 user
.project_id
= result
['data']['project_id']
46 user
.project_name
= result
['data']['project_name']
47 user
.token_expires
= result
['data']['expires']
48 user
.is_admin
= bool(result
['data']['admin'])
50 except OsmUser
.DoesNotExist
:
51 user
= OsmUser(username
=username
, psw
=password
, token
=result
['data']['id'],
52 project_id
=result
['data']['project_id'],
53 token_expires
=result
['data']['expires'], is_admin
=result
['data']['admin'])
60 def get_user(self
, user_id
):
62 return OsmUser
.objects
.get(pk
=user_id
)
63 except OsmUser
.DoesNotExist
: