blob: e8e94bde8d2e131cfb3a55f3588df032fad4074e [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.cn.anubis.provider;
import org.apache.fineract.cn.anubis.config.AnubisConstants;
import org.apache.fineract.cn.lang.security.RsaPublicKeyBuilder;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.math.BigInteger;
import java.security.PublicKey;
/**
* @author Myrle Krantz
*/
@Component
public class SystemRsaKeyProvider {
private final String keyTimestamp;
private final String systemPublicKeyMod;
private final String systemPublicKeyExp;
private final Logger logger;
private PublicKey systemPublicKey;
@Autowired
public SystemRsaKeyProvider(
final @Value("${" + AnubisConstants.PUBLIC_KEY_TIMESTAMP_PROPERTY + "}") String keyTimestamp,
final @Value("${" + AnubisConstants.PUBLIC_KEY_MOD_PROPERTY + "}") String systemPublicKeyMod,
final @Value("${" + AnubisConstants.PUBLIC_KEY_EXP_PROPERTY + "}") String systemPublicKeyExp,
final @Qualifier(AnubisConstants.LOGGER_NAME) Logger logger)
{
this.keyTimestamp = keyTimestamp;
this.systemPublicKeyMod = systemPublicKeyMod;
this.systemPublicKeyExp = systemPublicKeyExp;
this.logger = logger;
logger.info("System Key registered for timestamp: " + keyTimestamp);
}
@PostConstruct
public void init() {
this.systemPublicKey =
new RsaPublicKeyBuilder()
.setPublicKeyMod(new BigInteger(systemPublicKeyMod))
.setPublicKeyExp(new BigInteger(systemPublicKeyExp))
.build();
}
public PublicKey getPublicKey(final String timestamp) throws InvalidKeyTimestampException {
if (!timestamp.equals(keyTimestamp)) {
logger.info("System Key requested for non-existant key timestamp: " + timestamp);
throw new InvalidKeyTimestampException(timestamp);
}
return systemPublicKey;
}
}