fefc17ac8af33321d5d35ad71551f324f1935756
[aaa.git] / aaa-shiro / impl / src / main / java / org / opendaylight / aaa / shiro / realm / ODLJndiLdapRealmAuthNOnly.java
1 /*
2  * Copyright (c) 2016 - 2017 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.aaa.shiro.realm;
10
11 import org.apache.shiro.authc.AuthenticationException;
12 import org.apache.shiro.authc.AuthenticationInfo;
13 import org.apache.shiro.authc.AuthenticationToken;
14 import org.apache.shiro.realm.ldap.JndiLdapRealm;
15 import org.opendaylight.aaa.shiro.accounting.Accounter;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Wrapper class for <code>org.apache.shiro.realm.ldap.JndiLdapRealm</code>.
21  * This implementation disables Authorization so any LDAP user is able to access
22  * server resources. This is particularly useful for quickly prototyping ODL
23  * without worrying about resolving LDAP attributes (groups) to OpenDaylight
24  * roles.
25  *
26  * <p>
27  * The motivation for subclassing Shiro's implementation is two-fold: 1) Enhance
28  * the default logging of Shiro. This allows us to more easily log incoming
29  * connections, providing some security auditing. 2) Provide a common package in
30  * the classpath for ODL supported Realm implementations (i.e.,
31  * <code>org.opendaylight.aaa.shiro.realm</code>), which consolidates the number
32  * of <code>Import-Package</code> statements consumers need to enumerate. For
33  * example, the netconf project only needs to import
34  * <code>org.opendaylight.aaa.shiro.realm</code>, and does not need to worry
35  * about importing Shiro packages.
36  */
37 public class ODLJndiLdapRealmAuthNOnly extends JndiLdapRealm {
38
39     private static final Logger LOG = LoggerFactory.getLogger(ODLJndiLdapRealmAuthNOnly.class);
40
41     private static final String LDAP_CONNECTION_MESSAGE = "AAA LDAP connection from ";
42
43     /*
44      * Adds debugging information surrounding creation of ODLJndiLdapRealm
45      */
46     public ODLJndiLdapRealmAuthNOnly() {
47         LOG.info("ODLJndiLdapRealmAuthNOnly realm created");
48     }
49
50     /*
51      * (non-Javadoc) Overridden to expose important audit trail information for
52      * accounting.
53      *
54      * @see
55      * org.apache.shiro.realm.ldap.JndiLdapRealm#doGetAuthenticationInfo(org
56      * .apache.shiro.authc.AuthenticationToken)
57      */
58     @Override
59     protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
60             throws AuthenticationException {
61
62         try {
63             final String username = getUsername(token);
64             logIncomingConnection(username);
65             return super.doGetAuthenticationInfo(token);
66         } catch (ClassCastException e) {
67             LOG.info("Couldn't service the LDAP connection", e);
68         }
69         return null;
70     }
71
72     /**
73      * Logs an incoming LDAP connection.
74      *
75      * @param username
76      *            the requesting user
77      */
78     protected void logIncomingConnection(final String username) {
79         final String message = LDAP_CONNECTION_MESSAGE + username;
80         LOG.info(message);
81         Accounter.output(message);
82     }
83
84     /**
85      * Extracts the username from <code>token</code>.
86      *
87      * @param token Which possibly contains a username
88      * @return the username if it can be extracted
89      * @throws ClassCastException
90      *             The incoming token is not username/password (i.e., X.509
91      *             certificate)
92      */
93     public static String getUsername(AuthenticationToken token) throws ClassCastException {
94         if (null == token) {
95             return null;
96         }
97         return (String) token.getPrincipal();
98     }
99 }