1 package org.apache.maven.plugin.cxx.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
32
33
34
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
69
70
71
72
73
74
75
76 public static Credential createCredential( String url,
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 = aURL.getHost();
99
100 int port = 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
115 ret.setUsername( server.getUsername() );
116 }
117
118 if ( password == null )
119 {
120
121 ret.setPassword( decrypt( server.getPassword(), host, secDispatcher, log ) );
122 }
123
124
125
126
127
128
129
130
131
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 }