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  
20  package org.apache.wss4j.common.saml.bean;
21  
22  import java.time.Instant;
23  import java.util.Date;
24  import java.util.List;
25  
26  /**
27   * Class ConditionsBean represents a SAML Conditions object (can be used to create
28   * both SAML v1.1 and v2.0 statements)
29   */
30  public class ConditionsBean {
31      private Instant notBefore;
32      private Instant notAfter;
33      private long tokenPeriodSeconds;
34      private List<AudienceRestrictionBean> audienceRestrictions;
35      private boolean oneTimeUse;
36      private ProxyRestrictionBean proxyRestriction;
37      private List<DelegateBean> delegates;
38  
39      /**
40       * Constructor ConditionsBean creates a new ConditionsBean instance.
41       */
42      public ConditionsBean() {
43      }
44  
45      /**
46       * Constructor ConditionsBean creates a new ConditionsBean instance.
47       *
48       * @param notBefore The notBefore instance
49       * @param notAfter The notAfter instance
50       */
51      public ConditionsBean(
52          Instant notBefore,
53          Instant notAfter
54      ) {
55          if (notBefore != null) {
56              this.notBefore = Date.from(notBefore).toInstant();
57          }
58          if (notAfter != null) {
59              this.notAfter = Date.from(notAfter).toInstant();
60          }
61      }
62  
63      /**
64       * Constructor ConditionsBean creates a new ConditionsBean instance.
65       *
66       * @param tokenPeriodMinutes how long the token is valid for in minutes
67       */
68      public ConditionsBean(
69          int tokenPeriodMinutes
70      ) {
71          this.tokenPeriodSeconds = tokenPeriodMinutes * 60L;
72      }
73  
74      /**
75       * Get the notBefore instance
76       *
77       * @return the notBefore instance
78       */
79      public Instant getNotBefore() {
80          return notBefore;
81      }
82  
83      /**
84       * Set the notBefore instance
85       *
86       * @param notBefore the notBefore instance to set
87       */
88      public void setNotBefore(Instant notBefore) {
89          if (notBefore != null) {
90              this.notBefore = Date.from(notBefore).toInstant();
91          } else {
92              this.notBefore = null;
93          }
94      }
95  
96      /**
97       * Get the notAfter instance
98       *
99       * @return the notAfter instance
100      */
101     public Instant getNotAfter() {
102         return notAfter;
103     }
104 
105     /**
106      * Set the notAfter instance
107      *
108      * @param notAfter the notAfter instance to set
109      */
110     public void setNotAfter(Instant notAfter) {
111         if (notAfter != null) {
112             this.notAfter = Date.from(notAfter).toInstant();
113         } else {
114             this.notAfter = null;
115         }
116     }
117 
118     /**
119      * Get the tokenPeriodMinutes of this object.
120      *
121      * @return the tokenPeriodMinutes (type int)
122      */
123     public int getTokenPeriodMinutes() {
124         return (int)(tokenPeriodSeconds / 60L);
125     }
126 
127     /**
128      * Set the tokenPeriodMinutes.
129      *
130      * @param tokenPeriodMinutes the tokenPeriodMinutes to set
131      */
132     public void setTokenPeriodMinutes(int tokenPeriodMinutes) {
133         this.tokenPeriodSeconds = tokenPeriodMinutes * 60L;
134     }
135 
136     /**
137      * Get the tokenPeriodSeconds of this object.
138      *
139      * @return the tokenPeriodSeconds (type long)
140      */
141     public long getTokenPeriodSeconds() {
142         return tokenPeriodSeconds;
143     }
144 
145     /**
146      * Set the tokenPeriodSeconds.
147      *
148      * @param tokenPeriodSeconds the tokenPeriodSeconds to set
149      */
150     public void setTokenPeriodSeconds(long tokenPeriodSeconds) {
151         this.tokenPeriodSeconds = tokenPeriodSeconds;
152     }
153 
154     /**
155      * Get the audienceRestrictions instances
156      *
157      * @return the audienceRestrictions instances
158      */
159     public List<AudienceRestrictionBean> getAudienceRestrictions() {
160         return audienceRestrictions;
161     }
162 
163     /**
164      * Set the audienceRestrictions instance
165      *
166      * @param audienceRestrictions the audienceRestrictions instance to set
167      */
168     public void setAudienceRestrictions(List<AudienceRestrictionBean> audienceRestrictions) {
169         this.audienceRestrictions = audienceRestrictions;
170     }
171 
172     /**
173      * Get whether to include a OneTimeUse Element or not. Only applies to SAML2.
174      * @return whether to include a OneTimeUse Element or not.
175      */
176     public boolean isOneTimeUse() {
177         return oneTimeUse;
178     }
179 
180     /**
181      * Set whether to include a OneTimeUse Element or not. Only applies to SAML2.
182      * @param oneTimeUse whether to include a OneTimeUse Element or not.
183      */
184     public void setOneTimeUse(boolean oneTimeUse) {
185         this.oneTimeUse = oneTimeUse;
186     }
187 
188     public ProxyRestrictionBean getProxyRestriction() {
189         return proxyRestriction;
190     }
191 
192     public void setProxyRestriction(ProxyRestrictionBean proxyRestriction) {
193         this.proxyRestriction = proxyRestriction;
194     }
195 
196     public List<DelegateBean> getDelegates() {
197         return delegates;
198     }
199 
200     public void setDelegates(List<DelegateBean> delegates) {
201         this.delegates = delegates;
202     }
203 
204     /**
205      * Method equals ...
206      *
207      * @param o of type Object
208      * @return boolean
209      */
210     @Override
211     public boolean equals(Object o) {
212         if (this == o) {
213             return true;
214         }
215         if (!(o instanceof ConditionsBean)) {
216             return false;
217         }
218 
219         ConditionsBean that = (ConditionsBean) o;
220 
221         if (tokenPeriodSeconds != that.tokenPeriodSeconds) {
222             return false;
223         }
224 
225         if (notBefore == null && that.notBefore != null) {
226             return false;
227         } else if (notBefore != null && !notBefore.equals(that.notBefore)) {
228             return false;
229         }
230 
231         if (notAfter == null && that.notAfter != null) {
232             return false;
233         } else if (notAfter != null && !notAfter.equals(that.notAfter)) {
234             return false;
235         }
236 
237         if (audienceRestrictions == null && that.audienceRestrictions != null) {
238             return false;
239         } else if (audienceRestrictions != null
240                 && !audienceRestrictions.equals(that.audienceRestrictions)) {
241             return false;
242         }
243 
244         if (oneTimeUse != that.oneTimeUse) {
245             return false;
246         }
247 
248         if (proxyRestriction == null && that.proxyRestriction != null) {
249             return false;
250         } else if (proxyRestriction != null
251             && !proxyRestriction.equals(that.proxyRestriction)) {
252             return false;
253         }
254 
255         if (delegates == null && that.delegates != null) {
256             return false;
257         } else if (delegates != null && !delegates.equals(that.delegates)) {
258             return false;
259         }
260 
261         return true;
262     }
263 
264     /**
265      * @return the hashcode of this object
266      */
267     @Override
268     public int hashCode() {
269         int result = (int)tokenPeriodSeconds;
270         if (notBefore != null) {
271             result = 31 * result + notBefore.hashCode();
272         }
273         if (notAfter != null) {
274             result = 31 * result + notAfter.hashCode();
275         }
276         if (audienceRestrictions != null) {
277             result = 31 * result + audienceRestrictions.hashCode();
278         }
279         result = 31 * result + (oneTimeUse ? 1 : 0);
280         if (proxyRestriction != null) {
281             result = 31 * result + proxyRestriction.hashCode();
282         }
283         if (delegates != null) {
284             result = 31 * result + delegates.hashCode();
285         }
286         return result;
287     }
288 
289 }