2010-09-22 19:04:10 +02:00
|
|
|
package com.google.refine.oauth;
|
2010-04-21 23:08:34 +02:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2010-04-23 10:25:52 +02:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
2010-04-21 23:08:34 +02:00
|
|
|
import oauth.signpost.OAuthConsumer;
|
|
|
|
import oauth.signpost.OAuthProvider;
|
|
|
|
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
|
2010-04-23 10:25:52 +02:00
|
|
|
import oauth.signpost.http.HttpParameters;
|
|
|
|
|
2010-09-22 19:04:10 +02:00
|
|
|
import com.google.refine.util.FreebaseUtils;
|
2010-04-21 23:08:34 +02:00
|
|
|
|
|
|
|
public class OAuthUtilities {
|
|
|
|
|
|
|
|
static final private Map<String,Provider> providers = new HashMap<String,Provider>();
|
|
|
|
static final private Map<String,String[]> infos = new HashMap<String,String[]>();
|
|
|
|
|
2010-09-29 03:50:57 +02:00
|
|
|
static private final String[] FREEBASE_OAUTH_INFO = { "#9202a8c04000641f80000000185352db" , "4561ee02279e6f04ebd88a1557e4292489380adf"};
|
2010-04-21 23:08:34 +02:00
|
|
|
|
|
|
|
static {
|
2010-04-23 10:25:52 +02:00
|
|
|
Provider freebase = new FreebaseProvider(FreebaseUtils.FREEBASE_HOST);
|
|
|
|
providers.put(freebase.getHost(), freebase);
|
2010-04-21 23:08:34 +02:00
|
|
|
|
2010-04-23 10:25:52 +02:00
|
|
|
infos.put(freebase.getHost(), FREEBASE_OAUTH_INFO);
|
2010-04-21 23:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Provider getProvider(String name) {
|
|
|
|
return (name == null) ? null : providers.get(name);
|
|
|
|
}
|
|
|
|
|
2010-04-23 10:25:52 +02:00
|
|
|
public static Provider getProvider(HttpServletRequest request) {
|
|
|
|
String path = request.getPathInfo().substring(1);
|
|
|
|
int slash = path.lastIndexOf('/');
|
|
|
|
String provider_str = path.substring(slash + 1);
|
|
|
|
Provider provider = getProvider(provider_str);
|
|
|
|
if (provider == null) throw new RuntimeException("Can't find OAuth provider '" + provider_str + "'");
|
|
|
|
return provider;
|
|
|
|
}
|
|
|
|
|
2010-04-21 23:08:34 +02:00
|
|
|
public static OAuthConsumer getConsumer(Provider provider) {
|
|
|
|
if (provider == null) throw new RuntimeException("Provider can't be null");
|
|
|
|
String[] consumer_info = infos.get(provider.getHost());
|
|
|
|
if (consumer_info == null) throw new RuntimeException("Can't find secrets for provider '" + provider.getHost() + "'");
|
2010-05-11 01:24:58 +02:00
|
|
|
OAuthConsumer oauthConsumer = new FreebaseTimeCommonsHttpOAuthConsumer(consumer_info[0],consumer_info[1]);
|
2010-04-23 10:25:52 +02:00
|
|
|
HttpParameters params = new HttpParameters();
|
|
|
|
params.put("realm", provider.getHost());
|
|
|
|
oauthConsumer.setAdditionalParameters(params);
|
|
|
|
return oauthConsumer;
|
2010-04-21 23:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static OAuthConsumer getConsumer(Credentials credentials, Provider provider) {
|
|
|
|
OAuthConsumer consumer = getConsumer(provider);
|
2010-04-23 10:25:52 +02:00
|
|
|
if (credentials != null) {
|
|
|
|
consumer.setTokenWithSecret(credentials.getToken(), credentials.getSecret());
|
|
|
|
}
|
2010-04-21 23:08:34 +02:00
|
|
|
return consumer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static OAuthProvider getOAuthProvider(Provider p) {
|
|
|
|
return new CommonsHttpOAuthProvider(p.getRequestTokenServiceURL(), p.getAccessTokenServiceURL(), p.getUserAuthorizationURL());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|