2

I'm trying to execute a CURL request via command line which uses drush uli to log into my site as an administrative user. Then it's redirected to another destination url.

It works if the redirected url is a non-admin path available to anonymous users, but admin paths return a 403 Forbidden, which is something my site does for anonymous users trying to access admin pages.

This works:

curl -vk -L $(drush uli admin_username /node/1)

This returns HTTP 403 forbidden:

curl -vk -L $(drush uli admin_username /admin/structure)

The optional path parameter is being redirected to after logging in, but it's like when CURL gets there... it's not logged in anymore and is seeing the response as if it were an anonymous user.

Anyone experience this?

3 Answers 3

0

Is your site redirecting from http to https when you access admin pages? If so, try to set the --uri parameter you provide to Drush to an https instead of http protocol, and see if that works better.

1
  • Yes it does redirect using Secure Pages, but this still gives me a 403: curl -vk -L https://www.example.com/user/reset/1/1439211277/SomeExampleResetHashXXXXXXXXXXXXXXXXXX/login?destination=/admin/structure Commented Aug 10, 2015 at 13:00
0

Thanks to https://spinspire.com/article/automating-drupal-user-login-and-maintaining-session-curl, my issue was solved by using the --cookie-jar CURL option to maintain my session cookie while drush redirected to the admin path after generating a one-time login link.

Here's my final working command:

curl --cookie-jar cookie.txt -vk -L $(drush uli admin_username /admin/structure)

This results in an HTTP 200 response from /admin/structure.

0

Your curl fails, because it has problem passing the cookies between the redirects. Wget should be more suitable tool for this tasks, e.g.

wget -qO- $(drush -l http://mysite.local uli --no-browser admin admin/structure)

Note: To test the output in human readable format, you can pipe the output to html2text command.

To retrieve the page using curl, you can try the following shell script by using user credentials:

#!/usr/bin/env bash
url="https://www.example.com"
uri_php="/admin/structure"
user=admin
pass=admin
form_build_id=$(curl -s $url/user | grep -o 'form-[^" ]\{40,\}')
cookie=$(curl -sX POST -d "name=$user&pass=$pass&form_id=user_login&op=Log+in&form_build_id=$form_build_id" $url -D- | grep -o "SESS[^;]\{60,\}")
content=$(curl -s -H "Cookie: $cookie" ${url}${uri_php})
echo $content

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.