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.resolver.internal.ant.types;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.maven.resolver.internal.ant.AntRepoSys;
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.types.DataType;
27 import org.apache.tools.ant.types.Reference;
28
29 /**
30 * Defines credentials and authentication settings for remote repositories.
31 * <p>
32 * This Ant {@link DataType} provides authentication information such as
33 * username/password or private key/passphrase credentials. It is used by
34 * remote repositories to enable secure access for resolving or deploying artifacts.
35 * </p>
36 *
37 * <p>
38 * Authentication settings may be declared inline within repository declarations or
39 * referenced by ID to support reuse.
40 * </p>
41 *
42 * <h2>Supported Authentication Mechanisms:</h2>
43 * <ul>
44 * <li>Username/password</li>
45 * <li>Private key with optional passphrase (for SSH)</li>
46 * </ul>
47 *
48 * <h2>Example Usage:</h2>
49 * <pre>{@code
50 * <authentication id="repo.auth" username="deployer" password="secret"/>
51 *
52 * <repository id="private" url="sftp://repo.mycompany.com/releases">
53 * <authentication refid="repo.auth"/>
54 * </repository>
55 * }</pre>
56 *
57 * @see org.apache.maven.resolver.internal.ant.types.RemoteRepository
58 */
59 public class Authentication extends DataType {
60
61 private String username;
62
63 private String password;
64
65 private String privateKeyFile;
66
67 private String passphrase;
68
69 private final List<String> servers = new ArrayList<>();
70
71 /**
72 * Default constructor for {@code Authentication} data type.
73 */
74 public Authentication() {
75 // Default constructor
76 }
77
78 /**
79 * Registers this authentication object with the current project.
80 *
81 * @param project the current Ant project
82 */
83 @Override
84 public void setProject(Project project) {
85 super.setProject(project);
86
87 AntRepoSys.getInstance(project).addAuthentication(this);
88 }
89
90 /**
91 * Resolves this object if defined as a reference and verifies that it is a
92 * {@code Authentication} instance.
93 *
94 * @return the referenced {@code Authentication} instance
95 * @throws org.apache.tools.ant.BuildException if the reference is invalid
96 */
97 protected Authentication getRef() {
98 return getCheckedRef(Authentication.class);
99 }
100
101 /**
102 * Sets a reference to an existing {@code Authentication} object.
103 *
104 * @param ref the reference ID to use
105 * @throws org.apache.tools.ant.BuildException if any attributes have already been set
106 */
107 @Override
108 public void setRefid(Reference ref) {
109 if (username != null || password != null || privateKeyFile != null || passphrase != null) {
110 throw tooManyAttributes();
111 }
112 super.setRefid(ref);
113 }
114
115 /**
116 * Gets the username used for authentication.
117 *
118 * @return the username, or the referenced value if this is a reference
119 */
120 public String getUsername() {
121 if (isReference()) {
122 return getRef().getUsername();
123 }
124 return username;
125 }
126
127 /**
128 * Sets the username used for authentication.
129 *
130 * @param username the username to set
131 */
132 public void setUsername(String username) {
133 checkAttributesAllowed();
134 this.username = username;
135 }
136
137 /**
138 * Gets the password used for authentication.
139 *
140 * @return the password, or the referenced value if this is a reference
141 */
142 public String getPassword() {
143 if (isReference()) {
144 return getRef().getPassword();
145 }
146 return password;
147 }
148
149 /**
150 * Sets the password used for authentication.
151 *
152 * @param password the password to set
153 */
154 public void setPassword(String password) {
155 checkAttributesAllowed();
156 this.password = password;
157 }
158
159 /**
160 * Gets the path to the private key file (used for SSH authentication).
161 *
162 * @return the private key file path, or the referenced value if this is a reference
163 */
164 public String getPrivateKeyFile() {
165 if (isReference()) {
166 return getRef().getPrivateKeyFile();
167 }
168 return privateKeyFile;
169 }
170
171 /**
172 * Sets the path to the private key file (used for SSH authentication).
173 *
174 * @param privateKeyFile the private key file path to set
175 */
176 public void setPrivateKeyFile(String privateKeyFile) {
177 checkAttributesAllowed();
178 this.privateKeyFile = privateKeyFile;
179 }
180
181 /**
182 * Gets the passphrase used for the private key file (if required).
183 *
184 * @return the passphrase, or the referenced value if this is a reference
185 */
186 public String getPassphrase() {
187 if (isReference()) {
188 return getRef().getPassphrase();
189 }
190 return passphrase;
191 }
192
193 /**
194 * Sets the passphrase used for the private key file (if required).
195 *
196 * @param passphrase the passphrase to set
197 */
198 public void setPassphrase(String passphrase) {
199 checkAttributesAllowed();
200 this.passphrase = passphrase;
201 }
202
203 /**
204 * Gets the list of server IDs this authentication applies to.
205 *
206 * @return a list of server ID strings
207 */
208 public List<String> getServers() {
209 if (isReference()) {
210 return getRef().getServers();
211 }
212 return servers;
213 }
214
215 /**
216 * Sets the list of server IDs this authentication applies to.
217 * <p>
218 * Multiple server IDs may be specified using {@code ;} or {@code :} as separators.
219 * </p>
220 *
221 * @param servers a semicolon- or colon-separated list of server IDs
222 * @throws org.apache.tools.ant.BuildException if attributes are not allowed i.e.
223 * it is defined as a reference.
224 */
225 public void setServers(String servers) {
226 checkAttributesAllowed();
227 this.servers.clear();
228 String[] split = servers.split("[;:]");
229 for (String server : split) {
230 server = server.trim();
231 if (!server.isEmpty()) {
232 this.servers.add(server);
233 }
234 }
235 }
236 }