001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.ws.security;
015
016import org.slf4j.Logger;
017import org.slf4j.LoggerFactory;
018import org.springframework.context.annotation.Primary;
019import org.springframework.stereotype.Service;
020
021@Primary
022@Service
023public class AppKeySigningService extends BaseSigningService {
024
025  private static final Logger LOG = LoggerFactory.getLogger(AppKeySigningService.class);
026
027  private final KeyStore keyStore;
028
029  public AppKeySigningService(KeyStore keyStore) {
030    this.keyStore = keyStore;
031  }
032
033  /**
034   * Generates a Base64 encoded HMAC-SHA1 signature of the passed request data with the secret key
035   * associated with the given application key. See Message Authentication Code specs
036   * http://tools.ietf.org/html/rfc2104
037   *
038   * @param requestDataToSign the request data to be signed
039   * @param appKey            the application key
040   */
041  @Override
042  public String buildSignature(RequestDataToSign requestDataToSign, String appKey) {
043    // find private key for this app
044    final String secretKey = keyStore.getPrivateKey(appKey);
045    if (secretKey == null) {
046      LOG.error("Unknown application key: {}", appKey);
047      throw new PrivateKeyNotFoundException();
048    }
049
050    // sign
051    return super.buildSignature(requestDataToSign, secretKey);
052  }
053}