-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Expand file tree
/
Copy pathCookie.java
More file actions
355 lines (305 loc) · 9.94 KB
/
Cookie.java
File metadata and controls
355 lines (305 loc) · 9.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.TimeZone;
import java.util.TreeMap;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.internal.Require;
public class Cookie implements Serializable {
private static final long serialVersionUID = 4115876353625612383L;
private final String name;
private final String value;
private final String path;
private final @Nullable String domain;
private final @Nullable Date expiry;
private final boolean isSecure;
private final boolean isHttpOnly;
private final @Nullable String sameSite;
/**
* Creates an insecure non-httpOnly cookie with no domain specified.
*
* @param name The name of the cookie; may not be null or an empty string.
* @param value The cookie value; may not be null.
* @param path The path the cookie is visible to. If left blank or set to null, will be set to
* "/".
* @param expiry The cookie's expiration date; may be null.
* @see #Cookie(String, String, String, String, Date)
*/
public Cookie(String name, String value, @Nullable String path, @Nullable Date expiry) {
this(name, value, null, path, expiry);
}
/**
* Creates an insecure non-httpOnly cookie.
*
* @param name The name of the cookie; may not be null or an empty string.
* @param value The cookie value; may not be null.
* @param domain The domain the cookie is visible to.
* @param path The path the cookie is visible to. If left blank or set to null, will be set to
* "/".
* @param expiry The cookie's expiration date; may be null.
* @see #Cookie(String, String, String, String, Date, boolean)
*/
public Cookie(
String name,
String value,
@Nullable String domain,
@Nullable String path,
@Nullable Date expiry) {
this(name, value, domain, path, expiry, false);
}
/**
* Creates a non-httpOnly cookie.
*
* @param name The name of the cookie; may not be null or an empty string.
* @param value The cookie value; may not be null.
* @param domain The domain the cookie is visible to.
* @param path The path the cookie is visible to. If left blank or set to null, will be set to
* "/".
* @param expiry The cookie's expiration date; may be null.
* @param isSecure Whether this cookie requires a secure connection.
*/
public Cookie(
String name,
String value,
@Nullable String domain,
@Nullable String path,
@Nullable Date expiry,
boolean isSecure) {
this(name, value, domain, path, expiry, isSecure, false);
}
/**
* Creates a cookie.
*
* @param name The name of the cookie; may not be null or an empty string.
* @param value The cookie value; may not be null.
* @param domain The domain the cookie is visible to.
* @param path The path the cookie is visible to. If left blank or set to null, will be set to
* "/".
* @param expiry The cookie's expiration date; may be null.
* @param isSecure Whether this cookie requires a secure connection.
* @param isHttpOnly Whether this cookie is a httpOnly cooke.
*/
public Cookie(
String name,
String value,
@Nullable String domain,
@Nullable String path,
@Nullable Date expiry,
boolean isSecure,
boolean isHttpOnly) {
this(name, value, domain, path, expiry, isSecure, isHttpOnly, null);
}
/**
* Creates a cookie.
*
* @param name The name of the cookie; may not be null or an empty string.
* @param value The cookie value; may not be null.
* @param domain The domain the cookie is visible to.
* @param path The path the cookie is visible to. If left blank or set to null, will be set to
* "/".
* @param expiry The cookie's expiration date; may be null.
* @param isSecure Whether this cookie requires a secure connection.
* @param isHttpOnly Whether this cookie is a httpOnly cookie.
* @param sameSite The samesite attribute of this cookie; e.g. None, Lax, Strict.
*/
public Cookie(
String name,
String value,
@Nullable String domain,
@Nullable String path,
@Nullable Date expiry,
boolean isSecure,
boolean isHttpOnly,
@Nullable String sameSite) {
this.name = name;
this.value = value;
this.path = path == null || path.isEmpty() ? "/" : path;
this.domain = stripPort(domain);
this.isSecure = isSecure;
this.isHttpOnly = isHttpOnly;
if (expiry != null) {
// Expiration date is specified in seconds since (UTC) epoch time, so truncate the date.
this.expiry = new Date(expiry.getTime() / 1000 * 1000);
} else {
this.expiry = null;
}
this.sameSite = sameSite;
}
/**
* Create a cookie for the default path with the given name and value with no expiry set.
*
* @param name The cookie's name
* @param value The cookie's value
*/
public Cookie(String name, String value) {
this(name, value, "/", null);
}
/**
* Create a cookie.
*
* @param name The cookie's name
* @param value The cookie's value
* @param path The path the cookie is for
*/
public Cookie(String name, String value, String path) {
this(name, value, path, null);
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
public @Nullable String getDomain() {
return domain;
}
public String getPath() {
return path;
}
public boolean isSecure() {
return isSecure;
}
public boolean isHttpOnly() {
return isHttpOnly;
}
public @Nullable Date getExpiry() {
return expiry == null ? null : new Date(expiry.getTime());
}
public @Nullable String getSameSite() {
return sameSite;
}
private static @Nullable String stripPort(@Nullable String domain) {
return (domain == null) ? null : domain.split(":")[0];
}
public void validate() {
Require.nonEmpty("Name", name);
Require.nonNull("Value", value);
Require.nonNull("Path", path);
if (name.indexOf(';') != -1) {
throw new IllegalArgumentException("Cookie names cannot contain a ';': " + name);
}
if (domain != null && domain.contains(":")) {
throw new IllegalArgumentException("Domain should not contain a port: " + domain);
}
}
/**
* JSON object keys are defined in
* https://w3c.github.io/webdriver/#dfn-table-for-cookie-conversion.
*/
public Map<String, Object> toJson() {
Map<String, Object> toReturn = new TreeMap<>();
if (getDomain() != null) {
toReturn.put("domain", getDomain());
}
if (getExpiry() != null) {
toReturn.put("expiry", getExpiry());
}
if (getName() != null) {
toReturn.put("name", getName());
}
if (getPath() != null) {
toReturn.put("path", getPath());
}
if (getValue() != null) {
toReturn.put("value", getValue());
}
toReturn.put("secure", isSecure());
toReturn.put("httpOnly", isHttpOnly());
if (getSameSite() != null) {
toReturn.put("sameSite", getSameSite());
}
return toReturn;
}
@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
return name
+ "="
+ value
+ (expiry == null ? "" : "; expires=" + sdf.format(expiry))
+ ("".equals(path) ? "" : "; path=" + path)
+ (domain == null ? "" : "; domain=" + domain)
+ (isSecure ? ";secure;" : "")
+ (sameSite == null ? "" : "; sameSite=" + sameSite);
}
/** Two cookies are equal if the name and value match */
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Cookie)) {
return false;
}
Cookie cookie = (Cookie) o;
if (!name.equals(cookie.name)) {
return false;
}
return Objects.equals(value, cookie.value);
}
@Override
public int hashCode() {
return name.hashCode();
}
public static class Builder {
private final String name;
private final String value;
private @Nullable String path;
private @Nullable String domain;
private @Nullable Date expiry;
private boolean secure;
private boolean httpOnly;
private @Nullable String sameSite;
public Builder(String name, String value) {
this.name = name;
this.value = value;
}
public Builder domain(@Nullable String host) {
this.domain = stripPort(host);
return this;
}
public Builder path(@Nullable String path) {
this.path = path;
return this;
}
public Builder expiresOn(@Nullable Date expiry) {
this.expiry = expiry == null ? null : new Date(expiry.getTime());
return this;
}
public Builder isSecure(boolean secure) {
this.secure = secure;
return this;
}
public Builder isHttpOnly(boolean httpOnly) {
this.httpOnly = httpOnly;
return this;
}
public Builder sameSite(@Nullable String sameSite) {
this.sameSite = sameSite;
return this;
}
public Cookie build() {
return new Cookie(name, value, domain, path, expiry, secure, httpOnly, sameSite);
}
}
}