View Javadoc
1   package org.exoplatform.social.opensocial.auth;
2   
3   import java.io.File;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import javax.servlet.http.HttpServletRequest;
8   
9   import org.apache.shindig.auth.BlobCrypterSecurityToken;
10  import org.apache.shindig.auth.SecurityToken;
11  import org.apache.shindig.common.crypto.BasicBlobCrypter;
12  import org.apache.shindig.common.util.TimeSource;
13  import org.apache.shindig.social.core.oauth.OAuthAuthenticationHandler;
14  import org.apache.shindig.social.opensocial.oauth.OAuthDataStore;
15  import org.exoplatform.commons.utils.PropertyManager;
16  import org.exoplatform.container.PortalContainer;
17  import org.exoplatform.services.log.ExoLogger;
18  import org.exoplatform.services.log.Log;
19  
20  import com.google.inject.Inject;
21  import com.google.inject.name.Named;
22  
23  
24  /**
25   * Created by IntelliJ IDEA.
26   * User: zun
27   * Date: Jul 7, 2010
28   * Time: 5:34:35 PM
29   */
30  public class ExoOAuthAuthenticationHandler extends OAuthAuthenticationHandler {
31  
32    /**
33     * The logger.
34     */
35    private static final Log LOG = ExoLogger.getLogger(ExoOAuthAuthenticationHandler.class);
36  
37    private String portalContainerName;
38  
39    @Inject
40    public ExoOAuthAuthenticationHandler(OAuthDataStore store,
41                                         @Named("shindig.oauth.legacy-body-signing") boolean allowLegacyBodySigning) {
42      // TODO Check the side effects as if we remove allowLegacyBodySigning from constructor.
43      super(store, null);
44    }
45  
46    public String getName() {
47      return super.getName();
48    }
49  
50    public String getPortalContainerName() {
51      if (portalContainerName == null) {
52        RestPortalContainerNameConfig containerNameConfigRest = (RestPortalContainerNameConfig) PortalContainer.
53                getInstance().
54                getComponentInstanceOfType(RestPortalContainerNameConfig.class);
55        portalContainerName = containerNameConfigRest.getContainerName();
56      }
57  
58      return portalContainerName;
59    }
60  
61    public SecurityToken getSecurityTokenFromRequest(HttpServletRequest request) throws InvalidAuthenticationException {
62      final SecurityToken securityToken = super.getSecurityTokenFromRequest(request);
63  
64      final BasicBlobCrypter crypter;
65      final String portalContainer;
66      final String domain;
67      try {
68        String keyFile = getKeyFilePath();
69        crypter = new BasicBlobCrypter(new File(keyFile));
70        crypter.timeSource = new TimeSource();
71  
72        portalContainer = getPortalContainerName();
73        domain = securityToken.getDomain();
74      } catch (Exception e) {
75        LOG.warn("Failed to get security token from request", e);
76        return null;
77      }
78  
79      Map<String, String> values = new HashMap<>();
80      values.put(BlobCrypterSecurityToken.Keys.APP_URL.getKey(), securityToken.getAppUrl());
81      values.put(BlobCrypterSecurityToken.Keys.OWNER.getKey(), securityToken.getOwnerId());
82      values.put(BlobCrypterSecurityToken.Keys.VIEWER.getKey(), securityToken.getViewerId());
83      values.put(BlobCrypterSecurityToken.Keys.TRUSTED_JSON.getKey(), "trusted");
84  
85      final ExoBlobCrypterSecurityToken crypterSecurityToken = new ExoBlobCrypterSecurityToken(portalContainer, domain, null, values);
86  
87      return crypterSecurityToken;
88    }
89  
90    public String getWWWAuthenticateHeader(String realm) {
91      return super.getWWWAuthenticateHeader(realm);
92    }
93  
94    /**
95     * Method returns a path to the file containing the encryption key
96     */
97    private String getKeyFilePath() {
98  
99      String keyPath = PropertyManager.getProperty("gatein.gadgets.securitytokenkeyfile");
100 
101     File tokenKeyFile = null;
102     if (keyPath == null) {
103        LOG.warn("The gadgets token key is not configured. The default key.txt file in /bin will be used");
104        tokenKeyFile = new File("key.txt");
105     }
106     else {
107        tokenKeyFile = new File(keyPath);
108     }
109 
110     return tokenKeyFile.getAbsolutePath();
111     
112   }
113   
114 }