DKAN API CSRF Validation Failed

304 Views Asked by At

I'm trying to log into my DKAN instance via the API just through the terminal but I keep getting a CSRF validation failed error. Here's what I'm doing.

First I login an collect a CSRF token:

curl -X POST -i -H "Content-type: application/json" -H "Accept: application/json" -c cookies.txt -X POST http://192.168.33.10/?q=api/dataset/user/login -d '{
    "username":"admin",
    "password":"mypassword"
}'

This outputs the following:

HTTP/1.1 200 OK
Date: Mon, 24 Apr 2017 15:34:45 GMT
Server: Apache/2.4.18 (Ubuntu)
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: no-cache, must-revalidate
X-Content-Type-Options: nosniff
Vary: Accept
Set-Cookie: SESS37d01773a34cec5fc5828d6b58243814=ybUPfN7scbsTJnJQNf_yWkaqIBQlDJOBLYXDj8bdpO4; expires=Wed, 17-May-2017 19:08:05 GMT; Max-Age=2000000; path=/; HttpOnly
Content-Length: 807
Content-Type: application/json

{"sessid":"ybUPfN7scbsTJnJQNf_yWkaqIBQlDJOBLYXDj8bdpO4","session_name":"SESS37d01773a34cec5fc5828d6b58243814","token":"Of5m-_2V9vwLYmI49aArgd6K9HSqVJwpQwSfQH1jlBU","user":{"uid":"1","name":"admin","mail":"[email protected]","theme":"","signature":"","signature_format":null,"created":"1493037751","access":"1493047878","login":1493048085,"status":"1","timezone":"Europe/London","language":"","picture":null,"init":"[email protected]","data":false,"uuid":"21e95002-1c5a-4b1d-a515-8c8a245025c5","roles":{"2":"authenticated user"},"field_about":[],"og_user_node":{"und":[{"target_id":"1"},{"target_id":"2"},{"target_id":"3"}]},"rdf_mapping":{"rdftype":["sioc:UserAccount"],"name":{"predicates":["foaf:name"]},"homepage":{"predicates":["foaf:page"],"type":"rel"}}}}

Next I take the token from the above output and put it into the following command to log into DKAN and create a new dataset:

curl -X POST -i -H "Content-type: application/json" -H "X-CSRF-Token:Of5m-_2V9vwLYmI49aArgd6K9HSqVJwpQwSfQH1jlBU" -b cookies.txt -X POST http://192.168.33.10/?=api/dataset/node -d '{
    "title":"A node created via DKAN REST API",
    "type":"dataset",
    "body": {
        "und": [{"value": "This should be the description"}]
    }
}'

The output from this command is:

HTTP/1.1 403 Forbidden
Date: Mon, 24 Apr 2017 15:35:03 GMT
Server: Apache/2.4.18 (Ubuntu)
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: no-cache, must-revalidate
X-Content-Type-Options: nosniff
Content-Length: 41
Content-Type: text/html; charset=UTF-8

403 Access Denied: CSRF validation failed

I've just been following the API tutorial in the docs. Am I missing something?

1

There are 1 best solutions below

0
On BEST ANSWER

I solved my own problem. Everything was correct apart from a missing space after the X-CSRF-TOKEN header in the second curl command.

The command should have been:

curl --request POST --include --header "Content-type: application/json" --header "X-CSRF-Token: Of5m-_2V9vwLYmI49aArgd6K9HSqVJwpQwSfQH1jlBU" --cookie cookies.txt --request POST http://192.168.33.10/?=api/dataset/node -data '{
    "title":"A node created via DKAN REST API",
    "type":"dataset",
    "body": {
        "und": [{"value": "This should be the description"}]
    }
}'

Notice the space between "X-CSRF-Token: and Of5m-_2V9vwLYmI49aArgd6K9HSqVJwpQwSfQH1jlBU". Evidently that's very important. I also changed the tags to their more readable form to help me understand what's going on.