|
| 1 | +#!/bin/bash |
| 2 | +username=${1}; shift |
| 3 | +password=${1}; shift |
| 4 | + |
| 5 | +if [ -z "$username" ]; then |
| 6 | + echo "Please pass the username and password. " |
| 7 | + exit -1 |
| 8 | +fi |
| 9 | +if [ -z "$password." ]; then |
| 10 | + echo "Please pass the password. " |
| 11 | + exit -1 |
| 12 | +fi |
| 13 | + |
| 14 | +export HEDGEDOC_SERVER=${HEDGEDOC_SERVER} |
| 15 | +if [ -z "$HEDGEDOC_SERVER" ]; then |
| 16 | + echo "Please setup the HEDGEDOC_SERVER. " 1>&2 |
| 17 | + exit -1 |
| 18 | +fi |
| 19 | +cookie=${HEDGEDOC_COOKIES_FILE} |
| 20 | +if [ -z "$HEDGEDOC_COOKIES_FILE" ]; then |
| 21 | + echo "Please setup the HEDGEDOC_COOKIES_FILE. " 1>&2 |
| 22 | + exit -1 |
| 23 | +fi |
| 24 | + |
| 25 | +hedgedochost=$(echo $HEDGEDOC_SERVER | perl -ne 'print "$1" if /(https?:\/\/[^:^\/\\s]+)\//') |
| 26 | + |
| 27 | +function step1() { |
| 28 | + echo |
| 29 | + echo "Step 1:" |
| 30 | + local headers |
| 31 | + headers=$(curl "${HEDGEDOC_SERVER}/auth/gitlab" \ |
| 32 | + -H "authority: ${hedgedochost}" \ |
| 33 | + -H 'pragma: no-cache' \ |
| 34 | + -H 'cache-control: no-cache' \ |
| 35 | + -H 'upgrade-insecure-requests: 1' \ |
| 36 | + -H "referer: ${HEDGEDOC_SERVER}/" \ |
| 37 | + -b $cookie -c $cookie --insecure \ |
| 38 | + -s -o /dev/null \ |
| 39 | + -D - | grep location | cut -c11-) |
| 40 | + export GITLAB_CALLBACK_LOCATION=${headers//[$'\t\r\n']} # need to remove CRLF |
| 41 | + echo "GITLAB_CALLBACK_LOCATION is $GITLAB_CALLBACK_LOCATION" |
| 42 | + # get the host |
| 43 | + export GITLAB_HOST=$(echo $GITLAB_CALLBACK_LOCATION | perl -ne 'print "$1" if /(https?:\/\/[^:^\/\\s]+)\//') |
| 44 | + echo "GITLAB_HOST is $GITLAB_HOST" # with http/https prefix |
| 45 | +} |
| 46 | + |
| 47 | +function step2() { |
| 48 | + echo |
| 49 | + echo "Step 2:" |
| 50 | + |
| 51 | + local body |
| 52 | + body=$(curl $GITLAB_CALLBACK_LOCATION \ |
| 53 | + -H 'Connection: keep-alive' \ |
| 54 | + -H 'Pragma: no-cache' \ |
| 55 | + -H 'Cache-Control: no-cache' \ |
| 56 | + -H 'Upgrade-Insecure-Requests: 1' \ |
| 57 | + -b $cookie -c $cookie \ |
| 58 | + --insecure) |
| 59 | + export GITLAB_LOGIN_LOCATION=$(echo "$body" | perl -ne 'print "$1" if /.*?a href="(.+?)"/') |
| 60 | + echo "GITLAB_LOGIN_LOCATION is $GITLAB_LOGIN_LOCATION" |
| 61 | +} |
| 62 | + |
| 63 | +function step3() { |
| 64 | + echo |
| 65 | + echo "Step 3:" |
| 66 | + local body |
| 67 | + local gitlab_token |
| 68 | + local token |
| 69 | + |
| 70 | + # https://stackoverflow.com/questions/47948887/login-to-gitlab-with-username-and-password-using-curl |
| 71 | + body=$(curl $GITLAB_LOGIN_LOCATION \ |
| 72 | + -H 'Connection: keep-alive' \ |
| 73 | + -H 'Pragma: no-cache' \ |
| 74 | + -H 'Cache-Control: no-cache' \ |
| 75 | + -H 'Upgrade-Insecure-Requests: 1' \ |
| 76 | + -b $cookie -c $cookie --insecure) |
| 77 | + gitlab_token=$( echo "$body" | grep 'authenticity_token' | perl -ne 'print "$1\n" if /.*?authenticity_token"[[:blank:]]value="(.+?)"/' | sed -n 1p ) |
| 78 | + echo |
| 79 | + echo "GET TOKEN" |
| 80 | + echo $gitlab_token |
| 81 | + token= |
| 82 | + if [ -n "$gitlab_token" ]; then |
| 83 | + token="--data-urlencode authenticity_token=${gitlab_token}" |
| 84 | + fi |
| 85 | + local gitlab_login_real=$( echo "$body" | grep 'form' | perl -ne 'print "$1\n" if /.*?action="(.+?)"[[:blank:]]/' | sed -n 1p ) |
| 86 | + |
| 87 | + export GITLAB_LOGIN_REAL_LOCATION="${GITLAB_HOST}${gitlab_login_real}" |
| 88 | + echo "GITLAB_LOGIN_REAL_LOCATION is $GITLAB_LOGIN_REAL_LOCATION" # with http/https prefix |
| 89 | + |
| 90 | + |
| 91 | + local header |
| 92 | + header=$(curl $GITLAB_LOGIN_REAL_LOCATION \ |
| 93 | + -H 'Connection: keep-alive' \ |
| 94 | + -H 'Pragma: no-cache' \ |
| 95 | + -H 'Cache-Control: no-cache' \ |
| 96 | + -H 'Upgrade-Insecure-Requests: 1' \ |
| 97 | + -H "Origin: ${GITLAB_HOST}" \ |
| 98 | + -H 'Content-Type: application/x-www-form-urlencoded' \ |
| 99 | + -H "Referer: ${GITLAB_LOGIN_LOCATION}" \ |
| 100 | + -H 'Accept-Language: en' \ |
| 101 | + --data "grant_type=password&username=${username}&password=${password}" ${token} \ |
| 102 | + -b $cookie -c $cookie \ |
| 103 | + --insecure -D - | grep Location | cut -c11-) |
| 104 | + header=${header//[$'\t\r\n']} |
| 105 | + if [ "$header" = "$GITLAB_CALLBACK_LOCATION" ]; then |
| 106 | + echo "same callback $header" |
| 107 | + else |
| 108 | + echo "Mismatch callback: " 1>&2 |
| 109 | + echo "header: $header " 1>&2 |
| 110 | + echo "callback: $GITLAB_CALLBACK_LOCATION" 1>&2 |
| 111 | + exit -1 |
| 112 | + fi |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | +function step4-5() { |
| 117 | + echo |
| 118 | + echo "Step 4:" |
| 119 | + local body |
| 120 | + local link |
| 121 | + body=$(curl $GITLAB_CALLBACK_LOCATION \ |
| 122 | + -H 'Connection: keep-alive' \ |
| 123 | + -H 'Pragma: no-cache' \ |
| 124 | + -H 'Cache-Control: no-cache' \ |
| 125 | + -H 'Upgrade-Insecure-Requests: 1' \ |
| 126 | + -b $cookie -c $cookie \ |
| 127 | + --insecure) |
| 128 | + |
| 129 | + link=$(echo $body | grep window.location | perl -ne 'print "$1\n" if /.*?window.location= "(.+?)";/') |
| 130 | + echo "WILL REDIRECT TO $link " |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + echo |
| 135 | + echo "Step 5:" |
| 136 | + curl $link \ |
| 137 | + -H "authority: ${hedgedochost}" \ |
| 138 | + -H 'pragma: no-cache' \ |
| 139 | + -H 'cache-control: no-cache' \ |
| 140 | + -b $cookie -c $cookie \ |
| 141 | + --compressed --insecure |
| 142 | + |
| 143 | + echo |
| 144 | + echo "DONE" |
| 145 | + |
| 146 | +} |
| 147 | + |
| 148 | +# gitlab |
| 149 | +# check |
| 150 | + |
| 151 | +if [ ! -f "$cookie" ]; then |
| 152 | + step1 |
| 153 | + step2 |
| 154 | + step3 |
| 155 | + step4-5 |
| 156 | +fi |
| 157 | + |
0 commit comments