Skip to content

Commit

Permalink
Yet another fix of the OpenShiftOAuthInterceptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Oct 12, 2016
1 parent d760694 commit 97990ab
Showing 1 changed file with 24 additions and 14 deletions.
Expand Up @@ -64,23 +64,33 @@ public Response intercept(Chain chain) throws IOException {
request = builder.build();
Response response = chain.proceed(request);

if ((response.code() == 401 || response.code() == 403)
&& Utils.isNotNullOrEmpty(config.getUsername())
&& Utils.isNotNullOrEmpty(config.getPassword())) {
// Close the previous response to prevent leaked connections.
response.body().close();

synchronized (client) {
token = authorize();
if(token != null) {
oauthToken.set(token);
setAuthHeader(builder, token);
request = builder.build();
return chain.proceed(request); //repeat request with new token
}
//If response is Forbidden or Unauthorized, try to obtain a token via authorize() or via config.
if (response.code() != 401 && response.code() != 403) {
return response;
} else if (Utils.isNotNullOrEmpty(config.getUsername()) && Utils.isNotNullOrEmpty(config.getPassword())) {
synchronized (client) {
token = authorize();
if (token != null) {
oauthToken.set(token);
}
}
} else if (Utils.isNotNullOrEmpty(config.getOauthToken())) {
token = config.getOauthToken();
oauthToken.set(token);
}


//If token was obtain, then retry request using the obtained token.
if (Utils.isNotNullOrEmpty(token)) {
// Close the previous response to prevent leaked connections.
response.body().close();

setAuthHeader(builder, token);
request = builder.build();
return chain.proceed(request); //repeat request with new token
} else {
return response;
}
}

private void setAuthHeader(Request.Builder builder, String token) {
Expand Down

0 comments on commit 97990ab

Please sign in to comment.