0

The FOSRestBundle is working perfectly in my project but without authentification. Now, my goal is to make my requests with auth.

To do so, I added this firewall in security.yml

firewalls: # ... rest_api: pattern: ^/api/ stateless: true http_basic: provider: fos_userbundle # ... access_control: # ... - { path: ^/api/, role: IS_AUTHENTICATED_FULLY } 

To check this, I used this shell command:

curl -i http://localhost/tuto/web/app_dev.php/api/test/1 

The result is:

HTTP/1.1 302 Found Date: Fri, 11 Apr 2014 13:56:08 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.4.9-4ubuntu2.4 Set-Cookie: PHPSESSID=4dtr168vmj1eg523a07kbkjkh1; path=/ Cache-Control: no-cache Location: http://localhost/tuto/web/app_dev.php/login Vary: Accept-Language X-Debug-Token: 220df7 Transfer-Encoding: chunked Content-Type: application/json <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="refresh" content="1;url=http://localhost/tuto/web/app_dev.php/login" /> <title>Redirecting to http://localhost/tuto/web/app_dev.php/login</title> </head> <body> Redirecting to <a href="http://localhost/tuto/web/app_dev.php/login">http://localhost/tuto/web/app_dev.php/login</a>. </body> </html> 

As you can see, the returned code is 302 FOUND since it is URL is redirected to http://localhost/tuto/web/app_dev.php/login as I am using FOSUserBundle.

Now, I want to check if the authenfication is working well. I tried this: curl -i http://localhost/tuto/web/app_dev.php/api/test/1 --user user:password but still I have 302 FOUND.

I tried this also curl -i -H 'Accept:application/json' -H 'Authorization:Basic username:password' http://localhost/tuto/web/app_dev.php/api/test/1 but still I have 302 FOUND.

So is there any proposal of curl command to check the auth in my REST api?

Thanks,

Notice: I have tried solutions in many links like: basic authorization command for curl

1 Answer 1

1

Solution found!

All you have to do is:

  1. Change the firewall like this:

firewalls:

# ... rest_api: # pattern: ^/api/ # to be changed as in the next line pattern: /api/.* # the correct way stateless: true http_basic: provider: fos_userbundle # ... access_control: # ... # - { path: ^/api/, role: IS_AUTHENTICATED_FULLY } # to be removed 
  1. For the curl command, it has to be like this:

curl -i -H 'Accept:application/json' -H 'Authorization:Basic YW1pbmU6c3RpZ21hdGFn' http://localhost/tuto/web/app_dev.php/api/test/1

where YW1pbmU6c3RpZ21hdGFn is the result of encoding 'user:password' in base64.

Hope it will help others.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the hint with base64 helped me. For some reason cURL does not generate base64 if you use "-u user:password"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.