Skip to content

Commit 9abcf01

Browse files
committed
Add support for custom tls server names.
1 parent 2c7efda commit 9abcf01

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,29 @@ public class ApiClient {
433433
return this;
434434
}
435435

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

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

0 commit comments

Comments
 (0)