Skip to content

Commit 14633d2

Browse files
Added support for X-Forwarded-For
1 parent 4f0d888 commit 14633d2

5 files changed

Lines changed: 32 additions & 13 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ The token consists of two parts. Firstly, a header containing non-sensitive meta
1414
"ti": "159aba3e-55e1-4f54-b6ee-e5b943d7e885”,
1515
"c": "ticketania",
1616
"e": "demoevent”,
17-
"ip": "75.86.129.4"
17+
"ip": "75.86.129.4",
18+
"xff": "45.67.2.4,34.56.3.2"
1819
}
1920
```
2021
- `typ`: The type of the token. Value must be “QFT1”. Required.
@@ -24,7 +25,8 @@ The token consists of two parts. Firstly, a header containing non-sensitive meta
2425
- `ti`: Unique Token ID (e.g. uuid). Used to uniquely identify tokens and restrict replay attacks. Required.
2526
- `c`: The Customer ID of the issuer. Token will only be valid on events on this account. Required.
2627
- `e`: The Event ID. If provided, token will only be valid on this event. Optional.
27-
- `ip`: The IP address the user the token is issued to. If provided, token will only be valid for this IP address. Optional.
28+
- `ip`: The IP address the user the token is issued to. If provided, the IP address is validated before issuing the token. Optional.
29+
- `xff`: The X-Forwarded-For headerof the request when the token is issued. If provided, the X-Forwarded-For header is validated before issuing the token. Optional.
2830

2931
### Token Payload
3032
```

queuetoken/src/main/java/com/queue_it/queuetoken/EnqueueToken.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class EnqueueToken implements IEnqueueToken {
88
private final String customerId;
99
private String eventId;
1010
private String ipAddress;
11+
private String xForwaredFor;
1112
private IEnqueueTokenPayload payload;
1213
private long issued;
1314
private long expires = Long.MAX_VALUE;
@@ -27,12 +28,13 @@ private String getTokenIdentifier(String tokenIdentifierPrefix1) {
2728
return tokenIdentifierPrefix1 == null || tokenIdentifierPrefix1.isEmpty() ? UUID.randomUUID().toString() : tokenIdentifierPrefix1 + "~" + UUID.randomUUID().toString();
2829
}
2930

30-
public EnqueueToken(String tokenIdentifier, String customerId, String eventId, long issued, long expires, String ipAddress, IEnqueueTokenPayload payload)
31+
public EnqueueToken(String tokenIdentifier, String customerId, String eventId, long issued, long expires, String ipAddress, String xForwaredFor, IEnqueueTokenPayload payload)
3132
{
3233
this.tokenIdentifier = tokenIdentifier;
3334
this.customerId = customerId;
3435
this.eventId = eventId;
3536
this.ipAddress = ipAddress;
37+
this.xForwaredFor = xForwaredFor;
3638
this.issued = issued;
3739
this.expires = expires;
3840
this.payload = payload;
@@ -78,6 +80,11 @@ public String getIpAddress() {
7880
return this.ipAddress;
7981
}
8082

83+
@Override
84+
public String getXForwardedFor() {
85+
return this.xForwaredFor;
86+
}
87+
8188
@Override
8289
public IEnqueueTokenPayload getPayload() {
8390
return this.payload;
@@ -141,26 +148,31 @@ private String serializeHeader() {
141148
sb.append(this.getIpAddress());
142149
sb.append("\"");
143150
}
151+
if (this.getXForwardedFor()!= null) {
152+
sb.append(",\"xff\":\"");
153+
sb.append(this.getXForwardedFor());
154+
sb.append("\"");
155+
}
144156
sb.append("}");
145157

146158
return Base64UrlEncoder.encode(sb.toString().getBytes(Charset.forName("UTF-8")));
147159
}
148160

149-
static EnqueueToken addIPAddress(EnqueueToken token, String ipAddress)
161+
static EnqueueToken addIPAddress(EnqueueToken token, String ipAddress, String xForwaredFor)
150162
{
151-
return new EnqueueToken(token.getTokenIdentifier(), token.getCustomerId(), token.getEventId(), token.getIssued(), token.getExpires(), ipAddress, token.getPayload());
163+
return new EnqueueToken(token.getTokenIdentifier(), token.getCustomerId(), token.getEventId(), token.getIssued(), token.getExpires(), ipAddress, xForwaredFor, token.getPayload());
152164
}
153165
static EnqueueToken addEventId(EnqueueToken token, String eventId)
154166
{
155-
return new EnqueueToken(token.getTokenIdentifier(), token.getCustomerId(), eventId, token.getIssued(), token.getExpires(), token.getIpAddress(), token.getPayload());
167+
return new EnqueueToken(token.getTokenIdentifier(), token.getCustomerId(), eventId, token.getIssued(), token.getExpires(), token.getIpAddress(), token.getXForwardedFor(), token.getPayload());
156168
}
157169
static EnqueueToken addExpires(EnqueueToken token, Long expires)
158170
{
159-
return new EnqueueToken(token.getTokenIdentifier(), token.getCustomerId(), token.getEventId(), token.getIssued(), expires, token.getIpAddress(), token.getPayload());
171+
return new EnqueueToken(token.getTokenIdentifier(), token.getCustomerId(), token.getEventId(), token.getIssued(), expires, token.getIpAddress(), token.getXForwardedFor(), token.getPayload());
160172
}
161173
static EnqueueToken addPayload(EnqueueToken token, IEnqueueTokenPayload payload)
162174
{
163-
return new EnqueueToken(token.getTokenIdentifier(), token.getCustomerId(), token.getEventId(), token.getIssued(), token.getExpires(), token.getIpAddress(), payload);
175+
return new EnqueueToken(token.getTokenIdentifier(), token.getCustomerId(), token.getEventId(), token.getIssued(), token.getExpires(), token.getIpAddress(), token.getXForwardedFor(), payload);
164176
}
165177

166178
}

queuetoken/src/main/java/com/queue_it/queuetoken/EnqueueTokenGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public EnqueueTokenGenerator withEventId(String eventId) {
1919
return this;
2020
}
2121

22-
public EnqueueTokenGenerator withIpAddress(String ipAddress) {
23-
this.token = EnqueueToken.addIPAddress(this.token, ipAddress);
22+
public EnqueueTokenGenerator withIpAddress(String ipAddress, String xForwaredFor) {
23+
this.token = EnqueueToken.addIPAddress(this.token, ipAddress, xForwaredFor);
2424

2525
return this;
2626
}

queuetoken/src/main/java/com/queue_it/queuetoken/IEnqueueToken.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public interface IEnqueueToken {
99
String getCustomerId();
1010
String getEventId();
1111
String getIpAddress();
12+
String getXForwardedFor();
1213
IEnqueueTokenPayload getPayload();
1314

1415
String getTokenWithoutHash();

queuetoken/src/test/java/com/queue_it/queuetoken/EnqueueTokenTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,14 @@ public void factory_withEventId() throws Exception {
108108
@Test
109109
public void factory_withIpAddress() throws Exception {
110110
String expectedIpAddress = "1.5.8.9";
111-
111+
String expectedXForwardedFor = "45.67.2.4,34.56.3.2";
112112
IEnqueueToken token = Token
113113
.enqueue("ticketania")
114-
.withIpAddress(expectedIpAddress)
114+
.withIpAddress(expectedIpAddress, expectedXForwardedFor)
115115
.generate("5ebbf794-1665-4d48-80d6-21ac34be7faedf9e10b3-551a-4682-bb77-fee59d6355d6");
116116

117117
assertEquals(expectedIpAddress, token.getIpAddress());
118+
assertEquals(expectedXForwardedFor, token.getXForwardedFor());
118119
}
119120

120121
@Test
@@ -173,6 +174,7 @@ public void token_withPayload() throws Exception {
173174
1534723200000L,
174175
1539129600000L,
175176
null,
177+
null,
176178
payload);
177179
token.generate("5ebbf794-1665-4d48-80d6-21ac34be7faedf9e10b3-551a-4682-bb77-fee59d6355d6", false);
178180

@@ -184,7 +186,7 @@ public void token_withPayload() throws Exception {
184186
@Test
185187
public void token_withoutPayload() throws Exception {
186188

187-
String expectedSignedToken = "eyJ0eXAiOiJRVDEiLCJlbmMiOiJBRVMyNTYiLCJpc3MiOjE1MzQ3MjMyMDAwMDAsImV4cCI6MTUzOTEyOTYwMDAwMCwidGkiOiJhMjFkNDIzYS00M2ZkLTQ4MjEtODRmYS00MzkwZjZhMmZkM2UiLCJjIjoidGlja2V0YW5pYSIsImUiOiJteWV2ZW50IiwiaXAiOiI1LjcuOC42In0..rqQznIDybri70GrsJ-hd_Hzp98HUqcsBGnWaiyqjlvY";
189+
String expectedSignedToken = "eyJ0eXAiOiJRVDEiLCJlbmMiOiJBRVMyNTYiLCJpc3MiOjE1MzQ3MjMyMDAwMDAsImV4cCI6MTUzOTEyOTYwMDAwMCwidGkiOiJhMjFkNDIzYS00M2ZkLTQ4MjEtODRmYS00MzkwZjZhMmZkM2UiLCJjIjoidGlja2V0YW5pYSIsImUiOiJteWV2ZW50IiwiaXAiOiI1LjcuOC42IiwieGZmIjoiNDUuNjcuMi40LDM0LjU2LjMuMiJ9..wUOdVDIKlrIqumpU33bShDPdvTkicRk3q4Z-Vs8epFc";
188190

189191
EnqueueToken token = new EnqueueToken(
190192
"a21d423a-43fd-4821-84fa-4390f6a2fd3e",
@@ -193,6 +195,7 @@ public void token_withoutPayload() throws Exception {
193195
1534723200000L,
194196
1539129600000L,
195197
"5.7.8.6",
198+
"45.67.2.4,34.56.3.2",
196199
null);
197200
token.generate("5ebbf794-1665-4d48-80d6-21ac34be7faedf9e10b3-551a-4682-bb77-fee59d6355d6", false);
198201

@@ -213,6 +216,7 @@ public void token_minimalHeader() throws Exception {
213216
1534723200000L,
214217
Long.MAX_VALUE,
215218
null,
219+
null,
216220
null);
217221
token.generate("5ebbf794-1665-4d48-80d6-21ac34be7faedf9e10b3-551a-4682-bb77-fee59d6355d6", false);
218222

0 commit comments

Comments
 (0)