Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Store notification public keys during push registration

AmanRajSinghMourya4953869f7a40
diff --git a/server/ente/push.go b/server/ente/push.go
index 4dba725249..90cd738c91 100644
--- a/server/ente/push.go
+++ b/server/ente/push.go
@@ -9,6 +9,7 @@ type PushTokenRequest struct {
FCMToken string `json:"fcmToken" binding:"required"`
APNSToken string `json:"apnsToken"`
Platform *string `json:"platform" binding:"required,oneof=ios android"`
+ NotificationKey []byte `json:"notificationKey"`
LastNotificationTime int64
}
diff --git a/server/migrations/152_push_notification_keys.down.sql b/server/migrations/152_push_notification_keys.down.sql
new file mode 100644
index 0000000000..e3368c8230
--- /dev/null
+++ b/server/migrations/152_push_notification_keys.down.sql
@@ -0,0 +1 @@
+ALTER TABLE push_tokens DROP COLUMN notification_public_key;
diff --git a/server/migrations/152_push_notification_keys.up.sql b/server/migrations/152_push_notification_keys.up.sql
new file mode 100644
index 0000000000..f92530eeff
--- /dev/null
+++ b/server/migrations/152_push_notification_keys.up.sql
@@ -0,0 +1,2 @@
+ALTER TABLE push_tokens ADD COLUMN notification_public_key BYTEA
+ CHECK (notification_public_key IS NULL OR octet_length(notification_public_key) = 32);
diff --git a/server/pkg/controller/push.go b/server/pkg/controller/push.go
index 1b7fbbfc6c..d92d02c567 100644
--- a/server/pkg/controller/push.go
+++ b/server/pkg/controller/push.go
@@ -3,6 +3,7 @@ package controller
import (
"bytes"
"context"
+ "encoding/base64"
"encoding/json"
"errors"
"fmt"
@@ -17,6 +18,7 @@ import (
"github.com/ente/museum/ente"
"github.com/ente/museum/pkg/repo"
"github.com/ente/museum/pkg/utils/config"
+ "github.com/ente/museum/pkg/utils/crypto"
"github.com/ente/museum/pkg/utils/time"
"github.com/ente/stacktrace"
log "github.com/sirupsen/logrus"
@@ -100,6 +102,11 @@ func newFCMClient() (*fcmClient, error) {
}
func (c *PushController) AddToken(userID int64, sessionTokenHash []byte, token ente.PushTokenRequest) error {
+ if token.NotificationKey != nil {
+ if err := crypto.ValidateSealedBoxPublicKey(base64.StdEncoding.EncodeToString(token.NotificationKey)); err != nil {
+ return ente.NewBadRequestWithMessage("invalid notification public key")
+ }
+ }
return stacktrace.Propagate(c.PushRepo.AddToken(userID, sessionTokenHash, token), "")
}
diff --git a/server/pkg/repo/push.go b/server/pkg/repo/push.go
index 7a4e54ee44..abac4a5e02 100644
--- a/server/pkg/repo/push.go
+++ b/server/pkg/repo/push.go
@@ -15,13 +15,17 @@ type PushTokenRepository struct {
}
func (repo *PushTokenRepository) AddToken(userID int64, sessionTokenHash []byte, token ente.PushTokenRequest) error {
- result, err := repo.DB.Exec(`INSERT INTO push_tokens(user_id, fcm_token, apns_token, session_token_hash, platform)
- SELECT user_id, $2, $3, token_hash, $5 FROM tokens
+ var publicKey any
+ if token.NotificationKey != nil {
+ publicKey = token.NotificationKey
+ }
+ result, err := repo.DB.Exec(`INSERT INTO push_tokens(user_id, fcm_token, apns_token, session_token_hash, platform, notification_public_key)
+ SELECT user_id, $2, $3, token_hash, $5, $6 FROM tokens
WHERE user_id = $1 AND token_hash = $4 AND is_deleted = false FOR SHARE
ON CONFLICT (fcm_token) DO UPDATE
SET user_id = EXCLUDED.user_id, apns_token = EXCLUDED.apns_token, session_token_hash = EXCLUDED.session_token_hash,
- platform = EXCLUDED.platform`,
- userID, token.FCMToken, token.APNSToken, sessionTokenHash, token.Platform)
+ platform = EXCLUDED.platform, notification_public_key = EXCLUDED.notification_public_key`,
+ userID, token.FCMToken, token.APNSToken, sessionTokenHash, token.Platform, publicKey)
if err != nil {
return stacktrace.Propagate(err, "")
}