retrofit icon indicating copy to clipboard operation
retrofit copied to clipboard

Manage digest authentication with Retrofit

Open grena opened this issue 13 years ago • 10 comments

Hi everyone,

Do somebody have any idea on how to implement a digest authentication solution with Retrofit ? When the server answers with a 401 Unauthorized responses, the Callback object calls its failure() method..

Is it possible with retrofit ? Thanks in advance.

grena avatar Jul 23 '13 16:07 grena

looking for it too

Freyskeyd avatar Jul 24 '13 11:07 Freyskeyd

When the server returns with the 401 status, the RetrofitError response object is null, and the networkError flag is set to true, making it impossible for the app to check error.getResponse().getStatus() I believe the error lies on the http client. When using OkClient I get this error: java.io.IOException: No authentication challenges found When using ApacheClient, everything works fine.

Anndressa avatar Jul 25 '13 19:07 Anndressa

In my case the challenge is empty but i get a challenge. I use a php server to serve the REST api. This server return 401 when user is unauthenticated.

But when i add challengeResponse to the request it's like the request is send a second time. And the request is rejected a second time.

Freyskeyd avatar Jul 25 '13 20:07 Freyskeyd

Wild guess that implementing a custom client could do the trick.

class ChallengeResponseClient implements Client{
  Client delegate;

  public Response execute(Request request) throws IOException {
    Response response = delegate.execute(request);
    if (response.getStatus() == 401){
      // authenticate, etc.

codefromthecrypt avatar Aug 05 '13 21:08 codefromthecrypt

Had the same issue as @Anndressa. After I included Okhttp library, the correct response status, 401, is returning now instead of IOException. Retrofit uses OkClient if there is Okhttp library.

    <dependency>
        <groupId>com.squareup.okhttp</groupId>
        <artifactId>okhttp</artifactId>
        <version>1.2.1</version>
    </dependency>

Edit: had to remove Okhttp because it crashes the http client used by facebook sdk. https://github.com/square/okhttp/issues/184

The other possible solution is to include WWW-Authenticate header field with 401 response if you can modify the server. See discussion here. http://stackoverflow.com/questions/1357372/ioexception-received-authentication-challenge-is-null-apache-harmony-android

thihamin avatar Sep 11 '13 02:09 thihamin

I'm having a really weird issue related to this. I'm testing incorrect logins, where the server returns a 401 with an error message. Running on a Nexus 5 (api level 19) I get a retrofiterror and I have access to the server's response and error message, so it's fine. Testing on an api level 16 device, I get a retrofiterror where isNetworkError() is true, caused by "java.io.IOException: No authentication challenges found". Besides the exception message, there's nothing distinguishing this from an actual network error.

I searched around a lot, but there isn't a lot of documentation about this issue. I tried adding WWW-Authenticate: none to the server's 401 response, as suggested by various posts, but that doesn't seem to work. I'll have to add an ugly fix to this for the moment, but I'd like to understand why there's a difference, and how I can properly fix this. I'd appreciate any help :D

dslounge avatar Aug 06 '14 18:08 dslounge

We will not be implementing this at the Retrofit level. Tracked at https://github.com/square/okhttp/issues/205.

JakeWharton avatar Oct 04 '14 08:10 JakeWharton

@dslounge I'm experiencing the exact same problem. Did you ever find a solution to this problem?

Best regards Christian

petroski77 avatar Oct 26 '14 15:10 petroski77

It's been a while, so I don't remember if this was exactly the solution, but I added OkHttp to my project and it was fixed. Took me forever to figure that out, and there wasn't much documentation on this issue. Seems like the problem was caused by the default Android HTTP library doing things to a certain spec.

dslounge avatar Oct 28 '14 16:10 dslounge

Retrofit error: 401 Unauthorized. Previously it was working fine for me but now i am geting this error.

Prashant6001 avatar Jun 11 '15 06:06 Prashant6001