RoleBasedCredentials | Line # 46 | 26 | 10 | 15.1% |
0.1509434
|
(1) | |||
Result | |||
0.13207547
|
issrg.test.ds.TestDS.testIssuing issrg.test.ds.TestDS.testIssuing | 1 PASS | |
1 | /* | |
2 | * Copyright (c) 2000-2005, University of Salford | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * Redistributions of source code must retain the above copyright notice, this | |
9 | * list of conditions and the following disclaimer. | |
10 | * | |
11 | * Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
14 | * | |
15 | * Neither the name of the University of Salford nor the names of its | |
16 | * contributors may be used to endorse or promote products derived from this | |
17 | * software without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
29 | * POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | package issrg.pba.rbac; | |
33 | ||
34 | import issrg.pba.Credentials; | |
35 | ||
36 | /** | |
37 | * This is the implementation of a credential for a role based scheme. It | |
38 | * implements the Role interface. | |
39 | * | |
40 | * @author E Ball | |
41 | * @author D Chadwick | |
42 | * @author A Otenko | |
43 | * @version 0.2 | |
44 | */ | |
45 | ||
46 | public class RoleBasedCredentials extends SubsetCredentials implements Role { | |
47 | protected String roleType; | |
48 | private Object roleValue; | |
49 | ||
50 | /** | |
51 | * Utility method to help build Role Based Credentials out of two strings. | |
52 | * | |
53 | * @param roleType - the role type | |
54 | * @param roleValue - the value of the role | |
55 | * | |
56 | * @return RoleBasedCredentials object, representing the role of the given | |
57 | * type and value | |
58 | */ | |
59 | 0 | public static RoleBasedCredentials newCredentials(String roleType, String roleValue) { |
60 | 0 | return new RoleBasedCredentials(roleType, roleValue); |
61 | } | |
62 | ||
63 | 2919 | protected RoleBasedCredentials(){} |
64 | ||
65 | /** | |
66 | * This constructor builds the object by specifying the Role type and value. | |
67 | * | |
68 | * @param roleType is the String name of the Role type, as specified in the | |
69 | * Policy XML | |
70 | * @param roleValue is the object representing the value of the Role | |
71 | * | |
72 | * @throws IllegalArgumentException if the parameters to the constructor are | |
73 | * semantically incorrect | |
74 | */ | |
75 | 0 | public RoleBasedCredentials(String roleType, Object roleValue) { |
76 | 0 | if (roleType==null){ |
77 | 0 | throw new IllegalArgumentException("roleType cannot be null"); |
78 | } | |
79 | 0 | this.roleType=roleType; |
80 | 0 | this.roleValue=roleValue; |
81 | } | |
82 | ||
83 | /** | |
84 | * This method returns the type of the role. | |
85 | * | |
86 | * @return String name of the type | |
87 | */ | |
88 | 816 | public String getRoleType(){ |
89 | 816 | return roleType; |
90 | } | |
91 | ||
92 | /** | |
93 | * This method returns the value of the role. | |
94 | * | |
95 | * @return the object representing the value of the Role | |
96 | */ | |
97 | 0 | public Object getRoleValue(){ |
98 | 0 | return roleValue; |
99 | } | |
100 | ||
101 | /** | |
102 | * This method creates a copy of the RoleBasedCredential | |
103 | * | |
104 | * @return a new RoleBasedCredential with the same properties as the original | |
105 | * object | |
106 | */ | |
107 | 0 | public Object clone(){ |
108 | /** | |
109 | * TODO: make sure the roleValue is copied in such a way that it cannot be | |
110 | * amended | |
111 | */ | |
112 | 0 | return new RoleBasedCredentials(roleType, roleValue); |
113 | } | |
114 | ||
115 | /** | |
116 | * This method implements comparison of a credential to this object. | |
117 | * | |
118 | * <p>This is a basic Credential, which is unaware of any Role Hierarchy. | |
119 | * Therefore, this method is the same as equals. | |
120 | * | |
121 | * @param c is the credential to compare to | |
122 | * | |
123 | * @return true if c is contained within this object; false otherwise | |
124 | */ | |
125 | 0 | public boolean contains(Credentials c){ |
126 | 0 | if (!(c instanceof RoleBasedCredentials)){ |
127 | 0 | return super.contains(c); // that method knows how to handle the situation |
128 | } | |
129 | 0 | RoleBasedCredentials rbc = (RoleBasedCredentials)c; |
130 | ||
131 | 0 | return roleType.intern()==rbc.getRoleType().intern() && |
132 | (roleValue==null || roleValue.equals(rbc.getRoleValue())); | |
133 | } | |
134 | ||
135 | 7425 | public String toString(){ |
136 | 7425 | Object roleValue = getRoleValue(); |
137 | ||
138 | 7425 | return "role "+roleType+": "+(roleValue==null?"null":roleValue.toString()); |
139 | } | |
140 | ||
141 | /** | |
142 | * This method retrieves all the Role Values of a particular type the user | |
143 | * has got. | |
144 | * | |
145 | * <p>Example: | |
146 | * <code><p>Vector roleValues = RoleBasedCredentials.getRoleValues(subject.exportCreds(), "whatEverRoleTypeYouWant");</code> | |
147 | * | |
148 | * @param creds is the set of credentials to look though | |
149 | * @param type is a case-sensitive string name of the required role type, or | |
150 | * null, if roles of all types should be returned | |
151 | * | |
152 | * @return a vector of values of the role type the caller passed; if is empty, | |
153 | * there were no | |
154 | * roles of this type | |
155 | */ | |
156 | 0 | public static java.util.Vector getRoleValues(Credentials creds, String type){ |
157 | 0 | java.util.Vector result = new java.util.Vector(); |
158 | ||
159 | 0 | if (creds!=null){ |
160 | ||
161 | 0 | if (creds instanceof issrg.pba.rbac.SetOfSubsetsCredentials){ |
162 | 0 | java.util.Vector v = ((issrg.pba.rbac.SetOfSubsetsCredentials)creds).getValue(); |
163 | ||
164 | 0 | for (int i=v.size(); i-->0;){ |
165 | 0 | result.addAll(getRoleValues((Credentials)(v.get(i)), type)); |
166 | } | |
167 | }else{ | |
168 | 0 | if (creds instanceof RoleBasedCredentials){ |
169 | 0 | if (type==null || type.equals(((RoleBasedCredentials)creds).getRoleType())){ |
170 | 0 | result.add(((RoleBasedCredentials)creds).getRoleValue()); |
171 | } | |
172 | 0 | }else if (creds instanceof ExpirableCredentials){ |
173 | 0 | result.addAll(getRoleValues(((ExpirableCredentials)creds).expirable, type)); |
174 | } | |
175 | } | |
176 | } | |
177 | ||
178 | 0 | return result; |
179 | } | |
180 | } |
|