1 package org.eclipse.aether.transport.http;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.net.InetAddress;
23 import java.net.UnknownHostException;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27
28 import org.apache.http.auth.AuthScope;
29 import org.apache.http.auth.Credentials;
30 import org.apache.http.auth.NTCredentials;
31 import org.apache.http.auth.UsernamePasswordCredentials;
32 import org.apache.http.client.CredentialsProvider;
33 import org.apache.http.impl.client.BasicCredentialsProvider;
34 import org.eclipse.aether.repository.AuthenticationContext;
35
36
37
38
39 final class DeferredCredentialsProvider
40 implements CredentialsProvider
41 {
42
43 private final CredentialsProvider delegate;
44
45 private final Map<AuthScope, Factory> factories;
46
47 DeferredCredentialsProvider()
48 {
49 delegate = new BasicCredentialsProvider();
50 factories = new HashMap<AuthScope, Factory>();
51 }
52
53 public void setCredentials( AuthScope authScope, Factory factory )
54 {
55 factories.put( authScope, factory );
56 }
57
58 public void setCredentials( AuthScope authScope, Credentials credentials )
59 {
60 delegate.setCredentials( authScope, credentials );
61 }
62
63 public Credentials getCredentials( AuthScope authScope )
64 {
65 synchronized ( factories )
66 {
67 for ( Iterator<Map.Entry<AuthScope, Factory>> it = factories.entrySet().iterator(); it.hasNext(); )
68 {
69 Map.Entry<AuthScope, Factory> entry = it.next();
70 if ( authScope.match( entry.getKey() ) >= 0 )
71 {
72 it.remove();
73 delegate.setCredentials( entry.getKey(), entry.getValue().newCredentials() );
74 }
75 }
76 }
77 return delegate.getCredentials( authScope );
78 }
79
80 public void clear()
81 {
82 delegate.clear();
83 }
84
85 interface Factory
86 {
87
88 Credentials newCredentials();
89
90 }
91
92 static class BasicFactory
93 implements Factory
94 {
95
96 private final AuthenticationContext authContext;
97
98 BasicFactory( AuthenticationContext authContext )
99 {
100 this.authContext = authContext;
101 }
102
103 public Credentials newCredentials()
104 {
105 String username = authContext.get( AuthenticationContext.USERNAME );
106 if ( username == null )
107 {
108 return null;
109 }
110 String password = authContext.get( AuthenticationContext.PASSWORD );
111 return new UsernamePasswordCredentials( username, password );
112 }
113
114 }
115
116 static class NtlmFactory
117 implements Factory
118 {
119
120 private final AuthenticationContext authContext;
121
122 NtlmFactory( AuthenticationContext authContext )
123 {
124 this.authContext = authContext;
125 }
126
127 public Credentials newCredentials()
128 {
129 String username = authContext.get( AuthenticationContext.USERNAME );
130 if ( username == null )
131 {
132 return null;
133 }
134 String password = authContext.get( AuthenticationContext.PASSWORD );
135 String domain = authContext.get( AuthenticationContext.NTLM_DOMAIN );
136 String workstation = authContext.get( AuthenticationContext.NTLM_WORKSTATION );
137
138 if ( domain == null )
139 {
140 int backslash = username.indexOf( '\\' );
141 if ( backslash < 0 )
142 {
143 domain = guessDomain();
144 }
145 else
146 {
147 domain = username.substring( 0, backslash );
148 username = username.substring( backslash + 1 );
149 }
150 }
151 if ( workstation == null )
152 {
153 workstation = guessWorkstation();
154 }
155
156 return new NTCredentials( username, password, workstation, domain );
157 }
158
159 private static String guessDomain()
160 {
161 return safeNtlmString( System.getProperty( "http.auth.ntlm.domain" ), System.getenv( "USERDOMAIN" ) );
162 }
163
164 private static String guessWorkstation()
165 {
166 String localHost = null;
167 try
168 {
169 localHost = InetAddress.getLocalHost().getHostName();
170 }
171 catch ( UnknownHostException e )
172 {
173
174 }
175 return safeNtlmString( System.getProperty( "http.auth.ntlm.host" ), System.getenv( "COMPUTERNAME" ),
176 localHost );
177 }
178
179 private static String safeNtlmString( String... strings )
180 {
181 for ( String string : strings )
182 {
183 if ( string != null )
184 {
185 return string;
186 }
187 }
188
189 return "";
190 }
191
192 }
193
194 }