View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.wss4j.policy.stax.assertionStates;
20  
21  import org.apache.wss4j.common.WSSPolicyException;
22  import org.apache.wss4j.policy.model.AbstractSecurityAssertion;
23  import org.apache.wss4j.policy.model.AbstractToken;
24  import org.apache.wss4j.policy.model.RelToken;
25  import org.apache.wss4j.policy.stax.PolicyAsserter;
26  import org.apache.xml.security.stax.securityEvent.SecurityEventConstants;
27  import org.apache.xml.security.stax.securityEvent.TokenSecurityEvent;
28  import org.apache.xml.security.stax.securityToken.SecurityToken;
29  import org.apache.wss4j.stax.securityEvent.RelTokenSecurityEvent;
30  import org.apache.wss4j.stax.securityEvent.WSSecurityEventConstants;
31  
32  /**
33   * WSP1.3, 5.4.9 RelToken Assertion
34   */
35  
36  public class RelTokenAssertionState extends TokenAssertionState {
37  
38      public RelTokenAssertionState(AbstractSecurityAssertion assertion, boolean asserted,
39                                    PolicyAsserter policyAsserter, boolean initiator) {
40          super(assertion, asserted, policyAsserter, initiator);
41      }
42  
43      @Override
44      public SecurityEventConstants.Event[] getSecurityEventType() {
45          return new SecurityEventConstants.Event[]{
46                  WSSecurityEventConstants.REL_TOKEN
47          };
48      }
49  
50      @Override
51      public boolean assertToken(TokenSecurityEvent<? extends SecurityToken> tokenSecurityEvent,
52                                 AbstractToken abstractToken) throws WSSPolicyException {
53          if (!(tokenSecurityEvent instanceof RelTokenSecurityEvent)) {
54              throw new WSSPolicyException("Expected a RelTokenSecurityEvent but got " + tokenSecurityEvent.getClass().getName());
55          }
56  
57          RelTokenSecurityEvent relTokenSecurityEvent = (RelTokenSecurityEvent) tokenSecurityEvent;
58          RelToken relToken = (RelToken) abstractToken;
59  
60          if (relToken.getIssuerName() != null && !relToken.getIssuerName().equals(relTokenSecurityEvent.getIssuerName())) {
61              setErrorMessage("IssuerName in Policy (" + relToken.getIssuerName() + ") didn't match with the one in the RelToken ("
62                  + relTokenSecurityEvent.getIssuerName() + ")");
63              getPolicyAsserter().unassertPolicy(getAssertion(), getErrorMessage());
64              return false;
65          }
66  
67          //todo RequireKeyIdentifierReference
68          //todo WssRelV*
69          //always return true to prevent false alarm in case additional tokens with the same usage
70          //appears in the message but do not fulfill the policy and are also not needed to fulfil the policy.
71          getPolicyAsserter().assertPolicy(getAssertion());
72          return true;
73      }
74  }