001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.shiro.authc;
018
019import org.apache.activemq.command.ConnectionInfo;
020import org.apache.activemq.shiro.subject.SubjectConnectionReference;
021import org.apache.shiro.authc.AuthenticationToken;
022import org.apache.shiro.authc.UsernamePasswordToken;
023
024/**
025 * Default implementation of the {@link AuthenticationTokenFactory} interface that returns
026 * {@link org.apache.shiro.authc.UsernamePasswordToken UsernamePasswordToken} instances based on inspecting the
027 * {@link ConnectionInfo}.
028 *
029 * @since 5.10.0
030 */
031public class DefaultAuthenticationTokenFactory implements AuthenticationTokenFactory {
032
033    public DefaultAuthenticationTokenFactory() {
034    }
035
036    /**
037     * Returns a new {@link UsernamePasswordToken} instance populated based on the ConnectionInfo's
038     * {@link org.apache.activemq.command.ConnectionInfo#getUserName() userName} and
039     * {@link org.apache.activemq.command.ConnectionInfo#getPassword() password} properties.
040     *
041     * @param conn the subject's connection
042     * @return a new {@link UsernamePasswordToken} instance populated based on the ConnectionInfo's
043     *         ConnectionInfo's {@link org.apache.activemq.command.ConnectionInfo#getUserName() userName} and
044     *         {@link org.apache.activemq.command.ConnectionInfo#getPassword() password} properties.
045     */
046    @Override
047    public AuthenticationToken getAuthenticationToken(SubjectConnectionReference conn) {
048
049        String username = conn.getConnectionInfo().getUserName();
050        String password = conn.getConnectionInfo().getPassword();
051
052        if (username == null && password == null) {
053            //no identity or credentials provided by the client for the connection - return null to reflect this
054            return null;
055        }
056
057        return new UsernamePasswordToken(username, password);
058    }
059}