Fix: Token Model crashes in Django Admin when a token has been given to a Client using Client Credentials.

This commit is contained in:
Juan Ignacio Fiorentino 2018-07-05 18:37:01 -03:00
parent 6e442a5f35
commit d825061508
2 changed files with 29 additions and 11 deletions

View file

@ -145,15 +145,12 @@ class BaseCodeTokenModel(models.Model):
def scope(self, value):
self._scope = ' '.join(value)
def has_expired(self):
return timezone.now() >= self.expires_at
def __str__(self):
return u'{0} - {1}'.format(self.client, self.user.email)
def __unicode__(self):
return self.__str__()
def has_expired(self):
return timezone.now() >= self.expires_at
class Code(BaseCodeTokenModel):
@ -170,6 +167,9 @@ class Code(BaseCodeTokenModel):
verbose_name = _(u'Authorization Code')
verbose_name_plural = _(u'Authorization Codes')
def __str__(self):
return u'{0} - {1}'.format(self.client, self.code)
class Token(BaseCodeTokenModel):
@ -179,6 +179,10 @@ class Token(BaseCodeTokenModel):
refresh_token = models.CharField(max_length=255, unique=True, verbose_name=_(u'Refresh Token'))
_id_token = models.TextField(verbose_name=_(u'ID Token'))
class Meta:
verbose_name = _(u'Token')
verbose_name_plural = _(u'Tokens')
@property
def id_token(self):
return json.loads(self._id_token)
@ -187,9 +191,8 @@ class Token(BaseCodeTokenModel):
def id_token(self, value):
self._id_token = json.dumps(value)
class Meta:
verbose_name = _(u'Token')
verbose_name_plural = _(u'Tokens')
def __str__(self):
return u'{0} - {1}'.format(self.client, self.access_token)
@property
def at_hash(self):

View file

@ -751,8 +751,8 @@ class TokenTestCase(TestCase):
id_token = self._request_id_token_with_scope(['openid', 'profile'])
kwargs_passed = id_token.get('kwargs_passed_to_processing_hook')
assert kwargs_passed
self.assertEqual(kwargs_passed.get('token'),
'<Token: Some Client - johndoe@example.com>')
self.assertTrue(kwargs_passed.get('token').startswith(
'<Token: Some Client -'))
self.assertEqual(kwargs_passed.get('request'),
"<WSGIRequest: POST '/openid/token'>")
self.assertEqual(set(kwargs_passed.keys()), {'token', 'request'})
@ -835,3 +835,18 @@ class TokenTestCase(TestCase):
# It should fail when client does not have any scope added.
self.assertEqual(400, response.status_code)
self.assertEqual('invalid_scope', response_dict['error'])
def test_printing_token_used_by_client_credentials_grant_type(self):
# Add scope for this client.
self.client.scope = ['something']
self.client.save()
post_data = {
'client_id': self.client.client_id,
'client_secret': self.client.client_secret,
'grant_type': 'client_credentials',
}
response = self._post_request(post_data)
response_dict = json.loads(response.content.decode('utf-8'))
token = Token.objects.get(access_token=response_dict['access_token'])
self.assertTrue(str(token))