Skip to content

Instantly share code, notes, and snippets.

@mjtamlyn
Created September 7, 2012 15:17
Show Gist options
  • Select an option

  • Save mjtamlyn/3667084 to your computer and use it in GitHub Desktop.

Select an option

Save mjtamlyn/3667084 to your computer and use it in GitHub Desktop.
Authenticate view for rest framework
class GetAccessToken(PermissionsMixin, View):
authentication = [APIKeyAuthentication]
form = UserForm
def post(self, request, *args, **kwargs):
user = authenticate(username=self.CONTENT['username'], password=self.CONTENT['password'])
if not user:
raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'detail': 'invalid username or password'})
access, created = Access.objects.get_or_create(application=self.user, user=user)
response_data = {
'user_url': reverse('api-user', kwargs={'pk': user.pk}),
'access_token': access.token,
}
return response_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment