mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 11:23:58 +00:00
parent
1b117e7cec
commit
69d956f16b
@ -1,6 +1,7 @@
|
||||
package {{invokerPackage}}.auth;
|
||||
|
||||
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
|
||||
import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
@ -85,42 +86,55 @@ public class OAuth implements Interceptor {
|
||||
updateAccessToken(null);
|
||||
}
|
||||
|
||||
// Build the request
|
||||
Builder rb = request.newBuilder();
|
||||
|
||||
String requestAccessToken = new String(getAccessToken());
|
||||
try {
|
||||
oAuthRequest = new OAuthBearerClientRequest(request.url().toString())
|
||||
.setAccessToken(requestAccessToken)
|
||||
.buildHeaderMessage();
|
||||
} catch (OAuthSystemException e) {
|
||||
throw new IOException(e);
|
||||
if (getAccessToken() != null) {
|
||||
// Build the request
|
||||
Builder rb = request.newBuilder();
|
||||
|
||||
String requestAccessToken = new String(getAccessToken());
|
||||
try {
|
||||
oAuthRequest = new OAuthBearerClientRequest(request.urlString())
|
||||
.setAccessToken(requestAccessToken)
|
||||
.buildHeaderMessage();
|
||||
} catch (OAuthSystemException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
|
||||
for ( Map.Entry<String, String> header : oAuthRequest.getHeaders().entrySet() ) {
|
||||
rb.addHeader(header.getKey(), header.getValue());
|
||||
}
|
||||
rb.url( oAuthRequest.getLocationUri());
|
||||
|
||||
//Execute the request
|
||||
Response response = chain.proceed(rb.build());
|
||||
|
||||
// 401 most likely indicates that access token has expired.
|
||||
// Time to refresh and resend the request
|
||||
if ( response != null && (response.code() == HTTP_UNAUTHORIZED | response.code() == HTTP_FORBIDDEN) ) {
|
||||
if (updateAccessToken(requestAccessToken)) {
|
||||
return intercept( chain );
|
||||
}
|
||||
}
|
||||
return response;
|
||||
} else {
|
||||
return chain.proceed(chain.request());
|
||||
}
|
||||
|
||||
for ( Map.Entry<String, String> header : oAuthRequest.getHeaders().entrySet() ) {
|
||||
rb.addHeader(header.getKey(), header.getValue());
|
||||
}
|
||||
rb.url( oAuthRequest.getLocationUri());
|
||||
|
||||
//Execute the request
|
||||
Response response = chain.proceed(rb.build());
|
||||
|
||||
// 401 most likely indicates that access token has expired.
|
||||
// Time to refresh and resend the request
|
||||
if ( response.code() == HTTP_UNAUTHORIZED ) {
|
||||
updateAccessToken(requestAccessToken);
|
||||
return intercept( chain );
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public synchronized void updateAccessToken(String requestAccessToken) throws IOException {
|
||||
/*
|
||||
* Returns true if the access token has been updated
|
||||
*/
|
||||
public synchronized boolean updateAccessToken(String requestAccessToken) throws IOException {
|
||||
if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) {
|
||||
try {
|
||||
OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage());
|
||||
setAccessToken(accessTokenResponse.getAccessToken());
|
||||
if (accessTokenListener != null) {
|
||||
accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken());
|
||||
if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) {
|
||||
setAccessToken(accessTokenResponse.getAccessToken());
|
||||
if (accessTokenListener != null) {
|
||||
accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken());
|
||||
}
|
||||
return getAccessToken().equals(requestAccessToken);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (OAuthSystemException e) {
|
||||
throw new IOException(e);
|
||||
@ -128,6 +142,7 @@ public class OAuth implements Interceptor {
|
||||
throw new IOException(e);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
||||
|
Loading…
Reference in New Issue
Block a user