ndejong / pfsense_fauxapi

REST based API interface for pfSense 2.3.x and 2.4.x to facilitate devops
Apache License 2.0
354 stars 61 forks source link

Help with authentication #12

Closed buttersausage closed 7 years ago

buttersausage commented 7 years ago

Hi, I've been trying to authenticate using your guide to no avail.

As per the below:

fauxapi-auth: <apikey>:<timestamp>:<nonce>:<hash>

For example:-
fauxapi-auth: PFFA4797d073:20161119Z144328:833a45d8:9c4f96ab042f5140386178618be1ae40adc68dd9fd6b158fb82c99f3aaa2bb55

I have the following code:

apisecret="longsecrethere"
timestamp=`date +%Y%m%dZ%H%M%S`
nonce="**what is this and how do i get it?**"
hash=`echo ${apisecret}${timestamp}${nonce} | shasum -a 256 | tr -d "[:space:]-"`

curl -X GET --silent --insecure --header "fauxapi-auth: PFFAwZz0dFMaX1nrlr095Ei2:${timestamp}:${nonce}:${hash}" "https://192.168.1.1/fauxapi/v1/?action=rule_get"

With the above I'm getting failed auth. Any help on how to use the mentioned is appreciated

ndejong commented 7 years ago

Hi - sorry to hear you are having a hassle with this, I think the source of your trouble here is that you are not specifying the timestamp in UTC with the --utc flag in the date command line, I suspect I need to document this better.

I'd encourage you to enable debug mode by adding &__debug=true to the end of your URL, this way you get a lot more detail about why the auth request is failing. It's likely you also want to pipe the curl return data through jq to pretty print the JSON response data so you can human-read it easier.

I note that you are asking a question about what a "nonce" value is. A nonce value is simply nonsense random data that exists so that no two auth tokens will be the same.

However, you can save yourself a lot of time by just looking at the supplied BASH library fauxapi_lib.sh - the bash function you are looking for is below.

fauxapi_auth() {

    fauxapi_apikey=${1}
    fauxapi_apisecret=${2}

    fauxapi_timestamp=`date --utc +%Y%m%dZ%H%M%S`
    fauxapi_nonce=`head -c 40 /dev/urandom | md5sum | head -c 8`

    # NB:-
    #  auth = apikey:timestamp:nonce:HASH(apisecret:timestamp:nonce)

    fauxapi_hash=`echo -n ${fauxapi_apisecret}${fauxapi_timestamp}${fauxapi_nonce} | sha256sum | cut -d' ' -f1`
    fauxapi_auth=${fauxapi_apikey}:${fauxapi_timestamp}:${fauxapi_nonce}:${fauxapi_hash}

    echo ${fauxapi_auth}
}