Skip to content

Commit 919b3b6

Browse files
authored
[Java][jersey2] Add debugging to OAuth (#6757)
* add debugging to oauth * use fine instead
1 parent 8b9c070 commit 919b3b6

6 files changed

Lines changed: 447 additions & 417 deletions

File tree

modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public class ApiClient {
461461
public ApiClient setOauthCredentials(String clientId, String clientSecret) {
462462
for (Authentication auth : authentications.values()) {
463463
if (auth instanceof OAuth) {
464-
((OAuth) auth).setCredentials(clientId, clientSecret);
464+
((OAuth) auth).setCredentials(clientId, clientSecret, isDebugging());
465465
return this;
466466
}
467467
}

modules/openapi-generator/src/main/resources/Java/libraries/jersey2/auth/OAuth.mustache

Lines changed: 148 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -22,151 +22,161 @@ import java.util.logging.Logger;
2222

2323
{{>generatedAnnotation}}
2424
public class OAuth implements Authentication {
25-
private static final Logger log = Logger.getLogger(OAuth.class.getName());
26-
27-
private String tokenUrl;
28-
private String absoluteTokenUrl;
29-
private OAuthFlow flow = OAuthFlow.application;
30-
private OAuth20Service service;
31-
private DefaultApi20 authApi;
32-
private String scope;
33-
private String username;
34-
private String password;
35-
private String code;
36-
private volatile OAuth2AccessToken accessToken;
37-
38-
public OAuth(String basePath, String tokenUrl) {
39-
this.tokenUrl = tokenUrl;
40-
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
41-
authApi = new DefaultApi20() {
42-
@Override
43-
public String getAccessTokenEndpoint() {
44-
return absoluteTokenUrl;
45-
}
46-
47-
@Override
48-
protected String getAuthorizationBaseUrl() {
49-
throw new UnsupportedOperationException("Shouldn't get there !");
50-
}
51-
};
52-
}
53-
54-
private static String createAbsoluteTokenUrl(String basePath, String tokenUrl) {
55-
if (!URI.create(tokenUrl).isAbsolute()) {
56-
try {
57-
return UriBuilder.fromPath(basePath).path(tokenUrl).build().toURL().toString();
58-
} catch (MalformedURLException e) {
59-
log.log(Level.SEVERE, "Couldn't create absolute token URL", e);
60-
}
25+
private static final Logger log = Logger.getLogger(OAuth.class.getName());
26+
27+
private String tokenUrl;
28+
private String absoluteTokenUrl;
29+
private OAuthFlow flow = OAuthFlow.application;
30+
private OAuth20Service service;
31+
private DefaultApi20 authApi;
32+
private String scope;
33+
private String username;
34+
private String password;
35+
private String code;
36+
private volatile OAuth2AccessToken accessToken;
37+
38+
public OAuth(String basePath, String tokenUrl) {
39+
this.tokenUrl = tokenUrl;
40+
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
41+
authApi = new DefaultApi20() {
42+
@Override
43+
public String getAccessTokenEndpoint() {
44+
return absoluteTokenUrl;
45+
}
46+
47+
@Override
48+
protected String getAuthorizationBaseUrl() {
49+
throw new UnsupportedOperationException("Shouldn't get there !");
50+
}
51+
};
6152
}
62-
return tokenUrl;
63-
}
64-
65-
@Override
66-
public void applyToParams(
67-
List<Pair> queryParams,
68-
Map<String, String> headerParams,
69-
Map<String, String> cookieParams,
70-
String payload,
71-
String method,
72-
URI uri)
73-
throws ApiException {
74-
75-
if (accessToken == null) {
76-
obtainAccessToken(null);
53+
54+
private static String createAbsoluteTokenUrl(String basePath, String tokenUrl) {
55+
if (!URI.create(tokenUrl).isAbsolute()) {
56+
try {
57+
return UriBuilder.fromPath(basePath).path(tokenUrl).build().toURL().toString();
58+
} catch (MalformedURLException e) {
59+
log.log(Level.SEVERE, "Couldn't create absolute token URL", e);
60+
}
61+
}
62+
return tokenUrl;
63+
}
64+
65+
@Override
66+
public void applyToParams(
67+
List<Pair> queryParams,
68+
Map<String, String> headerParams,
69+
Map<String, String> cookieParams,
70+
String payload,
71+
String method,
72+
URI uri)
73+
throws ApiException {
74+
75+
if (accessToken == null) {
76+
obtainAccessToken(null);
77+
}
78+
if (accessToken != null) {
79+
headerParams.put("Authorization", "Bearer " + accessToken.getAccessToken());
80+
}
81+
}
82+
83+
public OAuth2AccessToken renewAccessToken() throws ApiException {
84+
String refreshToken = null;
85+
if (accessToken != null) {
86+
refreshToken = accessToken.getRefreshToken();
87+
accessToken = null;
88+
}
89+
return obtainAccessToken(refreshToken);
90+
}
91+
92+
public synchronized OAuth2AccessToken obtainAccessToken(String refreshToken) throws ApiException {
93+
if (service == null) {
94+
log.log(Level.FINE, "service is null in obtainAccessToken.");
95+
return null;
96+
}
97+
try {
98+
if (refreshToken != null) {
99+
return service.refreshAccessToken(refreshToken);
100+
}
101+
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
102+
log.log(Level.FINE, "Refreshing the access token using the refresh token failed", e);
103+
}
104+
try {
105+
switch (flow) {
106+
case password:
107+
if (username != null && password != null) {
108+
accessToken = service.getAccessTokenPasswordGrant(username, password, scope);
109+
}
110+
break;
111+
case accessCode:
112+
if (code != null) {
113+
accessToken = service.getAccessToken(code);
114+
code = null;
115+
}
116+
break;
117+
case application:
118+
accessToken = service.getAccessTokenClientCredentialsGrant(scope);
119+
break;
120+
default:
121+
log.log(Level.SEVERE, "Invalid flow in obtainAccessToken: " + flow);
122+
}
123+
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
124+
throw new ApiException(e);
125+
}
126+
return accessToken;
127+
}
128+
129+
public OAuth2AccessToken getAccessToken() {
130+
return accessToken;
77131
}
78-
if (accessToken != null) {
79-
headerParams.put("Authorization", "Bearer " + accessToken.getAccessToken());
132+
133+
public OAuth setAccessToken(OAuth2AccessToken accessToken) {
134+
this.accessToken = accessToken;
135+
return this;
80136
}
81-
}
82137

83-
public OAuth2AccessToken renewAccessToken() throws ApiException {
84-
String refreshToken = null;
85-
if (accessToken != null) {
86-
refreshToken = accessToken.getRefreshToken();
87-
accessToken = null;
138+
public OAuth setAccessToken(String accessToken) {
139+
this.accessToken = new OAuth2AccessToken(accessToken);
140+
return this;
88141
}
89-
return obtainAccessToken(refreshToken);
90-
}
91142

92-
public synchronized OAuth2AccessToken obtainAccessToken(String refreshToken) throws ApiException {
93-
if (service == null) {
94-
return null;
143+
public OAuth setScope(String scope) {
144+
this.scope = scope;
145+
return this;
95146
}
96-
try {
97-
if (refreshToken != null) {
98-
return service.refreshAccessToken(refreshToken);
99-
}
100-
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
101-
log.log(Level.FINE, "Refreshing the access token using the refresh token failed", e);
147+
148+
public OAuth setCredentials(String clientId, String clientSecret, Boolean debug) {
149+
if (Boolean.TRUE.equals(debug)) {
150+
service = new ServiceBuilder(clientId)
151+
.apiSecret(clientSecret).debug()
152+
.build(authApi);
153+
} else {
154+
service = new ServiceBuilder(clientId)
155+
.apiSecret(clientSecret)
156+
.build(authApi);
157+
}
158+
return this;
102159
}
103-
try {
104-
switch (flow) {
105-
case password:
106-
if (username != null && password != null) {
107-
accessToken = service.getAccessTokenPasswordGrant(username, password, scope);
108-
}
109-
break;
110-
case accessCode:
111-
if (code != null) {
112-
accessToken = service.getAccessToken(code);
113-
code = null;
114-
}
115-
break;
116-
case application:
117-
accessToken = service.getAccessTokenClientCredentialsGrant(scope);
118-
}
119-
} catch (OAuthException | InterruptedException | ExecutionException | IOException e) {
120-
throw new ApiException(e);
160+
161+
public OAuth usePasswordFlow(String username, String password) {
162+
this.flow = OAuthFlow.password;
163+
this.username = username;
164+
this.password = password;
165+
return this;
166+
}
167+
168+
public OAuth useAuthorizationCodeFlow(String code) {
169+
this.flow = OAuthFlow.accessCode;
170+
this.code = code;
171+
return this;
172+
}
173+
174+
public OAuth setFlow(OAuthFlow flow) {
175+
this.flow = flow;
176+
return this;
177+
}
178+
179+
public void setBasePath(String basePath) {
180+
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
121181
}
122-
return accessToken;
123-
}
124-
125-
public OAuth2AccessToken getAccessToken() {
126-
return accessToken;
127-
}
128-
129-
public OAuth setAccessToken(OAuth2AccessToken accessToken) {
130-
this.accessToken = accessToken;
131-
return this;
132-
}
133-
134-
public OAuth setAccessToken(String accessToken) {
135-
this.accessToken = new OAuth2AccessToken(accessToken);
136-
return this;
137-
}
138-
139-
public OAuth setScope(String scope) {
140-
this.scope = scope;
141-
return this;
142-
}
143-
144-
public OAuth setCredentials(String clientId, String clientSecret) {
145-
service = new ServiceBuilder(clientId)
146-
.apiSecret(clientSecret)
147-
.build(authApi);
148-
return this;
149-
}
150-
151-
public OAuth usePasswordFlow(String username, String password) {
152-
this.flow = OAuthFlow.password;
153-
this.username = username;
154-
this.password = password;
155-
return this;
156-
}
157-
158-
public OAuth useAuthorizationCodeFlow(String code) {
159-
this.flow = OAuthFlow.accessCode;
160-
this.code = code;
161-
return this;
162-
}
163-
164-
public OAuth setFlow(OAuthFlow flow) {
165-
this.flow = flow;
166-
return this;
167-
}
168-
169-
public void setBasePath(String basePath) {
170-
this.absoluteTokenUrl = createAbsoluteTokenUrl(basePath, tokenUrl);
171-
}
172182
}

samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public ApiClient setAccessToken(String accessToken) {
379379
public ApiClient setOauthCredentials(String clientId, String clientSecret) {
380380
for (Authentication auth : authentications.values()) {
381381
if (auth instanceof OAuth) {
382-
((OAuth) auth).setCredentials(clientId, clientSecret);
382+
((OAuth) auth).setCredentials(clientId, clientSecret, isDebugging());
383383
return this;
384384
}
385385
}

0 commit comments

Comments
 (0)