#!/bin/sh

# This file is part of Cockpit.
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
#

set -euf

auth=$(cat <&3)
success=0
case $auth in
    "failslow")
        sleep 2
        echo "{ \"error\": \"authentication-failed\" }" >&3
        ;;
    "fail")
        echo "{ \"error\": \"authentication-failed\" }" >&3
        ;;
    "denied")
        echo "{ \"error\": \"permission-denied\" }" >&3
        ;;
    "success")
        echo "{\"user\": \"me\" }" >&3
        success=1
        ;;
    "success-with-data")
        echo "{\"user\": \"me\", \"login-data\": { \"login\": \"data\"} }" >&3
        success=1
        ;;
    "success-bad-data")
        echo "{\"user\": \"me\", \"login-data\": \"bad\" }" >&3
        success=1
        ;;
    "no-user")
        echo "{ }" >&3
        ;;
    "with-error")
        echo "{ \"error\": \"unknown\", \"message\": \"detail for error\" }" >&3
        ;;
    "too-slow")
        sleep 10
        echo "{\"user\": \"me\", \"login-data\": { \"login\": \"data\"} }" >&3
        success=1
        ;;
    *)
        ;;
esac

exec 3<&-

if [ $success -eq 1 ]; then
    exec cat
fi
