com.launchkey.sdk.service.V1ServiceAbstract Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of launchkey-sdk Show documentation
Show all versions of launchkey-sdk Show documentation
SDK for interacting with the LaunchKey distributed authentication and authorization platform
/**
* Copyright 2015 LaunchKey, Inc. All rights reserved.
*
* Licensed under the MIT License.
* You may not use this file except in compliance with the License.
* A copy of the License is located in the "LICENSE.txt" file accompanying
* this file. This file 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 com.launchkey.sdk.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.launchkey.sdk.cache.CachePersistenceException;
import com.launchkey.sdk.cache.PingResponseCache;
import com.launchkey.sdk.crypto.Crypto;
import com.launchkey.sdk.service.error.LaunchKeyException;
import com.launchkey.sdk.transport.v1.Transport;
import com.launchkey.sdk.transport.v1.domain.LaunchKeyDateFormat;
import com.launchkey.sdk.transport.v1.domain.PingRequest;
import com.launchkey.sdk.transport.v1.domain.PingResponse;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.security.interfaces.RSAPublicKey;
import java.util.Date;
/**
* Abstract V1 based service providing shared funcitonality between auth and whitelabel services
*/
public abstract class V1ServiceAbstract {
protected final Transport transport;
protected final Crypto crypto;
protected final PingResponseCache pingResponseCache;
protected final long rocketKey;
protected final String secretKey;
protected final Base64 base64 = new Base64(0);
protected final ObjectMapper objectMapper = new ObjectMapper();
protected final Log log;
protected final LaunchKeyDateFormat launchKeyDateFormat = new LaunchKeyDateFormat();
/**
* @param transport
* @param crypto
* @param pingResponseCache
* @param rocketKey
* @param secretKey
*/
public V1ServiceAbstract(
Transport transport,
Crypto crypto,
PingResponseCache pingResponseCache,
long rocketKey,
String secretKey
) {
this.transport = transport;
this.secretKey = secretKey;
this.pingResponseCache = pingResponseCache;
this.rocketKey = rocketKey;
this.crypto = crypto;
this.log = LogFactory.getLog(getClass());
}
protected RSAPublicKey getLaunchKeyPublicKey() throws LaunchKeyException {
PingResponse pingResponse = null;
try {
pingResponse = pingResponseCache.getPingResponse();
} catch (CachePersistenceException e) {
log.error("Error getting ping response from cache", e);
}
if (pingResponse == null) {
pingResponse = transport.ping(new PingRequest());
try {
pingResponseCache.setPingResponse(pingResponse);
} catch (CachePersistenceException e) {
log.error("Error placing ping response in cache", e);
}
}
RSAPublicKey publicKey = crypto.getRSAPublicKeyFromPEM(pingResponse.getPublicKey());
return publicKey;
}
protected byte[] getSecret() throws LaunchKeyException {
try {
String json = objectMapper.writeValueAsString(new Object() {
public final String secret = secretKey;
public final String stamped = launchKeyDateFormat.format(new Date());
});
return crypto.encryptRSA(json.getBytes(), getLaunchKeyPublicKey());
} catch (JsonProcessingException e) {
throw new LaunchKeyException("Unable to create JSON from secret key", e, 0);
}
}
}