X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=authosm%2Fbackend.py;h=628eb6aab77a40318205059309e78fb273d2d091;hb=refs%2Fchanges%2F01%2F6601%2F1;hp=1b9215659209632b13f339a982a914159aeee3ed;hpb=099364f3465712ac0232f9535ee15b3b5f902fa9;p=osm%2FLW-UI.git diff --git a/authosm/backend.py b/authosm/backend.py index 1b92156..628eb6a 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,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'] - - print username - print password - print project_id - try: + client = Client() + result = client.auth(kwargs) + + 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: + 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 +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