use of org.teiid.OAuthCredential in project teiid by teiid.
the class SalesforceConnectionImpl method login.
private void login(SalesForceManagedConnectionFactory mcf) throws ResourceException {
config = new SalesforceConnectorConfig();
String username = mcf.getUsername();
String password = mcf.getPassword();
// if security-domain is specified and caller identity is used; then use
// credentials from subject
boolean useCXFTransport = mcf.getConfigFile() != null;
Subject subject = ConnectionContext.getSubject();
if (subject != null) {
OAuthCredential oauthCredential = ConnectionContext.getSecurityCredential(subject, OAuthCredential.class);
if (oauthCredential != null) {
config.setCredential(OAuthCredential.class.getName(), oauthCredential);
useCXFTransport = true;
} else {
username = ConnectionContext.getUserName(subject, mcf, username);
password = ConnectionContext.getPassword(subject, mcf, username, password);
}
}
config.setCxfConfigFile(mcf.getConfigFile());
if (useCXFTransport) {
config.setTransport(SalesforceCXFTransport.class);
}
config.setCompression(true);
config.setTraceMessage(false);
// set the catch all properties
String props = mcf.getConfigProperties();
if (props != null) {
Properties p = new Properties();
try {
p.load(new StringReader(props));
} catch (IOException e) {
throw new ResourceException(e);
}
PropertiesUtils.setBeanProperties(config, p, null);
}
config.setUsername(username);
config.setPassword(password);
config.setAuthEndpoint(mcf.getURL());
// set proxy if needed
if (mcf.getProxyURL() != null) {
try {
URL proxyURL = new URL(mcf.getProxyURL());
config.setProxy(proxyURL.getHost(), proxyURL.getPort());
config.setProxyUsername(mcf.getProxyUsername());
config.setProxyPassword(mcf.getProxyPassword());
} catch (MalformedURLException e) {
throw new ResourceException(e);
}
}
if (mcf.getConnectTimeout() != null) {
config.setConnectionTimeout((int) Math.min(Integer.MAX_VALUE, mcf.getConnectTimeout()));
}
if (mcf.getRequestTimeout() != null) {
config.setReadTimeout((int) Math.min(Integer.MAX_VALUE, mcf.getRequestTimeout()));
}
try {
partnerConnection = new TeiidPartnerConnection(config);
String endpoint = config.getServiceEndpoint();
// The endpoint for the Bulk API service is the same as for the normal
// SOAP uri until the /Soap/ part. From here it's '/async/versionNumber'
// $NON-NLS-1$
int index = endpoint.indexOf("Soap/u/");
int endIndex = endpoint.indexOf('/', index + 7);
apiVersion = endpoint.substring(index + 7, endIndex);
// $NON-NLS-1$ //$NON-NLS-2$
String bulkEndpoint = endpoint.substring(0, endpoint.indexOf("Soap/")) + "async/" + apiVersion;
config.setRestEndpoint(bulkEndpoint);
// This value identifies Teiid as a SF certified solution.
// It was provided by SF and should not be changed.
// $NON-NLS-1$
partnerConnection.setCallOptions("RedHat/MetaMatrix/", null);
bulkConnection = new BulkConnection(config);
// Test the connection.
partnerConnection.getUserInfo();
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
restEndpoint = endpoint.substring(0, endpoint.indexOf("Soap/")) + "data/" + "v30.0";
} catch (AsyncApiException e) {
throw new ResourceException(e);
} catch (ConnectionException e) {
throw new ResourceException(e);
}
// $NON-NLS-1$
LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Login was successful for username", username);
}
use of org.teiid.OAuthCredential in project teiid by teiid.
the class TeiidPartnerConnection method login.
public com.sforce.soap.partner.LoginResult login(java.lang.String username, java.lang.String password) throws com.sforce.ws.ConnectionException {
SalesforceConnectorConfig config = (SalesforceConnectorConfig) getConfig();
if (config.getCredential(OAuthCredential.class.getName()) == null) {
return super.login(username, password);
}
// for details see
// https://developer.salesforce.com/blogs/developer-relations/2011/03/oauth-and-the-soap-api.html
OAuthCredential credential = (OAuthCredential) config.getCredential(OAuthCredential.class.getName());
String id = credential.getAuthrorizationProperty("id");
if (id == null) {
throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection");
}
String accessToken = credential.getAuthorizationHeader(null, "POST");
com.sforce.soap.partner.LoginResult loginResult = null;
WebClient client = WebClient.create(id);
client.header(AUTHORIZATION, accessToken);
String response = client.get(String.class);
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new ByteArrayInputStream(response.getBytes()));
doc.getDocumentElement().normalize();
Element urls = (Element) doc.getDocumentElement().getElementsByTagName("urls").item(0);
loginResult = new com.sforce.soap.partner.LoginResult();
// remove "Bearer " prefix.
loginResult.setSessionId(accessToken.substring(7));
String endpoint = config.getAuthEndpoint();
// $NON-NLS-1$
int index = endpoint.indexOf("Soap/u/");
String apiVersion = endpoint.substring(index + 7);
String partnerURL = urls.getElementsByTagName("partner").item(0).getTextContent();
partnerURL = partnerURL.replace("{version}", apiVersion);
loginResult.setServerUrl(partnerURL);
} catch (IOException e) {
throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; " + "Failed to get user information", e);
} catch (ParserConfigurationException e) {
throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; " + "Failed to get user information", e);
} catch (IllegalStateException e) {
throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; " + "Failed to get user information", e);
} catch (SAXException e) {
throw new com.sforce.ws.ConnectionException("Failed to get OAuth based connection; " + "Failed to get user information", e);
}
return loginResult;
}
use of org.teiid.OAuthCredential in project teiid by teiid.
the class WSConnectionImpl method setDispatchProperties.
private <T> void setDispatchProperties(Dispatch<T> dispatch, String binding) {
if (this.mcf.getAsSecurityType() == WSManagedConnectionFactory.SecurityType.HTTPBasic || this.mcf.getAsSecurityType() == WSManagedConnectionFactory.SecurityType.Digest) {
String userName = this.mcf.getAuthUserName();
String password = this.mcf.getAuthPassword();
// if security-domain is specified and caller identity is used; then use
// credentials from subject
Subject subject = ConnectionContext.getSubject();
if (subject != null) {
userName = ConnectionContext.getUserName(subject, this.mcf, userName);
password = ConnectionContext.getPassword(subject, this.mcf, userName, password);
}
AuthorizationPolicy policy = new AuthorizationPolicy();
policy.setUserName(userName);
policy.setPassword(password);
if (this.mcf.getAsSecurityType() == WSManagedConnectionFactory.SecurityType.Digest) {
policy.setAuthorizationType("Digest");
} else {
policy.setAuthorizationType("Basic");
}
dispatch.getRequestContext().put(AuthorizationPolicy.class.getName(), policy);
} else if (this.mcf.getAsSecurityType() == WSManagedConnectionFactory.SecurityType.Kerberos) {
boolean credentialFound = false;
Subject subject = ConnectionContext.getSubject();
if (subject != null) {
GSSCredential credential = ConnectionContext.getSecurityCredential(subject, GSSCredential.class);
if (credential != null) {
dispatch.getRequestContext().put(GSSCredential.class.getName(), credential);
credentialFound = true;
}
}
if (!credentialFound) {
// $NON-NLS-1$
throw new WebServiceException(WSManagedConnectionFactory.UTIL.getString("no_gss_credential"));
}
} else if (this.mcf.getAsSecurityType() == WSManagedConnectionFactory.SecurityType.OAuth) {
boolean credentialFound = false;
Subject subject = ConnectionContext.getSubject();
if (subject != null) {
OAuthCredential credential = ConnectionContext.getSecurityCredential(subject, OAuthCredential.class);
if (credential != null) {
dispatch.getRequestContext().put(OAuthCredential.class.getName(), credential);
credentialFound = true;
}
}
if (!credentialFound) {
// $NON-NLS-1$
throw new WebServiceException(WSManagedConnectionFactory.UTIL.getString("no_oauth_credential"));
}
}
if (this.mcf.getRequestTimeout() != null) {
dispatch.getRequestContext().put(RECEIVE_TIMEOUT, this.mcf.getRequestTimeout());
}
if (this.mcf.getConnectTimeout() != null) {
dispatch.getRequestContext().put(CONNECTION_TIMEOUT, this.mcf.getConnectTimeout());
}
if (HTTPBinding.HTTP_BINDING.equals(binding)) {
Map<String, List<String>> httpHeaders = (Map<String, List<String>>) dispatch.getRequestContext().get(MessageContext.HTTP_REQUEST_HEADERS);
if (httpHeaders == null) {
httpHeaders = new HashMap<String, List<String>>();
}
// $NON-NLS-1$ //$NON-NLS-2$
httpHeaders.put("Content-Type", Collections.singletonList("text/xml; charset=utf-8"));
// $NON-NLS-1$ //$NON-NLS-2$
httpHeaders.put("User-Agent", Collections.singletonList("Teiid Server"));
dispatch.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, httpHeaders);
}
}
Aggregations