| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.shindig.social.sample.oauth; |
| 19 | |
|
| 20 | |
import org.apache.shindig.auth.SecurityToken; |
| 21 | |
import org.apache.shindig.social.core.oauth.OAuthSecurityToken; |
| 22 | |
import org.apache.shindig.social.opensocial.oauth.OAuthLookupService; |
| 23 | |
|
| 24 | |
import com.google.common.collect.ImmutableMap; |
| 25 | |
import com.google.common.collect.Lists; |
| 26 | |
import com.google.common.collect.Maps; |
| 27 | |
|
| 28 | |
import net.oauth.OAuthAccessor; |
| 29 | |
import net.oauth.OAuthConsumer; |
| 30 | |
import net.oauth.OAuthException; |
| 31 | |
import net.oauth.OAuthMessage; |
| 32 | |
import net.oauth.OAuthServiceProvider; |
| 33 | |
import net.oauth.SimpleOAuthValidator; |
| 34 | |
|
| 35 | |
import java.io.IOException; |
| 36 | |
import java.net.URISyntaxException; |
| 37 | |
import java.util.ArrayList; |
| 38 | |
import java.util.List; |
| 39 | |
import java.util.Map; |
| 40 | |
|
| 41 | 3 | public class SampleContainerOAuthLookupService implements OAuthLookupService { |
| 42 | |
|
| 43 | 1 | private static Map<String, String> sampleContainerUrlToAppIdMap = ImmutableMap.of( |
| 44 | |
"http://localhost:8080/gadgets/files/samplecontainer/examples/SocialHelloWorld.xml", |
| 45 | |
"7810", |
| 46 | |
"http://localhost:8080/gadgets/files/samplecontainer/examples/SocialActivitiesWorld.xml", |
| 47 | |
"8355" |
| 48 | |
); |
| 49 | |
|
| 50 | |
|
| 51 | 1 | private static Map<String, ArrayList<String>> sampleContainerAppInstalls = ImmutableMap.of( |
| 52 | |
"john.doe", Lists.newArrayList("7810", "8355") |
| 53 | |
); |
| 54 | |
|
| 55 | |
|
| 56 | 1 | private static Map<String, String> sampleContainerSharedSecrets = ImmutableMap.of( |
| 57 | |
"7810", "SocialHelloWorldSharedSecret", |
| 58 | |
"8355", "SocialActivitiesWorldSharedSecret" |
| 59 | |
); |
| 60 | |
|
| 61 | |
public boolean thirdPartyHasAccessToUser(OAuthMessage message, String appUrl, String userId) { |
| 62 | 0 | String appId = getAppId(appUrl); |
| 63 | 0 | return hasValidSignature(message, appUrl, appId) |
| 64 | |
&& userHasAppInstalled(userId, appId); |
| 65 | |
} |
| 66 | |
|
| 67 | |
private boolean hasValidSignature(OAuthMessage message, String appUrl, String appId) { |
| 68 | 0 | String sharedSecret = sampleContainerSharedSecrets.get(appId); |
| 69 | 0 | if (sharedSecret == null) { |
| 70 | 0 | return false; |
| 71 | |
} |
| 72 | |
|
| 73 | 0 | OAuthServiceProvider provider = new OAuthServiceProvider(null, null, null); |
| 74 | 0 | OAuthConsumer consumer = new OAuthConsumer(null, appUrl, sharedSecret, provider); |
| 75 | 0 | OAuthAccessor accessor = new OAuthAccessor(consumer); |
| 76 | |
|
| 77 | 0 | SimpleOAuthValidator validator = new SimpleOAuthValidator(); |
| 78 | |
try { |
| 79 | 0 | validator.validateMessage(message, accessor); |
| 80 | 0 | } catch (OAuthException e) { |
| 81 | 0 | return false; |
| 82 | 0 | } catch (IOException e) { |
| 83 | 0 | return false; |
| 84 | 0 | } catch (URISyntaxException e) { |
| 85 | 0 | return false; |
| 86 | 0 | } |
| 87 | |
|
| 88 | 0 | return true; |
| 89 | |
} |
| 90 | |
|
| 91 | |
private boolean userHasAppInstalled(String userId, String appId) { |
| 92 | 0 | List<String> appInstalls = sampleContainerAppInstalls.get(userId); |
| 93 | 0 | if (appInstalls != null) { |
| 94 | 0 | for (String appInstall : appInstalls) { |
| 95 | 0 | if (appInstall.equals(appId)) { |
| 96 | 0 | return true; |
| 97 | |
} |
| 98 | 0 | } |
| 99 | |
} |
| 100 | |
|
| 101 | 0 | return false; |
| 102 | |
} |
| 103 | |
|
| 104 | |
public SecurityToken getSecurityToken(String appUrl, String userId) { |
| 105 | 0 | return new OAuthSecurityToken(userId, appUrl, getAppId(appUrl), "samplecontainer"); |
| 106 | |
} |
| 107 | |
|
| 108 | |
private String getAppId(String appUrl) { |
| 109 | 0 | return sampleContainerUrlToAppIdMap.get(appUrl); |
| 110 | |
} |
| 111 | |
|
| 112 | |
} |