Search in sources :

Example 1 with LifetimeType

use of org.apache.cxf.ws.security.sts.provider.model.LifetimeType in project cxf by apache.

the class AbstractOperation method createLifetime.

/**
 * Create a LifetimeType object given a created + expires Dates
 */
protected static LifetimeType createLifetime(Instant tokenCreated, Instant tokenExpires) {
    AttributedDateTime created = QNameConstants.UTIL_FACTORY.createAttributedDateTime();
    AttributedDateTime expires = QNameConstants.UTIL_FACTORY.createAttributedDateTime();
    Instant now = Instant.now();
    Instant creationTime = tokenCreated;
    if (tokenCreated == null) {
        creationTime = now;
    }
    Instant expirationTime = tokenExpires;
    if (tokenExpires == null) {
        long lifeTimeOfToken = 300L;
        expirationTime = now.plusSeconds(lifeTimeOfToken);
    }
    created.setValue(creationTime.atZone(ZoneOffset.UTC).format(DateUtil.getDateTimeFormatter(true)));
    expires.setValue(expirationTime.atZone(ZoneOffset.UTC).format(DateUtil.getDateTimeFormatter(true)));
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Token lifetime creation: " + created.getValue());
        LOG.fine("Token lifetime expiration: " + expires.getValue());
    }
    LifetimeType lifetimeType = QNameConstants.WS_TRUST_FACTORY.createLifetimeType();
    lifetimeType.setCreated(created);
    lifetimeType.setExpires(expires);
    return lifetimeType;
}
Also used : Instant(java.time.Instant) LifetimeType(org.apache.cxf.ws.security.sts.provider.model.LifetimeType) AttributedDateTime(org.apache.cxf.ws.security.sts.provider.model.utility.AttributedDateTime)

Example 2 with LifetimeType

use of org.apache.cxf.ws.security.sts.provider.model.LifetimeType in project cxf by apache.

the class TokenRenewOperation method createResponse.

protected RequestSecurityTokenResponseType createResponse(EncryptionProperties encryptionProperties, TokenRenewerResponse tokenRenewerResponse, TokenRequirements tokenRequirements, KeyRequirements keyRequirements) throws WSSecurityException {
    RequestSecurityTokenResponseType response = QNameConstants.WS_TRUST_FACTORY.createRequestSecurityTokenResponseType();
    String context = tokenRequirements.getContext();
    if (context != null) {
        response.setContext(context);
    }
    // TokenType
    JAXBElement<String> jaxbTokenType = QNameConstants.WS_TRUST_FACTORY.createTokenType(tokenRequirements.getTokenType());
    response.getAny().add(jaxbTokenType);
    // RequestedSecurityToken
    RequestedSecurityTokenType requestedTokenType = QNameConstants.WS_TRUST_FACTORY.createRequestedSecurityTokenType();
    JAXBElement<RequestedSecurityTokenType> requestedToken = QNameConstants.WS_TRUST_FACTORY.createRequestedSecurityToken(requestedTokenType);
    LOG.fine("Encrypting Issued Token: " + encryptIssuedToken);
    requestedTokenType.setAny(tokenRenewerResponse.getToken());
    response.getAny().add(requestedToken);
    if (returnReferences) {
        // RequestedAttachedReference
        TokenReference attachedReference = tokenRenewerResponse.getAttachedReference();
        RequestedReferenceType requestedAttachedReferenceType = null;
        if (attachedReference != null) {
            requestedAttachedReferenceType = createRequestedReference(attachedReference, true);
        } else {
            requestedAttachedReferenceType = createRequestedReference(tokenRenewerResponse.getTokenId(), tokenRequirements.getTokenType(), true);
        }
        JAXBElement<RequestedReferenceType> requestedAttachedReference = QNameConstants.WS_TRUST_FACTORY.createRequestedAttachedReference(requestedAttachedReferenceType);
        response.getAny().add(requestedAttachedReference);
        // RequestedUnattachedReference
        TokenReference unAttachedReference = tokenRenewerResponse.getUnAttachedReference();
        RequestedReferenceType requestedUnattachedReferenceType = null;
        if (unAttachedReference != null) {
            requestedUnattachedReferenceType = createRequestedReference(unAttachedReference, false);
        } else {
            requestedUnattachedReferenceType = createRequestedReference(tokenRenewerResponse.getTokenId(), tokenRequirements.getTokenType(), false);
        }
        JAXBElement<RequestedReferenceType> requestedUnattachedReference = QNameConstants.WS_TRUST_FACTORY.createRequestedUnattachedReference(requestedUnattachedReferenceType);
        response.getAny().add(requestedUnattachedReference);
    }
    // AppliesTo
    response.getAny().add(tokenRequirements.getAppliesTo());
    // Lifetime
    if (includeLifetimeElement) {
        LifetimeType lifetime = createLifetime(tokenRenewerResponse.getCreated(), tokenRenewerResponse.getExpires());
        JAXBElement<LifetimeType> lifetimeType = QNameConstants.WS_TRUST_FACTORY.createLifetime(lifetime);
        response.getAny().add(lifetimeType);
    }
    return response;
}
Also used : RequestedReferenceType(org.apache.cxf.ws.security.sts.provider.model.RequestedReferenceType) LifetimeType(org.apache.cxf.ws.security.sts.provider.model.LifetimeType) RequestSecurityTokenResponseType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType) RequestedSecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType) TokenReference(org.apache.cxf.sts.token.provider.TokenReference)

Example 3 with LifetimeType

use of org.apache.cxf.ws.security.sts.provider.model.LifetimeType in project cxf by apache.

the class TokenValidateOperation method createResponse.

protected RequestSecurityTokenResponseType createResponse(TokenValidatorResponse tokenResponse, TokenProviderResponse tokenProviderResponse, TokenRequirements tokenRequirements) throws WSSecurityException {
    RequestSecurityTokenResponseType response = QNameConstants.WS_TRUST_FACTORY.createRequestSecurityTokenResponseType();
    String context = tokenRequirements.getContext();
    if (context != null) {
        response.setContext(context);
    }
    // TokenType
    boolean valid = tokenResponse.getToken().getState() == STATE.VALID;
    String tokenType = tokenRequirements.getTokenType();
    if (valid || STSConstants.STATUS.equals(tokenType)) {
        JAXBElement<String> jaxbTokenType = QNameConstants.WS_TRUST_FACTORY.createTokenType(tokenType);
        response.getAny().add(jaxbTokenType);
    }
    // Status
    StatusType statusType = QNameConstants.WS_TRUST_FACTORY.createStatusType();
    if (valid) {
        statusType.setCode(STSConstants.VALID_CODE);
        statusType.setReason(STSConstants.VALID_REASON);
    } else {
        statusType.setCode(STSConstants.INVALID_CODE);
        statusType.setReason(STSConstants.INVALID_REASON);
    }
    JAXBElement<StatusType> status = QNameConstants.WS_TRUST_FACTORY.createStatus(statusType);
    response.getAny().add(status);
    // RequestedSecurityToken
    if (valid && !STSConstants.STATUS.equals(tokenType) && tokenProviderResponse != null && tokenProviderResponse.getToken() != null) {
        RequestedSecurityTokenType requestedTokenType = QNameConstants.WS_TRUST_FACTORY.createRequestedSecurityTokenType();
        JAXBElement<RequestedSecurityTokenType> requestedToken = QNameConstants.WS_TRUST_FACTORY.createRequestedSecurityToken(requestedTokenType);
        tokenWrapper.wrapToken(tokenProviderResponse.getToken(), requestedTokenType);
        response.getAny().add(requestedToken);
        // Lifetime
        if (includeLifetimeElement) {
            LifetimeType lifetime = createLifetime(tokenProviderResponse.getCreated(), tokenProviderResponse.getExpires());
            JAXBElement<LifetimeType> lifetimeType = QNameConstants.WS_TRUST_FACTORY.createLifetime(lifetime);
            response.getAny().add(lifetimeType);
        }
        if (returnReferences) {
            // RequestedAttachedReference
            TokenReference attachedReference = tokenProviderResponse.getAttachedReference();
            RequestedReferenceType requestedAttachedReferenceType = null;
            if (attachedReference != null) {
                requestedAttachedReferenceType = createRequestedReference(attachedReference, true);
            } else {
                requestedAttachedReferenceType = createRequestedReference(tokenProviderResponse.getTokenId(), tokenRequirements.getTokenType(), true);
            }
            JAXBElement<RequestedReferenceType> requestedAttachedReference = QNameConstants.WS_TRUST_FACTORY.createRequestedAttachedReference(requestedAttachedReferenceType);
            response.getAny().add(requestedAttachedReference);
            // RequestedUnattachedReference
            TokenReference unAttachedReference = tokenProviderResponse.getUnAttachedReference();
            RequestedReferenceType requestedUnattachedReferenceType = null;
            if (unAttachedReference != null) {
                requestedUnattachedReferenceType = createRequestedReference(unAttachedReference, false);
            } else {
                requestedUnattachedReferenceType = createRequestedReference(tokenProviderResponse.getTokenId(), tokenRequirements.getTokenType(), false);
            }
            JAXBElement<RequestedReferenceType> requestedUnattachedReference = QNameConstants.WS_TRUST_FACTORY.createRequestedUnattachedReference(requestedUnattachedReferenceType);
            response.getAny().add(requestedUnattachedReference);
        }
    }
    return response;
}
Also used : RequestedReferenceType(org.apache.cxf.ws.security.sts.provider.model.RequestedReferenceType) RequestSecurityTokenResponseType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType) RequestedSecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType) StatusType(org.apache.cxf.ws.security.sts.provider.model.StatusType) LifetimeType(org.apache.cxf.ws.security.sts.provider.model.LifetimeType) TokenReference(org.apache.cxf.sts.token.provider.TokenReference)

Example 4 with LifetimeType

use of org.apache.cxf.ws.security.sts.provider.model.LifetimeType in project cxf by apache.

the class IssueUnitTest method createLifetime.

/**
 * Create a LifetimeType object given a lifetime in seconds
 */
private LifetimeType createLifetime(long lifetime) {
    AttributedDateTime created = QNameConstants.UTIL_FACTORY.createAttributedDateTime();
    AttributedDateTime expires = QNameConstants.UTIL_FACTORY.createAttributedDateTime();
    if (lifetime <= 0) {
        lifetime = 300L;
    }
    Instant creationTime = Instant.now();
    Instant expirationTime = creationTime.plusSeconds(lifetime);
    created.setValue(creationTime.atZone(ZoneOffset.UTC).format(DateUtil.getDateTimeFormatter(true)));
    expires.setValue(expirationTime.atZone(ZoneOffset.UTC).format(DateUtil.getDateTimeFormatter(true)));
    LifetimeType lifetimeType = QNameConstants.WS_TRUST_FACTORY.createLifetimeType();
    lifetimeType.setCreated(created);
    lifetimeType.setExpires(expires);
    return lifetimeType;
}
Also used : Instant(java.time.Instant) LifetimeType(org.apache.cxf.ws.security.sts.provider.model.LifetimeType) AttributedDateTime(org.apache.cxf.ws.security.sts.provider.model.utility.AttributedDateTime)

Example 5 with LifetimeType

use of org.apache.cxf.ws.security.sts.provider.model.LifetimeType in project cxf by apache.

the class IssueUnitTest method testLifetime.

/**
 * Test to successfully issue a (dummy) token with a supplied lifetime. It only tests that
 * the lifetime can be successfully processed by the RequestParser for now.
 */
@org.junit.Test
public void testLifetime() throws Exception {
    TokenIssueOperation issueOperation = new TokenIssueOperation();
    // Add Token Provider
    List<TokenProvider> providerList = new ArrayList<>();
    providerList.add(new DummyTokenProvider());
    issueOperation.setTokenProviders(providerList);
    // Add Service
    ServiceMBean service = new StaticService();
    service.setEndpoints(Collections.singletonList("http://dummy-service.com/dummy"));
    issueOperation.setServices(Collections.singletonList(service));
    // Add STSProperties object
    STSPropertiesMBean stsProperties = new StaticSTSProperties();
    issueOperation.setStsProperties(stsProperties);
    // Mock up a request
    RequestSecurityTokenType request = new RequestSecurityTokenType();
    JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, DummyTokenProvider.TOKEN_TYPE);
    request.getAny().add(tokenType);
    request.getAny().add(createAppliesToElement("http://dummy-service.com/dummy"));
    LifetimeType lifetime = createLifetime(300L * 5L);
    JAXBElement<LifetimeType> lifetimeJaxb = new JAXBElement<LifetimeType>(QNameConstants.LIFETIME, LifetimeType.class, lifetime);
    request.getAny().add(lifetimeJaxb);
    // Mock up message context
    MessageImpl msg = new MessageImpl();
    WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
    // Issue a token
    RequestSecurityTokenResponseCollectionType response = issueOperation.issue(request, null, msgCtx);
    List<RequestSecurityTokenResponseType> securityTokenResponse = response.getRequestSecurityTokenResponse();
    assertTrue(!securityTokenResponse.isEmpty());
}
Also used : ServiceMBean(org.apache.cxf.sts.service.ServiceMBean) RequestSecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType) ArrayList(java.util.ArrayList) RequestSecurityTokenResponseType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType) StaticSTSProperties(org.apache.cxf.sts.StaticSTSProperties) JAXBElement(javax.xml.bind.JAXBElement) StaticService(org.apache.cxf.sts.service.StaticService) RequestSecurityTokenResponseCollectionType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseCollectionType) TokenProvider(org.apache.cxf.sts.token.provider.TokenProvider) STSPropertiesMBean(org.apache.cxf.sts.STSPropertiesMBean) LifetimeType(org.apache.cxf.ws.security.sts.provider.model.LifetimeType) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl)

Aggregations

LifetimeType (org.apache.cxf.ws.security.sts.provider.model.LifetimeType)7 RequestSecurityTokenResponseType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType)4 JAXBElement (javax.xml.bind.JAXBElement)3 TokenReference (org.apache.cxf.sts.token.provider.TokenReference)3 RequestedReferenceType (org.apache.cxf.ws.security.sts.provider.model.RequestedReferenceType)3 RequestedSecurityTokenType (org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType)3 Instant (java.time.Instant)2 AttributedDateTime (org.apache.cxf.ws.security.sts.provider.model.utility.AttributedDateTime)2 ArrayList (java.util.ArrayList)1 WrappedMessageContext (org.apache.cxf.jaxws.context.WrappedMessageContext)1 MessageImpl (org.apache.cxf.message.MessageImpl)1 ClaimCollection (org.apache.cxf.rt.security.claims.ClaimCollection)1 STSPropertiesMBean (org.apache.cxf.sts.STSPropertiesMBean)1 StaticSTSProperties (org.apache.cxf.sts.StaticSTSProperties)1 ServiceMBean (org.apache.cxf.sts.service.ServiceMBean)1 StaticService (org.apache.cxf.sts.service.StaticService)1 TokenProvider (org.apache.cxf.sts.token.provider.TokenProvider)1 CancelTargetType (org.apache.cxf.ws.security.sts.provider.model.CancelTargetType)1 ClaimsType (org.apache.cxf.ws.security.sts.provider.model.ClaimsType)1 EntropyType (org.apache.cxf.ws.security.sts.provider.model.EntropyType)1