# Path: doac/utils.py
# def total_seconds(delta):
#     """
#     Get the total seconds that a `datetime.timedelta` object covers.  Used for returning the total
#     time until a token expires during the handshake process.
#     """
# 
#     return delta.days * 86400 + delta.seconds


from django.template.response import TemplateResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from .utils import total_seconds
from . import utils
        from django.http import QueryDict, HttpResponseRedirect
        from .http import HttpResponseUnauthorized
        from .http import JsonResponse
        from .models import Client
        from .exceptions.invalid_client import ClientDoesNotExist
        from .exceptions.invalid_request import ClientNotProvided
        from urlparse import urlparse
        from .models import RedirectUri
        from .exceptions.invalid_request import RedirectUriDoesNotValidate, RedirectUriNotProvided
        from django.http import HttpResponseRedirect
        from .models import AuthorizationToken
        from .exceptions.access_denied import AuthorizationDenied
        from django.http import QueryDict
        from .models import AuthorizationCode
        from .exceptions.invalid_request import AuthorizationCodeNotValid, AuthorizationCodeNotProvided
        from django.contrib.auth.views import redirect_to_login
        from .models import AuthorizationCode
        from .exceptions.unsupported_response_type import ResponseTypeNotValid
        from .exceptions.invalid_request import ResponseTypeNotProvided
        from .models import Scope
        from .exceptions.invalid_scope import ScopeNotProvided, ScopeNotValid
        from .compat import now
        from .http import JsonResponse
        from .compat import now
        from .http import JsonResponse
        from .exceptions.invalid_client import ClientSecretNotValid
        from .exceptions.invalid_request import ClientSecretNotProvided
        from .exceptions.invalid_request import AuthorizationCodeAlreadyUsed, AuthorizationCodeNotProvided, AuthorizationCodeNotValid
        from .models import AuthorizationToken
        from .exceptions.unsupported_grant_type import GrantTypeNotProvided, GrantTypeNotValid
        from .exceptions.invalid_request import RefreshTokenNotProvided, RefreshTokenNotValid
        from .models import RefreshToken
            return self.render_authorization_token()

        elif self.grant_type == "refresh_token":
            try:
                self.verify_dictionary(request.POST, "refresh_token")
            except Exception, e:
                return self.render_exception_js(e)

            self.access_token = self.refresh_token.generate_access_token()

            return self.render_refresh_token()

    def render_authorization_token(self):

        remaining = self.refresh_token.expires_at - now()

        response = {}
        response["refresh_token"] = self.refresh_token.token
        response["token_type"] = "bearer"
        response["expires_in"] = int(total_seconds(remaining))
        response["access_token"] = self.access_token.token

        return JsonResponse(response)

    def render_refresh_token(self):

        remaining = self.access_token.expires_at - now()

        response = {}
        response["token_type"] = "bearer"

# Path: doac/utils.py
# def total_seconds(delta):
#     """
#     Get the total seconds that a `datetime.timedelta` object covers.  Used for returning the total
#     time until a token expires during the handshake process.
#     """
# 
#     return delta.days * 86400 + delta.seconds


# Path: doac/views.py
from django.template.response import TemplateResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from .utils import total_seconds
from . import utils
        from django.http import QueryDict, HttpResponseRedirect
        from .http import HttpResponseUnauthorized
        from .http import JsonResponse
        from .models import Client
        from .exceptions.invalid_client import ClientDoesNotExist
        from .exceptions.invalid_request import ClientNotProvided
        from urlparse import urlparse
        from .models import RedirectUri
        from .exceptions.invalid_request import RedirectUriDoesNotValidate, RedirectUriNotProvided
        from django.http import HttpResponseRedirect
        from .models import AuthorizationToken
        from .exceptions.access_denied import AuthorizationDenied
        from django.http import QueryDict
        from .models import AuthorizationCode
        from .exceptions.invalid_request import AuthorizationCodeNotValid, AuthorizationCodeNotProvided
        from django.contrib.auth.views import redirect_to_login
        from .models import AuthorizationCode
        from .exceptions.unsupported_response_type import ResponseTypeNotValid
        from .exceptions.invalid_request import ResponseTypeNotProvided
        from .models import Scope
        from .exceptions.invalid_scope import ScopeNotProvided, ScopeNotValid
        from .compat import now
        from .http import JsonResponse
        from .compat import now
        from .http import JsonResponse
        from .exceptions.invalid_client import ClientSecretNotValid
        from .exceptions.invalid_request import ClientSecretNotProvided
        from .exceptions.invalid_request import AuthorizationCodeAlreadyUsed, AuthorizationCodeNotProvided, AuthorizationCodeNotValid
        from .models import AuthorizationToken
        from .exceptions.unsupported_grant_type import GrantTypeNotProvided, GrantTypeNotValid
        from .exceptions.invalid_request import RefreshTokenNotProvided, RefreshTokenNotValid
        from .models import RefreshToken

            return self.render_authorization_token()

        elif self.grant_type == "refresh_token":
            try:
                self.verify_dictionary(request.POST, "refresh_token")
            except Exception, e:
                return self.render_exception_js(e)

            self.access_token = self.refresh_token.generate_access_token()

            return self.render_refresh_token()

    def render_authorization_token(self):

        remaining = self.refresh_token.expires_at - now()

        response = {}
        response["refresh_token"] = self.refresh_token.token
        response["token_type"] = "bearer"
        response["expires_in"] = int(total_seconds(remaining))
        response["access_token"] = self.access_token.token

        return JsonResponse(response)

    def render_refresh_token(self):

        remaining = self.access_token.expires_at - now()

        response = {}
        response["token_type"] = "bearer"

        response["expires_in"] = int(total_seconds(remaining))