X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=authosm%2Fbackend.py;h=628eb6aab77a40318205059309e78fb273d2d091;hb=4908f382154fddb84d72ce8d516229649c01b606;hp=4bf9e729cca7182f097c22490e5ea34df377508a;hpb=0793022e37fb0d73e46bcdc0b74a673709508603;p=osm%2FLW-UI.git diff --git a/authosm/backend.py b/authosm/backend.py index 4bf9e72..628eb6a 100644 --- a/authosm/backend.py +++ b/authosm/backend.py @@ -13,8 +13,12 @@ # 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,27 +30,28 @@ 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'] client = Client() result = client.auth(kwargs) - print "######" - print result - if 'error' in result and result['error'] == True: - return None + if 'error' in result and result['error'] is True: + raise OSMAuthException(result['data']) else: try: user = OsmUser.objects.get(username=username) - + user.psw = password + user.token = result['data']['id'] + user.project_id = result['data']['project_id'] + user.token_expires = result['data']['expires'] + user.is_admin = bool(result['data']['admin']) + user.save() 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 = 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() - user.session = result['data'] + return user return None @@ -55,4 +60,4 @@ class OsmBackend(object): try: return OsmUser.objects.get(pk=user_id) except OsmUser.DoesNotExist: - return None \ No newline at end of file + return None