forked from loafle/openapi-generator-original
[Java][jersey2] Add debugging to OAuth (#6757)
* add debugging to oauth * use fine instead
This commit is contained in:
parent
8b9c070e5d
commit
919b3b6bef
@ -461,7 +461,7 @@ public class ApiClient {
|
|||||||
public ApiClient setOauthCredentials(String clientId, String clientSecret) {
|
public ApiClient setOauthCredentials(String clientId, String clientSecret) {
|
||||||
for (Authentication auth : authentications.values()) {
|
for (Authentication auth : authentications.values()) {
|
||||||
if (auth instanceof OAuth) {
|
if (auth instanceof OAuth) {
|
||||||
((OAuth) auth).setCredentials(clientId, clientSecret);
|
((OAuth) auth).setCredentials(clientId, clientSecret, isDebugging());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,151 +22,161 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
{{>generatedAnnotation}}
|
{{>generatedAnnotation}}
|
||||||
public class OAuth implements Authentication {
|
public class OAuth implements Authentication {
|
||||||
private static final Logger log = Logger.getLogger(OAuth.class.getName());
|
private static final Logger log = Logger.getLogger(OAuth.class.getName());
|
||||||
|
|
||||||
private String tokenUrl;
|
private String tokenUrl;
|
||||||
private String absoluteTokenUrl;
|
private String absoluteTokenUrl;
|
||||||
private OAuthFlow flow = OAuthFlow.application;
|
private OAuthFlow flow = OAuthFlow.application;
|
||||||
private OAuth20Service service;
|
private OAuth20Service service;
|
||||||
private DefaultApi20 authApi;
|
private DefaultApi20 authApi;
|
||||||
private String scope;
|
private String scope;
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private String code;
|
private String code;
|
||||||
private volatile OAuth2AccessToken accessToken;
|
private volatile OAuth2AccessToken accessToken;
|
||||||
|
|
||||||
public OAuth(String basePath, String tokenUrl) {
|
public OAuth(String basePath, String tokenUrl) {
|
||||||
this.tokenUrl = tokenUrl;
|
this.tokenUrl = tokenUrl;
|
||||||
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
|
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
|
||||||
authApi = new DefaultApi20() {
|
authApi = new DefaultApi20() {
|
||||||
@Override
|
@Override
|
||||||
public String getAccessTokenEndpoint() {
|
public String getAccessTokenEndpoint() {
|
||||||
return absoluteTokenUrl;
|
return absoluteTokenUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getAuthorizationBaseUrl() {
|
protected String getAuthorizationBaseUrl() {
|
||||||
throw new UnsupportedOperationException("Shouldn't get there !");
|
throw new UnsupportedOperationException("Shouldn't get there !");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
private static String createAbsoluteTokenUrl(String basePath, String tokenUrl) {
|
|
||||||
if (!URI.create(tokenUrl).isAbsolute()) {
|
|
||||||
try {
|
|
||||||
return UriBuilder.fromPath(basePath).path(tokenUrl).build().toURL().toString();
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
log.log(Level.SEVERE, "Couldn't create absolute token URL", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return tokenUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
private static String createAbsoluteTokenUrl(String basePath, String tokenUrl) {
|
||||||
public void applyToParams(
|
if (!URI.create(tokenUrl).isAbsolute()) {
|
||||||
List<Pair> queryParams,
|
try {
|
||||||
Map<String, String> headerParams,
|
return UriBuilder.fromPath(basePath).path(tokenUrl).build().toURL().toString();
|
||||||
Map<String, String> cookieParams,
|
} catch (MalformedURLException e) {
|
||||||
String payload,
|
log.log(Level.SEVERE, "Couldn't create absolute token URL", e);
|
||||||
String method,
|
}
|
||||||
URI uri)
|
}
|
||||||
throws ApiException {
|
return tokenUrl;
|
||||||
|
|
||||||
if (accessToken == null) {
|
|
||||||
obtainAccessToken(null);
|
|
||||||
}
|
}
|
||||||
if (accessToken != null) {
|
|
||||||
headerParams.put("Authorization", "Bearer " + accessToken.getAccessToken());
|
@Override
|
||||||
|
public void applyToParams(
|
||||||
|
List<Pair> queryParams,
|
||||||
|
Map<String, String> headerParams,
|
||||||
|
Map<String, String> cookieParams,
|
||||||
|
String payload,
|
||||||
|
String method,
|
||||||
|
URI uri)
|
||||||
|
throws ApiException {
|
||||||
|
|
||||||
|
if (accessToken == null) {
|
||||||
|
obtainAccessToken(null);
|
||||||
|
}
|
||||||
|
if (accessToken != null) {
|
||||||
|
headerParams.put("Authorization", "Bearer " + accessToken.getAccessToken());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth2AccessToken renewAccessToken() throws ApiException {
|
public OAuth2AccessToken renewAccessToken() throws ApiException {
|
||||||
String refreshToken = null;
|
String refreshToken = null;
|
||||||
if (accessToken != null) {
|
if (accessToken != null) {
|
||||||
refreshToken = accessToken.getRefreshToken();
|
refreshToken = accessToken.getRefreshToken();
|
||||||
accessToken = null;
|
accessToken = null;
|
||||||
|
}
|
||||||
|
return obtainAccessToken(refreshToken);
|
||||||
}
|
}
|
||||||
return obtainAccessToken(refreshToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized OAuth2AccessToken obtainAccessToken(String refreshToken) throws ApiException {
|
public synchronized OAuth2AccessToken obtainAccessToken(String refreshToken) throws ApiException {
|
||||||
if (service == null) {
|
if (service == null) {
|
||||||
return null;
|
log.log(Level.FINE, "service is null in obtainAccessToken.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (refreshToken != null) {
|
||||||
|
return service.refreshAccessToken(refreshToken);
|
||||||
|
}
|
||||||
|
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
|
||||||
|
log.log(Level.FINE, "Refreshing the access token using the refresh token failed", e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
switch (flow) {
|
||||||
|
case password:
|
||||||
|
if (username != null && password != null) {
|
||||||
|
accessToken = service.getAccessTokenPasswordGrant(username, password, scope);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case accessCode:
|
||||||
|
if (code != null) {
|
||||||
|
accessToken = service.getAccessToken(code);
|
||||||
|
code = null;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case application:
|
||||||
|
accessToken = service.getAccessTokenClientCredentialsGrant(scope);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
log.log(Level.SEVERE, "Invalid flow in obtainAccessToken: " + flow);
|
||||||
|
}
|
||||||
|
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
|
||||||
|
throw new ApiException(e);
|
||||||
|
}
|
||||||
|
return accessToken;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
if (refreshToken != null) {
|
public OAuth2AccessToken getAccessToken() {
|
||||||
return service.refreshAccessToken(refreshToken);
|
return accessToken;
|
||||||
}
|
|
||||||
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
|
|
||||||
log.log(Level.FINE, "Refreshing the access token using the refresh token failed", e);
|
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
switch (flow) {
|
public OAuth setAccessToken(OAuth2AccessToken accessToken) {
|
||||||
case password:
|
this.accessToken = accessToken;
|
||||||
if (username != null && password != null) {
|
return this;
|
||||||
accessToken = service.getAccessTokenPasswordGrant(username, password, scope);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case accessCode:
|
|
||||||
if (code != null) {
|
|
||||||
accessToken = service.getAccessToken(code);
|
|
||||||
code = null;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case application:
|
|
||||||
accessToken = service.getAccessTokenClientCredentialsGrant(scope);
|
|
||||||
}
|
|
||||||
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
|
|
||||||
throw new ApiException(e);
|
|
||||||
}
|
}
|
||||||
return accessToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth2AccessToken getAccessToken() {
|
public OAuth setAccessToken(String accessToken) {
|
||||||
return accessToken;
|
this.accessToken = new OAuth2AccessToken(accessToken);
|
||||||
}
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public OAuth setAccessToken(OAuth2AccessToken accessToken) {
|
public OAuth setScope(String scope) {
|
||||||
this.accessToken = accessToken;
|
this.scope = scope;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OAuth setAccessToken(String accessToken) {
|
public OAuth setCredentials(String clientId, String clientSecret, Boolean debug) {
|
||||||
this.accessToken = new OAuth2AccessToken(accessToken);
|
if (Boolean.TRUE.equals(debug)) {
|
||||||
return this;
|
service = new ServiceBuilder(clientId)
|
||||||
}
|
.apiSecret(clientSecret).debug()
|
||||||
|
.build(authApi);
|
||||||
|
} else {
|
||||||
|
service = new ServiceBuilder(clientId)
|
||||||
|
.apiSecret(clientSecret)
|
||||||
|
.build(authApi);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public OAuth setScope(String scope) {
|
public OAuth usePasswordFlow(String username, String password) {
|
||||||
this.scope = scope;
|
this.flow = OAuthFlow.password;
|
||||||
return this;
|
this.username = username;
|
||||||
}
|
this.password = password;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public OAuth setCredentials(String clientId, String clientSecret) {
|
public OAuth useAuthorizationCodeFlow(String code) {
|
||||||
service = new ServiceBuilder(clientId)
|
this.flow = OAuthFlow.accessCode;
|
||||||
.apiSecret(clientSecret)
|
this.code = code;
|
||||||
.build(authApi);
|
return this;
|
||||||
return this;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth usePasswordFlow(String username, String password) {
|
public OAuth setFlow(OAuthFlow flow) {
|
||||||
this.flow = OAuthFlow.password;
|
this.flow = flow;
|
||||||
this.username = username;
|
return this;
|
||||||
this.password = password;
|
}
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth useAuthorizationCodeFlow(String code) {
|
public void setBasePath(String basePath) {
|
||||||
this.flow = OAuthFlow.accessCode;
|
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
|
||||||
this.code = code;
|
}
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth setFlow(OAuthFlow flow) {
|
|
||||||
this.flow = flow;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBasePath(String basePath) {
|
|
||||||
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -379,7 +379,7 @@ public class ApiClient {
|
|||||||
public ApiClient setOauthCredentials(String clientId, String clientSecret) {
|
public ApiClient setOauthCredentials(String clientId, String clientSecret) {
|
||||||
for (Authentication auth : authentications.values()) {
|
for (Authentication auth : authentications.values()) {
|
||||||
if (auth instanceof OAuth) {
|
if (auth instanceof OAuth) {
|
||||||
((OAuth) auth).setCredentials(clientId, clientSecret);
|
((OAuth) auth).setCredentials(clientId, clientSecret, isDebugging());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,151 +33,161 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||||
public class OAuth implements Authentication {
|
public class OAuth implements Authentication {
|
||||||
private static final Logger log = Logger.getLogger(OAuth.class.getName());
|
private static final Logger log = Logger.getLogger(OAuth.class.getName());
|
||||||
|
|
||||||
private String tokenUrl;
|
private String tokenUrl;
|
||||||
private String absoluteTokenUrl;
|
private String absoluteTokenUrl;
|
||||||
private OAuthFlow flow = OAuthFlow.application;
|
private OAuthFlow flow = OAuthFlow.application;
|
||||||
private OAuth20Service service;
|
private OAuth20Service service;
|
||||||
private DefaultApi20 authApi;
|
private DefaultApi20 authApi;
|
||||||
private String scope;
|
private String scope;
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private String code;
|
private String code;
|
||||||
private volatile OAuth2AccessToken accessToken;
|
private volatile OAuth2AccessToken accessToken;
|
||||||
|
|
||||||
public OAuth(String basePath, String tokenUrl) {
|
public OAuth(String basePath, String tokenUrl) {
|
||||||
this.tokenUrl = tokenUrl;
|
this.tokenUrl = tokenUrl;
|
||||||
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
|
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
|
||||||
authApi = new DefaultApi20() {
|
authApi = new DefaultApi20() {
|
||||||
@Override
|
@Override
|
||||||
public String getAccessTokenEndpoint() {
|
public String getAccessTokenEndpoint() {
|
||||||
return absoluteTokenUrl;
|
return absoluteTokenUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getAuthorizationBaseUrl() {
|
protected String getAuthorizationBaseUrl() {
|
||||||
throw new UnsupportedOperationException("Shouldn't get there !");
|
throw new UnsupportedOperationException("Shouldn't get there !");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
private static String createAbsoluteTokenUrl(String basePath, String tokenUrl) {
|
|
||||||
if (!URI.create(tokenUrl).isAbsolute()) {
|
|
||||||
try {
|
|
||||||
return UriBuilder.fromPath(basePath).path(tokenUrl).build().toURL().toString();
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
log.log(Level.SEVERE, "Couldn't create absolute token URL", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return tokenUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
private static String createAbsoluteTokenUrl(String basePath, String tokenUrl) {
|
||||||
public void applyToParams(
|
if (!URI.create(tokenUrl).isAbsolute()) {
|
||||||
List<Pair> queryParams,
|
try {
|
||||||
Map<String, String> headerParams,
|
return UriBuilder.fromPath(basePath).path(tokenUrl).build().toURL().toString();
|
||||||
Map<String, String> cookieParams,
|
} catch (MalformedURLException e) {
|
||||||
String payload,
|
log.log(Level.SEVERE, "Couldn't create absolute token URL", e);
|
||||||
String method,
|
}
|
||||||
URI uri)
|
}
|
||||||
throws ApiException {
|
return tokenUrl;
|
||||||
|
|
||||||
if (accessToken == null) {
|
|
||||||
obtainAccessToken(null);
|
|
||||||
}
|
}
|
||||||
if (accessToken != null) {
|
|
||||||
headerParams.put("Authorization", "Bearer " + accessToken.getAccessToken());
|
@Override
|
||||||
|
public void applyToParams(
|
||||||
|
List<Pair> queryParams,
|
||||||
|
Map<String, String> headerParams,
|
||||||
|
Map<String, String> cookieParams,
|
||||||
|
String payload,
|
||||||
|
String method,
|
||||||
|
URI uri)
|
||||||
|
throws ApiException {
|
||||||
|
|
||||||
|
if (accessToken == null) {
|
||||||
|
obtainAccessToken(null);
|
||||||
|
}
|
||||||
|
if (accessToken != null) {
|
||||||
|
headerParams.put("Authorization", "Bearer " + accessToken.getAccessToken());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth2AccessToken renewAccessToken() throws ApiException {
|
public OAuth2AccessToken renewAccessToken() throws ApiException {
|
||||||
String refreshToken = null;
|
String refreshToken = null;
|
||||||
if (accessToken != null) {
|
if (accessToken != null) {
|
||||||
refreshToken = accessToken.getRefreshToken();
|
refreshToken = accessToken.getRefreshToken();
|
||||||
accessToken = null;
|
accessToken = null;
|
||||||
|
}
|
||||||
|
return obtainAccessToken(refreshToken);
|
||||||
}
|
}
|
||||||
return obtainAccessToken(refreshToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized OAuth2AccessToken obtainAccessToken(String refreshToken) throws ApiException {
|
public synchronized OAuth2AccessToken obtainAccessToken(String refreshToken) throws ApiException {
|
||||||
if (service == null) {
|
if (service == null) {
|
||||||
return null;
|
log.log(Level.FINE, "service is null in obtainAccessToken.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (refreshToken != null) {
|
||||||
|
return service.refreshAccessToken(refreshToken);
|
||||||
|
}
|
||||||
|
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
|
||||||
|
log.log(Level.FINE, "Refreshing the access token using the refresh token failed", e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
switch (flow) {
|
||||||
|
case password:
|
||||||
|
if (username != null && password != null) {
|
||||||
|
accessToken = service.getAccessTokenPasswordGrant(username, password, scope);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case accessCode:
|
||||||
|
if (code != null) {
|
||||||
|
accessToken = service.getAccessToken(code);
|
||||||
|
code = null;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case application:
|
||||||
|
accessToken = service.getAccessTokenClientCredentialsGrant(scope);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
log.log(Level.SEVERE, "Invalid flow in obtainAccessToken: " + flow);
|
||||||
|
}
|
||||||
|
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
|
||||||
|
throw new ApiException(e);
|
||||||
|
}
|
||||||
|
return accessToken;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
if (refreshToken != null) {
|
public OAuth2AccessToken getAccessToken() {
|
||||||
return service.refreshAccessToken(refreshToken);
|
return accessToken;
|
||||||
}
|
|
||||||
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
|
|
||||||
log.log(Level.FINE, "Refreshing the access token using the refresh token failed", e);
|
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
switch (flow) {
|
public OAuth setAccessToken(OAuth2AccessToken accessToken) {
|
||||||
case password:
|
this.accessToken = accessToken;
|
||||||
if (username != null && password != null) {
|
return this;
|
||||||
accessToken = service.getAccessTokenPasswordGrant(username, password, scope);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case accessCode:
|
|
||||||
if (code != null) {
|
|
||||||
accessToken = service.getAccessToken(code);
|
|
||||||
code = null;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case application:
|
|
||||||
accessToken = service.getAccessTokenClientCredentialsGrant(scope);
|
|
||||||
}
|
|
||||||
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
|
|
||||||
throw new ApiException(e);
|
|
||||||
}
|
}
|
||||||
return accessToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth2AccessToken getAccessToken() {
|
public OAuth setAccessToken(String accessToken) {
|
||||||
return accessToken;
|
this.accessToken = new OAuth2AccessToken(accessToken);
|
||||||
}
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public OAuth setAccessToken(OAuth2AccessToken accessToken) {
|
public OAuth setScope(String scope) {
|
||||||
this.accessToken = accessToken;
|
this.scope = scope;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OAuth setAccessToken(String accessToken) {
|
public OAuth setCredentials(String clientId, String clientSecret, Boolean debug) {
|
||||||
this.accessToken = new OAuth2AccessToken(accessToken);
|
if (Boolean.TRUE.equals(debug)) {
|
||||||
return this;
|
service = new ServiceBuilder(clientId)
|
||||||
}
|
.apiSecret(clientSecret).debug()
|
||||||
|
.build(authApi);
|
||||||
|
} else {
|
||||||
|
service = new ServiceBuilder(clientId)
|
||||||
|
.apiSecret(clientSecret)
|
||||||
|
.build(authApi);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public OAuth setScope(String scope) {
|
public OAuth usePasswordFlow(String username, String password) {
|
||||||
this.scope = scope;
|
this.flow = OAuthFlow.password;
|
||||||
return this;
|
this.username = username;
|
||||||
}
|
this.password = password;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public OAuth setCredentials(String clientId, String clientSecret) {
|
public OAuth useAuthorizationCodeFlow(String code) {
|
||||||
service = new ServiceBuilder(clientId)
|
this.flow = OAuthFlow.accessCode;
|
||||||
.apiSecret(clientSecret)
|
this.code = code;
|
||||||
.build(authApi);
|
return this;
|
||||||
return this;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth usePasswordFlow(String username, String password) {
|
public OAuth setFlow(OAuthFlow flow) {
|
||||||
this.flow = OAuthFlow.password;
|
this.flow = flow;
|
||||||
this.username = username;
|
return this;
|
||||||
this.password = password;
|
}
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth useAuthorizationCodeFlow(String code) {
|
public void setBasePath(String basePath) {
|
||||||
this.flow = OAuthFlow.accessCode;
|
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
|
||||||
this.code = code;
|
}
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth setFlow(OAuthFlow flow) {
|
|
||||||
this.flow = flow;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBasePath(String basePath) {
|
|
||||||
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -458,7 +458,7 @@ public class ApiClient {
|
|||||||
public ApiClient setOauthCredentials(String clientId, String clientSecret) {
|
public ApiClient setOauthCredentials(String clientId, String clientSecret) {
|
||||||
for (Authentication auth : authentications.values()) {
|
for (Authentication auth : authentications.values()) {
|
||||||
if (auth instanceof OAuth) {
|
if (auth instanceof OAuth) {
|
||||||
((OAuth) auth).setCredentials(clientId, clientSecret);
|
((OAuth) auth).setCredentials(clientId, clientSecret, isDebugging());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,151 +33,161 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||||
public class OAuth implements Authentication {
|
public class OAuth implements Authentication {
|
||||||
private static final Logger log = Logger.getLogger(OAuth.class.getName());
|
private static final Logger log = Logger.getLogger(OAuth.class.getName());
|
||||||
|
|
||||||
private String tokenUrl;
|
private String tokenUrl;
|
||||||
private String absoluteTokenUrl;
|
private String absoluteTokenUrl;
|
||||||
private OAuthFlow flow = OAuthFlow.application;
|
private OAuthFlow flow = OAuthFlow.application;
|
||||||
private OAuth20Service service;
|
private OAuth20Service service;
|
||||||
private DefaultApi20 authApi;
|
private DefaultApi20 authApi;
|
||||||
private String scope;
|
private String scope;
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private String code;
|
private String code;
|
||||||
private volatile OAuth2AccessToken accessToken;
|
private volatile OAuth2AccessToken accessToken;
|
||||||
|
|
||||||
public OAuth(String basePath, String tokenUrl) {
|
public OAuth(String basePath, String tokenUrl) {
|
||||||
this.tokenUrl = tokenUrl;
|
this.tokenUrl = tokenUrl;
|
||||||
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
|
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
|
||||||
authApi = new DefaultApi20() {
|
authApi = new DefaultApi20() {
|
||||||
@Override
|
@Override
|
||||||
public String getAccessTokenEndpoint() {
|
public String getAccessTokenEndpoint() {
|
||||||
return absoluteTokenUrl;
|
return absoluteTokenUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getAuthorizationBaseUrl() {
|
protected String getAuthorizationBaseUrl() {
|
||||||
throw new UnsupportedOperationException("Shouldn't get there !");
|
throw new UnsupportedOperationException("Shouldn't get there !");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
private static String createAbsoluteTokenUrl(String basePath, String tokenUrl) {
|
|
||||||
if (!URI.create(tokenUrl).isAbsolute()) {
|
|
||||||
try {
|
|
||||||
return UriBuilder.fromPath(basePath).path(tokenUrl).build().toURL().toString();
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
log.log(Level.SEVERE, "Couldn't create absolute token URL", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return tokenUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
private static String createAbsoluteTokenUrl(String basePath, String tokenUrl) {
|
||||||
public void applyToParams(
|
if (!URI.create(tokenUrl).isAbsolute()) {
|
||||||
List<Pair> queryParams,
|
try {
|
||||||
Map<String, String> headerParams,
|
return UriBuilder.fromPath(basePath).path(tokenUrl).build().toURL().toString();
|
||||||
Map<String, String> cookieParams,
|
} catch (MalformedURLException e) {
|
||||||
String payload,
|
log.log(Level.SEVERE, "Couldn't create absolute token URL", e);
|
||||||
String method,
|
}
|
||||||
URI uri)
|
}
|
||||||
throws ApiException {
|
return tokenUrl;
|
||||||
|
|
||||||
if (accessToken == null) {
|
|
||||||
obtainAccessToken(null);
|
|
||||||
}
|
}
|
||||||
if (accessToken != null) {
|
|
||||||
headerParams.put("Authorization", "Bearer " + accessToken.getAccessToken());
|
@Override
|
||||||
|
public void applyToParams(
|
||||||
|
List<Pair> queryParams,
|
||||||
|
Map<String, String> headerParams,
|
||||||
|
Map<String, String> cookieParams,
|
||||||
|
String payload,
|
||||||
|
String method,
|
||||||
|
URI uri)
|
||||||
|
throws ApiException {
|
||||||
|
|
||||||
|
if (accessToken == null) {
|
||||||
|
obtainAccessToken(null);
|
||||||
|
}
|
||||||
|
if (accessToken != null) {
|
||||||
|
headerParams.put("Authorization", "Bearer " + accessToken.getAccessToken());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth2AccessToken renewAccessToken() throws ApiException {
|
public OAuth2AccessToken renewAccessToken() throws ApiException {
|
||||||
String refreshToken = null;
|
String refreshToken = null;
|
||||||
if (accessToken != null) {
|
if (accessToken != null) {
|
||||||
refreshToken = accessToken.getRefreshToken();
|
refreshToken = accessToken.getRefreshToken();
|
||||||
accessToken = null;
|
accessToken = null;
|
||||||
|
}
|
||||||
|
return obtainAccessToken(refreshToken);
|
||||||
}
|
}
|
||||||
return obtainAccessToken(refreshToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized OAuth2AccessToken obtainAccessToken(String refreshToken) throws ApiException {
|
public synchronized OAuth2AccessToken obtainAccessToken(String refreshToken) throws ApiException {
|
||||||
if (service == null) {
|
if (service == null) {
|
||||||
return null;
|
log.log(Level.FINE, "service is null in obtainAccessToken.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (refreshToken != null) {
|
||||||
|
return service.refreshAccessToken(refreshToken);
|
||||||
|
}
|
||||||
|
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
|
||||||
|
log.log(Level.FINE, "Refreshing the access token using the refresh token failed", e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
switch (flow) {
|
||||||
|
case password:
|
||||||
|
if (username != null && password != null) {
|
||||||
|
accessToken = service.getAccessTokenPasswordGrant(username, password, scope);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case accessCode:
|
||||||
|
if (code != null) {
|
||||||
|
accessToken = service.getAccessToken(code);
|
||||||
|
code = null;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case application:
|
||||||
|
accessToken = service.getAccessTokenClientCredentialsGrant(scope);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
log.log(Level.SEVERE, "Invalid flow in obtainAccessToken: " + flow);
|
||||||
|
}
|
||||||
|
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
|
||||||
|
throw new ApiException(e);
|
||||||
|
}
|
||||||
|
return accessToken;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
if (refreshToken != null) {
|
public OAuth2AccessToken getAccessToken() {
|
||||||
return service.refreshAccessToken(refreshToken);
|
return accessToken;
|
||||||
}
|
|
||||||
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
|
|
||||||
log.log(Level.FINE, "Refreshing the access token using the refresh token failed", e);
|
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
switch (flow) {
|
public OAuth setAccessToken(OAuth2AccessToken accessToken) {
|
||||||
case password:
|
this.accessToken = accessToken;
|
||||||
if (username != null && password != null) {
|
return this;
|
||||||
accessToken = service.getAccessTokenPasswordGrant(username, password, scope);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case accessCode:
|
|
||||||
if (code != null) {
|
|
||||||
accessToken = service.getAccessToken(code);
|
|
||||||
code = null;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case application:
|
|
||||||
accessToken = service.getAccessTokenClientCredentialsGrant(scope);
|
|
||||||
}
|
|
||||||
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
|
|
||||||
throw new ApiException(e);
|
|
||||||
}
|
}
|
||||||
return accessToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth2AccessToken getAccessToken() {
|
public OAuth setAccessToken(String accessToken) {
|
||||||
return accessToken;
|
this.accessToken = new OAuth2AccessToken(accessToken);
|
||||||
}
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public OAuth setAccessToken(OAuth2AccessToken accessToken) {
|
public OAuth setScope(String scope) {
|
||||||
this.accessToken = accessToken;
|
this.scope = scope;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OAuth setAccessToken(String accessToken) {
|
public OAuth setCredentials(String clientId, String clientSecret, Boolean debug) {
|
||||||
this.accessToken = new OAuth2AccessToken(accessToken);
|
if (Boolean.TRUE.equals(debug)) {
|
||||||
return this;
|
service = new ServiceBuilder(clientId)
|
||||||
}
|
.apiSecret(clientSecret).debug()
|
||||||
|
.build(authApi);
|
||||||
|
} else {
|
||||||
|
service = new ServiceBuilder(clientId)
|
||||||
|
.apiSecret(clientSecret)
|
||||||
|
.build(authApi);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public OAuth setScope(String scope) {
|
public OAuth usePasswordFlow(String username, String password) {
|
||||||
this.scope = scope;
|
this.flow = OAuthFlow.password;
|
||||||
return this;
|
this.username = username;
|
||||||
}
|
this.password = password;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public OAuth setCredentials(String clientId, String clientSecret) {
|
public OAuth useAuthorizationCodeFlow(String code) {
|
||||||
service = new ServiceBuilder(clientId)
|
this.flow = OAuthFlow.accessCode;
|
||||||
.apiSecret(clientSecret)
|
this.code = code;
|
||||||
.build(authApi);
|
return this;
|
||||||
return this;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth usePasswordFlow(String username, String password) {
|
public OAuth setFlow(OAuthFlow flow) {
|
||||||
this.flow = OAuthFlow.password;
|
this.flow = flow;
|
||||||
this.username = username;
|
return this;
|
||||||
this.password = password;
|
}
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth useAuthorizationCodeFlow(String code) {
|
public void setBasePath(String basePath) {
|
||||||
this.flow = OAuthFlow.accessCode;
|
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
|
||||||
this.code = code;
|
}
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OAuth setFlow(OAuthFlow flow) {
|
|
||||||
this.flow = flow;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBasePath(String basePath) {
|
|
||||||
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user