Now syncs email and name

This commit is contained in:
nikolark committed 2013-12-27 22:59:42 +01:00
1 parent 88b526d184
commit a91d1c94d3
3 files changed
+18 -5

No files matched your search

+3
View File
@@ -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'))
+4 -3
View File
@@ -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(
+11 -2
View File
@@ -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: