View Javadoc

1   package no.feide.mellon.jaas.principals;
2   
3   import java.util.Iterator;
4   
5   import javax.security.auth.Subject;
6   
7   import com.sun.security.auth.PrincipalComparator;
8   
9   
10  /***
11   * A special principal for the eduPersonEntitlement attribute in FEIDE
12   * <br><br>
13   * Note that no subject will have any EntitlementPrincipals, only MoriaPrincipals.
14   * An EntitlementPrincipal implies a MoriaPrincipal with <code>attributeName</code> "eduPersonEntitlement"
15   * and the sufficient attribute value. See the <code>implies</code> method.
16   * 
17   * EntitlementPrincipals are only used in the policy file. In the policy file it cannot
18   * be replaced by the corresponding MoriaPrincipal if you want to make use of wildcards. 
19   * 
20   * @author Rikke Amilde Løvlid
21   */
22  public class EntitlementPrincipal extends MoriaPrincipal implements PrincipalComparator{
23  
24  	/***
25  	 * @param name	the attribute value of the eduPersonEntitlement attribute in FEIDE,
26  	 * 				may contain wildcards.
27  	 */
28  	public EntitlementPrincipal(String name){
29  		super("eduPersonEntitlement", name);
30  	}
31  	
32  	/***
33  	 * This EntitlementPrincipal implies the Subject subject if the subject has a MoriaPrincipal
34  	 * with attributeName "eduPersonEntitlement" (this.attributeName is "eduPersonEntitlement") 
35  	 * and an attributeValue which is implied by the attributeValue belonging to this EntitlementPrincipal.
36  	 */
37  	public boolean implies(Subject subject){
38  		Iterator it = subject.getPrincipals(MoriaPrincipal.class).iterator();
39  		while(it.hasNext()){
40  			MoriaPrincipal mp = (MoriaPrincipal)it.next();
41  			if(mp.getAttributeName().equals(this.attributeName) && implies(this.attributeValue, mp.getAttributeValue())){
42  				return true;
43  			}	
44  		}
45  		return false;
46  	}
47  	
48  	/***
49  	 * @param string1
50  	 * @param string2
51  	 * 
52  	 * A wildcard, "*", in <code>string1</code> can match any substring of <code>string2</code> 
53  	 * that does not contain any ':'. The wildcards have to be between two ":".
54  	 * 
55  	 * @return whether string1 implies string 2
56  	 */
57  	public boolean implies(String string1, String string2){
58  		/***
59  		 * <code>string1</code> impies <code>string2</code> if they are equal.
60  		 */
61  		//if(string1.equals(string2) ||
62  		//		(string1.equals("*") && string2.indexOf(":")==-1)){
63  		if(string1.equals(string2)){
64  			return true;
65  		}
66  		/***
67  		 * <code>string1</code>: a:*:b <BR>
68  		 * <code>string2</code>: c:d:e <BR>
69  		 * <code>string1</code> implies <code>string2</code> if there are c,d,e so that 
70  		 * a implies c (here equivalent with a equals c), b implies e and d does not contain any ':'.
71  		 */
72  		//index1 is the position of the first "*".
73  		int index1 = string1.indexOf("*");
74  	
75  		//The "*" mentioned is the first occurence of "*" in string1.
76  		if(index1!=-1 &&
77  				//string1 starts with the "*" (string1: *:..) or 
78  				//the "*" follows a ":" and the substring befor "*" is the same in both string1 and string2 (a equals c)
79  				(index1==0 || 
80  						(string1.charAt(index1-1)==':') && string1.substring(0,index1).equals(string2.substring(0,index1))) &&
81  		        //string1 ends with "*" and part e does not exist or
82  				//the "*" is followd by a ":" and b implies e.
83  				((index1==string1.length()-1 && string2.indexOf(":", index1)==-1) ||
84  						(string1.charAt(index1+1)==':' && implies(string1.substring(index1+2), string2.substring(string2.indexOf(":", index1)+1))))){
85  			return true;
86  		}
87  		return false;
88  	}
89  }