Android Question WebApp digest authentication example?

barx

Well-Known Member
Licensed User
Longtime User
Hi all,

I am currently working on a web project that is being written using the CakePHP framework. The framwork supports Digest authentication. I need to make an Android app to compliment the system. The app will be use to collate data and store locally and then once on a wifi connection, the data will be uploaded to the web project. Now I need this to be relatively secure, so going with digest as SSL is not available. Never really done anything http with b4a so if anyone has a basic example of how to do the auth and how to put and get data I would be eternally grateful. I know it's a lot to ask but I have no idea where to start. Well, not strictly true as I know I need to start with the Http lib, but that is as far as I have got, lol
 

DonManfred

Expert
Licensed User
Longtime User
Setting Job.Username and Job.Password before sendind the request (httpjob) does not work?

B4X:
Dim job As HttpJob
job.Initialize("testhtaccess",Me)
job.Username = "myusername"
job.Password = "mypassword"
job.Download("http://mydomain:8080/test.php")
this calls the test.php which is in a HTACCESS secured folder
 
Last edited:
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
I have not tried anything yet as I didn't know where to start. Been reading articles all day, my eyes are burning out :confused:
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I have not tried anything yet as I didn't know where to start.

Starting at httputils Tutorial is a good start i think.
Search for other examples (there are a lot).

Feel free to ask if you encounter any problems

Been reading articles all day, my eyes are burning out :confused:

Dont read too much! ;-)

PS: I have tested the above code-example on my own xampp server on my pc. But with real username and password ;)
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
looking at the logs, the request is GET
B4X:
192.168.0.12 - - [15/Jun/2014:21:17:10 +0100] "GET /pvcollection/sites/pull/HFDH0001.json HTTP/1.1" 302 -

The framework I am using requires it to be a POST.

doing a cURL request as POST from another machine works fine

B4X:
127.0.0.1 - admin [15/Jun/2014:21:00:47 +0100] "POST /pvcollection/sites/pull/HFDH0001.json HTTP/1.1" 200 303

any way to get it to be POST???
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
I think I'm not quite getting this. The request I am making is like so...
B4X:
//server/pvcollections/sites/pull/HFDH0001.json
Which in effect is /server/path-to-wwwroot/controller/action/param.json
The .json tells the framework that I would like the response in json format

I was doing

B4X:
Dim httpAction as HttpJob
httpAction.Initialize("Get Site", Me)
httpAction.Username = "admin"
httpAction.Password = "test"
httpAction.Download(Shared.GetSetting("h") & "/pvcollection/sites/pull/" & txtSiteRef.text & ".json")

Now if I do this with Basic or Digest auth enabled on framework I get a redirect response and I believe this is because the request is GET method and needs to be POST. If I disable auth, it works nice (so at least I know it is auth that is issue)

after reply above I tried PostString

B4X:
httpAction.PostString(Shared.GetSetting("host") & "/pvcollection/sites/pull/", txtSiteRef.text & ".json")

or

httpAction.PostString(Shared.GetSetting("host") & "/pvcollection/sites/pull/" txtSiteRef.text & ".json", "")

Both give me the same error
java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=/pvcollection/sites/pull/

The host setting above is returned as expected (Shared.GetSetting("host")). So, I'm kinda lost again :confused:o_O

p.s. thanks so far @DonManfred,
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
ok, I fixed that error, I wasn't appending "http://". Using the post strings above I now seem to get a POST request sent but it still doesn't authenticate :(
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
doing a cURL request as POST from another machine works fine

B4X:
127.0.0.1 - admin [15/Jun/2014:21:00:47 +0100] "POST /pvcollection/sites/pull/HFDH0001.json HTTP/1.1" 200 303

That´s a POST-Request made from the machine the server is running on. 127.0.0.1 is localhost. Are you sure the request came from an other machine??

Maybe a firewall/router Issue?
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
OK made a little progress but not quite there.

I have got Basic authentication working, but not Digest. Digest works from the browser but NOT cURL or app. Which I don't reeally understand and they should all be singing from the same book.

B4X:
curl --digest --user admin:admin http://127.0.0.1/pvcollection/sites/pull/HFDH0001.json

Also tried it with a -d to make the request POST.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Unfortunately Don, this is currently running locally on my laptop in wamp. I'm just about to wrap up for the night, so I'll look at setting up a port forward and making it available to online. Not sure how far I will get as I don't know if my ISP allows port 80 servers, may have to change port, etc. I'll PM you some details if I get it live and leave laptop live until I hear back.
 
Upvote 0
Top