Skip to content

Commit a39b4e2

Browse files
#4138 - Added ip address
1 parent 6faecbc commit a39b4e2

5 files changed

Lines changed: 64 additions & 35 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ The token consists of two parts. Firstly, a header containing non-sensitive meta
1313
"exp": 1526524517,
1414
"ti": "159aba3e-55e1-4f54-b6ee-e5b943d7e885”,
1515
"c": "ticketania",
16-
"e": "demoevent”
16+
"e": "demoevent”,
17+
"ip": "75.86.129.4"
1718
}
1819
```
1920
- `typ`: The type of the token. Value must be “QFT1”. Required.
@@ -23,6 +24,7 @@ The token consists of two parts. Firstly, a header containing non-sensitive meta
2324
- `ti`: Unique Token ID (e.g. uuid). Used to uniquely identify tokens and restrict replay attacks. Required.
2425
- `c`: The Customer ID of the issuer. Token will only be valid on events on this account. Required.
2526
- `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.
2628

2729
### Token Payload
2830
```
@@ -48,6 +50,7 @@ IEnqueueToken token = Token
4850
.withCustomData("size", "medium")
4951
.qenerate())
5052
.withEventId("demoevent")
53+
.WithIpAddress("75.86.129.4")
5154
.withValidity(60000)
5255
.generate(secretKey);
5356

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class EnqueueToken implements IEnqueueToken {
77

88
private final String customerId;
99
private String eventId;
10+
private String ipAddress;
1011
private IEnqueueTokenPayload payload;
1112
private long issued;
1213
private long expires = Long.MAX_VALUE;
@@ -26,38 +27,12 @@ private String getTokenIdentifier(String tokenIdentifierPrefix1) {
2627
return tokenIdentifierPrefix1 == null || tokenIdentifierPrefix1.isEmpty() ? UUID.randomUUID().toString() : tokenIdentifierPrefix1 + "~" + UUID.randomUUID().toString();
2728
}
2829

29-
public EnqueueToken(EnqueueToken token, Long expires) {
30-
this(token.customerId, token.tokenIdentifierPrefix);
31-
32-
this.eventId = token.eventId;
33-
this.payload = token.payload;
34-
this.expires = expires;
35-
this.issued = token.issued;
36-
}
37-
38-
public EnqueueToken(EnqueueToken token, String eventId) {
39-
this(token.customerId, token.tokenIdentifierPrefix);
40-
41-
this.eventId = eventId;
42-
this.payload = token.payload;
43-
this.expires = token.expires;
44-
this.issued = token.issued;
45-
}
46-
47-
public EnqueueToken(EnqueueToken token, IEnqueueTokenPayload payload) {
48-
this(token.customerId, token.tokenIdentifierPrefix);
49-
50-
this.eventId = token.eventId;
51-
this.payload = payload;
52-
this.expires = token.expires;
53-
this.issued = token.issued;
54-
}
55-
56-
public EnqueueToken(String tokenIdentifier, String customerId, String eventId, long issued, long expires, IEnqueueTokenPayload payload)
30+
public EnqueueToken(String tokenIdentifier, String customerId, String eventId, long issued, long expires, String ipAddress, IEnqueueTokenPayload payload)
5731
{
5832
this.tokenIdentifier = tokenIdentifier;
5933
this.customerId = customerId;
6034
this.eventId = eventId;
35+
this.ipAddress = ipAddress;
6136
this.issued = issued;
6237
this.expires = expires;
6338
this.payload = payload;
@@ -98,6 +73,11 @@ public String getEventId() {
9873
return this.eventId;
9974
}
10075

76+
@Override
77+
public String getIpAddress() {
78+
return this.ipAddress;
79+
}
80+
10181
@Override
10282
public IEnqueueTokenPayload getPayload() {
10383
return this.payload;
@@ -156,8 +136,31 @@ private String serializeHeader() {
156136
sb.append(this.getEventId());
157137
sb.append("\"");
158138
}
139+
if (this.getIpAddress()!= null) {
140+
sb.append(",\"ip\":\"");
141+
sb.append(this.getIpAddress());
142+
sb.append("\"");
143+
}
159144
sb.append("}");
160145

161146
return Base64UrlEncoder.encode(sb.toString().getBytes(Charset.forName("UTF-8")));
162147
}
148+
149+
static EnqueueToken addIPAddress(EnqueueToken token, String ipAddress)
150+
{
151+
return new EnqueueToken(token.getTokenIdentifier(), token.getCustomerId(), token.getEventId(), token.getIssued(), token.getExpires(), ipAddress, token.getPayload());
152+
}
153+
static EnqueueToken addEventId(EnqueueToken token, String eventId)
154+
{
155+
return new EnqueueToken(token.getTokenIdentifier(), token.getCustomerId(), eventId, token.getIssued(), token.getExpires(), token.getIpAddress(), token.getPayload());
156+
}
157+
static EnqueueToken addExpires(EnqueueToken token, Long expires)
158+
{
159+
return new EnqueueToken(token.getTokenIdentifier(), token.getCustomerId(), token.getEventId(), token.getIssued(), expires, token.getIpAddress(), token.getPayload());
160+
}
161+
static EnqueueToken addPayload(EnqueueToken token, IEnqueueTokenPayload payload)
162+
{
163+
return new EnqueueToken(token.getTokenIdentifier(), token.getCustomerId(), token.getEventId(), token.getIssued(), token.getExpires(), token.getIpAddress(), payload);
164+
}
165+
163166
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,31 @@ public EnqueueTokenGenerator(String customerId, String tokenIdentifierPrefix) {
1414
}
1515

1616
public EnqueueTokenGenerator withEventId(String eventId) {
17-
this.token = new EnqueueToken(this.token, eventId);
17+
this.token = EnqueueToken.addEventId(this.token, eventId);
1818

1919
return this;
2020
}
2121

22+
public EnqueueTokenGenerator withIpAddress(String ipAddress) {
23+
this.token = EnqueueToken.addIPAddress(this.token, ipAddress);
24+
25+
return this;
26+
}
27+
2228
public EnqueueTokenGenerator withValidity(long validityMillis) {
23-
this.token = new EnqueueToken(this.token, this.token.getIssued() + validityMillis);
29+
this.token = EnqueueToken.addExpires(this.token, this.token.getIssued() + validityMillis);
2430

2531
return this;
2632
}
2733

2834
public EnqueueTokenGenerator withValidity(Date expires) {
29-
this.token = new EnqueueToken(this.token, expires.getTime());
35+
this.token = EnqueueToken.addExpires(this.token, expires.getTime());
3036

3137
return this;
3238
}
3339

3440
public EnqueueTokenGenerator withPayload(IEnqueueTokenPayload payload) {
35-
this.token = new EnqueueToken(this.token, payload);
41+
this.token = EnqueueToken.addPayload(this.token, payload);
3642

3743
return this;
3844
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public interface IEnqueueToken {
88
String getTokenIdentifier();
99
String getCustomerId();
1010
String getEventId();
11+
String getIpAddress();
1112
IEnqueueTokenPayload getPayload();
1213

1314
String getTokenWithoutHash();

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public void factory_simple() throws Exception {
5151
assertTrue(endTime >= token.getIssued());
5252
assertEquals(token.getExpires(), Long.MAX_VALUE);
5353
assertNull(token.getEventId());
54+
assertNull(token.getIpAddress());
5455
assertNull(token.getPayload());
5556
}
5657

@@ -104,6 +105,18 @@ public void factory_withEventId() throws Exception {
104105
assertEquals(expectedEventId, token.getEventId());
105106
}
106107

108+
@Test
109+
public void factory_withIpAddress() throws Exception {
110+
String expectedIpAddress = "1.5.8.9";
111+
112+
IEnqueueToken token = Token
113+
.enqueue("ticketania")
114+
.withIpAddress(expectedIpAddress)
115+
.generate("5ebbf794-1665-4d48-80d6-21ac34be7faedf9e10b3-551a-4682-bb77-fee59d6355d6");
116+
117+
assertEquals(expectedIpAddress, token.getIpAddress());
118+
}
119+
107120
@Test
108121
public void factory_withBody() throws Exception {
109122
IEnqueueTokenPayload expectedPayload = Payload.enqueue().withKey("somekey").generate();
@@ -159,6 +172,7 @@ public void token_withPayload() throws Exception {
159172
"myevent",
160173
1534723200000L,
161174
1539129600000L,
175+
null,
162176
payload);
163177
token.generate("5ebbf794-1665-4d48-80d6-21ac34be7faedf9e10b3-551a-4682-bb77-fee59d6355d6", false);
164178

@@ -170,14 +184,15 @@ public void token_withPayload() throws Exception {
170184
@Test
171185
public void token_withoutPayload() throws Exception {
172186

173-
String expectedSignedToken = "eyJ0eXAiOiJRVDEiLCJlbmMiOiJBRVMyNTYiLCJpc3MiOjE1MzQ3MjMyMDAwMDAsImV4cCI6MTUzOTEyOTYwMDAwMCwidGkiOiJhMjFkNDIzYS00M2ZkLTQ4MjEtODRmYS00MzkwZjZhMmZkM2UiLCJjIjoidGlja2V0YW5pYSIsImUiOiJteWV2ZW50In0..nN4Q5wIYKktChsK1_UEuP_tjiZj9xYOd65iYv4E9hbY";
187+
String expectedSignedToken = "eyJ0eXAiOiJRVDEiLCJlbmMiOiJBRVMyNTYiLCJpc3MiOjE1MzQ3MjMyMDAwMDAsImV4cCI6MTUzOTEyOTYwMDAwMCwidGkiOiJhMjFkNDIzYS00M2ZkLTQ4MjEtODRmYS00MzkwZjZhMmZkM2UiLCJjIjoidGlja2V0YW5pYSIsImUiOiJteWV2ZW50IiwiaXAiOiI1LjcuOC42In0..rqQznIDybri70GrsJ-hd_Hzp98HUqcsBGnWaiyqjlvY";
174188

175189
EnqueueToken token = new EnqueueToken(
176190
"a21d423a-43fd-4821-84fa-4390f6a2fd3e",
177191
"ticketania",
178192
"myevent",
179193
1534723200000L,
180194
1539129600000L,
195+
"5.7.8.6",
181196
null);
182197
token.generate("5ebbf794-1665-4d48-80d6-21ac34be7faedf9e10b3-551a-4682-bb77-fee59d6355d6", false);
183198

@@ -196,7 +211,8 @@ public void token_minimalHeader() throws Exception {
196211
"ticketania",
197212
null,
198213
1534723200000L,
199-
Long.MAX_VALUE,
214+
Long.MAX_VALUE,
215+
null,
200216
null);
201217
token.generate("5ebbf794-1665-4d48-80d6-21ac34be7faedf9e10b3-551a-4682-bb77-fee59d6355d6", false);
202218

0 commit comments

Comments
 (0)