1
0

Enable VAPID

This commit is contained in:
2020-07-30 20:36:56 +02:00
parent b189ccc83c
commit c52aa1a514
14 changed files with 221 additions and 10 deletions

View File

@@ -0,0 +1,39 @@
package de.pushservice.server.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "push-service-server")
public class PushServiceServerConfig {
public static class Vapid {
private String publicKeyPath;
private String privateKeyPath;
public String getPublicKeyPath() {
return publicKeyPath;
}
public void setPublicKeyPath(String publicKeyPath) {
this.publicKeyPath = publicKeyPath;
}
public String getPrivateKeyPath() {
return privateKeyPath;
}
public void setPrivateKeyPath(String privateKeyPath) {
this.privateKeyPath = privateKeyPath;
}
}
private Vapid vapid;
public Vapid getVapid() {
return vapid;
}
public void setVapid(Vapid vapid) {
this.vapid = vapid;
}
}

View File

@@ -8,6 +8,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@@ -22,16 +23,29 @@ public class PushServiceController {
private PushService pushService;
static {
Security.addProvider(new BouncyCastleProvider());
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
Security.addProvider(new BouncyCastleProvider());
}
}
@PostMapping("/notify")
public ResponseEntity notify(@RequestBody NotificationRequestDto notificationRequestDto) {
LOGGER.debug(String.format("notify called with %s", notificationRequestDto));
return this.pushService.notify(notificationRequestDto).toResponseEntity();
}
@PostMapping("/subscribe")
public ResponseEntity subscribe(@RequestBody SubscriptionDto subscriptionDto) {
LOGGER.debug(String.format("subscribe called with %s", subscriptionDto));
return this.pushService.subscribe(subscriptionDto).toResponseEntity();
}
@GetMapping("/vapidPublicKey")
public String getVapidPublicKey() {
LOGGER.debug("vapidPublicKey called");
return this.pushService.getVapidPublicKey();
}
}

View File

@@ -4,12 +4,18 @@ import de.pushservice.client.ResponseReason;
import de.pushservice.client.dto.MessageDto;
import de.pushservice.client.dto.NotificationRequestDto;
import de.pushservice.client.dto.SubscriptionDto;
import de.pushservice.server.config.PushServiceServerConfig;
import de.pushservice.server.dba.SubscriptionRepository;
import de.pushservice.server.decorator.SubscriptionDecorator;
import de.pushservice.server.model.Subscription;
import nl.martijndwars.webpush.Notification;
import nl.martijndwars.webpush.Urgency;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -17,7 +23,13 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.security.KeyPair;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.Optional;
@Service
public class PushService {
@@ -29,10 +41,21 @@ public class PushService {
@Autowired
private ExternalPushServiceResponseHandler handler;
@Autowired
private PushServiceServerConfig config;
@Transactional(propagation = Propagation.REQUIRED)
public ResponseReason subscribe(SubscriptionDto subscriptionDto) {
if (this.subscriptionRepository
.getForScopeAndEndpoint(subscriptionDto.getScope(), subscriptionDto.getEndpoint()).isPresent()) {
Optional<Subscription> optSubscription = this.subscriptionRepository
.getForScopeAndEndpoint(subscriptionDto.getScope(), subscriptionDto.getEndpoint());
if (optSubscription.isPresent()) {
final Subscription subscription = optSubscription.get();
subscription.setLastSeen(LocalDateTime.now());
this.subscriptionRepository.save(subscription);
return ResponseReason.OK;
}
@@ -58,9 +81,14 @@ public class PushService {
final Iterable<Subscription> subscriptions = this.subscriptionRepository
.getAllForScope(notificationRequestDto.getScope());
final nl.martijndwars.webpush.PushService extPushService = new nl.martijndwars.webpush.PushService();
LOGGER.debug(String.format("Found %s subscriptions for scope %s", IterableUtils.size(subscriptions), notificationRequestDto.getScope()));
final nl.martijndwars.webpush.PushService extPushService = new nl.martijndwars.webpush.PushService(getKeyPair());
for (Subscription subscription : subscriptions) {
LOGGER.debug(String.format("Process endpoint %s", subscription.getEndpoint()));
try {
SubscriptionDecorator subscriptionDecorator = SubscriptionDecorator.from(subscription);
Notification notification = Notification.builder()
@@ -87,4 +115,51 @@ public class PushService {
private Urgency getExtUrgency(MessageDto messageDto) {
return Urgency.valueOf(messageDto.getUrgency().name());
}
public String getVapidPublicKey() {
if (StringUtils.isEmpty(this.config.getVapid().getPublicKeyPath())) {
return null;
}
final File publicKeyFile = new File(this.config.getVapid().getPublicKeyPath());
if (!publicKeyFile.exists()) {
return null;
}
try (FileReader fr = new FileReader(publicKeyFile)) {
PEMParser pemParser = new PEMParser(fr);
SubjectPublicKeyInfo keyInfo = (SubjectPublicKeyInfo) pemParser.readObject();
return Base64.getEncoder().encodeToString(keyInfo.getPublicKeyData().getBytes());
}
catch (IOException ioe) {
LOGGER.error(String.format("Could not read key! %s", this.config.getVapid().getPublicKeyPath()), ioe);
return null;
}
}
private KeyPair getKeyPair() {
if (StringUtils.isEmpty(this.config.getVapid().getPrivateKeyPath())) {
return null;
}
final File publicKeyFile = new File(this.config.getVapid().getPrivateKeyPath());
if (!publicKeyFile.exists()) {
return null;
}
try (FileReader fr = new FileReader(publicKeyFile)) {
PEMParser pemParser = new PEMParser(fr);
return new JcaPEMKeyConverter().getKeyPair((PEMKeyPair) pemParser.readObject());
}
catch (IOException ioe) {
LOGGER.error(String.format("Could not read key! %s", this.config.getVapid().getPrivateKeyPath()), ioe);
return null;
}
}
}

View File

@@ -1,3 +1,6 @@
spring.datasource.url=jdbc:postgresql://localhost/push_service_mk
spring.datasource.username=push_service_mk
spring.datasource.password=push_service_mk
spring.datasource.password=push_service_mk
push-service-server.vapid.public-key-path=/opt/push-service/mk/vapid_public.pem
push-service-server.vapid.private-key-path=/opt/push-service/mk/vapid_private.pem

View File

@@ -18,4 +18,8 @@ logging.file.max-size=50MB
# because the connection pool cannot shutdown properly
spring.jmx.enabled=false
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.ddl-auto=validate
# Follow https://github.com/web-push-libs/webpush-java/wiki/VAPID how to create the keys
push-service-server.vapid.public-key-path=/tmp/vapid_public.pem
push-service-server.vapid.private-key-path=/tmp/vapid_private.pem