mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 19:33:55 +00:00
parent
1b117e7cec
commit
69d956f16b
@ -1,6 +1,7 @@
|
|||||||
package {{invokerPackage}}.auth;
|
package {{invokerPackage}}.auth;
|
||||||
|
|
||||||
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
|
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
|
||||||
|
import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -85,42 +86,55 @@ public class OAuth implements Interceptor {
|
|||||||
updateAccessToken(null);
|
updateAccessToken(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the request
|
if (getAccessToken() != null) {
|
||||||
Builder rb = request.newBuilder();
|
// Build the request
|
||||||
|
Builder rb = request.newBuilder();
|
||||||
String requestAccessToken = new String(getAccessToken());
|
|
||||||
try {
|
String requestAccessToken = new String(getAccessToken());
|
||||||
oAuthRequest = new OAuthBearerClientRequest(request.url().toString())
|
try {
|
||||||
.setAccessToken(requestAccessToken)
|
oAuthRequest = new OAuthBearerClientRequest(request.urlString())
|
||||||
.buildHeaderMessage();
|
.setAccessToken(requestAccessToken)
|
||||||
} catch (OAuthSystemException e) {
|
.buildHeaderMessage();
|
||||||
throw new IOException(e);
|
} 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)) {
|
if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) {
|
||||||
try {
|
try {
|
||||||
OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage());
|
OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(this.tokenRequestBuilder.buildBodyMessage());
|
||||||
setAccessToken(accessTokenResponse.getAccessToken());
|
if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) {
|
||||||
if (accessTokenListener != null) {
|
setAccessToken(accessTokenResponse.getAccessToken());
|
||||||
accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken());
|
if (accessTokenListener != null) {
|
||||||
|
accessTokenListener.notify((BasicOAuthToken) accessTokenResponse.getOAuthToken());
|
||||||
|
}
|
||||||
|
return getAccessToken().equals(requestAccessToken);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
} catch (OAuthSystemException e) {
|
} catch (OAuthSystemException e) {
|
||||||
throw new IOException(e);
|
throw new IOException(e);
|
||||||
@ -128,6 +142,7 @@ public class OAuth implements Interceptor {
|
|||||||
throw new IOException(e);
|
throw new IOException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
public void registerAccessTokenListener(AccessTokenListener accessTokenListener) {
|
||||||
|
Loading…
Reference in New Issue
Block a user