mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 11:33:59 +03:00
Tweak authtoken
This commit is contained in:
parent
beea6487b2
commit
5c17a60176
|
@ -126,7 +126,7 @@ class TokenAuthentication(BaseAuthentication):
|
|||
except self.model.DoesNotExist:
|
||||
return None
|
||||
|
||||
if token.user.is_active and not getattr(token, 'revoked', False):
|
||||
if token.user.is_active:
|
||||
return (token.user, token)
|
||||
|
||||
# TODO: OAuthAuthentication
|
||||
|
|
|
@ -11,8 +11,7 @@ class Migration(SchemaMigration):
|
|||
# Adding model 'Token'
|
||||
db.create_table('authtoken_token', (
|
||||
('key', self.gf('django.db.models.fields.CharField')(max_length=40, primary_key=True)),
|
||||
('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'])),
|
||||
('revoked', self.gf('django.db.models.fields.BooleanField')(default=False)),
|
||||
('user', self.gf('django.db.models.fields.related.OneToOneField')(related_name='api_key', unique=True, to=orm['auth.User'])),
|
||||
('created', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)),
|
||||
))
|
||||
db.send_create_signal('authtoken', ['Token'])
|
||||
|
@ -57,8 +56,7 @@ class Migration(SchemaMigration):
|
|||
'Meta': {'object_name': 'Token'},
|
||||
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
|
||||
'key': ('django.db.models.fields.CharField', [], {'max_length': '40', 'primary_key': 'True'}),
|
||||
'revoked': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
|
||||
'user': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'api_key'", 'unique': 'True', 'to': "orm['auth.User']"})
|
||||
},
|
||||
'contenttypes.contenttype': {
|
||||
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
|
||||
|
|
|
@ -9,8 +9,7 @@ class Token(models.Model):
|
|||
The default authorization token model.
|
||||
"""
|
||||
key = models.CharField(max_length=40, primary_key=True)
|
||||
user = models.ForeignKey('auth.User')
|
||||
revoked = models.BooleanField(default=False)
|
||||
user = models.OneToOneField('auth.User', related_name='api_key')
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
|
@ -21,3 +20,6 @@ class Token(models.Model):
|
|||
def generate_key(self):
|
||||
unique = str(uuid.uuid4())
|
||||
return hmac.new(unique, digestmod=sha1).hexdigest()
|
||||
|
||||
def __unicode__(self):
|
||||
return self.key
|
||||
|
|
|
@ -149,5 +149,6 @@ class TokenAuthTests(TestCase):
|
|||
|
||||
def test_token_has_auto_assigned_key_if_none_provided(self):
|
||||
"""Ensure creating a token with no key will auto-assign a key"""
|
||||
self.token.delete()
|
||||
token = Token.objects.create(user=self.user)
|
||||
self.assertTrue(bool(token.key))
|
||||
|
|
Loading…
Reference in New Issue
Block a user