11package coursierapi ;
22
33import java .io .Serializable ;
4+ import java .util .Objects ;
45
56public 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}
0 commit comments