001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.git.gitexe.command;
020
021import java.util.regex.Matcher;
022import java.util.regex.Pattern;
023
024import org.codehaus.plexus.util.cli.Commandline;
025
026/**
027 * CommandLine extension to mask password
028 * @since 1.9.3
029 */
030public class AnonymousCommandLine extends Commandline {
031
032    public static final String PASSWORD_PLACE_HOLDER = "********";
033
034    private Pattern passwordPattern = Pattern.compile("^.*:(.*)@.*$");
035
036    /**
037     * Provides an anonymous output to mask password. Considering URL of type :
038     * <<protocol>>://<<user>>:<<password>>@
039     * <<host_definition>>
040     */
041    @Override
042    public String toString() {
043        String output = super.toString();
044        final Matcher passwordMatcher = passwordPattern.matcher(output);
045        if (passwordMatcher.find()) {
046            // clear password
047            final String clearPassword = passwordMatcher.group(1);
048            // to be replaced in output by stars
049            output = output.replace(clearPassword, PASSWORD_PLACE_HOLDER);
050        }
051        return output;
052    }
053}