Dim hj As HttpJob
hj.Initialize("",Me)
hj.Username="admin"
hj.Password="password"
hj.Download("http://192.168.5.188/relay/0?turn=on")
Wait for (hj) JobDone(hj As HttpJob)
If hj.Success Then
label1.Text = hj.GetString
Else
label1.Text ="Error"
End If
Try the code below. I've tested it against https://httpbin.org/digest-auth/auth/test/test3/SHA-256 and it seems to work. The code is not all-encompassing regarding the RFC spec and only handles GET requests, but hopefully, it implements enough to work for your needs.what would be the proper format for sha-256 digest auth ? code snippet would be nice
Wait For(AuthenticateUrl("https://httpbin.org/digest-auth/auth/test/test3/SHA-256", "test", "test3")) Complete (j As HttpJob)
If j.Success = True
'successfully authenticated
Log(j.GetString)
Else
Log(j.ErrorMessage)
End If
'See...
Correct. And HTTP Digest via HttpJob will do that for you (that is how digest works). It's just that currently the underlying OkHttpClientWrapper does not support it. I do understand what you are doing, but the API doc also says that it would work with HTTP Digest. If it would, the HTTP Digest would take care of all the realm, nonce, hashing, etc, without the programmer having to do it himself.When communicating over HTTP, this process must be repeated for each request you send to the device.
Shelly.SetAuth
which should not be called before calling the method above first.Shelly.GetStatus
hakana commented on Dec 18, 2019
Look like you have restricted login enabled on Shelly, you need to specify the same user and password in config.yaml.
paparapapapapa commented on Dec 19, 2019
This was the issue thank you. It worked adding:
shelly:
username: xxx
password: xxx
hj.Username = "admin"
hj.Password = "password"
hj.Download("http://192.168.5.188/relay/0?turn=on")
hj.Download("http://192.168.5.188/relay/0?turn=on")
hj.GetRequest.SetHeader("Authorization", "Basic " & SU.EncodeBase64("admin:password".GetBytes("UTF8")))
hj.Download("http://admin:password@192.168.5.188/relay/0?turn=on")
The interesting part about that one is that the issue poster uses BASIC authentication with the Shelly implementation he is working with
So here we are looking at the digest authentication method. The reason OP is getting the 401, is that the following occurs (with the code posted the first post).Communication through HTTP and Websocket channels is secured by a digest authentication mechanism using the SHA256 hmac algorithm as defined in RFC7616.
At this point, my strong believe are:The interesting part about that one is that the issue poster uses BASIC authentication with the Shelly implementation he is working with
The doc provided by the poster though mentions
So here we are looking at the digest authentication method. The reason OP is getting the 401, is that the following occurs (with the code posted the first post).
1) The underlying library is set up to handle a digest authentication via a custom Authenticator
2) The URL call is made w/o passing any user/password information
3) The server answers with 401, with various pieces of information in the WWW-Authenticate header used by the client to create the response
4) The underlying library calls the custom Authenticator.
5) The custom Authenticator checks if the request is for a digest authentication. If so it creates the MD5 digest response (using the information provided in the WWW-Authenticate header) and passes a new request back to the underlying library that has the digest information set in the Authorization header
6) The underlying library makes another call to the server
7) The server, expecting a SHA256-generated response, fails to authenticate the user and returns another 401
8) This second 401 is what is returned to the B4X application
Where the B4A digest implementation fails is in step#5. The custom Authenticator does not check the algorithm entry in the WWW-Authenticate header but just assumes that the algorithm is MD5.
where can i find config.yaml ???config.yaml as mentioned in post #22.
To be fair, according to https://caniuse.com/mdn-http_headers_www-authenticate_digest_sha-256 most browsers did not implement sha256 digest until late last year. Firefox was on the forefront of this, implementing this feature int the fall of 2021. I did place a wish to expand HttpJob's capabilities to support RFC7616 algorithms. See Thread '[B4X] Support for RFC7616 algorithms for digest authentication' https://www.b4x.com/android/forum/t...-algorithms-for-digest-authentication.158862/Look: any browser can log in these shelly devices without knowing anything about "config.yaml", so Username and password is enough.
Does it have something call ioBroker?I don't know what "restricted login" means.
What happen if you use web browser to open this url?My issue is login on the web ui of the device
hj.Download("http://192.168.5.188/relay/0?turn=on")
hj.GetRequest.SetHeader("Authorization", "Basic " & SU.EncodeBase64("admin:password".GetBytes("UTF8")))
No, never seen. I'm not interested in "integrating Shelly devices in MQTT" , but simply make the well-known http-requests to switch the relays.Does it have something call ioBroker?
That works. Relay is switched. Confirmed with Google ChromeWhat happen if you use web browser to open this url?
http://admin:password@192.168.5.188/relay/0?turn=on
Yes i tried that, but it fails. Error message is: ResponseError. Reason: UnauthorizedDo you also try the second code in post #25?
B4X:hj.Download("http://192.168.5.188/relay/0?turn=on") hj.GetRequest.SetHeader("Authorization", "Basic " & SU.EncodeBase64("admin:password".GetBytes("UTF8")))
I think, the reason for this mess is plain and simple: B4A does not support sha256 digest
Is that mean Google Chrome supports and knows that automatically it should use SHA-256 instead of Base64Encode ?That works. Relay is switched. Confirmed with Google Chrome
hj.Download("http://admin:password@192.168.5.188/relay/0?turn=on")
Obviously yes; btw: other browsers work too, not only Google Chrome. Maybe these browser try different encodings consecutively.Is that mean Google Chrome supports and knows that automatically it should use SHA-256 instead of Base64Encode
No. The browser does not use B4A's job.Download.What I understand is,
Opening this URL in browser is same as using job.Download
No, they know this by the response header they receive when they first try to access an URL the "normal" way and then receive a status code of 401. Included in the headers returned from the server, there should be a WWW-Authenticate entry. This entry consists of various fields (see https://datatracker.ietf.org/doc/html/rfc7616#section-3.3). One of those fields may be the "algorithm" field. This field tells the requestor (be it a browser, or some library) what algorithm to use. If the field does not exist, the default is MD5. For SHA-256, it's, well, SHA-256. The information returned by the server is used to create an authorization response header and re-request the same URL.automatically it should use SHA-256 instead of Base64Encode
I'm pretty sure RFC 7235 supersedes RFC 2616. I don't see anything really superseding RFC 7616.RFC7616 (which, of course, has been superseded by RFC 7235)
Yes. The OkHttp.jar file is the same for both platforms.I am wondering is this the case in B4J too?
@drgottjr i tried your mod today, but i'm sorry, it doesn't work. Server returns still 401.have op unzip the attached to his additional libraries folders.
then, in his project in the additional libraries tab, have him
uncheck "okhttp" and check "okhttp2". if the project is already
open when he unzips the archive, he will need to refresh the
additional libraries tab (right click).
Dim hr As OkHttpRequest
hr.InitializeGet("http://192.168.5.188/relay/0?turn=on")
hc.ExecuteCredentials(hr,66,"admin","password")
the username is always "admin" (unchangeable). So, yes, this name is certainly stored.the last thing i'll have to say in this matter has to do with whether or not the device has your username (at least) stored
okhttp2 and okhttputils2 are activated, but code fails to authenticate.when used with okhttputils2, my mod is called
when i enter okhttpjob instead httpjob, B4A IDE says "unknown type"you're using httpjob, not okhttpjob
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?