Skip to content

Commit 2510b7b

Browse files
authored
Merge pull request #102 from konkit/optional-vapid-subject
Make VAPID subject claim optional
2 parents 111afcd + 16d93cc commit 2510b7b

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public class PushService {
3434
private String gcmApiKey;
3535

3636
/**
37-
* Subject used in the JWT payload (for VAPID)
37+
* Subject used in the JWT payload (for VAPID). When left as null, then no subject will be used
38+
* (RFC-8292 2.1 says that it is optional)
3839
*/
3940
private String subject;
4041

@@ -55,15 +56,23 @@ public PushService(String gcmApiKey) {
5556
this.gcmApiKey = gcmApiKey;
5657
}
5758

58-
public PushService(KeyPair keyPair, String subject) {
59+
public PushService(KeyPair keyPair) {
5960
this.publicKey = keyPair.getPublic();
6061
this.privateKey = keyPair.getPrivate();
62+
}
63+
64+
public PushService(KeyPair keyPair, String subject) {
65+
this(keyPair);
6166
this.subject = subject;
6267
}
6368

64-
public PushService(String publicKey, String privateKey, String subject) throws GeneralSecurityException {
69+
public PushService(String publicKey, String privateKey) throws GeneralSecurityException {
6570
this.publicKey = Utils.loadPublicKey(publicKey);
6671
this.privateKey = Utils.loadPrivateKey(privateKey);
72+
}
73+
74+
public PushService(String publicKey, String privateKey, String subject) throws GeneralSecurityException {
75+
this(publicKey, privateKey);
6776
this.subject = subject;
6877
}
6978

@@ -226,7 +235,9 @@ public HttpPost preparePost(Notification notification, Encoding encoding) throws
226235
JwtClaims claims = new JwtClaims();
227236
claims.setAudience(notification.getOrigin());
228237
claims.setExpirationTimeMinutesInTheFuture(12 * 60);
229-
claims.setSubject(subject);
238+
if (subject != null) {
239+
claims.setSubject(subject);
240+
}
230241

231242
JsonWebSignature jws = new JsonWebSignature();
232243
jws.setHeader("typ", "JWT");

0 commit comments

Comments
 (0)