001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.amazonrankings.util;
016    
017    import com.liferay.portal.kernel.util.Base64;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.net.URLEncoder;
024    
025    import java.util.Map;
026    import java.util.Set;
027    import java.util.TreeMap;
028    
029    import javax.crypto.Mac;
030    import javax.crypto.spec.SecretKeySpec;
031    
032    /**
033     * @author Barrie Selack
034     * @author Brian Wing Shun Chan
035     */
036    public class AmazonSignedRequestsUtil {
037    
038            public static String generateUrlWithSignature(
039                            Map<String, String> parameters)
040                    throws Exception {
041    
042                    String canonicalizedParameters = _canonicalizeParameters(parameters);
043    
044                    String signature = _generateSignature(
045                            "GET\necs.amazonaws.com\n/onca/xml\n" + canonicalizedParameters);
046    
047                    return "http://ecs.amazonaws.com/onca/xml?" + canonicalizedParameters +
048                            "&Signature=" + signature;
049            }
050    
051            private static String _canonicalizeParameters(
052                            Map<String, String> parameters)
053                    throws Exception {
054    
055                    if (parameters.isEmpty()) {
056                            return StringPool.BLANK;
057                    }
058    
059                    parameters = new TreeMap<String, String>(parameters);
060    
061                    Set<Map.Entry<String, String>> parametersSet = parameters.entrySet();
062    
063                    StringBundler sb = new StringBundler(parametersSet.size() * 4);
064    
065                    for (Map.Entry<String, String> parameter : parametersSet) {
066                            sb.append(_rfc3986Encode(parameter.getKey()));
067                            sb.append(StringPool.EQUAL);
068                            sb.append(_rfc3986Encode(parameter.getValue()));
069                            sb.append(StringPool.AMPERSAND);
070                    }
071    
072                    sb.setIndex(sb.index() - 1);
073    
074                    return sb.toString();
075            }
076    
077            private static String _generateSignature(String data) throws Exception {
078                    String amazonSecretAccessKey =
079                            AmazonRankingsUtil.getAmazonSecretAccessKey();
080    
081                    if (Validator.isNull(amazonSecretAccessKey)) {
082                            return StringPool.BLANK;
083                    }
084    
085                    SecretKeySpec secretKeySpec = new SecretKeySpec(
086                            amazonSecretAccessKey.getBytes(), _HMAC_SHA256_ALGORITHM);
087    
088                    Mac mac = Mac.getInstance(_HMAC_SHA256_ALGORITHM);
089    
090                    mac.init(secretKeySpec);
091    
092                    byte[] bytes = mac.doFinal(data.getBytes());
093    
094                    String signature = Base64.encode(bytes);
095    
096                    return StringUtil.replace(
097                            signature, new String[] {StringPool.EQUAL, StringPool.PLUS},
098                            new String[] {"%3D", "%2B"});
099            }
100    
101            private static String _rfc3986Encode(String string) throws Exception {
102                    if (Validator.isNull(string)) {
103                            return StringPool.BLANK;
104                    }
105    
106                    string = URLEncoder.encode(string, StringPool.UTF8);
107    
108                    string = StringUtil.replace(
109                            string, new String[] {StringPool.STAR, StringPool.PLUS, "%7E"},
110                            new String[] {"%2A", "%2B", "~"});
111    
112                    return string;
113            }
114    
115            private static final String _HMAC_SHA256_ALGORITHM = "HmacSHA256";
116    
117    }