use of com.vmware.vim.binding.vim.ServiceInstance in project flowgate by vmware.
the class AuthVcUser method authenticateUser.
public void authenticateUser(String name, String password) throws URISyntaxException, InvalidLogin, InvalidLocale, SSLException {
Client vmomiClient = null;
SessionManager sessionManager = null;
try {
URI uri = new URI(serviceUrl);
HttpConfiguration httpConfig = new HttpConfigurationImpl();
httpConfig.setThumbprintVerifier(getThumbprintVerifier());
HttpClientConfiguration clientConfig = HttpClientConfiguration.Factory.newInstance();
// set customized SSL protocols
// TlsClientConfiguration tlsClientConfiguration = new TlsClientConfiguration();
httpConfig.setEnabledProtocols(new String[] { "TLSv1.1", "TLSv1.2" });
clientConfig.setHttpConfiguration(httpConfig);
vmomiClient = Client.Factory.createClient(uri, version, clientConfig);
ManagedObjectReference svcRef = new ManagedObjectReference();
svcRef.setType("ServiceInstance");
svcRef.setValue("ServiceInstance");
if (name == null || name.isEmpty()) {
// VC session token auth
vmomiClient.getBinding().setSession(vmomiClient.getBinding().createSession(password));
}
ServiceInstance instance = vmomiClient.createStub(ServiceInstance.class, svcRef);
ServiceInstanceContent instanceContent = instance.retrieveContent();
sessionManager = vmomiClient.createStub(SessionManager.class, instanceContent.getSessionManager());
if (name != null && !name.isEmpty()) {
// username/passowrd auth
sessionManager.login(name, password, sessionManager.getDefaultLocale());
sessionManager.logout();
} else {
// VC session token auth
UserSession session = sessionManager.getCurrentSession();
if (session == null) {
throw new WormholeException("invalid vc session.");
} else {
logger.info(session.getUserName() + " is authenticated");
}
}
} finally {
if (vmomiClient != null) {
vmomiClient.shutdown();
}
}
}
Aggregations