Skip to content

Commit fe5601a

Browse files
authored
Java jersey OAuth2 add support for public client #13827 (#13828)
1 parent a04b262 commit fe5601a

12 files changed

Lines changed: 174 additions & 0 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,22 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
524524
throw new RuntimeException("No OAuth2 authentication configured!");
525525
}
526526

527+
/**
528+
* Helper method to set the credentials of a public client for the first OAuth2 authentication.
529+
*
530+
* @param clientId the client ID
531+
* @return a {@link org.openapitools.client.ApiClient} object.
532+
*/
533+
public ApiClient setOauthCredentialsForPublicClient(String clientId) {
534+
for (Authentication auth : authentications.values()) {
535+
if (auth instanceof OAuth) {
536+
((OAuth) auth).setCredentialsForPublicClient(clientId, isDebugging());
537+
return this;
538+
}
539+
}
540+
throw new RuntimeException("No OAuth2 authentication configured!");
541+
}
542+
527543
/**
528544
* Helper method to set the password flow for the first OAuth2 authentication.
529545
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ public class OAuth implements Authentication {
158158
return this;
159159
}
160160

161+
public OAuth setCredentialsForPublicClient(String clientId, Boolean debug) {
162+
if (Boolean.TRUE.equals(debug)) {
163+
service = new ServiceBuilder(clientId)
164+
.apiSecretIsEmptyStringUnsafe().debug()
165+
.build(authApi);
166+
} else {
167+
service = new ServiceBuilder(clientId)
168+
.apiSecretIsEmptyStringUnsafe()
169+
.build(authApi);
170+
}
171+
return this;
172+
}
173+
161174
public OAuth usePasswordFlow(String username, String password) {
162175
this.flow = OAuthFlow.PASSWORD;
163176
this.username = username;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,22 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
524524
throw new RuntimeException("No OAuth2 authentication configured!");
525525
}
526526

527+
/**
528+
* Helper method to set the credentials of a public client for the first OAuth2 authentication.
529+
*
530+
* @param clientId the client ID
531+
* @return a {@link org.openapitools.client.ApiClient} object.
532+
*/
533+
public ApiClient setOauthCredentialsForPublicClient(String clientId) {
534+
for (Authentication auth : authentications.values()) {
535+
if (auth instanceof OAuth) {
536+
((OAuth) auth).setCredentialsForPublicClient(clientId, isDebugging());
537+
return this;
538+
}
539+
}
540+
throw new RuntimeException("No OAuth2 authentication configured!");
541+
}
542+
527543
/**
528544
* Helper method to set the password flow for the first OAuth2 authentication.
529545
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ public class OAuth implements Authentication {
158158
return this;
159159
}
160160

161+
public OAuth setCredentialsForPublicClient(String clientId, Boolean debug) {
162+
if (Boolean.TRUE.equals(debug)) {
163+
service = new ServiceBuilder(clientId)
164+
.apiSecretIsEmptyStringUnsafe().debug()
165+
.build(authApi);
166+
} else {
167+
service = new ServiceBuilder(clientId)
168+
.apiSecretIsEmptyStringUnsafe()
169+
.build(authApi);
170+
}
171+
return this;
172+
}
173+
161174
public OAuth usePasswordFlow(String username, String password) {
162175
this.flow = OAuthFlow.PASSWORD;
163176
this.username = username;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,22 @@ public ApiClient setOauthCredentials(String clientId, String clientSecret) {
446446
throw new RuntimeException("No OAuth2 authentication configured!");
447447
}
448448

449+
/**
450+
* Helper method to set the credentials of a public client for the first OAuth2 authentication.
451+
*
452+
* @param clientId the client ID
453+
* @return a {@link org.openapitools.client.ApiClient} object.
454+
*/
455+
public ApiClient setOauthCredentialsForPublicClient(String clientId) {
456+
for (Authentication auth : authentications.values()) {
457+
if (auth instanceof OAuth) {
458+
((OAuth) auth).setCredentialsForPublicClient(clientId, isDebugging());
459+
return this;
460+
}
461+
}
462+
throw new RuntimeException("No OAuth2 authentication configured!");
463+
}
464+
449465
/**
450466
* Helper method to set the password flow for the first OAuth2 authentication.
451467
*

samples/client/petstore/java/jersey2-java8-localdatetime/src/main/java/org/openapitools/client/auth/OAuth.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,19 @@ public OAuth setCredentials(String clientId, String clientSecret, Boolean debug)
169169
return this;
170170
}
171171

172+
public OAuth setCredentialsForPublicClient(String clientId, Boolean debug) {
173+
if (Boolean.TRUE.equals(debug)) {
174+
service = new ServiceBuilder(clientId)
175+
.apiSecretIsEmptyStringUnsafe().debug()
176+
.build(authApi);
177+
} else {
178+
service = new ServiceBuilder(clientId)
179+
.apiSecretIsEmptyStringUnsafe()
180+
.build(authApi);
181+
}
182+
return this;
183+
}
184+
172185
public OAuth usePasswordFlow(String username, String password) {
173186
this.flow = OAuthFlow.PASSWORD;
174187
this.username = username;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,22 @@ public ApiClient setOauthCredentials(String clientId, String clientSecret) {
446446
throw new RuntimeException("No OAuth2 authentication configured!");
447447
}
448448

449+
/**
450+
* Helper method to set the credentials of a public client for the first OAuth2 authentication.
451+
*
452+
* @param clientId the client ID
453+
* @return a {@link org.openapitools.client.ApiClient} object.
454+
*/
455+
public ApiClient setOauthCredentialsForPublicClient(String clientId) {
456+
for (Authentication auth : authentications.values()) {
457+
if (auth instanceof OAuth) {
458+
((OAuth) auth).setCredentialsForPublicClient(clientId, isDebugging());
459+
return this;
460+
}
461+
}
462+
throw new RuntimeException("No OAuth2 authentication configured!");
463+
}
464+
449465
/**
450466
* Helper method to set the password flow for the first OAuth2 authentication.
451467
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,19 @@ public OAuth setCredentials(String clientId, String clientSecret, Boolean debug)
169169
return this;
170170
}
171171

172+
public OAuth setCredentialsForPublicClient(String clientId, Boolean debug) {
173+
if (Boolean.TRUE.equals(debug)) {
174+
service = new ServiceBuilder(clientId)
175+
.apiSecretIsEmptyStringUnsafe().debug()
176+
.build(authApi);
177+
} else {
178+
service = new ServiceBuilder(clientId)
179+
.apiSecretIsEmptyStringUnsafe()
180+
.build(authApi);
181+
}
182+
return this;
183+
}
184+
172185
public OAuth usePasswordFlow(String username, String password) {
173186
this.flow = OAuthFlow.PASSWORD;
174187
this.username = username;

samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,22 @@ public ApiClient setOauthCredentials(String clientId, String clientSecret) {
530530
throw new RuntimeException("No OAuth2 authentication configured!");
531531
}
532532

533+
/**
534+
* Helper method to set the credentials of a public client for the first OAuth2 authentication.
535+
*
536+
* @param clientId the client ID
537+
* @return a {@link org.openapitools.client.ApiClient} object.
538+
*/
539+
public ApiClient setOauthCredentialsForPublicClient(String clientId) {
540+
for (Authentication auth : authentications.values()) {
541+
if (auth instanceof OAuth) {
542+
((OAuth) auth).setCredentialsForPublicClient(clientId, isDebugging());
543+
return this;
544+
}
545+
}
546+
throw new RuntimeException("No OAuth2 authentication configured!");
547+
}
548+
533549
/**
534550
* Helper method to set the password flow for the first OAuth2 authentication.
535551
*

samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/auth/OAuth.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,19 @@ public OAuth setCredentials(String clientId, String clientSecret, Boolean debug)
169169
return this;
170170
}
171171

172+
public OAuth setCredentialsForPublicClient(String clientId, Boolean debug) {
173+
if (Boolean.TRUE.equals(debug)) {
174+
service = new ServiceBuilder(clientId)
175+
.apiSecretIsEmptyStringUnsafe().debug()
176+
.build(authApi);
177+
} else {
178+
service = new ServiceBuilder(clientId)
179+
.apiSecretIsEmptyStringUnsafe()
180+
.build(authApi);
181+
}
182+
return this;
183+
}
184+
172185
public OAuth usePasswordFlow(String username, String password) {
173186
this.flow = OAuthFlow.PASSWORD;
174187
this.username = username;

0 commit comments

Comments
 (0)