Example #1
0
    def post(self, request, format=None):
        '''
        Example reqs:
            curl -X POST -H 'Accept: application/json; indent=4' -d 'token=abc' -u root:root localhost:8000/token_info/
            curl -X POST -H 'Accept: application/json; indent=4' -d 'token=abc' -u sage-api-user:test localhost:8000/token_info/
        '''

        # TODO do this via token permission
        if request.user.username != "sage-api-server":
            content = {
                'error':
                'StatusUnauthorized ({})'.format(request.user.username)
            }
            return Response(content, status=status.HTTP_401_UNAUTHORIZED)

        data = request.data  # is a QueryDict

        token = data.get("token", "")

        try:
            tobject = Token.objects.get(tokenValue=token)
        except Token.DoesNotExist:
            content = {'error': 'token not found'}
            return Response(content, status=status.HTTP_410_GONE)

        if tobject == None:
            content = {'error': 'tobject is empty'}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        expires = tobject.expires

        if expires == None:
            content = {'error': 'expires is empty'}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        if timezone.now() > expires:
            content = {'error': 'token expired ({})'.format(str(expires))}
            return Response(content, status=status.HTTP_404_NOT_FOUND)

        scope = tobject.scope
        user = tobject.user

        unix_expires = int(dformat(expires, 'U'))

        content = {
            "active":
            True,  # Required. This is a boolean value of whether or not the presented token is currently active. The value should be “true” if the token has been issued by this authorization server, has not been revoked by the user, and has not expired.
            "scope":
            scope,  # A JSON string containing a space-separated list of scopes associated with this token.
            "client_id":
            "some-client-id",  # The client identifier for the OAuth 2.0 client that the token was issued to.
            "username":
            user,  # A human-readable identifier for the user who authorized this token.
            "exp":
            unix_expires,  # The unix timestamp (integer timestamp, number of seconds since January 1, 1970 UTC) indicating when this token will expire.
        }

        return Response(content)
Example #2
0
def format_datetime_local(datetime, format="DATETIME_FORMAT"):
    # type: (datetime.datetime, str) -> str
    """
    Format a datetime object to a localized string via python.

    Note: The datetime rendered in template is itself locale aware.
    A custom format must be defined in settings.py.
    When a custom format uses a same name with an existing built-in
    format, it will be overrided by built-in format if l10n
    is enabled.
    """

    from django.utils import formats
    try:
        return formats.date_format(datetime, format)
    except AttributeError:
        try:
            from django.utils.dateformat import format as dformat
            return dformat(datetime, format)
        except AttributeError:
            return formats.date_format(datetime, "DATETIME_FORMAT")
Example #3
0
def format_datetime_local(datetime, format='DATETIME_FORMAT'):
    # type: (datetime.datetime, str) -> str
    """
    Format a datetime object to a localized string via python.

    Note: The datetime rendered in template is itself locale aware.
    A custom format must be defined in settings.py.
    When a custom format uses a same name with an existing built-in
    format, it will be overrided by built-in format if l10n
    is enabled.
    """

    from django.utils import formats
    try:
        return formats.date_format(datetime, format)
    except AttributeError:
        try:
            from django.utils.dateformat import format as dformat
            return dformat(datetime, format)
        except AttributeError:
            return formats.date_format(datetime, "DATETIME_FORMAT")