Skip to content

Commit 8a4246c

Browse files
Add support for custom tls server names. (#22372)
* Add support for custom tls server names. * Update samples * Fix missing declaration.
1 parent a1b962d commit 8a4246c

File tree

16 files changed

+576
-32
lines changed
  • modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson
  • samples/client
    • echo_api/java
      • okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client
      • okhttp-gson/src/main/java/org/openapitools/client
    • others/java
      • okhttp-gson-oneOf-array/src/main/java/org/openapitools/client
      • okhttp-gson-oneOf/src/main/java/org/openapitools/client
      • okhttp-gson-streaming/src/main/java/org/openapitools/client
    • petstore/java
      • okhttp-gson-3.1-duplicated-operationid/src/main/java/org/openapitools/client
      • okhttp-gson-3.1/src/main/java/org/openapitools/client
      • okhttp-gson-awsv4signature/src/main/java/org/openapitools/client
      • okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client
      • okhttp-gson-group-parameter/src/main/java/org/openapitools/client
      • okhttp-gson-nullable-required/src/main/java/org/openapitools/client
      • okhttp-gson-parcelableModel/src/main/java/org/openapitools/client
      • okhttp-gson-swagger1/src/main/java/org/openapitools/client
      • okhttp-gson-swagger2/src/main/java/org/openapitools/client
      • okhttp-gson/src/main/java/org/openapitools/client

16 files changed

+576
-32
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ public class ApiClient {
120120
protected InputStream sslCaCert;
121121
protected boolean verifyingSsl;
122122
protected KeyManager[] keyManagers;
123-
123+
protected String tlsServerName;
124+
124125
protected OkHttpClient httpClient;
125126
protected JSON json;
126127

@@ -433,6 +434,29 @@ public class ApiClient {
433434
return this;
434435
}
435436

437+
/**
438+
* Get TLS server name for SNI (Server Name Indication).
439+
*
440+
* @return The TLS server name
441+
*/
442+
public String getTlsServerName() {
443+
return tlsServerName;
444+
}
445+
446+
/**
447+
* Set TLS server name for SNI (Server Name Indication).
448+
* This is used to verify the server certificate against a specific hostname
449+
* instead of the hostname in the URL.
450+
*
451+
* @param tlsServerName The TLS server name to use for certificate verification
452+
* @return ApiClient
453+
*/
454+
public ApiClient setTlsServerName(String tlsServerName) {
455+
this.tlsServerName = tlsServerName;
456+
applySslSettings();
457+
return this;
458+
}
459+
436460
/**
437461
* <p>Getter for the field <code>dateFormat</code>.</p>
438462
*
@@ -1820,7 +1844,17 @@ public class ApiClient {
18201844
trustManagerFactory.init(caKeyStore);
18211845
}
18221846
trustManagers = trustManagerFactory.getTrustManagers();
1823-
hostnameVerifier = OkHostnameVerifier.INSTANCE;
1847+
if (tlsServerName != null && !tlsServerName.isEmpty()) {
1848+
hostnameVerifier = new HostnameVerifier() {
1849+
@Override
1850+
public boolean verify(String hostname, SSLSession session) {
1851+
// Verify the certificate against tlsServerName instead of the actual hostname
1852+
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
1853+
}
1854+
};
1855+
} else {
1856+
hostnameVerifier = OkHostnameVerifier.INSTANCE;
1857+
}
18241858
}
18251859

18261860
SSLContext sslContext = SSLContext.getInstance("TLS");

samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ public class ApiClient {
9191
protected InputStream sslCaCert;
9292
protected boolean verifyingSsl;
9393
protected KeyManager[] keyManagers;
94-
94+
protected String tlsServerName;
95+
9596
protected OkHttpClient httpClient;
9697
protected JSON json;
9798

@@ -376,6 +377,29 @@ public ApiClient setKeyManagers(KeyManager[] managers) {
376377
return this;
377378
}
378379

380+
/**
381+
* Get TLS server name for SNI (Server Name Indication).
382+
*
383+
* @return The TLS server name
384+
*/
385+
public String getTlsServerName() {
386+
return tlsServerName;
387+
}
388+
389+
/**
390+
* Set TLS server name for SNI (Server Name Indication).
391+
* This is used to verify the server certificate against a specific hostname
392+
* instead of the hostname in the URL.
393+
*
394+
* @param tlsServerName The TLS server name to use for certificate verification
395+
* @return ApiClient
396+
*/
397+
public ApiClient setTlsServerName(String tlsServerName) {
398+
this.tlsServerName = tlsServerName;
399+
applySslSettings();
400+
return this;
401+
}
402+
379403
/**
380404
* <p>Getter for the field <code>dateFormat</code>.</p>
381405
*
@@ -1635,7 +1659,17 @@ public boolean verify(String hostname, SSLSession session) {
16351659
trustManagerFactory.init(caKeyStore);
16361660
}
16371661
trustManagers = trustManagerFactory.getTrustManagers();
1638-
hostnameVerifier = OkHostnameVerifier.INSTANCE;
1662+
if (tlsServerName != null && !tlsServerName.isEmpty()) {
1663+
hostnameVerifier = new HostnameVerifier() {
1664+
@Override
1665+
public boolean verify(String hostname, SSLSession session) {
1666+
// Verify the certificate against tlsServerName instead of the actual hostname
1667+
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
1668+
}
1669+
};
1670+
} else {
1671+
hostnameVerifier = OkHostnameVerifier.INSTANCE;
1672+
}
16391673
}
16401674

16411675
SSLContext sslContext = SSLContext.getInstance("TLS");

samples/client/echo_api/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public class ApiClient {
8686
protected InputStream sslCaCert;
8787
protected boolean verifyingSsl;
8888
protected KeyManager[] keyManagers;
89-
89+
protected String tlsServerName;
90+
9091
protected OkHttpClient httpClient;
9192
protected JSON json;
9293

@@ -304,6 +305,29 @@ public ApiClient setKeyManagers(KeyManager[] managers) {
304305
return this;
305306
}
306307

308+
/**
309+
* Get TLS server name for SNI (Server Name Indication).
310+
*
311+
* @return The TLS server name
312+
*/
313+
public String getTlsServerName() {
314+
return tlsServerName;
315+
}
316+
317+
/**
318+
* Set TLS server name for SNI (Server Name Indication).
319+
* This is used to verify the server certificate against a specific hostname
320+
* instead of the hostname in the URL.
321+
*
322+
* @param tlsServerName The TLS server name to use for certificate verification
323+
* @return ApiClient
324+
*/
325+
public ApiClient setTlsServerName(String tlsServerName) {
326+
this.tlsServerName = tlsServerName;
327+
applySslSettings();
328+
return this;
329+
}
330+
307331
/**
308332
* <p>Getter for the field <code>dateFormat</code>.</p>
309333
*
@@ -1565,7 +1589,17 @@ public boolean verify(String hostname, SSLSession session) {
15651589
trustManagerFactory.init(caKeyStore);
15661590
}
15671591
trustManagers = trustManagerFactory.getTrustManagers();
1568-
hostnameVerifier = OkHostnameVerifier.INSTANCE;
1592+
if (tlsServerName != null && !tlsServerName.isEmpty()) {
1593+
hostnameVerifier = new HostnameVerifier() {
1594+
@Override
1595+
public boolean verify(String hostname, SSLSession session) {
1596+
// Verify the certificate against tlsServerName instead of the actual hostname
1597+
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
1598+
}
1599+
};
1600+
} else {
1601+
hostnameVerifier = OkHostnameVerifier.INSTANCE;
1602+
}
15691603
}
15701604

15711605
SSLContext sslContext = SSLContext.getInstance("TLS");

samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public class ApiClient {
8686
protected InputStream sslCaCert;
8787
protected boolean verifyingSsl;
8888
protected KeyManager[] keyManagers;
89-
89+
protected String tlsServerName;
90+
9091
protected OkHttpClient httpClient;
9192
protected JSON json;
9293

@@ -300,6 +301,29 @@ public ApiClient setKeyManagers(KeyManager[] managers) {
300301
return this;
301302
}
302303

304+
/**
305+
* Get TLS server name for SNI (Server Name Indication).
306+
*
307+
* @return The TLS server name
308+
*/
309+
public String getTlsServerName() {
310+
return tlsServerName;
311+
}
312+
313+
/**
314+
* Set TLS server name for SNI (Server Name Indication).
315+
* This is used to verify the server certificate against a specific hostname
316+
* instead of the hostname in the URL.
317+
*
318+
* @param tlsServerName The TLS server name to use for certificate verification
319+
* @return ApiClient
320+
*/
321+
public ApiClient setTlsServerName(String tlsServerName) {
322+
this.tlsServerName = tlsServerName;
323+
applySslSettings();
324+
return this;
325+
}
326+
303327
/**
304328
* <p>Getter for the field <code>dateFormat</code>.</p>
305329
*
@@ -1539,7 +1563,17 @@ public boolean verify(String hostname, SSLSession session) {
15391563
trustManagerFactory.init(caKeyStore);
15401564
}
15411565
trustManagers = trustManagerFactory.getTrustManagers();
1542-
hostnameVerifier = OkHostnameVerifier.INSTANCE;
1566+
if (tlsServerName != null && !tlsServerName.isEmpty()) {
1567+
hostnameVerifier = new HostnameVerifier() {
1568+
@Override
1569+
public boolean verify(String hostname, SSLSession session) {
1570+
// Verify the certificate against tlsServerName instead of the actual hostname
1571+
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
1572+
}
1573+
};
1574+
} else {
1575+
hostnameVerifier = OkHostnameVerifier.INSTANCE;
1576+
}
15431577
}
15441578

15451579
SSLContext sslContext = SSLContext.getInstance("TLS");

samples/client/others/java/okhttp-gson-oneOf/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public class ApiClient {
8686
protected InputStream sslCaCert;
8787
protected boolean verifyingSsl;
8888
protected KeyManager[] keyManagers;
89-
89+
protected String tlsServerName;
90+
9091
protected OkHttpClient httpClient;
9192
protected JSON json;
9293

@@ -300,6 +301,29 @@ public ApiClient setKeyManagers(KeyManager[] managers) {
300301
return this;
301302
}
302303

304+
/**
305+
* Get TLS server name for SNI (Server Name Indication).
306+
*
307+
* @return The TLS server name
308+
*/
309+
public String getTlsServerName() {
310+
return tlsServerName;
311+
}
312+
313+
/**
314+
* Set TLS server name for SNI (Server Name Indication).
315+
* This is used to verify the server certificate against a specific hostname
316+
* instead of the hostname in the URL.
317+
*
318+
* @param tlsServerName The TLS server name to use for certificate verification
319+
* @return ApiClient
320+
*/
321+
public ApiClient setTlsServerName(String tlsServerName) {
322+
this.tlsServerName = tlsServerName;
323+
applySslSettings();
324+
return this;
325+
}
326+
303327
/**
304328
* <p>Getter for the field <code>dateFormat</code>.</p>
305329
*
@@ -1539,7 +1563,17 @@ public boolean verify(String hostname, SSLSession session) {
15391563
trustManagerFactory.init(caKeyStore);
15401564
}
15411565
trustManagers = trustManagerFactory.getTrustManagers();
1542-
hostnameVerifier = OkHostnameVerifier.INSTANCE;
1566+
if (tlsServerName != null && !tlsServerName.isEmpty()) {
1567+
hostnameVerifier = new HostnameVerifier() {
1568+
@Override
1569+
public boolean verify(String hostname, SSLSession session) {
1570+
// Verify the certificate against tlsServerName instead of the actual hostname
1571+
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
1572+
}
1573+
};
1574+
} else {
1575+
hostnameVerifier = OkHostnameVerifier.INSTANCE;
1576+
}
15431577
}
15441578

15451579
SSLContext sslContext = SSLContext.getInstance("TLS");

samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public class ApiClient {
8686
protected InputStream sslCaCert;
8787
protected boolean verifyingSsl;
8888
protected KeyManager[] keyManagers;
89-
89+
protected String tlsServerName;
90+
9091
protected OkHttpClient httpClient;
9192
protected JSON json;
9293

@@ -300,6 +301,29 @@ public ApiClient setKeyManagers(KeyManager[] managers) {
300301
return this;
301302
}
302303

304+
/**
305+
* Get TLS server name for SNI (Server Name Indication).
306+
*
307+
* @return The TLS server name
308+
*/
309+
public String getTlsServerName() {
310+
return tlsServerName;
311+
}
312+
313+
/**
314+
* Set TLS server name for SNI (Server Name Indication).
315+
* This is used to verify the server certificate against a specific hostname
316+
* instead of the hostname in the URL.
317+
*
318+
* @param tlsServerName The TLS server name to use for certificate verification
319+
* @return ApiClient
320+
*/
321+
public ApiClient setTlsServerName(String tlsServerName) {
322+
this.tlsServerName = tlsServerName;
323+
applySslSettings();
324+
return this;
325+
}
326+
303327
/**
304328
* <p>Getter for the field <code>dateFormat</code>.</p>
305329
*
@@ -1562,7 +1586,17 @@ public boolean verify(String hostname, SSLSession session) {
15621586
trustManagerFactory.init(caKeyStore);
15631587
}
15641588
trustManagers = trustManagerFactory.getTrustManagers();
1565-
hostnameVerifier = OkHostnameVerifier.INSTANCE;
1589+
if (tlsServerName != null && !tlsServerName.isEmpty()) {
1590+
hostnameVerifier = new HostnameVerifier() {
1591+
@Override
1592+
public boolean verify(String hostname, SSLSession session) {
1593+
// Verify the certificate against tlsServerName instead of the actual hostname
1594+
return OkHostnameVerifier.INSTANCE.verify(tlsServerName, session);
1595+
}
1596+
};
1597+
} else {
1598+
hostnameVerifier = OkHostnameVerifier.INSTANCE;
1599+
}
15661600
}
15671601

15681602
SSLContext sslContext = SSLContext.getInstance("TLS");

0 commit comments

Comments
 (0)