π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Store notification public keys during push registration
AmanRajSinghMourya4953869f7a40
diff --git a/server/ente/push.go b/server/ente/push.goindex 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.sqlnew file mode 100644index 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.sqlnew file mode 100644index 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.goindex 1b7fbbfc6c..d92d02c567 100644--- a/server/pkg/controller/push.go+++ b/server/pkg/controller/push.go@@ -3,6 +3,7 @@ package controllerimport ("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.goindex 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 tokensWHERE user_id = $1 AND token_hash = $4 AND is_deleted = false FOR SHAREON CONFLICT (fcm_token) DO UPDATESET 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, "")}