Search in sources :

Example 11 with JsonMapObjectReaderWriter

use of org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter in project cxf by apache.

the class SwaggerToOpenApiConversionUtilsTest method testConvertFromSwaggerToOpenApi.

@Test
public void testConvertFromSwaggerToOpenApi() {
    OpenApiConfiguration cfg = new OpenApiConfiguration();
    String s = SwaggerToOpenApiConversionUtils.getOpenApiFromSwaggerLoc("/swagger2petShop.json");
    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);
}
Also used : JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter) JsonMapObject(org.apache.cxf.jaxrs.json.basic.JsonMapObject) Test(org.junit.Test)

Example 12 with JsonMapObjectReaderWriter

use of org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter in project cxf by apache.

the class JwsMultipartSignatureInFilter method filter.

@Override
public void filter(List<Attachment> atts) {
    if (atts.size() < 2) {
        throw ExceptionUtils.toBadRequestException(null, null);
    }
    Attachment sigPart = atts.remove(atts.size() - 1);
    String jwsSequence = null;
    try {
        jwsSequence = IOUtils.readStringFromStream(sigPart.getDataHandler().getInputStream());
    } catch (IOException ex) {
        throw ExceptionUtils.toBadRequestException(null, null);
    }
    String base64UrlEncodedHeaders = null;
    String base64UrlEncodedSignature = null;
    if (!useJwsJsonSignatureFormat) {
        String[] parts = JoseUtils.getCompactParts(jwsSequence);
        if (parts.length != 3 || parts[1].length() > 0) {
            throw ExceptionUtils.toBadRequestException(null, null);
        }
        base64UrlEncodedHeaders = parts[0];
        base64UrlEncodedSignature = parts[2];
    } else {
        Map<String, Object> parts = reader.fromJson(jwsSequence);
        if (parts.size() != 2 || !parts.containsKey("protected") || !parts.containsKey("signature")) {
            throw ExceptionUtils.toBadRequestException(null, null);
        }
        base64UrlEncodedHeaders = (String) parts.get("protected");
        base64UrlEncodedSignature = (String) parts.get("signature");
    }
    JwsHeaders headers = new JwsHeaders(new JsonMapObjectReaderWriter().fromJson(JoseUtils.decodeToString(base64UrlEncodedHeaders)));
    JoseUtils.traceHeaders(headers);
    if (Boolean.FALSE != headers.getPayloadEncodingStatus()) {
        throw ExceptionUtils.toBadRequestException(null, null);
    }
    JwsSignatureVerifier theVerifier = null;
    if (verifier == null) {
        Properties props = KeyManagementUtils.loadStoreProperties(message, true, JoseConstants.RSSEC_SIGNATURE_IN_PROPS, JoseConstants.RSSEC_SIGNATURE_PROPS);
        theVerifier = JwsUtils.loadSignatureVerifier(message, props, headers);
    } else {
        theVerifier = verifier;
    }
    JwsVerificationSignature sig = theVerifier.createJwsVerificationSignature(headers);
    if (sig == null) {
        throw ExceptionUtils.toBadRequestException(null, null);
    }
    byte[] signatureBytes = JoseUtils.decode(base64UrlEncodedSignature);
    byte[] headerBytesWithDot = StringUtils.toBytesASCII(base64UrlEncodedHeaders + ".");
    sig.update(headerBytesWithDot, 0, headerBytesWithDot.length);
    int attSize = atts.size();
    for (int i = 0; i < attSize; i++) {
        Attachment dataPart = atts.remove(i);
        InputStream dataPartStream = null;
        try {
            dataPartStream = dataPart.getDataHandler().getDataSource().getInputStream();
        } catch (IOException ex) {
            throw ExceptionUtils.toBadRequestException(ex, null);
        }
        boolean verifyOnLastRead = i == attSize - 1 ? true : false;
        JwsInputStream jwsStream = new JwsInputStream(dataPartStream, sig, signatureBytes, verifyOnLastRead);
        InputStream newStream = null;
        if (bufferPayload) {
            CachedOutputStream cos = new CachedOutputStream();
            try {
                IOUtils.copy(jwsStream, cos);
                newStream = cos.getInputStream();
            } catch (Exception ex) {
                throw ExceptionUtils.toBadRequestException(ex, null);
            }
        } else {
            newStream = jwsStream;
        }
        Attachment newDataPart = new Attachment(newStream, dataPart.getHeaders());
        atts.add(i, newDataPart);
    }
}
Also used : JwsInputStream(org.apache.cxf.rs.security.jose.jws.JwsInputStream) InputStream(java.io.InputStream) JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) JwsSignatureVerifier(org.apache.cxf.rs.security.jose.jws.JwsSignatureVerifier) JwsHeaders(org.apache.cxf.rs.security.jose.jws.JwsHeaders) JwsVerificationSignature(org.apache.cxf.rs.security.jose.jws.JwsVerificationSignature) JwsInputStream(org.apache.cxf.rs.security.jose.jws.JwsInputStream)

Example 13 with JsonMapObjectReaderWriter

use of org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter in project cxf by apache.

the class JoseUtils method traceHeaders.

public static void traceHeaders(JoseHeaders headers) {
    Message m = PhaseInterceptorChain.getCurrentMessage();
    if (MessageUtils.getContextualBoolean(m, JoseConstants.JOSE_DEBUG, false)) {
        JsonMapObjectReaderWriter writer = new JsonMapObjectReaderWriter(true);
        String thePrefix = headers instanceof JwsHeaders ? "JWS" : headers instanceof JweHeaders ? "JWE" : "JOSE";
        LOG.info(thePrefix + " Headers: \r\n" + writer.toJson(headers));
    }
}
Also used : JwsHeaders(org.apache.cxf.rs.security.jose.jws.JwsHeaders) Message(org.apache.cxf.message.Message) JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter) JweHeaders(org.apache.cxf.rs.security.jose.jwe.JweHeaders)

Example 14 with JsonMapObjectReaderWriter

use of org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter in project cxf by apache.

the class JweJsonEncryptionEntry method toJson.

public String toJson() {
    JsonMapObjectReaderWriter jsonWriter = new JsonMapObjectReaderWriter();
    Map<String, Object> recipientsEntry = new LinkedHashMap<String, Object>();
    if (unprotectedHeader != null && !unprotectedHeader.asMap().isEmpty()) {
        recipientsEntry.put("header", this.unprotectedHeader);
    }
    if (encodedEncryptedKey != null) {
        recipientsEntry.put("encrypted_key", this.encodedEncryptedKey);
    }
    return jsonWriter.toJson(recipientsEntry);
}
Also used : JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter) JsonObject(org.apache.cxf.jaxrs.json.basic.JsonObject) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with JsonMapObjectReaderWriter

use of org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter in project cxf by apache.

the class JwkUtils method getThumbprint.

public static String getThumbprint(JsonWebKey key) {
    List<String> fields = getRequiredFields(key.getKeyType());
    JsonWebKey thumbprintKey = new JsonWebKey();
    for (String f : fields) {
        thumbprintKey.setProperty(f, key.getProperty(f));
    }
    String json = new JsonMapObjectReaderWriter().toJson(thumbprintKey);
    byte[] digest = MessageDigestUtils.createDigest(json, MessageDigestUtils.ALGO_SHA_256);
    return Base64UrlUtility.encode(digest);
}
Also used : JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter)

Aggregations

JsonMapObjectReaderWriter (org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter)20 Test (org.junit.Test)9 JwsHeaders (org.apache.cxf.rs.security.jose.jws.JwsHeaders)7 JsonWebKey (org.apache.cxf.rs.security.jose.jwk.JsonWebKey)6 JsonWebKeys (org.apache.cxf.rs.security.jose.jwk.JsonWebKeys)6 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 JsonMapObject (org.apache.cxf.jaxrs.json.basic.JsonMapObject)5 JwsCompactConsumer (org.apache.cxf.rs.security.jose.jws.JwsCompactConsumer)5 JwsCompactProducer (org.apache.cxf.rs.security.jose.jws.JwsCompactProducer)5 JwsJsonConsumer (org.apache.cxf.rs.security.jose.jws.JwsJsonConsumer)5 JwsJsonProducer (org.apache.cxf.rs.security.jose.jws.JwsJsonProducer)5 List (java.util.List)3 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Parameter (org.apache.cxf.jaxrs.model.Parameter)2