Auth fixes, forwards error messages from api
This commit is contained in:
1 parent
3600df6f16
commit
515008488f
4 files changed
+40
-17
No files matched your search
+3
-3
@@ -4,8 +4,8 @@ import json
|
||||
import pydusken
|
||||
|
||||
# oauth
|
||||
client_id = "d802768a8afd9dec2660"
|
||||
client_secret = "3962467fe60a94727812053c8a4f44533f4ef610"
|
||||
client_id = "4c90c83fa400acadafdf"
|
||||
client_secret = "49d2c94c50da6216059f44d89bd87fa18b31f9f7"
|
||||
|
||||
# credentials
|
||||
username = "admin"
|
||||
@@ -26,7 +26,7 @@ pprint.pprint(client.members.me())
|
||||
pprint.pprint(client.members.get_groups('1'))
|
||||
|
||||
# update member password to the same password as you have
|
||||
pprint.pprint(client.members.update('1', address='admin'))
|
||||
pprint.pprint(client.members.update('1', password='admin'))
|
||||
|
||||
# filter members
|
||||
pprint.pprint(client.members.filter(email="nikolai@studentersamfundet.no"))
|
||||
+19
-1
@@ -29,7 +29,7 @@ class DuskenApi(object):
|
||||
def authenticate(self, username='', password=''):
|
||||
try:
|
||||
self.oauth2_access_token = self.get_access_token(username, password, scope='read+write')
|
||||
self.access_token = self.oauth2_access_token
|
||||
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 and password?\n{0}".format(e))
|
||||
return None
|
||||
@@ -50,3 +50,21 @@ class DuskenApi(object):
|
||||
resp.raise_for_status()
|
||||
|
||||
return json.loads(resp.content)
|
||||
|
||||
def raise_for_status(self, resp):
|
||||
"""
|
||||
Custom raise_for_status that raises stored :class:`HTTPError` on resp, if one occurred.
|
||||
Prints Error message from Dusken API
|
||||
"""
|
||||
|
||||
http_error_msg = ''
|
||||
|
||||
if 400 <= resp.status_code < 500:
|
||||
http_error_msg = '%s Client Error: %s' % (resp.status_code, resp.content)
|
||||
|
||||
elif 500 <= resp.status_code < 600:
|
||||
http_error_msg = '%s Server Error: %s' % (resp.status_code, resp.content)
|
||||
|
||||
if http_error_msg:
|
||||
raise requests.exceptions.HTTPError(http_error_msg, response=resp)
|
||||
|
||||
@@ -43,12 +43,17 @@ class DuskenBackend(object):
|
||||
return user
|
||||
|
||||
def _refresh_access_token(self, user, access_token):
|
||||
print "lol"
|
||||
try:
|
||||
d = DuskenAccessToken(user=user, **access_token)
|
||||
d.save()
|
||||
except Exception as e:
|
||||
print e
|
||||
d = DuskenAccessToken.objects.filter(user=user)
|
||||
if len(d) == 0:
|
||||
d = DuskenAccessToken(user=user)
|
||||
else:
|
||||
d = d[0]
|
||||
|
||||
d.access_token = access_token['access_token']
|
||||
d.expires_in = access_token['expires_in']
|
||||
d.refresh_token = access_token['refresh_token']
|
||||
d.scope = access_token['scope']
|
||||
d.save()
|
||||
|
||||
def _sync_user_detail(self, user):
|
||||
me = self._api.members.me()['objects'][0]
|
||||
@@ -69,7 +74,7 @@ class DuskenBackend(object):
|
||||
user = self._get_or_create_local_user(username)
|
||||
|
||||
self._refresh_access_token(user, access_token)
|
||||
|
||||
|
||||
self._sync_user_detail(user)
|
||||
|
||||
return user
|
||||
|
||||
+6
-6
@@ -33,12 +33,12 @@ class Members(object):
|
||||
params['offset'] = offset
|
||||
|
||||
resp = self.__GET("{0}/member/".format(self._api.base_url), params=params)
|
||||
resp.raise_for_status()
|
||||
self._api.raise_for_status(resp)
|
||||
return json.loads(resp.content)
|
||||
|
||||
def get(self, member_id, memberships=None, groups=None):
|
||||
resp = self.__GET("{0}/member/{1}/".format(self._api.base_url, member_id), params=dict(memberships=memberships, groups=groups))
|
||||
resp.raise_for_status()
|
||||
self._api.raise_for_status(resp)
|
||||
return json.loads(resp.content)
|
||||
|
||||
def me(self):
|
||||
@@ -46,12 +46,12 @@ class Members(object):
|
||||
Full member with all related data associated to the authenticated user.
|
||||
"""
|
||||
resp = self.__GET("{0}/me/".format(self._api.base_url))
|
||||
resp.raise_for_status()
|
||||
self._api.raise_for_status(resp)
|
||||
return json.loads(resp.content)
|
||||
|
||||
def filter(self, username=None, first_name=None, last_name=None, email=None):
|
||||
resp = self.__GET("{0}/member/".format(self._api.base_url), params=dict(username=username, first_name=first_name, last_name=last_name, email=email))
|
||||
resp.raise_for_status()
|
||||
self._api.raise_for_status(resp)
|
||||
return json.loads(resp.content)
|
||||
|
||||
def get_username_from_email(self, email):
|
||||
@@ -60,12 +60,12 @@ class Members(object):
|
||||
|
||||
def get_groups(self, member_id):
|
||||
resp = self.__GET("{0}/member/{1}/groups/".format(self._api.base_url, member_id))
|
||||
resp.raise_for_status()
|
||||
self._api.raise_for_status(resp)
|
||||
return json.loads(resp.content)
|
||||
|
||||
def update(self, member_id, password=None):
|
||||
#TODO more attributes
|
||||
resp = self.__PATCH("{0}/member/{1}/".format(self._api.base_url, member_id), data=json.dumps(dict(password=password)), headers={'content-type': 'application/json'})
|
||||
resp.raise_for_status()
|
||||
self._api.raise_for_status(resp)
|
||||
# Response body is empty
|
||||
return True
|
||||
Reference in new issue
Block a user