Search in sources :

Example 1 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class MessageContextImpl method convertToAttachments.

private void convertToAttachments(Object value) {
    List<?> handlers = (List<?>) value;
    List<org.apache.cxf.message.Attachment> atts = new ArrayList<org.apache.cxf.message.Attachment>();
    for (int i = 1; i < handlers.size(); i++) {
        Attachment handler = (Attachment) handlers.get(i);
        AttachmentImpl att = new AttachmentImpl(handler.getContentId(), handler.getDataHandler());
        for (String key : handler.getHeaders().keySet()) {
            att.setHeader(key, handler.getHeader(key));
        }
        att.setXOP(false);
        atts.add(att);
    }
    Message outMessage = getOutMessage();
    outMessage.setAttachments(atts);
    outMessage.put(AttachmentOutInterceptor.WRITE_ATTACHMENTS, "true");
    Attachment root = (Attachment) handlers.get(0);
    String rootContentType = root.getContentType().toString();
    MultivaluedMap<String, String> rootHeaders = new MetadataMap<String, String>(root.getHeaders());
    if (!AttachmentUtil.isMtomEnabled(outMessage)) {
        rootHeaders.putSingle(Message.CONTENT_TYPE, rootContentType);
    }
    String messageContentType = outMessage.get(Message.CONTENT_TYPE).toString();
    int index = messageContentType.indexOf(";type");
    if (index > 0) {
        messageContentType = messageContentType.substring(0, index).trim();
    }
    AttachmentOutputInterceptor attInterceptor = new AttachmentOutputInterceptor(messageContentType, rootHeaders);
    outMessage.put(Message.CONTENT_TYPE, rootContentType);
    Map<String, List<String>> allHeaders = CastUtils.cast((Map<?, ?>) outMessage.get(Message.PROTOCOL_HEADERS));
    if (allHeaders != null) {
        allHeaders.remove(Message.CONTENT_TYPE);
    }
    attInterceptor.handleMessage(outMessage);
}
Also used : Message(org.apache.cxf.message.Message) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) Endpoint(org.apache.cxf.endpoint.Endpoint) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) AttachmentOutputInterceptor(org.apache.cxf.jaxrs.interceptor.AttachmentOutputInterceptor) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl)

Example 2 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class OAuthUtils method checkRequestURI.

public static boolean checkRequestURI(String servletPath, String uri) {
    boolean wildcard = uri.endsWith("*");
    String theURI = wildcard ? uri.substring(0, uri.length() - 1) : uri;
    try {
        URITemplate template = new URITemplate(theURI);
        MultivaluedMap<String, String> map = new MetadataMap<String, String>();
        if (template.match(servletPath, map)) {
            String finalGroup = map.getFirst(URITemplate.FINAL_MATCH_GROUP);
            if (wildcard || StringUtils.isEmpty(finalGroup) || "/".equals(finalGroup)) {
                return true;
            }
        }
    } catch (Exception ex) {
    // ignore
    }
    return false;
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) OAuthProblemException(net.oauth.OAuthProblemException) IOException(java.io.IOException)

Example 3 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class JoseClientCodeStateManager method toRedirectState.

@Override
public MultivaluedMap<String, String> toRedirectState(MessageContext mc, MultivaluedMap<String, String> requestState) {
    JweEncryptionProvider theEncryptionProvider = getInitializedEncryptionProvider();
    JwsSignatureProvider theSigProvider = getInitializedSigProvider(theEncryptionProvider);
    if (theEncryptionProvider == null && theSigProvider == null) {
        throw new OAuthServiceException("The state can not be protected");
    }
    MultivaluedMap<String, String> redirectMap = new MetadataMap<String, String>();
    if (generateNonce && theSigProvider != null) {
        JwsCompactProducer nonceProducer = new JwsCompactProducer(OAuthUtils.generateRandomTokenKey());
        String nonceParam = nonceProducer.signWith(theSigProvider);
        requestState.putSingle(OAuthConstants.NONCE, nonceParam);
        redirectMap.putSingle(OAuthConstants.NONCE, nonceParam);
    }
    Map<String, Object> stateMap = CastUtils.cast((Map<?, ?>) requestState);
    String json = jsonp.toJson(stateMap);
    String stateParam = null;
    if (theSigProvider != null) {
        JwsCompactProducer stateProducer = new JwsCompactProducer(json);
        stateParam = stateProducer.signWith(theSigProvider);
    }
    if (theEncryptionProvider != null) {
        stateParam = theEncryptionProvider.encrypt(StringUtils.toBytesUTF8(stateParam), null);
    }
    if (storeInSession) {
        String sessionStateAttribute = OAuthUtils.generateRandomTokenKey();
        OAuthUtils.setSessionToken(mc, stateParam, sessionStateAttribute, 0);
        stateParam = sessionStateAttribute;
    }
    redirectMap.putSingle(OAuthConstants.STATE, stateParam);
    return redirectMap;
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) JweEncryptionProvider(org.apache.cxf.rs.security.jose.jwe.JweEncryptionProvider) OAuthServiceException(org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException) JwsCompactProducer(org.apache.cxf.rs.security.jose.jws.JwsCompactProducer) JwsSignatureProvider(org.apache.cxf.rs.security.jose.jws.JwsSignatureProvider) NoneJwsSignatureProvider(org.apache.cxf.rs.security.jose.jws.NoneJwsSignatureProvider)

Example 4 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class AccessTokenValidatorClient method validateAccessToken.

public AccessTokenValidation validateAccessToken(MessageContext mc, String authScheme, String authSchemeData, MultivaluedMap<String, String> extraProps) throws OAuthServiceException {
    WebClient client = WebClient.fromClient(tokenValidatorClient, true);
    MultivaluedMap<String, String> props = new MetadataMap<String, String>();
    props.putSingle(OAuthConstants.AUTHORIZATION_SCHEME_TYPE, authScheme);
    props.putSingle(OAuthConstants.AUTHORIZATION_SCHEME_DATA, authSchemeData);
    if (extraProps != null) {
        props.putAll(extraProps);
    }
    try {
        return client.post(props, AccessTokenValidation.class);
    } catch (WebApplicationException ex) {
        throw new OAuthServiceException(ex);
    }
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) WebApplicationException(javax.ws.rs.WebApplicationException) OAuthServiceException(org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 5 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class JwtRequestCodeFilter method process.

@Override
public MultivaluedMap<String, String> process(MultivaluedMap<String, String> params, UserSubject endUser, Client client) {
    String requestToken = params.getFirst(REQUEST_PARAM);
    if (requestToken == null) {
        String requestUri = params.getFirst(REQUEST_URI_PARAM);
        if (isRequestUriValid(client, requestUri)) {
            requestToken = WebClient.create(requestUri).get(String.class);
        }
    }
    if (requestToken != null) {
        JweDecryptionProvider theDecryptor = super.getInitializedDecryptionProvider(client.getClientSecret());
        JwsSignatureVerifier theSigVerifier = getInitializedSigVerifier(client);
        JwtToken jwt = getJwtToken(requestToken, theDecryptor, theSigVerifier);
        JwtClaims claims = jwt.getClaims();
        // Check issuer
        String iss = issuer != null ? issuer : client.getClientId();
        if (!iss.equals(claims.getIssuer())) {
            throw new SecurityException();
        }
        // Check client_id - if present it must match the client_id specified in the request
        if (claims.getClaim(OAuthConstants.CLIENT_ID) != null && !claims.getStringProperty(OAuthConstants.CLIENT_ID).equals(client.getClientId())) {
            throw new SecurityException();
        }
        // Check response_type - if present it must match the response_type specified in the request
        String tokenResponseType = (String) claims.getClaim(OAuthConstants.RESPONSE_TYPE);
        if (tokenResponseType != null && !tokenResponseType.equals(params.getFirst(OAuthConstants.RESPONSE_TYPE))) {
            throw new SecurityException();
        }
        MultivaluedMap<String, String> newParams = new MetadataMap<String, String>(params);
        Map<String, Object> claimsMap = claims.asMap();
        for (Map.Entry<String, Object> entry : claimsMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof Map) {
                Map<String, Object> map = CastUtils.cast((Map<?, ?>) value);
                value = jsonHandler.toJson(map);
            } else if (value instanceof List) {
                List<Object> list = CastUtils.cast((List<?>) value);
                value = jsonHandler.toJson(list);
            }
            newParams.putSingle(key, value.toString());
        }
        return newParams;
    }
    return params;
}
Also used : JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) JwsSignatureVerifier(org.apache.cxf.rs.security.jose.jws.JwsSignatureVerifier) JwtToken(org.apache.cxf.rs.security.jose.jwt.JwtToken) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) JweDecryptionProvider(org.apache.cxf.rs.security.jose.jwe.JweDecryptionProvider) List(java.util.List) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Aggregations

MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)80 Test (org.junit.Test)43 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)36 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)34 ByteArrayInputStream (java.io.ByteArrayInputStream)25 Message (org.apache.cxf.message.Message)25 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)15 List (java.util.List)13 Method (java.lang.reflect.Method)12 ArrayList (java.util.ArrayList)11 Map (java.util.Map)10 Endpoint (org.apache.cxf.endpoint.Endpoint)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 LinkedHashMap (java.util.LinkedHashMap)9 Customer (org.apache.cxf.jaxrs.Customer)9 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 Annotation (java.lang.annotation.Annotation)8 HashMap (java.util.HashMap)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 URITemplate (org.apache.cxf.jaxrs.model.URITemplate)7