| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds 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 |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.gogrid.filters; |
| 20 | |
| 21 | import static java.lang.String.format; |
| 22 | import static org.jclouds.Constants.PROPERTY_CREDENTIAL; |
| 23 | import static org.jclouds.Constants.PROPERTY_IDENTITY; |
| 24 | |
| 25 | import java.net.URI; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | import javax.inject.Inject; |
| 29 | import javax.inject.Named; |
| 30 | |
| 31 | import org.jclouds.Constants; |
| 32 | import org.jclouds.crypto.CryptoStreams; |
| 33 | import org.jclouds.date.TimeStamp; |
| 34 | import org.jclouds.http.HttpRequest; |
| 35 | import org.jclouds.http.HttpRequestFilter; |
| 36 | import org.jclouds.http.HttpUtils; |
| 37 | import org.jclouds.http.utils.ModifyRequest; |
| 38 | import org.jclouds.io.InputSuppliers; |
| 39 | import org.jclouds.logging.Logger; |
| 40 | |
| 41 | import com.google.common.collect.ImmutableSet; |
| 42 | import com.google.common.collect.Multimap; |
| 43 | |
| 44 | /** |
| 45 | * @author Oleksiy Yarmula |
| 46 | */ |
| 47 | public class SharedKeyLiteAuthentication implements HttpRequestFilter { |
| 48 | |
| 49 | private final String apiKey; |
| 50 | private final String secret; |
| 51 | private final Long timeStamp; |
| 52 | private final HttpUtils utils; |
| 53 | |
| 54 | @Resource |
| 55 | @Named(Constants.LOGGER_SIGNATURE) |
| 56 | Logger signatureLog = Logger.NULL; |
| 57 | |
| 58 | @Inject |
| 59 | public SharedKeyLiteAuthentication(@Named(PROPERTY_IDENTITY) String apiKey, |
| 60 | @Named(PROPERTY_CREDENTIAL) String secret, @TimeStamp Long timeStamp, HttpUtils utils) { |
| 61 | this.apiKey = apiKey; |
| 62 | this.secret = secret; |
| 63 | this.timeStamp = timeStamp; |
| 64 | this.utils = utils; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public HttpRequest filter(HttpRequest request) { |
| 69 | |
| 70 | String toSign = createStringToSign(); |
| 71 | String signatureMd5 = getMd5For(toSign); |
| 72 | |
| 73 | String query = request.getEndpoint().getQuery(); |
| 74 | Multimap<String, String> decodedParams = ModifyRequest.parseQueryToMap(query); |
| 75 | |
| 76 | decodedParams.replaceValues("sig", ImmutableSet.of(signatureMd5)); |
| 77 | decodedParams.replaceValues("api_key", ImmutableSet.of(apiKey)); |
| 78 | |
| 79 | String updatedQuery = ModifyRequest.makeQueryLine(decodedParams, null); |
| 80 | String requestBasePart = request.getEndpoint().toASCIIString(); |
| 81 | String updatedEndpoint = requestBasePart.substring(0, requestBasePart.indexOf("?") + 1) + updatedQuery; |
| 82 | request = request.toBuilder().endpoint(URI.create(updatedEndpoint)).build(); |
| 83 | utils.logRequest(signatureLog, request, "<<"); |
| 84 | return request; |
| 85 | } |
| 86 | |
| 87 | private String createStringToSign() { |
| 88 | return format("%s%s%s", apiKey, secret, timeStamp); |
| 89 | } |
| 90 | |
| 91 | private String getMd5For(String stringToHash) { |
| 92 | try { |
| 93 | return CryptoStreams.md5Hex(InputSuppliers.of(stringToHash)); |
| 94 | } catch (Exception e) { |
| 95 | throw new RuntimeException(e); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | } |