mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 09:57:55 +03:00 
			
		
		
		
	Create a key by default if none is specified
This commit is contained in:
		
							parent
							
								
									f3e65eab6b
								
							
						
					
					
						commit
						5a3874ee11
					
				| 
						 | 
					@ -146,3 +146,8 @@ class TokenAuthTests(TestCase):
 | 
				
			||||||
        """Ensure POSTing json over token auth without correct credentials fails"""
 | 
					        """Ensure POSTing json over token auth without correct credentials fails"""
 | 
				
			||||||
        response = self.csrf_client.post('/', json.dumps({'example': 'example'}), 'application/json')
 | 
					        response = self.csrf_client.post('/', json.dumps({'example': 'example'}), 'application/json')
 | 
				
			||||||
        self.assertEqual(response.status_code, 403)
 | 
					        self.assertEqual(response.status_code, 403)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_token_has_auto_assigned_key_if_none_provided(self):
 | 
				
			||||||
 | 
					        """Ensure creating a token with no key will auto-assign a key"""
 | 
				
			||||||
 | 
					        token = Token.objects.create(user=self.user)
 | 
				
			||||||
 | 
					        self.assertEqual(len(token.key), 32)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,16 +1,22 @@
 | 
				
			||||||
 | 
					import uuid
 | 
				
			||||||
from django.db import models
 | 
					from django.db import models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BaseToken(models.Model):
 | 
					class BaseToken(models.Model):
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    The base abstract authorization token model class.
 | 
					    The base abstract authorization token model class.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    key = models.CharField(max_length=32, primary_key=True)
 | 
					    key = models.CharField(max_length=32, primary_key=True, blank=True)
 | 
				
			||||||
    user = models.ForeignKey('auth.User')
 | 
					    user = models.ForeignKey('auth.User')
 | 
				
			||||||
    revoked = models.BooleanField(default=False)
 | 
					    revoked = models.BooleanField(default=False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        abstract=True
 | 
					        abstract=True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def save(self, *args, **kwargs):
 | 
				
			||||||
 | 
					        if not self.key:
 | 
				
			||||||
 | 
					            self.key = uuid.uuid4().hex
 | 
				
			||||||
 | 
					        return super(BaseToken, self).save(*args, **kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Token(BaseToken):
 | 
					class Token(BaseToken):
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										0
									
								
								djangorestframework/tokenauth/views.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								djangorestframework/tokenauth/views.py
									
									
									
									
									
										Normal file
									
								
							
		Loading…
	
		Reference in New Issue
	
	Block a user