Coverage Report - org.apache.shindig.social.sample.oauth.SampleContainerOAuthLookupService
 
Classes in this File Line Coverage Branch Coverage Complexity
SampleContainerOAuthLookupService
13%
4/31
0%
0/12
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 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  
   // If we were a real social network this would probably be a function
 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  
   // If we were a real social network we would probably be keeping track of this in a db somewhere
 51  1
   private static Map<String, ArrayList<String>> sampleContainerAppInstalls = ImmutableMap.of(
 52  
       "john.doe", Lists.newArrayList("7810", "8355")
 53  
   );
 54  
 
 55  
   // If we were a real social network we would establish shared secrets with each of our gadgets
 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  
 }