View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.cling.invoker.mvnenc;
20  
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Optional;
24  
25  import org.apache.maven.api.di.Inject;
26  import org.apache.maven.api.di.Named;
27  import org.apache.maven.api.di.Singleton;
28  import org.apache.maven.api.services.Prompter;
29  import org.apache.maven.api.services.PrompterException;
30  import org.codehaus.plexus.components.secdispatcher.MasterSource;
31  import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
32  import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
33  import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
34  
35  /**
36   * Trivial master password source using Maven {@link Prompter} service.
37   */
38  @Singleton
39  @Named(ConsolePasswordPrompt.NAME)
40  public class ConsolePasswordPrompt implements MasterSource, MasterSourceMeta {
41      public static final String NAME = "console-prompt";
42  
43      private final Prompter prompter;
44  
45      @Inject
46      public ConsolePasswordPrompt(Prompter prompter) {
47          this.prompter = prompter;
48      }
49  
50      @Override
51      public String description() {
52          return "Secure console password prompt";
53      }
54  
55      @Override
56      public Optional<String> configTemplate() {
57          return Optional.empty();
58      }
59  
60      @Override
61      public String handle(String config) throws SecDispatcherException {
62          if (NAME.equals(config)) {
63              try {
64                  return prompter.promptForPassword("Enter the master password: ");
65              } catch (PrompterException e) {
66                  throw new SecDispatcherException("Could not collect the password", e);
67              }
68          }
69          return null;
70      }
71  
72      @Override
73      public SecDispatcher.ValidationResponse validateConfiguration(String config) {
74          if (NAME.equals(config)) {
75              return new SecDispatcher.ValidationResponse(getClass().getSimpleName(), true, Map.of(), List.of());
76          }
77          return null;
78      }
79  }