View Javadoc
1   package org.apache.maven.plugin.cxx.utils;
2   
3   /*
4    * Copyright (C) 2011-2016, Neticoa SAS France - Tous droits réservés.
5    * Author(s) : Franck Bonin, Neticoa SAS France
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   *
19   */
20   
21  import org.codehaus.plexus.util.StringUtils;
22  import java.net.URL;
23  import java.net.MalformedURLException;
24  import org.apache.maven.plugin.logging.Log;
25  import org.apache.maven.settings.Server;
26  import org.apache.maven.settings.Settings;
27  import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
28  import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
29  
30  /**
31   * TODO
32   * 
33   * @version 8 mars 2016
34   * @author fbonin
35   */
36  public final class Credential
37  {
38      private String username;
39      private String password;
40  
41      public Credential( String username, String password )
42      {
43          this.username = username;
44          this.password = password;
45      }
46  
47      public String getUsername()
48      {
49          return username;
50      }
51  
52      public String getPassword()
53      {
54          return password;
55      }
56      
57      public void setUsername( String username )
58      {
59          this.username = username;
60      }
61  
62      public void setPassword( String password )
63      {
64          this.password = password;
65      }
66      
67      /**
68       * Load username password from settings if user has not set them in JVM properties
69       * 
70       * @see http://blog.sonatype.com/2009/10/maven-tips-and-tricks-encrypting-passwords
71       * 
72       * origin : derived from org.apache.maven.scm.plugin.AbstractScmMojo::loadInfosFromSettings()
73       *
74       * @param url not null
75       */
76      public static Credential createCredential( String url, /*ScmProviderRepositoryWithHost repo*/
77          String username, String password, String settingsServerId, 
78          Settings settings, SecDispatcher secDispatcher, Log log )
79      {
80          Credential ret = new Credential( username, password );
81          if ( username == null || password == null )
82          {
83              String host = settingsServerId;
84              if ( StringUtils.isEmpty( host ) )
85              {
86                  URL aURL = null;
87                  try
88                  {
89                      aURL = new URL( url );
90                  }
91                  catch ( MalformedURLException e )
92                  {
93                      log.warn( "Failed to parse url '" + url
94                          + "' while trying to find associated maven credentials info from maven settings" );
95                      return ret;
96                  }
97  
98                  host = /*repo*/aURL.getHost();
99  
100                 int port = /*repo*/aURL.getPort();
101 
102                 if ( port > 0 )
103                 {
104                     host += ":" + port;
105                 }
106             }
107 
108             Server server = settings.getServer( host );
109 
110             if ( server != null )
111             {
112                 if ( username == null )
113                 {
114                     //username = server.getUsername();
115                     ret.setUsername( server.getUsername() );
116                 }
117 
118                 if ( password == null )
119                 {
120                     //password = decrypt( server.getPassword(), host );
121                     ret.setPassword( decrypt( server.getPassword(), host, secDispatcher, log ) );
122                 }
123 
124                 /*if ( privateKey == null )
125                 {
126                     privateKey = server.getPrivateKey();
127                 }
128 
129                 if ( passphrase == null )
130                 {
131                     passphrase = decrypt( server.getPassphrase(), host );
132                 }*/
133             }
134         }
135         return ret;
136     }
137 
138     private static String decrypt( String str, String server, SecDispatcher secDispatcher, Log log )
139     {
140         try
141         {
142             return secDispatcher.decrypt( str );
143         }
144         catch ( SecDispatcherException e )
145         {
146             log.warn( "Failed to decrypt password/passphrase for server " + server + ", using auth token as is" );
147             return str;
148         }
149     }
150 }