diff --git a/example.py b/example.py index 99acdc2..e43aa1f 100644 --- a/example.py +++ b/example.py @@ -19,6 +19,9 @@ client.authenticate(username, password) # get member pprint.pprint(client.members.get('1')) +# get authenticated user +pprint.pprint(client.members.me()) + # get member groups pprint.pprint(client.members.get_groups('1')) diff --git a/pydusken/__init__.py b/pydusken/__init__.py index 2a2b9e3..9df61ec 100644 --- a/pydusken/__init__.py +++ b/pydusken/__init__.py @@ -28,14 +28,15 @@ class DuskenApi(object): def authenticate(self, username='', password=''): try: - self.access_token = self.get_auth_token(username, password, scope='write')['access_token'] + self.oauth2_access_token = self.get_access_token(username, password, scope='write') + self.access_token = self.oauth2_access_token['access_token'] except requests.exceptions.HTTPError as e: - logging.error("Could not authenticate with API. Correct client_id, client_secret, username or password?. {}".format(e)) + logging.error("Could not authenticate with API. Correct client_id, client_secret, username and password?\n{}".format(e)) return None return self.access_token - def get_auth_token(self, username, password, scope='read', grant_type='password'): + def get_access_token(self, username, password, scope='read', grant_type='password'): resp = requests.post( "{}/oauth2/access_token/".format(self.base_url), data=dict( diff --git a/pydusken/authentication.py b/pydusken/authentication.py index 94b3286..30dbb33 100644 --- a/pydusken/authentication.py +++ b/pydusken/authentication.py @@ -9,6 +9,9 @@ from . import * from models import DuskenAccessToken class DuskenBackend(object): + """ + Django Authentication Backend + """ def __init__(self): assert settings.DUSKEN_CLIENT_ID @@ -23,6 +26,12 @@ class DuskenBackend(object): except ValidationError as e: return False + def _sync_user_detail(self, user): + me = self._api.members.me()['objects'][0] + attrs = ['email', 'first_name', 'last_name'] + [setattr(user, attr, me[attr]) for attr in attrs] + user.save() + def authenticate(self, username=None, password=None): # Check the username/password and return a User. Username can be an email is_using_email = self._is_using_email(username) @@ -48,15 +57,15 @@ class DuskenBackend(object): if user.duskenaccesstoken.access_token != access_token: user.duskenaccesstoken.access_token = access_token=access_token user.save() - # if local user exist, sync email (for password reset) except User.DoesNotExist: # if user does not exist, create a local user - # TODO ...with synced data user = User(username=username) user.set_unusable_password() user.save() DuskenAccessToken.objects.create(access_token=access_token['access_token'], user=user) + self._sync_user_detail(user) + return user except HTTPError as e: