use of org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter in project cxf by apache.
the class SwaggerToOpenApiConversionUtils method getOpenApiFromSwaggerJson.
public static String getOpenApiFromSwaggerJson(MessageContext ctx, String json, OpenApiConfiguration cfg) throws IOException {
JsonMapObjectReaderWriter readerWriter = new JsonMapObjectReaderWriter();
JsonMapObject sw2 = readerWriter.fromJsonToJsonObject(json);
JsonMapObject sw3 = new JsonMapObject();
// "openapi"
sw3.setProperty("openapi", "3.0.0");
// "servers"
setServersProperty(ctx, sw2, sw3);
// "info"
JsonMapObject infoObject = sw2.getJsonMapProperty("info");
if (infoObject != null) {
sw3.setProperty("info", infoObject);
}
// "tags"
List<Map<String, Object>> tagsObject = sw2.getListMapProperty("tags");
if (tagsObject != null) {
sw3.setProperty("tags", tagsObject);
}
// paths
Map<String, JsonMapObject> requestBodies = cfg != null && cfg.isCreateRequestBodies() ? new LinkedHashMap<>() : null;
setPathsProperty(sw2, sw3, requestBodies);
// components
setComponentsProperty(sw2, sw3, requestBodies);
// externalDocs
Object externalDocsObject = sw2.getProperty("externalDocs");
if (externalDocsObject != null) {
sw3.setProperty("externalDocs", externalDocsObject);
}
return readerWriter.toJson(sw3).replace("#/definitions/", "#/components/schemas/");
}
use of org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter in project cxf by apache.
the class SwaggerToOpenApiConversionUtilsTest method testConvertFromSwaggerToOpenApiWithRequestBodies.
@Test
public void testConvertFromSwaggerToOpenApiWithRequestBodies() {
OpenApiConfiguration cfg = new OpenApiConfiguration();
cfg.setCreateRequestBodies(true);
String s = SwaggerToOpenApiConversionUtils.getOpenApiFromSwaggerLoc("/swagger2petShop.json", cfg);
JsonMapObjectReaderWriter readerWriter = new JsonMapObjectReaderWriter();
JsonMapObject sw3 = readerWriter.fromJsonToJsonObject(s);
assertEquals("3.0.0", sw3.getStringProperty("openapi"));
verifyServersProperty(sw3);
verifyInfoProperty(sw3);
verifyTagsProperty(sw3);
verifyPathsProperty(sw3, cfg);
verifyComponentsProperty(sw3, cfg);
}
use of org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter in project cxf by apache.
the class JwsJoseCookBookTest method testDetachedHMACSignature.
@SuppressWarnings("deprecation")
@Test
public void testDetachedHMACSignature() throws Exception {
JwsCompactProducer compactProducer = new JwsCompactProducer(PAYLOAD, true);
compactProducer.getJwsHeaders().setSignatureAlgorithm(SignatureAlgorithm.HS256);
compactProducer.getJwsHeaders().setKeyId(HMAC_KID_VALUE);
JsonMapObjectReaderWriter reader = new JsonMapObjectReaderWriter();
assertEquals(reader.toJson(compactProducer.getJwsHeaders().asMap()), HMAC_SIGNATURE_PROTECTED_HEADER_JSON);
assertEquals(compactProducer.getUnsignedEncodedJws(), HMAC_SIGNATURE_PROTECTED_HEADER + ".");
JsonWebKeys jwks = readKeySet("cookbookSecretSet.txt");
List<JsonWebKey> keys = jwks.getKeys();
JsonWebKey key = keys.get(0);
compactProducer.signWith(key);
assertEquals(compactProducer.getSignedEncodedJws(), DETACHED_HMAC_JWS);
JwsCompactConsumer compactConsumer = new JwsCompactConsumer(compactProducer.getSignedEncodedJws(), ENCODED_PAYLOAD);
assertTrue(compactConsumer.verifySignatureWith(key, SignatureAlgorithm.HS256));
JwsJsonProducer jsonProducer = new JwsJsonProducer(PAYLOAD);
assertEquals(jsonProducer.getPlainPayload(), PAYLOAD);
assertEquals(jsonProducer.getUnsignedEncodedPayload(), ENCODED_PAYLOAD);
JwsHeaders protectedHeader = new JwsHeaders();
protectedHeader.setSignatureAlgorithm(SignatureAlgorithm.HS256);
protectedHeader.setKeyId(HMAC_KID_VALUE);
jsonProducer.signWith(JwsUtils.getSignatureProvider(key, SignatureAlgorithm.HS256), protectedHeader);
assertEquals(jsonProducer.getJwsJsonSignedDocument(true), HMAC_DETACHED_JSON_GENERAL_SERIALIZATION);
JwsJsonConsumer jsonConsumer = new JwsJsonConsumer(jsonProducer.getJwsJsonSignedDocument(true), ENCODED_PAYLOAD);
assertTrue(jsonConsumer.verifySignatureWith(key, SignatureAlgorithm.HS256));
jsonProducer = new JwsJsonProducer(PAYLOAD, true);
jsonProducer.signWith(JwsUtils.getSignatureProvider(key, SignatureAlgorithm.HS256), protectedHeader);
assertEquals(jsonProducer.getJwsJsonSignedDocument(true), HMAC_DETACHED_JSON_FLATTENED_SERIALIZATION);
jsonConsumer = new JwsJsonConsumer(jsonProducer.getJwsJsonSignedDocument(true), ENCODED_PAYLOAD);
assertTrue(jsonConsumer.verifySignatureWith(key, SignatureAlgorithm.HS256));
}
use of org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter in project cxf by apache.
the class JwsJoseCookBookTest method testRSAv15Signature.
@Test
public void testRSAv15Signature() throws Exception {
JwsCompactProducer compactProducer = new JwsCompactProducer(PAYLOAD);
compactProducer.getJwsHeaders().setSignatureAlgorithm(SignatureAlgorithm.RS256);
compactProducer.getJwsHeaders().setKeyId(RSA_KID_VALUE);
JsonMapObjectReaderWriter reader = new JsonMapObjectReaderWriter();
assertEquals(reader.toJson(compactProducer.getJwsHeaders().asMap()), RSA_V1_5_SIGNATURE_PROTECTED_HEADER_JSON);
assertEquals(compactProducer.getUnsignedEncodedJws(), RSA_V1_5_SIGNATURE_PROTECTED_HEADER + "." + ENCODED_PAYLOAD);
JsonWebKeys jwks = readKeySet("cookbookPrivateSet.txt");
List<JsonWebKey> keys = jwks.getKeys();
JsonWebKey rsaKey = keys.get(1);
compactProducer.signWith(rsaKey);
assertEquals(compactProducer.getSignedEncodedJws(), RSA_V1_5_SIGNATURE_PROTECTED_HEADER + "." + ENCODED_PAYLOAD + "." + RSA_V1_5_SIGNATURE_VALUE);
JwsCompactConsumer compactConsumer = new JwsCompactConsumer(compactProducer.getSignedEncodedJws());
JsonWebKeys publicJwks = readKeySet("cookbookPublicSet.txt");
List<JsonWebKey> publicKeys = publicJwks.getKeys();
JsonWebKey rsaPublicKey = publicKeys.get(1);
assertTrue(compactConsumer.verifySignatureWith(rsaPublicKey, SignatureAlgorithm.RS256));
JwsJsonProducer jsonProducer = new JwsJsonProducer(PAYLOAD);
assertEquals(jsonProducer.getPlainPayload(), PAYLOAD);
assertEquals(jsonProducer.getUnsignedEncodedPayload(), ENCODED_PAYLOAD);
JwsHeaders protectedHeader = new JwsHeaders();
protectedHeader.setSignatureAlgorithm(SignatureAlgorithm.RS256);
protectedHeader.setKeyId(RSA_KID_VALUE);
jsonProducer.signWith(JwsUtils.getSignatureProvider(rsaKey, SignatureAlgorithm.RS256), protectedHeader);
assertEquals(jsonProducer.getJwsJsonSignedDocument(), RSA_V1_5_JSON_GENERAL_SERIALIZATION);
JwsJsonConsumer jsonConsumer = new JwsJsonConsumer(jsonProducer.getJwsJsonSignedDocument());
assertTrue(jsonConsumer.verifySignatureWith(rsaPublicKey, SignatureAlgorithm.RS256));
jsonProducer = new JwsJsonProducer(PAYLOAD, true);
jsonProducer.signWith(JwsUtils.getSignatureProvider(rsaKey, SignatureAlgorithm.RS256), protectedHeader);
assertEquals(jsonProducer.getJwsJsonSignedDocument(), RSA_V1_5_JSON_FLATTENED_SERIALIZATION);
jsonConsumer = new JwsJsonConsumer(jsonProducer.getJwsJsonSignedDocument());
assertTrue(jsonConsumer.verifySignatureWith(rsaPublicKey, SignatureAlgorithm.RS256));
}
use of org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter in project cxf by apache.
the class OAuthJSONProvider method fromMapToTokenIntrospection.
private Object fromMapToTokenIntrospection(InputStream is) throws IOException {
TokenIntrospection resp = new TokenIntrospection();
Map<String, Object> params = new JsonMapObjectReaderWriter().fromJson(is);
resp.setActive((Boolean) params.get("active"));
String clientId = (String) params.get(OAuthConstants.CLIENT_ID);
if (clientId != null) {
resp.setClientId(clientId);
}
String username = (String) params.get("username");
if (username != null) {
resp.setUsername(username);
}
String scope = (String) params.get(OAuthConstants.SCOPE);
if (scope != null) {
resp.setScope(scope);
}
String tokenType = (String) params.get(OAuthConstants.ACCESS_TOKEN_TYPE);
if (tokenType != null) {
resp.setTokenType(tokenType);
}
Object aud = params.get("aud");
if (aud != null) {
if (aud.getClass() == String.class) {
resp.setAud(Collections.singletonList((String) aud));
} else {
@SuppressWarnings("unchecked") List<String> auds = (List<String>) aud;
resp.setAud(auds);
}
}
String iss = (String) params.get("iss");
if (iss != null) {
resp.setIss(iss);
}
Long iat = (Long) params.get("iat");
if (iat != null) {
resp.setIat(iat);
}
Long exp = (Long) params.get("exp");
if (exp != null) {
resp.setExp(exp);
}
Long nbf = (Long) params.get("nbf");
if (nbf != null) {
resp.setNbf(nbf);
}
Map<String, Object> cnf = CastUtils.cast((Map<?, ?>) params.get(JwtConstants.CLAIM_CONFIRMATION));
if (cnf != null) {
String thumbprint = (String) cnf.get(JoseConstants.HEADER_X509_THUMBPRINT_SHA256);
if (thumbprint != null) {
resp.getExtensions().put(JoseConstants.HEADER_X509_THUMBPRINT_SHA256, thumbprint);
}
}
return resp;
}
Aggregations