View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.common.binding.security;
18  
19  import org.joda.time.DateTime;
20  import org.opensaml.common.binding.SAMLMessageContext;
21  import org.opensaml.ws.message.MessageContext;
22  import org.opensaml.ws.security.SecurityPolicyException;
23  import org.opensaml.ws.security.SecurityPolicyRule;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * Security policy rule implementation that checks for validity of SAML message issue instant date and time.
29   */
30  public class IssueInstantRule implements SecurityPolicyRule {
31  
32      /** Class logger. */
33      private final Logger log = LoggerFactory.getLogger(IssueInstantRule.class);
34  
35      /**
36       * Clock skew - the number of seconds before a lower time bound, or after an upper time bound, to consider still
37       * acceptable.
38       */
39      private int clockSkew;
40  
41      /** Number of seconds after a message issue instant after which the message is considered expired. */
42      private int expires;
43      
44      /** Whether this rule is required to be met. */
45      private boolean requiredRule;
46  
47      /**
48       * Constructor.
49       * 
50       * @param newClockSkew the new clock skew value
51       * @param newExpires the new expiration value
52       */
53      public IssueInstantRule(int newClockSkew, int newExpires) {
54          clockSkew = newClockSkew;
55          expires = newExpires;
56          requiredRule = true;
57      }
58      
59      /**
60       * Gets whether this rule is required to be met.
61       * 
62       * @return whether this rule is required to be met
63       */
64      public boolean isRequiredRule() {
65          return requiredRule;
66      }
67      
68      /**
69       * Sets whether this rule is required to be met.
70       * 
71       * @param required whether this rule is required to be met
72       */
73      public void setRequiredRule(boolean required) {
74          requiredRule = required;
75      }
76  
77      /** {@inheritDoc} */
78      public void evaluate(MessageContext messageContext) throws SecurityPolicyException {
79          if (!(messageContext instanceof SAMLMessageContext)) {
80              log.debug("Invalid message context type, this policy rule only supports SAMLMessageContext");
81              return;
82          }
83          SAMLMessageContext samlMsgCtx = (SAMLMessageContext) messageContext;
84  
85          if (samlMsgCtx.getInboundSAMLMessageIssueInstant() == null) {
86              if(requiredRule){
87                  log.warn("Inbound SAML message issue instant not present in message context");
88                  throw new SecurityPolicyException("Inbound SAML message issue instant not present in message context");
89              }else{
90                  return;
91              }
92          }
93  
94          DateTime issueInstant = samlMsgCtx.getInboundSAMLMessageIssueInstant();
95          DateTime now = new DateTime();
96          DateTime latestValid = now.plusSeconds(clockSkew);
97          DateTime expiration = issueInstant.plusSeconds(clockSkew + expires);
98  
99          // Check message wasn't issued in the future
100         if (issueInstant.isAfter(latestValid)) {
101             log.warn("Message was not yet valid: message time was {}, latest valid is: {}", issueInstant, latestValid);
102             throw new SecurityPolicyException("Message was rejected because was issued in the future");
103         }
104 
105         // Check message has not expired
106         if (expiration.isBefore(now)) {
107             log.warn("Message was expired: message issue time was '" + issueInstant + "', message expired at: '"
108                     + expiration + "', current time: '" + now + "'");
109             throw new SecurityPolicyException("Message was rejected due to issue instant expiration");
110         }
111 
112     }
113 }