com.saltedge.connector.sdk.tools.JsonTools Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of compliance-connector-sdk Show documentation
Show all versions of compliance-connector-sdk Show documentation
Salt Edge Compliance Connector Java SDK (BG)
/*
* @author Constantin Chelban ([email protected])
* Copyright (c) 2020 Salt Edge.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.saltedge.connector.sdk.tools;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.saltedge.connector.sdk.SDKConstants;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.jackson.io.JacksonSerializer;
import java.security.PrivateKey;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
public class JsonTools {
/**
* Creates mapper with deserialization option to fail on unknown property
* and serialization option to skip NULL values
*
* @return Jackson's ObjectMapper
*/
public static ObjectMapper createDefaultMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
}
/**
* Convert to JWT encoded and signed string for Authorization header
*
* @param requestData to encode
* @param key RSA private key for signing
* @return jwt string
*/
public static String createAuthorizationHeaderValue(Object requestData, PrivateKey key) {
if (key == null) return "";
return "Bearer " + Jwts.builder()
.serializeToJsonWith(new JacksonSerializer<>(createDefaultMapper()))
.claim(SDKConstants.KEY_DATA, requestData)
.signWith(key)
.setExpiration(Date.from(Instant.now().plus(1, ChronoUnit.MINUTES)))
.compact();
}
}