a5f0c10d1093cdcdf9846e2c158310686055fac6
[moon.git] /
1 /*
2  * Copyright (c) 2016 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.filters;
10
11 import com.google.common.base.Preconditions;
12 import org.apache.shiro.authc.AuthenticationToken;
13 import org.apache.shiro.authc.UsernamePasswordToken;
14
15 /**
16  * Utility methods for forming audit trail output based on an <code>AuthenticationToken</code>.
17  *
18  * @author Ryan Goulding (ryandgoulding@gmail.com)
19  */
20 public class AuthenticationTokenUtils {
21
22     /**
23      * default value used in messaging when the "user" field is unparsable from the HTTP REST request
24      */
25     static final String DEFAULT_USERNAME = "an unknown user";
26
27     /**
28      * default value used in messaging when the "user" field is not present in the HTTP REST request, implying
29      * a different implementation of <code>AuthenticationToken</code> such as <code>CasToken</code>.
30      */
31     static final String DEFAULT_TOKEN = "an un-parsable token type";
32
33     /**
34      * default value used in messaging when the "host" field cannot be determined.
35      */
36     static final String DEFAULT_HOSTNAME = "an unknown host";
37
38     private AuthenticationTokenUtils() {
39         // private to prevent instantiation
40     }
41
42     /**
43      * Determines whether the supplied <code>Token</code> is a <code>UsernamePasswordToken</code>.
44      *
45      * @param token A generic <code>Token</code>, which might be a <code>UsernamePasswordToken</code>
46      * @return Whether the supplied <code>Token</code> is a <code>UsernamePasswordToken</code>
47      */
48     public static boolean isUsernamePasswordToken(final AuthenticationToken token) {
49         return token instanceof UsernamePasswordToken;
50     }
51
52     /**
53      * Extracts the username if possible.  If the supplied token is a <code>UsernamePasswordToken</code>
54      * and the username field is not set, <code>DEFAULT_USERNAME</code> is returned.  If the supplied
55      * token is not a <code>UsernamePasswordToken</code> (i.e., a <code>CasToken</code> or other
56      * implementation of <code>AuthenticationToken</code>), then <code>DEFAULT_TOKEN</code> is
57      * returned.
58      *
59      * @param token An <code>AuthenticationToken</code>, possibly a <code>UsernamePasswordToken</code>
60      * @return the username, <code>DEFAULT_USERNAME</code> or <code>DEFAULT_TOKEN</code> depending on input
61      */
62     public static String extractUsername(final AuthenticationToken token) {
63         if (isUsernamePasswordToken(token)) {
64             final UsernamePasswordToken upt = (UsernamePasswordToken) token;
65             return extractField(upt.getUsername(), DEFAULT_USERNAME);
66         }
67         return DEFAULT_TOKEN;
68     }
69
70     /**
71      * Extracts the hostname if possible.  If the supplied token is a <code>UsernamePasswordToken</code>
72      * and the hostname field is not set, <code>DEFAULT_HOSTNAME</code> is returned.  If the supplied
73      * token is not a <code>UsernamePasswordToken</code> (i.e., a <code>CasToken</code> or other
74      * implementation of <code>AuthenticationToken</code>), then <code>DEFAULT_HOSTNAME</code> is
75      * returned.
76      *
77      * @param token An <code>AuthenticationToken</code>, possibly a <code>UsernamePasswordToken</code>
78      * @return the hostname, or <code>DEFAULT_USERNAME</code> depending on input
79      */
80     public static String extractHostname(final AuthenticationToken token) {
81         if (isUsernamePasswordToken(token)) {
82             final UsernamePasswordToken upt = (UsernamePasswordToken) token;
83             return extractField(upt.getHost(), DEFAULT_HOSTNAME);
84         }
85         return DEFAULT_HOSTNAME;
86     }
87
88     /**
89      * Utility method to generate a generic message indicating Authentication was unsuccessful.
90      *
91      * @param token An <code>AuthenticationToken</code>, possibly a <code>UsernamePasswordToken</code>
92      * @return A message indicating authentication was unsuccessful
93      */
94     public static String generateUnsuccessfulAuthenticationMessage(final AuthenticationToken token) {
95         final String username = extractUsername(token);
96         final String remoteHostname = extractHostname(token);
97         return String.format("Unsuccessful authentication attempt by %s from %s", username, remoteHostname);
98     }
99
100     /**
101      * Utility method to generate a generic message indicating Authentication was successful.
102      *
103      * @param token An <code>AuthenticationToken</code>, possibly a <code>UsernamePasswordToken</code>
104      * @return A message indicating authentication was successful
105      */
106     public static String generateSuccessfulAuthenticationMessage(final AuthenticationToken token) {
107         final String username = extractUsername(token);
108         final String remoteHostname = extractHostname(token);
109         return String.format("Successful authentication attempt by %s from %s", username, remoteHostname);
110     }
111
112     /**
113      * Utility method that returns <code>field</code>, or <code>defaultValue</code> if <code>field</code> is null.
114      *
115      * @param field A generic string, which is possibly null.
116      * @param defaultValue A non-null value returned if <code>field</code> is null
117      * @return <code>field</code> or <code>defaultValue</code> if field is null
118      * @throws IllegalArgumentException If <code>defaultValue</code> is null
119      */
120     private static String extractField(final String field, final String defaultValue)
121         throws IllegalArgumentException {
122
123         Preconditions.checkNotNull(defaultValue, "defaultValue can't be null");
124         if (field != null) {
125             return field;
126         }
127         return defaultValue;
128     }
129 }