wx/src/functions/login.sh
Christer Warén 963770b974 Source Update
2024-04-29 06:00:07 +03:00

190 lines
4.8 KiB
Bash

wx-login(){
echo -n "$wxItalic"
echo " >> Login << "
echo -n "$wxNormal";
echo "------------------------------"
ORG=$1
if [[ -z "$HOSTNAME" || ${#HOSTNAME} -lt 5 ]]; then
wx-stop
fi
if [[ $(hostname -d) == "devices.waren.io" ]]
then
ORG=warengroup
elif [[ $(hostname -d) == "devices.cwinfo.net" ]]
then
ORG=cwinfo
elif [[ $(hostname -d) == "devices.christerwaren.fi" ]]
then
ORG=cwchristerw
elif [[ -z $ORG ]]
then
echo -n "Status: "
echo -n $wxItalic
echo -n "Organization Required"
echo -n $wxNormal
wx-stop
fi
if [[ $ORG == "warengroup" ]]
then
DOMAIN=waren.io
elif [[ $ORG == "cwinfo" ]]
then
DOMAIN=cwinfo.net
elif [[ $ORG == "cwchristerw" ]]
then
DOMAIN=christerwaren.fi
else
echo -n "Status: "
echo -n $wxItalic
echo -n "Organization Unsupported"
echo -n $wxNormal
wx-stop
fi
FOLDER=$ORG
DEVICE_DOMAIN="devices.$DOMAIN"
IDM_DOMAIN="idm.cwinfo.net"
VAULT_DOMAIN="vault.cwinfo.net"
GIT_DOMAIN="git.cwinfo.net"
if [[ $(hostname -d) ]]
then
HOSTNAME=$(hostname --fqdn)
elif [[ -z $(hostname -d) ]] && [[ ! -z $2 ]]
then
HOSTNAME="$2.$DEVICE_DOMAIN"
else
echo -n "Status: "
echo -n $wxItalic
echo -n "Hostname Required"
echo -n $wxNormal
wx-stop
fi
if [[ -z $USER ]]
then
if [[ -z $SUDO_USER ]]
then
if [[ -z LOGNAME ]]
then
echo -n "Status: "
echo -n $wxItalic
echo -n "Username Required"
echo -n $wxNormal
wx-stop
else
USER=$LOGNAME
fi
else
USER=$SUDO_USER
fi
fi
VAULT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$VAULT_DOMAIN/v1/sys/health)
if [[ $VAULT_STATUS -eq 200 ]]
then
IDM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://$IDM_DOMAIN)
if [[ $IDM_STATUS -eq 301 ]]
then
echo -n $wxBold
echo -n $ORG
echo $wxNormal
if [[ -z $USER || $USER == "root" || $USER == "local" ]]
then
echo -n "Username: "
echo -n $wxItalic
read USERNAME
echo -n $wxNormal
else
echo -n "Username: "
echo -n $wxItalic
echo -n $USER
echo $wxNormal
USERNAME=$USER
fi
echo -n "Password: "
echo -n $wxItalic
read -s PASSWORD
echo -n "****************"
echo $wxNormal
if [[ -z $USERNAME || -z $PASSWORD ]]
then
echo -n "Status: "
echo -n $wxItalic
echo -n "Username & Password Required"
echo -n $wxNormal
wx-stop
else
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/ldap/login/$USERNAME -X POST -d '{ "password": "'$PASSWORD'" }' -s | jq -r '.auth.client_token')
if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]]
then
echo -n "Status: "
echo -n $wxItalic
echo -n "Login Failed"
echo -n $wxNormal
wx-stop
fi
fi
config[${ORG}]=$VAULT_LOGIN
wx-start
else
echo -n $wxBold
echo -n $ORG
echo $wxNormal
echo -n "Token: "
read -s TOKEN
echo "****************"
if [[ -z $TOKEN || ${#TOKEN} -lt 95 || ${#TOKEN} -gt 95 ]]
then
echo -n "Status: "
echo -n $wxItalic
echo -n "Vault Token Required"
echo -n $wxNormal
wx-stop
fi
VAULT_LOGIN=$(curl https://$VAULT_DOMAIN/v1/auth/token/renew -X POST --header "X-Vault-Token: $TOKEN" -d '{ "token": "'$TOKEN'" }' -s | jq -r '.auth.client_token')
if [[ -z $VAULT_LOGIN || ${#VAULT_LOGIN} -lt 95 || ${#VAULT_LOGIN} -gt 95 ]]
then
echo -n "Status: "
echo -n $wxItalic
echo -n "Login Failed"
echo -n $wxNormal
wx-stop
fi
config[${ORG}]=$VAULT_LOGIN
wx-start
fi
else
echo -n $wxBold
echo -n $ORG
echo $wxNormal
echo -n "Status: "
echo -n $wxItalic
echo -n "Vault Offline"
echo -n $wxNormal
wx-stop
fi
}