# Path: doac/exceptions/base.py
# class InvalidToken(Exception):
#     error = "invalid_token"
# 
# Path: doac/exceptions/invalid_request.py
# class CredentialsNotProvided(InvalidRequest):
#     reason = "No credentials were provided to authenticate the request to view this page."
# 
# Path: doac/models.py
# class AccessToken(models.Model):
#     user = models.ForeignKey(user_model, related_name="access_tokens")
#     client = models.ForeignKey("Client", related_name="access_tokens")
# 
#     refresh_token = models.ForeignKey("RefreshToken", related_name="access_tokens")
#     token = models.CharField(
#         max_length=options.access_token["length"],
#         blank=True,
#         help_text=AUTO_GENERATION_HELP_TEXT,
#     )
#     scope = models.ManyToManyField("Scope", related_name="access_tokens")
# 
#     created_at = models.DateTimeField(auto_now_add=True)
#     expires_at = models.DateTimeField()
#     is_active = models.BooleanField(default=True)
# 
#     objects = managers.AccessTokenManager()
# 
#     def __unicode__(self):
#         return self.token
# 
#     def generate_token(self):
#         from .compat import get_random_string
# 
#         return get_random_string(options.access_token["length"])
# 
#     def revoke(self):
#         """
#         Revokes an individual access token.  This prevents the access token from being used in any future requests.
#         """
# 
#         self.is_active = False
#         self.save()
# 
#     def save(self, *args, **kwargs):
#         from .compat import now
# 
#         if not self.token:
#             self.token = self.generate_token()
# 
#         if not self.expires_at:
#             self.expires_at = now() + options.access_token["expires"]
# 
#         super(AccessToken, self).save(*args, **kwargs)
# 
# Path: doac/utils.py
# def request_error_header(exception):
#     """
#     Generates the error header for a request using a Bearer token based on a given OAuth exception.
#     """
# 
#     from .conf import options
#     
#     header = "Bearer realm=\"%s\"" % (options.realm, )
#     
#     if hasattr(exception, "error"):
#         header = header + ", error=\"%s\"" % (exception.error, )
#     
#     if hasattr(exception, "reason"):
#         header = header + ", error_description=\"%s\"" % (exception.reason, )
#     
#     return header


from ..exceptions.base import InvalidToken
from ..exceptions.invalid_request import CredentialsNotProvided
from ..models import AccessToken
from ..utils import request_error_header
        from django.http import HttpResponseBadRequest
        from doac.http import HttpResponseUnauthorized

        if self.validate(value, request) is not None:
            return None
        
        access_token = AccessToken.objects.for_token(value)
        
        return access_token.user
    
    def validate(self, value, request):
        """
        Try to get the `AccessToken` associated with the given token.

        The return value is determined based n a few things:

        - If no token is provided (`value` is None), a 400 response will  be returned.
        - If an invalid token is provided, a 401 response will be returned.
        - If the token provided is valid, `None` will be returned.
        """

        
        if not value:
            response = HttpResponseBadRequest()
            response["WWW-Authenticate"] = request_error_header(CredentialsNotProvided)
            
            return response
        
        try:
            access_token = AccessToken.objects.for_token(value)
        except AccessToken.DoesNotExist:
            response = HttpResponseUnauthorized()

# Path: doac/exceptions/base.py
# class InvalidToken(Exception):
#     error = "invalid_token"
# 
# Path: doac/exceptions/invalid_request.py
# class CredentialsNotProvided(InvalidRequest):
#     reason = "No credentials were provided to authenticate the request to view this page."
# 
# Path: doac/models.py
# class AccessToken(models.Model):
#     user = models.ForeignKey(user_model, related_name="access_tokens")
#     client = models.ForeignKey("Client", related_name="access_tokens")
# 
#     refresh_token = models.ForeignKey("RefreshToken", related_name="access_tokens")
#     token = models.CharField(
#         max_length=options.access_token["length"],
#         blank=True,
#         help_text=AUTO_GENERATION_HELP_TEXT,
#     )
#     scope = models.ManyToManyField("Scope", related_name="access_tokens")
# 
#     created_at = models.DateTimeField(auto_now_add=True)
#     expires_at = models.DateTimeField()
#     is_active = models.BooleanField(default=True)
# 
#     objects = managers.AccessTokenManager()
# 
#     def __unicode__(self):
#         return self.token
# 
#     def generate_token(self):
#         from .compat import get_random_string
# 
#         return get_random_string(options.access_token["length"])
# 
#     def revoke(self):
#         """
#         Revokes an individual access token.  This prevents the access token from being used in any future requests.
#         """
# 
#         self.is_active = False
#         self.save()
# 
#     def save(self, *args, **kwargs):
#         from .compat import now
# 
#         if not self.token:
#             self.token = self.generate_token()
# 
#         if not self.expires_at:
#             self.expires_at = now() + options.access_token["expires"]
# 
#         super(AccessToken, self).save(*args, **kwargs)
# 
# Path: doac/utils.py
# def request_error_header(exception):
#     """
#     Generates the error header for a request using a Bearer token based on a given OAuth exception.
#     """
# 
#     from .conf import options
#     
#     header = "Bearer realm=\"%s\"" % (options.realm, )
#     
#     if hasattr(exception, "error"):
#         header = header + ", error=\"%s\"" % (exception.error, )
#     
#     if hasattr(exception, "reason"):
#         header = header + ", error_description=\"%s\"" % (exception.reason, )
#     
#     return header


# Path: doac/handlers/bearer.py
from ..exceptions.base import InvalidToken
from ..exceptions.invalid_request import CredentialsNotProvided
from ..models import AccessToken
from ..utils import request_error_header
        from django.http import HttpResponseBadRequest
        from doac.http import HttpResponseUnauthorized


        if self.validate(value, request) is not None:
            return None
        
        access_token = AccessToken.objects.for_token(value)
        
        return access_token.user
    
    def validate(self, value, request):
        """
        Try to get the `AccessToken` associated with the given token.

        The return value is determined based n a few things:

        - If no token is provided (`value` is None), a 400 response will  be returned.
        - If an invalid token is provided, a 401 response will be returned.
        - If the token provided is valid, `None` will be returned.
        """

        
        if not value:
            response = HttpResponseBadRequest()
            response["WWW-Authenticate"] = request_error_header(CredentialsNotProvided)
            
            return response
        
        try:
            access_token = AccessToken.objects.for_token(value)
        except AccessToken.DoesNotExist:
            response = HttpResponseUnauthorized()

            response["WWW-Authenticate"] = request_error_header(InvalidToken)