View Javadoc
1   /*
2    * Copyright 2013–2019 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  
20  import org.apache.catalina.realm.RealmBase;
21  import org.apache.juli.logging.Log;
22  import org.apache.juli.logging.LogFactory;
23  import org.apache.tomcat.util.res.StringManager;
24  import org.ietf.jgss.GSSContext;
25  import org.ietf.jgss.GSSName;
26  
27  /**
28   * Base realm which is able to retrieve principals from {@link GSSName GSS names} or fully
29   * established {@link GSSContext GSS contexts}.
30   *
31   * @version $Id: ActiveDirectoryRealmBase.java 346 2019-12-08 13:17:01Z michael-o $
32   */
33  public abstract class ActiveDirectoryRealmBase extends RealmBase {
34  
35  	protected final Log logger = LogFactory.getLog(getClass());
36  	protected final StringManager sm = StringManager.getManager(getClass());
37  
38  	/**
39  	 * @throws UnsupportedOperationException
40  	 *             always throws because not implemented
41  	 */
42  	@Override
43  	protected String getPassword(String username) {
44  		throw new UnsupportedOperationException(
45  				"getPassword(String) is not supported by this realm");
46  	}
47  
48  	/**
49  	 * @throws UnsupportedOperationException
50  	 *             always throws because not implemented
51  	 */
52  	@Override
53  	protected Principal getPrincipal(String username) {
54  		throw new UnsupportedOperationException(
55  				"getPrincipal(String) is not supported by this realm");
56  	}
57  
58  	@Override
59  	protected boolean hasRoleInternal(Principal principal, String role) {
60  		if (!(principal instanceof ActiveDirectoryPrincipal))
61  			return false;
62  
63  		ActiveDirectoryPrincipal adp = (ActiveDirectoryPrincipal) principal;
64  		return adp.hasRole(role);
65  	}
66  
67  	@Override
68  	public String[] getRoles(Principal principal) {
69  		if (principal instanceof ActiveDirectoryPrincipal) {
70  			return ((ActiveDirectoryPrincipal) principal).getRoles();
71  		}
72  
73  		String className = principal.getClass().getName();
74  		throw new IllegalStateException(sm.getString("activeDirectoryRealmBase.cannotGetRoles",
75  				principal.getName(), className));
76  	}
77  
78  }