Skip to content

Commit b3c58d7

Browse files
authored
Merge pull request #1562 from marklogic/feature/417-cloud-401
DEVEXP-417 Tweaked token renewal to be based on 401 instead of 403
2 parents 55dcc91 + 285c8c4 commit b3c58d7

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/impl/okhttp/MarkLogicCloudAuthenticationConfigurer.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ public TokenAuthenticationInterceptor(TokenGenerator tokenGenerator) {
157157
@Override
158158
public Response intercept(Chain chain) throws IOException {
159159
Response response = chain.proceed(addTokenToRequest(chain));
160-
if (response.code() == 403) {
161-
logger.info("Received 403; will generate new token if necessary and retry request");
160+
if (response.code() == 401) {
161+
logger.info("Received 401; will generate new token if necessary and retry request");
162162
response.close();
163163
final String currentToken = this.token;
164164
generateNewTokenIfNecessary(currentToken);
@@ -169,7 +169,7 @@ public Response intercept(Chain chain) throws IOException {
169169

170170
/**
171171
* In the case of N threads using the same DatabaseClient - e.g. when using DMSDK - all of them
172-
* may make a request at the same time and get a 403 back. Functionally, it should be fine if all
172+
* may make a request at the same time and get a 401 back. Functionally, it should be fine if all
173173
* make their own requests to renew the token, with the last thread being the one whose token
174174
* value is retained on this class. But to simplify matters, this block is synchronized so only one
175175
* thread can be in here. And if that thread finds that this.token is different from currentToken,
@@ -182,7 +182,7 @@ public Response intercept(Chain chain) throws IOException {
182182
*/
183183
private synchronized void generateNewTokenIfNecessary(String currentToken) {
184184
if (currentToken.equals(this.token)) {
185-
logger.info("Generating new token based on receiving 403");
185+
logger.info("Generating new token based on receiving 401");
186186
this.token = tokenGenerator.generateToken();
187187
} else if (logger.isDebugEnabled()) {
188188
logger.debug("This instance's token has already been updated, presumably by another thread");

marklogic-client-api/src/test/java/com/marklogic/client/impl/okhttp/TokenAuthenticationInterceptorTest.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,30 @@ void beforeEach() {
4040
}
4141

4242
@Test
43-
void receive403() {
44-
enqueueResponseCodes(200, 200, 403, 200);
43+
void receive401() {
44+
enqueueResponseCodes(200, 200, 401, 200);
4545

4646
verifyRequestReturnsResponseCode(200);
4747
verifyRequestReturnsResponseCode(200);
4848
verifyRequestReturnsResponseCode(200,
49-
"If a 403 is received from the server, then the token should be renewed, and then the 200 should be " +
49+
"If a 401 is received from the server, then the token should be renewed, and then the 200 should be " +
5050
"returned to the caller.");
5151

5252
assertEquals(2, fakeTokenGenerator.timesInvoked,
53-
"A token should have been generated for the first request and then again when the 403 was received.");
53+
"A token should have been generated for the first request and then again when the 401 was received.");
5454
}
5555

5656
@Test
57-
void receive401() {
58-
enqueueResponseCodes(200, 200, 401);
57+
void receive403() {
58+
enqueueResponseCodes(200, 200, 403);
5959

6060
verifyRequestReturnsResponseCode(200);
6161
verifyRequestReturnsResponseCode(200);
62-
verifyRequestReturnsResponseCode(401);
62+
verifyRequestReturnsResponseCode(403);
6363

6464
assertEquals(1, fakeTokenGenerator.timesInvoked,
65-
"A token should have been generated for the first request, and the 401 should not have resulted in the " +
66-
"token being renewed; only a 403 should.");
65+
"A token should have been generated for the first request, and the 403 should not have resulted in the " +
66+
"token being renewed; only a 401 should.");
6767
}
6868

6969
@Test
@@ -76,8 +76,8 @@ void multipleThreads() throws Exception {
7676
};
7777

7878
// Mock up 4 responses for each of the 2 threads created below. For each thread, the first call succeeds; the
79-
// second receives a 403 and then succeeds; and the third call succeeds.
80-
enqueueResponseCodes(200, 200, 403, 403, 200, 200, 200, 200);
79+
// second receives a 401 and then succeeds; and the third call succeeds.
80+
enqueueResponseCodes(200, 200, 401, 401, 200, 200, 200, 200);
8181

8282
// Spawn two threads and wait for them to complete.
8383
ExecutorService service = Executors.newFixedThreadPool(2);
@@ -88,7 +88,7 @@ void multipleThreads() throws Exception {
8888

8989
assertEquals(2, fakeTokenGenerator.timesInvoked,
9090
"The fake token generator should have been invoked twice - once when the interceptor was created, and then " +
91-
"only one more time when the two threads received 403's at almost the exact same time. The interceptor " +
91+
"only one more time when the two threads received 401's at almost the exact same time. The interceptor " +
9292
"is expected to synchronize the call for generating a token such that only one thread will generate a " +
9393
"new token. The other token is expected to see that the token has changed and uses the new token " +
9494
"instead of generating a new token itself.");

0 commit comments

Comments
 (0)