| 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.atmos.filters; |
| 20 | |
| 21 | import static org.jclouds.Constants.LOGGER_SIGNATURE; |
| 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 | import javax.inject.Singleton; |
| 31 | import javax.ws.rs.core.UriBuilder; |
| 32 | |
| 33 | import org.jclouds.crypto.Crypto; |
| 34 | import org.jclouds.crypto.CryptoStreams; |
| 35 | import org.jclouds.date.TimeStamp; |
| 36 | import org.jclouds.http.HttpException; |
| 37 | import org.jclouds.io.InputSuppliers; |
| 38 | import org.jclouds.location.Provider; |
| 39 | import org.jclouds.logging.Logger; |
| 40 | |
| 41 | import com.google.common.base.Function; |
| 42 | |
| 43 | /** |
| 44 | * Signs the EMC Atmos Online Storage request. |
| 45 | * |
| 46 | * @see <a href="https://community.emc.com/community/labs/atmos_online" /> |
| 47 | * @author Adrian Cole |
| 48 | * |
| 49 | */ |
| 50 | @Singleton |
| 51 | public class ShareUrl implements Function<String, URI> { |
| 52 | |
| 53 | private final String uid; |
| 54 | private final byte[] key; |
| 55 | private final URI provider; |
| 56 | private final javax.inject.Provider<Long> timeStampProvider; |
| 57 | private final javax.inject.Provider<UriBuilder> uriBuilders; |
| 58 | private final Crypto crypto; |
| 59 | |
| 60 | @Resource |
| 61 | Logger logger = Logger.NULL; |
| 62 | |
| 63 | @Resource |
| 64 | @Named(LOGGER_SIGNATURE) |
| 65 | Logger signatureLog = Logger.NULL; |
| 66 | |
| 67 | @Inject |
| 68 | public ShareUrl(@Named(PROPERTY_IDENTITY) String uid, @Named(PROPERTY_CREDENTIAL) String encodedKey, |
| 69 | @Provider URI provider, @TimeStamp javax.inject.Provider<Long> timeStampProvider, |
| 70 | javax.inject.Provider<UriBuilder> uriBuilders, Crypto crypto) { |
| 71 | this.uid = uid; |
| 72 | this.key = CryptoStreams.base64(encodedKey); |
| 73 | this.provider = provider; |
| 74 | this.uriBuilders = uriBuilders; |
| 75 | this.timeStampProvider = timeStampProvider; |
| 76 | this.crypto = crypto; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public URI apply(String path) throws HttpException { |
| 81 | String requestedResource = new StringBuilder().append("/rest/namespace/").append(path).toString(); |
| 82 | long expires = timeStampProvider.get(); |
| 83 | String signature = signString(createStringToSign(requestedResource, expires)); |
| 84 | return uriBuilders.get().uri(provider).path(requestedResource).queryParam("uid", uid).queryParam("expires", |
| 85 | expires).queryParam("signature", signature).build(); |
| 86 | } |
| 87 | |
| 88 | public String createStringToSign(String requestedResource, long expires) { |
| 89 | StringBuilder toSign = new StringBuilder(); |
| 90 | toSign.append("GET\n"); |
| 91 | toSign.append(requestedResource.toLowerCase()).append("\n"); |
| 92 | toSign.append(uid).append("\n"); |
| 93 | toSign.append(expires); |
| 94 | return toSign.toString(); |
| 95 | } |
| 96 | |
| 97 | public String signString(String toSign) { |
| 98 | String signature; |
| 99 | try { |
| 100 | signature = CryptoStreams.base64(CryptoStreams.mac(InputSuppliers.of(toSign), crypto.hmacSHA1(key))); |
| 101 | } catch (Exception e) { |
| 102 | throw new HttpException("error signing request", e); |
| 103 | } |
| 104 | return signature; |
| 105 | } |
| 106 | |
| 107 | } |