B4J Question HttpJob and Digest Auth

Star-Dust

Expert
Licensed User
Longtime User
I tried to override a server with Digest authentication.
(In this way)
B4X:
Dim job As HttpJob
job.Initialize("testhtaccess",Me)
job.Username = "Star"
job.Password = "Dust"

job.Download("http:/192.168.1.101/")
The Digest server requires a counter to increase for each connection, this to distinguish the various requests.
(DigestHash = MD5(HA1:nonce:nonceCount:cnonce:qop:HA2))

But in HttpJob, this counter does not increment, and therefore accesses correctly at the first access, in the following some servers refuse the connection ..

How to fix it?

 

Star-Dust

Expert
Licensed User
Longtime User
It seems that the problem is related to OkHttpClient, when using Digest authentication it leaves the NC value at 1 and does not increment it,
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are correct. It is hard coded to 1.

There seems to be a newer implementation (https://github.com/rburgst/okhttp-digest), however an initial test didn't work properly.

The code I've tested it with:
B4X:
Private Sub ConfigureHttpClient (Username As String, Password As String)
    Dim jo As JavaObject = HttpUtils2Service.hc
    Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
    Dim authCache  As JavaObject
    authCache .InitializeNewInstance("java.util.concurrent.ConcurrentHashMap", Null)
    Dim credentials As JavaObject
    credentials.InitializeNewInstance("com.burgstaller.okhttp.digest.Credentials", Array(Username, Password))
    Dim BasicAuth, DigestAuth As JavaObject
    BasicAuth.InitializeNewInstance("com.burgstaller.okhttp.basic.BasicAuthenticator", Array(credentials))
    DigestAuth.InitializeNewInstance("com.burgstaller.okhttp.digest.DigestAuthenticator", Array(credentials))
    Dim auth As JavaObject
    auth.InitializeNewInstance("com.burgstaller.okhttp.DispatchingAuthenticator$Builder", Null)
    auth.RunMethod("with", Array("digest", DigestAuth))
    auth.RunMethod("with", Array("basic", BasicAuth))
    Dim CachingAuth As JavaObject
    CachingAuth.InitializeNewInstance("com.burgstaller.okhttp.CachingAuthenticatorDecorator", Array(auth.RunMethod("build", Null), authCache))
    Dim interceptor As JavaObject
    interceptor.InitializeNewInstance("com.burgstaller.okhttp.AuthenticationCacheInterceptor", Array(authCache))
    builder.RunMethod("authenticator", Array(CachingAuth))
    builder.RunMethod("addInterceptor", Array(interceptor))
    jo.SetField("client", builder.RunMethod("build", Null))
End Sub

You need to add a conditional symbol: HU2_PUBLIC

Keeping it open for now.
 

Attachments

  • okhttp-digest-2.5.jar
    38.5 KB · Views: 191
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
You are correct. It is hard coded to 1.

There seems to be a newer implementation (https://github.com/rburgst/okhttp-digest), however an initial test didn't work properly.

The code I've tested it with:
B4X:
Private Sub ConfigureHttpClient (Username As String, Password As String)
    Dim jo As JavaObject = HttpUtils2Service.hc
    Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
    Dim authCache  As JavaObject
    authCache .InitializeNewInstance("java.util.concurrent.ConcurrentHashMap", Null)
    Dim credentials As JavaObject
    credentials.InitializeNewInstance("com.burgstaller.okhttp.digest.Credentials", Array(Username, Password))
    Dim BasicAuth, DigestAuth As JavaObject
    BasicAuth.InitializeNewInstance("com.burgstaller.okhttp.basic.BasicAuthenticator", Array(credentials))
    DigestAuth.InitializeNewInstance("com.burgstaller.okhttp.digest.DigestAuthenticator", Array(credentials))
    Dim auth As JavaObject
    auth.InitializeNewInstance("com.burgstaller.okhttp.DispatchingAuthenticator$Builder", Null)
    auth.RunMethod("with", Array("digest", DigestAuth))
    auth.RunMethod("with", Array("basic", BasicAuth))
    Dim CachingAuth As JavaObject
    CachingAuth.InitializeNewInstance("com.burgstaller.okhttp.CachingAuthenticatorDecorator", Array(auth.RunMethod("build", Null), authCache))
    Dim interceptor As JavaObject
    interceptor.InitializeNewInstance("com.burgstaller.okhttp.AuthenticationCacheInterceptor", Array(authCache))
    builder.RunMethod("authenticator", Array(CachingAuth))
    builder.RunMethod("addInterceptor", Array(interceptor))
    jo.SetField("client", builder.RunMethod("build", Null))
End Sub

You need to add a conditional symbol: HU2_PUBLIC

Keeping it open for now.
Thanks, thanks and thanks. ?????????
 
Upvote 0
Top