Exemplo n.º 1
0
def test_update_token_usage(data_fixture):
    token_1 = data_fixture.create_token()

    handler = TokenHandler()

    assert token_1.handled_calls == 0
    assert token_1.last_call is None

    with freeze_time('2020-01-01 12:00'):
        token_1 = handler.update_token_usage(token_1)

    assert token_1.handled_calls == 1
    assert token_1.last_call == datetime(2020,
                                         1,
                                         1,
                                         12,
                                         00,
                                         tzinfo=timezone('UTC'))
Exemplo n.º 2
0
    def authenticate(self, request):
        auth = get_authorization_header(request).split()

        if not auth or auth[0].lower() != b"token":
            return None

        if len(auth) == 1:
            msg = _("Invalid token header. No token provided.")
            raise AuthenticationFailed(
                {"detail": msg, "error": "ERROR_INVALID_TOKEN_HEADER"}
            )
        elif len(auth) > 2:
            msg = _("Invalid token header. Token string should not contain spaces.")
            raise AuthenticationFailed(
                {"detail": msg, "error": "ERROR_INVALID_TOKEN_HEADER"}
            )

        decoded_key = auth[1].decode(HTTP_HEADER_ENCODING)
        handler = TokenHandler()

        try:
            token = handler.get_by_key(decoded_key)
        except UserNotInGroup:
            msg = _("The token's user does not belong to the group anymore.")
            raise AuthenticationFailed(
                {"detail": msg, "error": "ERROR_TOKEN_GROUP_MISMATCH"}
            )
        except TokenDoesNotExist:
            msg = _("The provided token does not exist.")
            raise AuthenticationFailed(
                {"detail": msg, "error": "ERROR_TOKEN_DOES_NOT_EXIST"}
            )

        if not token.user.is_active:
            raise AuthenticationFailed(
                {
                    "detail": "The user related to the token is disabled.",
                    "error": "ERROR_USER_NOT_ACTIVE",
                }
            )

        token = handler.update_token_usage(token)
        request.user_token = token
        return token.user, token
Exemplo n.º 3
0
    def authenticate(self, request):
        auth = get_authorization_header(request).split()

        if not auth or auth[0].lower() != b'token':
            return None

        if len(auth) == 1:
            msg = _('Invalid token header. No token provided.')
            raise AuthenticationFailed({
                'detail': msg,
                'error': 'ERROR_INVALID_TOKEN_HEADER'
            })
        elif len(auth) > 2:
            msg = _(
                'Invalid token header. Token string should not contain spaces.'
            )
            raise AuthenticationFailed({
                'detail': msg,
                'error': 'ERROR_INVALID_TOKEN_HEADER'
            })

        decoded_key = auth[1].decode(HTTP_HEADER_ENCODING)
        handler = TokenHandler()

        try:
            token = handler.get_by_key(decoded_key)
        except UserNotInGroupError:
            msg = _('The token\'s user does not belong to the group anymore.')
            raise AuthenticationFailed({
                'detail': msg,
                'error': 'ERROR_TOKEN_GROUP_MISMATCH'
            })
        except TokenDoesNotExist:
            msg = _('The provided token does not exist.')
            raise AuthenticationFailed({
                'detail': msg,
                'error': 'ERROR_TOKEN_DOES_NOT_EXIST'
            })

        token = handler.update_token_usage(token)
        request.user_token = token
        return token.user, token