Skip to content

Commit 63dd15b

Browse files
authored
Merge pull request #20 from stanwood/feature/FEM-1900-add-authenticator
Feature/fem 1900 add authenticator
2 parents 5075c48 + 6867a7d commit 63dd15b

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,15 @@ issue you won't get a 401 propagated to `Callback.onSuccess()` as in case of suc
101101
after receiving a 401 for the initial request your callback will get the response code of the following
102102
originally intended request).
103103

104-
Additionally/alternatively you can always subclass the `Authenticator` class and override
104+
If you're having problems retrieving a token in the `AuthenticationProvider` (e.g. due to an invalid
105+
refresh token) always try to resolve those issues there as well if possible. Throwing an
106+
`AuthenticationException` should just be a last resort.
107+
108+
Alternatively you can also subclass the `Authenticator` class and override
105109
`onAuthenticationFailed()` if you want to trigger special handling for failed authentication from
106-
which we couldn't recover with our default handling.
110+
which we couldn't recover with our default handling. This usually tends to be a bit harder to get
111+
right as it expects you to modify the failed request directly without any means of intercepting the
112+
response. Thus handling token retrieval issues should preferably handled in the `AuthenticationProvider`
113+
as explained above.
107114

108115
We're looking forward to streamlining this as soon as the okhttp bug has been resolved.

network/src/main/java/io/stanwood/framework/network/auth/Authenticator.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public Request authenticate(@NonNull Route route, @NonNull Response response) {
4444
// Authentication failed. Try to re-authenticate with fresh token
4545
String token;
4646
try {
47+
/*
48+
do not force refresh the token as we might already have gotten a new
49+
one due to another request having triggered a 401 and re-authenticating
50+
before us getting here
51+
*/
4752
token = authenticationProvider.getToken(false);
4853
} catch (AuthenticationException e) {
4954
/*
@@ -57,8 +62,7 @@ public Request authenticate(@NonNull Route route, @NonNull Response response) {
5762
if (oldToken.equals(token)) {
5863
/*
5964
if the token we receive from the AuthenticationProvider hasn't changed in
60-
the meantime (e.g. due to another request having triggered a 401 and
61-
re-authenticating before us getting here), try to get a new one
65+
the meantime, try to get a new one
6266
*/
6367
try {
6468
token = authenticationProvider.getToken(true);
@@ -71,7 +75,7 @@ the meantime (e.g. due to another request having triggered a 401 and
7175
return retryOrFail(route, response);
7276
}
7377

74-
if (token == null) {
78+
if (token == null || oldToken.equals(token)) {
7579
return retryOrFail(route, response);
7680
}
7781

0 commit comments

Comments
 (0)