Add authentication scheme selection

Authentication scheme can be basic or digest.
The default digest behavior has been kept.

I tried to keep the code formatting as close to the original as possible.