Skip to content

Commit

Permalink
TEIID-5013: Google Spreadsheet Data Sources with custom client_id and…
Browse files Browse the repository at this point in the history
… client_secret (allowing the client id/secret to be changed)
  • Loading branch information
shawkins authored and johnathonlee committed Sep 20, 2018
1 parent 42e102e commit 4f495a4
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ <h4 class="western">from ${project.version}</h4>
<li/>
<p style="margin-bottom: 0in">
<a href='https://issues.jboss.org/browse/TEIID-4731'>TEIID-4731</a> - Unable to connect to MongoDB pod from JBoss Data Virtualization (on OpenShift) (changing the default of authentication scheme used for MongoDB to SCRAM_SHA_1 as MongoDB >= 3.0 uses this auth scheme)
<li/>
<p style="margin-bottom: 0in">
<a href='https://issues.jboss.org/browse/TEIID-5013'>TEIID-5013</a> - Google Spreadsheet Data Sources with custom client_id and client_secret (allowing the client id/secret to be changed)
<li/>
<p style="margin-bottom: 0in">
<a href='https://issues.jboss.org/browse/TEIID-5329'>TEIID-5329</a> - Problem with salesforce url (updating salesforce url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import org.teiid.logging.LogConstants;
import org.teiid.logging.LogManager;
import org.teiid.resource.adapter.google.auth.AuthHeaderFactory;
import org.teiid.resource.adapter.google.auth.OAuth2HeaderFactory;
import org.teiid.resource.adapter.google.common.UpdateResult;
import org.teiid.resource.adapter.google.common.UpdateSet;
Expand All @@ -53,7 +52,11 @@ public class SpreadsheetConnectionImpl extends BasicConnection implements Google
public SpreadsheetConnectionImpl(SpreadsheetManagedConnectionFactory config, AtomicReference<SpreadsheetInfo> spreadsheetInfo) {
this.config = config;
this.spreadsheetInfo = spreadsheetInfo;
AuthHeaderFactory authHeaderFactory = new OAuth2HeaderFactory(config.getRefreshToken().trim());
OAuth2HeaderFactory authHeaderFactory = new OAuth2HeaderFactory(config.getRefreshToken().trim());
if (config.getClientId() != null) {
authHeaderFactory.setClientId(config.getClientId());
authHeaderFactory.setClientSecret(config.getClientSecret());
}
gdata=new GDataClientLoginAPI();
dataProtocol = new GoogleDataProtocolAPI();
authHeaderFactory.login();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public class SpreadsheetManagedConnectionFactory extends BasicManagedConnectionF

private String spreadsheetName;

//Can be either ClientLogin or OAuth2
private String authMethod;

//In case of OAuth2 authentiation user has to supply refreshToken
private String authMethod = SpreadsheetManagedConnectionFactory.OAUTH2_LOGIN;
private String refreshToken;

private String clientId;
private String clientSecret;

private Boolean key = false;

@Override
Expand Down Expand Up @@ -154,5 +154,21 @@ public void setKey(Boolean key) {
}
this.key = key;
}

public String getClientId() {
return clientId;
}

public void setClientId(String clientId) {
this.clientId = clientId;
}

public String getClientSecret() {
return clientSecret;
}

public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}

}
12 changes: 11 additions & 1 deletion connectors/connector-google/src/main/rar/META-INF/ra.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
</config-property>

<config-property>
<description>{$display:"Authentication method",$description:""}</description>
<description>{$display:"Authentication method",$description:"",$required:"false",$advanced:"true"}</description>
<config-property-name>AuthMethod</config-property-name>
<config-property-type>java.lang.String</config-property-type>
</config-property>
Expand All @@ -67,6 +67,16 @@
<config-property-name>Key</config-property-name>
<config-property-type>java.lang.Boolean</config-property-type>
</config-property>
<config-property>
<description>{$display:"client ID",$description:"client ID",$required:"false"}</description>
<config-property-name>ClientId</config-property-name>
<config-property-type>java.lang.String</config-property-type>
</config-property>
<config-property>
<description>{$display:"client secret",$description:"client secret",$required:"false"}</description>
<config-property-name>ClientSecret</config-property-name>
<config-property-type>java.lang.String</config-property-type>
</config-property>
<connectionfactory-interface>javax.resource.cci.ConnectionFactory</connectionfactory-interface>
<connectionfactory-impl-class>org.teiid.resource.spi.WrappedConnectionFactory</connectionfactory-impl-class>
<connection-interface>javax.resource.cci.Connection</connection-interface>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,54 +65,16 @@
*
*/
public class OAuth2Authenticator {
private final String CLIENT_ID = "217138521084.apps.googleusercontent.com"; //$NON-NLS-1$
private final String CLIENT_SECRET = "gXQ6-lOkEjE1lVcz7giB4Poy"; //$NON-NLS-1$
private final String SCOPE_SPREADSHEET = "https://spreadsheets.google.com/feeds/"; //$NON-NLS-1$
final static String CLIENT_ID = "217138521084.apps.googleusercontent.com"; //$NON-NLS-1$
final static String CLIENT_SECRET = "gXQ6-lOkEjE1lVcz7giB4Poy"; //$NON-NLS-1$
// private final String SCOPE_DRIVE = "https://docs.google.com/feeds";
private final String SCOPE_DRIVE= " https://www.googleapis.com/auth/drive"; //$NON-NLS-1$

private final String AUTHORIZATION_SERVER_URL = "https://accounts.google.com/o/oauth2/device/code"; //$NON-NLS-1$
private final String GRANT_TYPE = "http://oauth.net/grant_type/device/1.0"; //$NON-NLS-1$
private final String TOKEN_URL = "https://accounts.google.com/o/oauth2/token"; //$NON-NLS-1$



/**
* AuthUrl is url that user has to type into his browser.
* @return
*/
public AuthUrlResponse getAuthUrl() {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("client_id", CLIENT_ID)); //$NON-NLS-1$
nvps.add(new BasicNameValuePair("scope", SCOPE_SPREADSHEET+ SCOPE_DRIVE)); //$NON-NLS-1$
Map<?, ?> json = jsonResponseHttpPost(AUTHORIZATION_SERVER_URL, nvps);

return new AuthUrlResponse(json.get("device_code"), //$NON-NLS-1$
json.get("user_code"), json.get("verification_url"), //$NON-NLS-1$ //$NON-NLS-2$
json.get("expires_in"), json.get("interval")); //$NON-NLS-1$ //$NON-NLS-2$
}

/**
* Use this method to retrieve access tokens when you think user has already authorized the
* request.
*
* @param deviceCode
* @return
*/
public OAuth2Tokens getAccessGoogleTokens(String deviceCode) {

List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("client_id", CLIENT_ID)); //$NON-NLS-1$
nvps.add(new BasicNameValuePair("client_secret", CLIENT_SECRET)); //$NON-NLS-1$
nvps.add(new BasicNameValuePair("code", deviceCode)); //$NON-NLS-1$
nvps.add(new BasicNameValuePair("grant_type", GRANT_TYPE)); //$NON-NLS-1$
Map<?, ?> json = jsonResponseHttpPost(TOKEN_URL, nvps);
return new OAuth2Tokens(json.get("access_token"), //$NON-NLS-1$
json.get("refresh_token"), json.get("expires_in")); //$NON-NLS-1$ //$NON-NLS-2$
}

/**
*
* @param clientSecret
* @param clientId
* @param new immutable instance of OAuth2Tokens
* @return
*/
Expand All @@ -121,11 +83,11 @@ public OAuth2Tokens getAccessGoogleTokens(String deviceCode) {
// --data-urlencode refresh_token=1/A6ifXgNxCYVGTkPUTnD6Y35v_GyfmuRAsKKL4eww8xs \
// --data-urlencode grant_type=refresh_token \
// https://accounts.google.com/o/oauth2/token
public OAuth2Tokens refreshToken(OAuth2Tokens at) {
public OAuth2Tokens refreshToken(OAuth2Tokens at, String clientId, String clientSecret) {

List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("client_id", CLIENT_ID)); //$NON-NLS-1$
nvps.add(new BasicNameValuePair("client_secret", CLIENT_SECRET)); //$NON-NLS-1$
nvps.add(new BasicNameValuePair("client_id", clientId)); //$NON-NLS-1$
nvps.add(new BasicNameValuePair("client_secret", clientSecret)); //$NON-NLS-1$
nvps.add(new BasicNameValuePair("refresh_token", at.getRefreshToken())); //$NON-NLS-1$
nvps.add(new BasicNameValuePair("grant_type", "refresh_token")); //$NON-NLS-1$ //$NON-NLS-2$
Map<?, ?> json = jsonResponseHttpPost(TOKEN_URL, nvps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@
public class OAuth2HeaderFactory implements AuthHeaderFactory {
private OAuth2Tokens tokens = null;
private OAuth2Authenticator authenticator = new OAuth2Authenticator();
private String clientId = OAuth2Authenticator.CLIENT_ID;
private String clientSecret = OAuth2Authenticator.CLIENT_SECRET;

public OAuth2HeaderFactory(String refreshToken){
this.tokens = new OAuth2Tokens("", refreshToken, 1000); //$NON-NLS-1$
}

public void setClientId(String clientId) {
this.clientId = clientId;
}

public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}

public void login() {
tokens = authenticator.refreshToken(tokens);
tokens = authenticator.refreshToken(tokens, clientId, clientSecret);
}

@Override
public String getAuthHeader() {
return "Bearer "+ tokens.getAccessToken(); //$NON-NLS-1$

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,18 @@ public void testCreateConnectionFactory() throws Exception{
admin.deleteDataSource(deployedName);
}

@Test
public void testCreateGoogleSpreadSheetSource() throws AdminException {
Properties p = new Properties();
p.setProperty("ClientSecret", "a");
p.setProperty("ClientId", "b");
p.setProperty("RefreshToken", "c");
p.setProperty("class-name", "org.teiid.resource.adapter.google.SpreadsheetManagedConnectionFactory");

admin.createDataSource("my-sheet", "google", p);
admin.deleteDataSource("my-sheet");
}

@Test
public void testVDBRestart() throws Exception{
String vdbName = "test";
Expand Down

0 comments on commit 4f495a4

Please sign in to comment.