View Javadoc

1   // ========================================================================
2   // Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.http.security;
15  
16  import java.io.Serializable;
17  
18  /* ------------------------------------------------------------ */
19  /**
20   * Describe an auth and/or data constraint.
21   * 
22   * 
23   */
24  public class Constraint implements Cloneable, Serializable
25  {
26      /* ------------------------------------------------------------ */
27      public final static String __BASIC_AUTH = "BASIC";
28  
29      public final static String __FORM_AUTH = "FORM";
30  
31      public final static String __DIGEST_AUTH = "DIGEST";
32  
33      public final static String __CERT_AUTH = "CLIENT_CERT";
34  
35      public final static String __CERT_AUTH2 = "CLIENT-CERT";
36      
37      public static boolean validateMethod (String method)
38      {
39          if (method == null)
40              return false;
41          method = method.trim();
42          return (method.equals(__FORM_AUTH) 
43                  || method.equals(__BASIC_AUTH) 
44                  || method.equals (__DIGEST_AUTH) 
45                  || method.equals (__CERT_AUTH) 
46                  || method.equals(__CERT_AUTH2));
47      }
48  
49      /* ------------------------------------------------------------ */
50      public final static int DC_UNSET = -1, DC_NONE = 0, DC_INTEGRAL = 1, DC_CONFIDENTIAL = 2, DC_FORBIDDEN = 3;
51  
52      /* ------------------------------------------------------------ */
53      public final static String NONE = "NONE";
54  
55      public final static String ANY_ROLE = "*";
56  
57      /* ------------------------------------------------------------ */
58      private String _name;
59  
60      private String[] _roles;
61  
62      private int _dataConstraint = DC_UNSET;
63  
64      private boolean _anyRole = false;
65  
66      private boolean _authenticate = false;
67  
68      /* ------------------------------------------------------------ */
69      /**
70       * Constructor.
71       */
72      public Constraint()
73      {
74      }
75  
76      /* ------------------------------------------------------------ */
77      /**
78       * Conveniance Constructor.
79       * 
80       * @param name
81       * @param role
82       */
83      public Constraint(String name, String role)
84      {
85          setName(name);
86          setRoles(new String[] { role });
87      }
88  
89      /* ------------------------------------------------------------ */
90      @Override
91      public Object clone() throws CloneNotSupportedException
92      {
93          return super.clone();
94      }
95  
96      /* ------------------------------------------------------------ */
97      /**
98       * @param name
99       */
100     public void setName(String name)
101     {
102         _name = name;
103     }
104 
105     /* ------------------------------------------------------------ */
106     public void setRoles(String[] roles)
107     {
108         _roles = roles;
109         _anyRole = false;
110         if (roles != null) 
111             for (int i = roles.length; !_anyRole && i-- > 0;)
112                 _anyRole |= ANY_ROLE.equals(roles[i]);
113     }
114 
115     /* ------------------------------------------------------------ */
116     /**
117      * @return True if any user role is permitted.
118      */
119     public boolean isAnyRole()
120     {
121         return _anyRole;
122     }
123 
124     /* ------------------------------------------------------------ */
125     /**
126      * @return List of roles for this constraint.
127      */
128     public String[] getRoles()
129     {
130         return _roles;
131     }
132 
133     /* ------------------------------------------------------------ */
134     /**
135      * @param role
136      * @return True if the constraint contains the role.
137      */
138     public boolean hasRole(String role)
139     {
140         if (_anyRole) return true;
141         if (_roles != null) for (int i = _roles.length; i-- > 0;)
142             if (role.equals(_roles[i])) return true;
143         return false;
144     }
145 
146     /* ------------------------------------------------------------ */
147     /**
148      * @param authenticate True if users must be authenticated
149      */
150     public void setAuthenticate(boolean authenticate)
151     {
152         _authenticate = authenticate;
153     }
154 
155     /* ------------------------------------------------------------ */
156     /**
157      * @return True if the constraint requires request authentication
158      */
159     public boolean getAuthenticate()
160     {
161         return _authenticate;
162     }
163 
164     /* ------------------------------------------------------------ */
165     /**
166      * @return True if authentication required but no roles set
167      */
168     public boolean isForbidden()
169     {
170         return _authenticate && !_anyRole && (_roles == null || _roles.length == 0);
171     }
172 
173     /* ------------------------------------------------------------ */
174     /**
175      * @param c Data constrain indicator: 0=DC+NONE, 1=DC_INTEGRAL &
176      *                2=DC_CONFIDENTIAL
177      */
178     public void setDataConstraint(int c)
179     {
180         if (c < 0 || c > DC_CONFIDENTIAL) throw new IllegalArgumentException("Constraint out of range");
181         _dataConstraint = c;
182     }
183 
184     /* ------------------------------------------------------------ */
185     /**
186      * @return Data constrain indicator: 0=DC+NONE, 1=DC_INTEGRAL &
187      *         2=DC_CONFIDENTIAL
188      */
189     public int getDataConstraint()
190     {
191         return _dataConstraint;
192     }
193 
194     /* ------------------------------------------------------------ */
195     /**
196      * @return True if a data constraint has been set.
197      */
198     public boolean hasDataConstraint()
199     {
200         return _dataConstraint >= DC_NONE;
201     }
202 
203     /* ------------------------------------------------------------ */
204     @Override
205     public String toString()
206     {
207         return "SC{" + _name
208                + ","
209                + (_anyRole ? "*" : (_roles == null ? "-" : _roles.toString()))
210                + ","
211                + (_dataConstraint == DC_UNSET ? "DC_UNSET}" : (_dataConstraint == DC_NONE ? "NONE}" : (_dataConstraint == DC_INTEGRAL ? "INTEGRAL}" : "CONFIDENTIAL}")));
212     }
213 
214 }