Skip to content

Commit 2a94ef4

Browse files
committed
Fix GCM without VAPID (fixes web-push-libs#73)
1 parent 1555864 commit 2a94ef4

File tree

6 files changed

+61
-23
lines changed

6 files changed

+61
-23
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 5.0.1
2+
3+
* Bugfix: Only verify the VAPID key pair if the keys are actually present (fixes #73).
4+
* Improvement: Add test configurations for GCM-only to the selenium test suite.
5+
16
# 5.0.0
27

38
* Use aes128gcm as the default encoding (#75).

src/main/java/nl/martijndwars/webpush/PushService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ public Future<HttpResponse> sendAsync(Notification notification) throws GeneralS
171171
* @throws JoseException
172172
*/
173173
public HttpPost preparePost(Notification notification, Encoding encoding) throws GeneralSecurityException, IOException, JoseException {
174-
assert (Utils.verifyKeyPair(privateKey, publicKey));
174+
if (privateKey != null && publicKey != null) {
175+
if (!Utils.verifyKeyPair(privateKey, publicKey)) {
176+
throw new IllegalStateException("Public key and private key do not match.");
177+
}
178+
}
175179

176180
Encrypted encrypted = encrypt(
177181
notification.getPayload(),

src/test/java/nl/martijndwars/webpush/selenium/BrowserTest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,21 @@
1010
import org.apache.http.HttpResponse;
1111
import org.junit.jupiter.api.function.Executable;
1212

13+
import java.security.GeneralSecurityException;
14+
1315
import static org.junit.jupiter.api.Assertions.assertEquals;
1416

1517
public class BrowserTest implements Executable {
16-
private PushService pushService;
18+
public static final String GCM_API_KEY = "AIzaSyBAU0VfXoskxUSg81K5VgLgwblHbZWe6tA";
19+
public static final String PUBLIC_KEY = "BNFDO1MUnNpx0SuQyQcAAWYETa2+W8z/uc5sxByf/UZLHwAhFLwEDxS5iB654KHiryq0AxDhFXS7DVqXDKjjN+8=";
20+
public static final String PRIVATE_KEY = "AM0aAyoIryzARADnIsSCwg1p1aWFAL3Idc8dNXpf74MH";
21+
public static final String VAPID_SUBJECT = "http://localhost:8090";
22+
1723
private TestingService testingService;
1824
private Configuration configuration;
1925
private int testSuiteId;
2026

21-
public BrowserTest(PushService pushService, TestingService testingService, Configuration configuration, int testSuiteId) {
22-
this.pushService = pushService;
27+
public BrowserTest(TestingService testingService, Configuration configuration, int testSuiteId) {
2328
this.configuration = configuration;
2429
this.testingService = testingService;
2530
this.testSuiteId = testSuiteId;
@@ -32,6 +37,8 @@ public BrowserTest(PushService pushService, TestingService testingService, Confi
3237
*/
3338
@Override
3439
public void execute() throws Throwable {
40+
PushService pushService = getPushService();
41+
3542
JsonObject test = testingService.getSubscription(testSuiteId, configuration);
3643

3744
int testId = test.get("testId").getAsInt();
@@ -49,6 +56,17 @@ public void execute() throws Throwable {
4956
assertEquals(new JsonPrimitive(message), messages.get(0));
5057
}
5158

59+
protected PushService getPushService() throws GeneralSecurityException {
60+
PushService pushService;
61+
62+
if (!configuration.isVapid()) {
63+
pushService = new PushService(GCM_API_KEY);
64+
} else {
65+
pushService = new PushService(PUBLIC_KEY, PRIVATE_KEY, VAPID_SUBJECT);
66+
}
67+
return pushService;
68+
}
69+
5270
/**
5371
* The name used by JUnit to display the test.
5472
*

src/test/java/nl/martijndwars/webpush/selenium/Configuration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ public class Configuration {
44
protected final String browser;
55
protected final String version;
66
protected final String publicKey;
7+
protected final String gcmSenderId;
78

8-
Configuration(String browser, String version, String publicKey) {
9+
Configuration(String browser, String version, String publicKey, String gcmSenderId) {
910
this.browser = browser;
1011
this.version = version;
1112
this.publicKey = publicKey;
13+
this.gcmSenderId = gcmSenderId;
1214
}
1315

1416
public boolean isVapid() {
15-
return !publicKey.isEmpty();
17+
return publicKey != null && !publicKey.isEmpty();
1618
}
1719

1820
@Override

src/test/java/nl/martijndwars/webpush/selenium/SeleniumTests.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package nl.martijndwars.webpush.selenium;
22

33
import nl.martijndwars.webpush.Base64Encoder;
4-
import nl.martijndwars.webpush.PushService;
54
import org.bouncycastle.jce.provider.BouncyCastleProvider;
65
import org.junit.jupiter.api.AfterAll;
76
import org.junit.jupiter.api.DynamicTest;
87
import org.junit.jupiter.api.TestFactory;
98

109
import java.io.IOException;
11-
import java.security.GeneralSecurityException;
1210
import java.security.Security;
1311
import java.util.stream.Stream;
1412

@@ -18,18 +16,14 @@
1816
* SeleniumTest performs integration testing.
1917
*/
2018
public class SeleniumTests {
21-
protected static String PUBLIC_KEY = "BNFDO1MUnNpx0SuQyQcAAWYETa2+W8z/uc5sxByf/UZLHwAhFLwEDxS5iB654KHiryq0AxDhFXS7DVqXDKjjN+8=";
22-
protected static String PRIVATE_KEY = "AM0aAyoIryzARADnIsSCwg1p1aWFAL3Idc8dNXpf74MH";
19+
protected static final String GCM_SENDER_ID = "759071690750";
20+
protected static final String PUBLIC_KEY = "BNFDO1MUnNpx0SuQyQcAAWYETa2+W8z/uc5sxByf/UZLHwAhFLwEDxS5iB654KHiryq0AxDhFXS7DVqXDKjjN+8=";
2321

2422
protected static TestingService testingService = new TestingService("http://localhost:8090/api/");
2523
protected static int testSuiteId;
2624

27-
protected PushService pushService;
28-
29-
public SeleniumTests() throws GeneralSecurityException {
25+
public SeleniumTests() {
3026
Security.addProvider(new BouncyCastleProvider());
31-
32-
pushService = new PushService(PUBLIC_KEY, PRIVATE_KEY, "http://localhost:8090");
3327
}
3428

3529
/**
@@ -52,7 +46,7 @@ public Stream<DynamicTest> dynamicTests() throws IOException {
5246
testSuiteId = testingService.startTestSuite();
5347

5448
return getConfigurations().map(configuration -> {
55-
BrowserTest browserTest = new BrowserTest(pushService, testingService, configuration, testSuiteId);
49+
BrowserTest browserTest = new BrowserTest(testingService, configuration, testSuiteId);
5650

5751
return dynamicTest(browserTest.getDisplayName(), browserTest);
5852
});
@@ -69,12 +63,19 @@ protected Stream<Configuration> getConfigurations() {
6963
);
7064

7165
return Stream.of(
72-
new Configuration("chrome", "stable", PUBLIC_KEY_NO_PADDING),
73-
new Configuration("chrome", "beta", PUBLIC_KEY_NO_PADDING),
74-
new Configuration("chrome", "unstable", PUBLIC_KEY_NO_PADDING),
66+
new Configuration("chrome", "stable", null, GCM_SENDER_ID),
67+
new Configuration("chrome", "beta", null, GCM_SENDER_ID),
68+
new Configuration("chrome", "unstable", null, GCM_SENDER_ID),
69+
70+
new Configuration("firefox", "stable", null, GCM_SENDER_ID),
71+
new Configuration("firefox", "beta", null, GCM_SENDER_ID),
72+
73+
new Configuration("chrome", "stable", PUBLIC_KEY_NO_PADDING, null),
74+
new Configuration("chrome", "beta", PUBLIC_KEY_NO_PADDING, null),
75+
new Configuration("chrome", "unstable", PUBLIC_KEY_NO_PADDING, null),
7576

76-
new Configuration("firefox", "stable", PUBLIC_KEY_NO_PADDING),
77-
new Configuration("firefox", "beta", PUBLIC_KEY_NO_PADDING)
77+
new Configuration("firefox", "stable", PUBLIC_KEY_NO_PADDING, null),
78+
new Configuration("firefox", "beta", PUBLIC_KEY_NO_PADDING, null)
7879
);
7980
}
8081
}

src/test/java/nl/martijndwars/webpush/selenium/TestingService.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package nl.martijndwars.webpush.selenium;
22

3-
import org.apache.commons.io.IOUtils;
43
import com.google.gson.JsonArray;
54
import com.google.gson.JsonElement;
65
import com.google.gson.JsonObject;
76
import com.google.gson.JsonParser;
7+
import org.apache.commons.io.IOUtils;
88
import org.apache.http.HttpEntity;
99
import org.apache.http.client.fluent.Request;
1010
import org.apache.http.entity.ContentType;
1111
import org.apache.http.entity.StringEntity;
1212
import org.apache.http.util.EntityUtils;
1313

1414
import java.io.IOException;
15+
1516
import static java.nio.charset.StandardCharsets.UTF_8;
1617

1718
/**
@@ -55,7 +56,14 @@ public JsonObject getSubscription(int testSuiteId, Configuration configuration)
5556
jsonObject.addProperty("testSuiteId", testSuiteId);
5657
jsonObject.addProperty("browserName", configuration.browser);
5758
jsonObject.addProperty("browserVersion", configuration.version);
58-
jsonObject.addProperty("vapidPublicKey", configuration.publicKey);
59+
60+
if (configuration.gcmSenderId != null) {
61+
jsonObject.addProperty("gcmSenderId", configuration.gcmSenderId);
62+
}
63+
64+
if (configuration.publicKey != null) {
65+
jsonObject.addProperty("vapidPublicKey", configuration.publicKey);
66+
}
5967

6068
HttpEntity entity = new StringEntity(jsonObject.toString(), ContentType.APPLICATION_JSON);
6169

0 commit comments

Comments
 (0)