Skip to content

Commit 8ca920e

Browse files
yisraelUYisrael Union
andauthored
Add credentials support (#473)
* add credentials to cache' * expose support at the top level * fix round tripping from java to scala realm/optional/httpsOnly/passOnRedirect were being dropped in the credentials(Authentication) and credentials(Credentials) round-trip methods at lines * add file credentials * fix tests on windows --------- Co-authored-by: Yisrael Union <yisrael.union@disneystreaming.com>
1 parent 4b39126 commit 8ca920e

7 files changed

Lines changed: 650 additions & 12 deletions

File tree

interface/src/main/java/coursierapi/Cache.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import coursier.internal.api.ApiHelper;
44

55
import java.io.File;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.Collections;
9+
import java.util.List;
610
import java.util.Objects;
711
import java.util.concurrent.ExecutorService;
812

@@ -11,11 +15,15 @@ public final class Cache {
1115
private ExecutorService pool;
1216
private File location;
1317
private Logger logger;
18+
private List<Credentials> credentials;
19+
private List<String> credentialFiles;
1420

1521
private Cache() {
1622
pool = ApiHelper.defaultPool();
1723
location = ApiHelper.defaultLocation();
1824
logger = null;
25+
credentials = Collections.emptyList();
26+
credentialFiles = Collections.emptyList();
1927
}
2028

2129
public static Cache create() {
@@ -32,14 +40,16 @@ public boolean equals(Object obj) {
3240
Cache other = (Cache) obj;
3341
return this.pool.equals(other.pool) &&
3442
this.location.equals(other.location) &&
35-
Objects.equals(this.logger, other.logger);
43+
Objects.equals(this.logger, other.logger) &&
44+
this.credentials.equals(other.credentials) &&
45+
this.credentialFiles.equals(other.credentialFiles);
3646
}
3747
return false;
3848
}
3949

4050
@Override
4151
public int hashCode() {
42-
return 37 * (37 * (17 + pool.hashCode()) + location.hashCode()) + Objects.hashCode(logger);
52+
return 37 * (37 * (37 * (37 * (17 + pool.hashCode()) + location.hashCode()) + Objects.hashCode(logger)) + credentials.hashCode()) + credentialFiles.hashCode();
4353
}
4454

4555
@Override
@@ -52,6 +62,14 @@ public String toString() {
5262
b.append(", logger=");
5363
b.append(logger.toString());
5464
}
65+
if (!credentials.isEmpty()) {
66+
b.append(", credentials=");
67+
b.append(credentials.toString());
68+
}
69+
if (!credentialFiles.isEmpty()) {
70+
b.append(", credentialFiles=");
71+
b.append(credentialFiles.toString());
72+
}
5573
b.append(")");
5674
return b.toString();
5775
}
@@ -71,6 +89,34 @@ public Cache withLogger(Logger logger) {
7189
return this;
7290
}
7391

92+
public Cache withCredentials(List<Credentials> credentials) {
93+
this.credentials = new ArrayList<>(credentials);
94+
return this;
95+
}
96+
97+
public Cache withCredentials(Credentials... credentials) {
98+
this.credentials = new ArrayList<>(Arrays.asList(credentials));
99+
return this;
100+
}
101+
102+
public Cache addCredentials(Credentials... credentials) {
103+
ArrayList<Credentials> newCredentials = new ArrayList<>(this.credentials);
104+
newCredentials.addAll(Arrays.asList(credentials));
105+
this.credentials = newCredentials;
106+
return this;
107+
}
108+
109+
public Cache addFileCredentials(String path) {
110+
ArrayList<String> newFiles = new ArrayList<>(this.credentialFiles);
111+
newFiles.add(path);
112+
this.credentialFiles = newFiles;
113+
return this;
114+
}
115+
116+
public Cache addFileCredentials(File file) {
117+
return addFileCredentials(file.getAbsolutePath());
118+
}
119+
74120
public ExecutorService getPool() {
75121
return pool;
76122
}
@@ -82,4 +128,12 @@ public File getLocation() {
82128
public Logger getLogger() {
83129
return logger;
84130
}
131+
132+
public List<Credentials> getCredentials() {
133+
return Collections.unmodifiableList(credentials);
134+
}
135+
136+
public List<String> getCredentialFiles() {
137+
return Collections.unmodifiableList(credentialFiles);
138+
}
85139
}

interface/src/main/java/coursierapi/Complete.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ public Complete withCache(Cache cache) {
8282
return this;
8383
}
8484

85+
public Complete addCredentials(Credentials... credentials) {
86+
this.cache = this.cache.addCredentials(credentials);
87+
return this;
88+
}
89+
90+
public Complete addFileCredentials(String path) {
91+
this.cache = this.cache.addFileCredentials(path);
92+
return this;
93+
}
94+
8595
public Complete withInput(String input) {
8696
this.input = input;
8797
return this;
Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,73 @@
11
package coursierapi;
22

33
import java.io.Serializable;
4+
import java.util.Objects;
45

56
public final class Credentials implements Serializable {
67

8+
private final String host;
79
private final String user;
810
private final String password;
11+
private final String realm;
12+
private final boolean optional;
13+
private final boolean matchHost;
14+
private final boolean httpsOnly;
15+
private final boolean passOnRedirect;
916

10-
private Credentials(String user, String password) {
17+
private Credentials(
18+
String host,
19+
String user,
20+
String password,
21+
String realm,
22+
boolean optional,
23+
boolean matchHost,
24+
boolean httpsOnly,
25+
boolean passOnRedirect
26+
) {
27+
this.host = host;
1128
this.user = user;
1229
this.password = password;
30+
this.realm = realm;
31+
this.optional = optional;
32+
this.matchHost = matchHost;
33+
this.httpsOnly = httpsOnly;
34+
this.passOnRedirect = passOnRedirect;
35+
}
36+
37+
public static Credentials of(String user, String password) {
38+
return new Credentials("", user, password, null, true, true, false, false);
39+
}
40+
41+
public static Credentials of(String host, String user, String password) {
42+
return new Credentials(host, user, password, null, true, true, false, false);
43+
}
44+
45+
public static Credentials of(String host, String user, String password, String realm) {
46+
return new Credentials(host, user, password, realm, true, true, false, false);
47+
}
48+
49+
public Credentials withHost(String host) {
50+
return new Credentials(host, this.user, this.password, this.realm, this.optional, this.matchHost, this.httpsOnly, this.passOnRedirect);
51+
}
52+
53+
public Credentials withRealm(String realm) {
54+
return new Credentials(this.host, this.user, this.password, realm, this.optional, this.matchHost, this.httpsOnly, this.passOnRedirect);
55+
}
56+
57+
public Credentials withOptional(boolean optional) {
58+
return new Credentials(this.host, this.user, this.password, this.realm, optional, this.matchHost, this.httpsOnly, this.passOnRedirect);
59+
}
60+
61+
public Credentials withMatchHost(boolean matchHost) {
62+
return new Credentials(this.host, this.user, this.password, this.realm, this.optional, matchHost, this.httpsOnly, this.passOnRedirect);
63+
}
64+
65+
public Credentials withHttpsOnly(boolean httpsOnly) {
66+
return new Credentials(this.host, this.user, this.password, this.realm, this.optional, this.matchHost, httpsOnly, this.passOnRedirect);
67+
}
68+
69+
public Credentials withPassOnRedirect(boolean passOnRedirect) {
70+
return new Credentials(this.host, this.user, this.password, this.realm, this.optional, this.matchHost, this.httpsOnly, passOnRedirect);
1371
}
1472

1573
@Override
@@ -18,23 +76,52 @@ public boolean equals(Object obj) {
1876
return true;
1977
if (obj instanceof Credentials) {
2078
Credentials other = (Credentials) obj;
21-
return this.user.equals(other.user) && this.password.equals(other.password);
79+
return this.host.equals(other.host) &&
80+
this.user.equals(other.user) &&
81+
this.password.equals(other.password) &&
82+
Objects.equals(this.realm, other.realm) &&
83+
this.optional == other.optional &&
84+
this.matchHost == other.matchHost &&
85+
this.httpsOnly == other.httpsOnly &&
86+
this.passOnRedirect == other.passOnRedirect;
2287
}
2388
return false;
2489
}
2590

2691
@Override
2792
public int hashCode() {
28-
return 37 * (17 + user.hashCode()) + password.hashCode();
93+
int h = 17;
94+
h = 37 * h + host.hashCode();
95+
h = 37 * h + user.hashCode();
96+
h = 37 * h + password.hashCode();
97+
h = 37 * h + Objects.hashCode(realm);
98+
h = 37 * h + Boolean.hashCode(optional);
99+
h = 37 * h + Boolean.hashCode(matchHost);
100+
h = 37 * h + Boolean.hashCode(httpsOnly);
101+
h = 37 * h + Boolean.hashCode(passOnRedirect);
102+
return h;
29103
}
30104

31105
@Override
32106
public String toString() {
33-
return "Credentials(" + user + ", ****)";
107+
StringBuilder b = new StringBuilder("Credentials(");
108+
if (!host.isEmpty()) {
109+
b.append("host=");
110+
b.append(host);
111+
b.append(", ");
112+
}
113+
b.append(user);
114+
b.append(", ****");
115+
if (realm != null) {
116+
b.append(", realm=");
117+
b.append(realm);
118+
}
119+
b.append(")");
120+
return b.toString();
34121
}
35122

36-
public static Credentials of(String user, String password) {
37-
return new Credentials(user, password);
123+
public String getHost() {
124+
return host;
38125
}
39126

40127
public String getUser() {
@@ -44,4 +131,24 @@ public String getUser() {
44131
public String getPassword() {
45132
return password;
46133
}
134+
135+
public String getRealm() {
136+
return realm;
137+
}
138+
139+
public boolean isOptional() {
140+
return optional;
141+
}
142+
143+
public boolean isMatchHost() {
144+
return matchHost;
145+
}
146+
147+
public boolean isHttpsOnly() {
148+
return httpsOnly;
149+
}
150+
151+
public boolean isPassOnRedirect() {
152+
return passOnRedirect;
153+
}
47154
}

interface/src/main/java/coursierapi/Fetch.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ public Fetch withCache(Cache cache) {
118118
return this;
119119
}
120120

121+
public Fetch addCredentials(Credentials... credentials) {
122+
this.cache = this.cache.addCredentials(credentials);
123+
return this;
124+
}
125+
126+
public Fetch addFileCredentials(String path) {
127+
this.cache = this.cache.addFileCredentials(path);
128+
return this;
129+
}
130+
121131
public Fetch withMainArtifacts(Boolean mainArtifacts) {
122132
this.mainArtifacts = mainArtifacts;
123133
return this;

interface/src/main/java/coursierapi/Versions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public Versions withCache(Cache cache) {
6868
return this;
6969
}
7070

71+
public Versions addCredentials(Credentials... credentials) {
72+
this.cache = this.cache.addCredentials(credentials);
73+
return this;
74+
}
75+
76+
public Versions addFileCredentials(String path) {
77+
this.cache = this.cache.addFileCredentials(path);
78+
return this;
79+
}
80+
7181
public Versions withModule(Module module) {
7282
this.module = module;
7383
return this;

0 commit comments

Comments
 (0)