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.security.cert.X509Certificate;
20  
21  import org.apache.catalina.realm.RealmBase;
22  import org.apache.juli.logging.Log;
23  import org.apache.juli.logging.LogFactory;
24  import org.apache.tomcat.util.res.StringManager;
25  import org.ietf.jgss.GSSContext;
26  import org.ietf.jgss.GSSName;
27  
28  /**
29   * Base Active Directory realm which is able to retrieve principals for {@link GSSName GSS names}, fully established
30   * {@link GSSContext security contexts} or {@link X509Certificate TLS client certificates}.
31   */
32  public abstract class ActiveDirectoryRealmBase extends RealmBase {
33  
34  	protected final Log logger = LogFactory.getLog(getClass());
35  	protected final StringManager sm = StringManager.getManager(getClass());
36  
37  	/** @return Always {@code null} as this realm has no way of obtaining this information. */
38  	@Override
39  	protected String getPassword(String username) {
40  		// Always return null
41  		return null;
42  	}
43  
44  	/** @throws UnsupportedOperationException always throws because not implemented */
45  	@Override
46  	protected Principal getPrincipal(String username) {
47  		throw new UnsupportedOperationException("getPrincipal(String) is not supported by this realm");
48  	}
49  
50  	@Override
51  	protected boolean hasRoleInternal(Principal principal, String role) {
52  		if (!(principal instanceof ActiveDirectoryPrincipal)) return false;
53  
54  		ActiveDirectoryPrincipal adp = (ActiveDirectoryPrincipal) principal;
55  		return adp.hasRole(role);
56  	}
57  
58  	@Override
59  	public String[] getRoles(Principal principal) {
60  		if (principal instanceof ActiveDirectoryPrincipal) {
61  			return ((ActiveDirectoryPrincipal) principal).getRoles();
62  		}
63  
64  		String className = principal.getClass().getName();
65  		throw new IllegalStateException(
66  				sm.getString("activeDirectoryRealmBase.cannotGetRoles", principal.getName(), className));
67  	}
68  }