Skip to content

Instantly share code, notes, and snippets.

@muxueqz
Created November 28, 2018 12:46
Show Gist options
  • Save muxueqz/d61007a357266b8c7476a3ae09b2723b to your computer and use it in GitHub Desktop.
Save muxueqz/d61007a357266b8c7476a3ae09b2723b to your computer and use it in GitHub Desktop.
#!/bin/sh
workdir=/dev/shm/gitlab_auth_by_sh
mkdir -pv ${workdir}
cookies_file=`mktemp -p ${workdir}`
rm -rfv $cookies_file
gitlab_host="https://git_host"
gitlab_user=$username
gitlab_password=$password
# curl for the login page to get a session cookie and the sources with the auth tokens
body_header=$(curl -c $cookies_file -i "${gitlab_host}/users/sign_in" -s)
# grep the auth token for the user login for
# not sure whether another token on the page will work, too - there are 3 of them
csrf_token=$(echo $body_header | grep -oP '(name="authenticity_token" value=")[^"]*' | cut -d'"' -f4)
# send login credentials with curl, using cookies and token from previous request
curl -b $cookies_file -c $cookies_file -i "${gitlab_host}/users/sign_in" \
--data "user[login]=${gitlab_user}&user[password]=${gitlab_password}" \
--data-urlencode "authenticity_token=${csrf_token}" -v
login_status=$(curl -b $cookies_file "${gitlab_host}/api/v4/user" | grep active -c)
rm -rfv ${cookies_file}
if [[ $login_status == 1 ]];then
echo 0
else
echo 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment