View Javadoc
1   /*
2    * Copyright 2013–2026 Michael Osipov
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  package net.sf.michaelo.tomcat.realm;
17  
18  import java.security.Principal;
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.catalina.TomcatPrincipal;
25  import org.ietf.jgss.GSSCredential;
26  import org.ietf.jgss.GSSName;
27  
28  /**
29   * Represents a principal from Active Directory with a list of roles.
30   *
31   * <p>An Active Directory principal is comprised of the following items:
32   *
33   * <ul>
34   *   <li>the GSS name,
35   *   <li>the security identifier (SID),
36   *   <li>an optional GSS credential for credential delegation (impersonation),
37   *   <li>an array of security groups the user has been assigned to, stored according to the role format configured in
38   *       the realm,
39   *   <li>and a map with additional attributes which are either a {@code String}, {@code byte[]} or a {@code List} of
40   *       either one.
41   * </ul>
42   */
43  public class ActiveDirectoryPrincipal implements TomcatPrincipal {
44  
45  	private final GSSName gssName;
46  	private final Sid sid;
47  	private final transient GSSCredential gssCredential;
48  	private final String[] roles;
49  	private final Map<String, Object> additionalAttributes;
50  
51  	/** Constructs a new principal for the given parameters. */
52  	public ActiveDirectoryPrincipal(GSSName gssName, Sid sid, GSSCredential gssCredential) {
53  		this(gssName, sid, null, gssCredential, null);
54  	}
55  
56  	/** Constructs a new principal for the given parameters. */
57  	public ActiveDirectoryPrincipal(
58  			GSSName gssName,
59  			Sid sid,
60  			List<String> roles,
61  			GSSCredential gssCredential,
62  			Map<String, Object> additionalAttributes) {
63  		this.gssName = gssName;
64  		this.sid = sid;
65  		if (roles == null || roles.isEmpty()) this.roles = new String[0];
66  		else {
67  			this.roles = roles.toArray(new String[0]);
68  			Arrays.sort(this.roles);
69  		}
70  		this.gssCredential = gssCredential;
71  		if (additionalAttributes == null || additionalAttributes.isEmpty())
72  			this.additionalAttributes = Collections.emptyMap();
73  		else this.additionalAttributes = Collections.unmodifiableMap(additionalAttributes);
74  	}
75  
76  	@Override
77  	public Principal getUserPrincipal() {
78  		return this;
79  	}
80  
81  	@Override
82  	public String getName() {
83  		return gssName.toString();
84  	}
85  
86  	/**
87  	 * Returns the underlying GSS name.
88  	 *
89  	 * @return the underlying GSS name
90  	 */
91  	public GSSName getGssName() {
92  		return gssName;
93  	}
94  
95  	/**
96  	 * Returns the security identifier (SID) of the principal.
97  	 *
98  	 * @return the security identifier
99  	 */
100 	public Sid getSid() {
101 		return sid;
102 	}
103 
104 	@Override
105 	public GSSCredential getGssCredential() {
106 		return gssCredential;
107 	}
108 
109 	/**
110 	 * Grants access if supplied role is associated with this principal.
111 	 *
112 	 * @param role the role to check
113 	 * @return true if principal is associated with the role, else false
114 	 */
115 	public boolean hasRole(String role) {
116 		if ("*".equals(role)) // Special 2.4 role meaning everyone
117 		return true;
118 		if (role == null) return false;
119 		return Arrays.binarySearch(roles, role) >= 0;
120 	}
121 
122 	/**
123 	 * Returns the sorted roles of the given principal.
124 	 *
125 	 * @return a sorted read-only view of the roles
126 	 */
127 	public String[] getRoles() {
128 		return Arrays.copyOf(roles, roles.length);
129 	}
130 
131 	/**
132 	 * Holds additional attributes for a given principal which may be stored in Active Directory.
133 	 *
134 	 * @return a read-only view of the additional attributes
135 	 */
136 	public Map<String, Object> getAdditionalAttributes() {
137 		return additionalAttributes;
138 	}
139 
140 	@Override
141 	public boolean equals(Object obj) {
142 		if (obj == null) return false;
143 
144 		if (!(obj instanceof ActiveDirectoryPrincipal)) return false;
145 
146 		ActiveDirectoryPrincipal other = (ActiveDirectoryPrincipal) obj;
147 
148 		return gssName.equals((Object) other.gssName);
149 	}
150 
151 	@Override
152 	public int hashCode() {
153 		return gssName.hashCode();
154 	}
155 
156 	@Override
157 	public String toString() {
158 		return gssName.toString();
159 	}
160 
161 	@Override
162 	public void logout() throws Exception {
163 		if (gssCredential != null) {
164 			gssCredential.dispose();
165 		}
166 	}
167 }