nil registration when unregistering
This commit is contained in:
parent
aa78b89b40
commit
46aef6f3df
|
@ -19,6 +19,11 @@ type Persistence interface {
|
||||||
// GetPushNotificationRegistrationPublicKeys return all the public keys stored
|
// GetPushNotificationRegistrationPublicKeys return all the public keys stored
|
||||||
GetPushNotificationRegistrationPublicKeys() ([][]byte, error)
|
GetPushNotificationRegistrationPublicKeys() ([][]byte, error)
|
||||||
|
|
||||||
|
//GetPushNotificationRegistrationVersion returns the latest version or 0 for a given pk and installationID
|
||||||
|
GetPushNotificationRegistrationVersion(publicKey []byte, installationID string) (uint64, error)
|
||||||
|
// UnregisterPushNotificationRegistration unregister a given pk/installationID
|
||||||
|
UnregisterPushNotificationRegistration(publicKey []byte, installationID string, version uint64) error
|
||||||
|
|
||||||
// DeletePushNotificationRegistration deletes a push notification registration from storage given a public key and installation id
|
// DeletePushNotificationRegistration deletes a push notification registration from storage given a public key and installation id
|
||||||
DeletePushNotificationRegistration(publicKey []byte, installationID string) error
|
DeletePushNotificationRegistration(publicKey []byte, installationID string) error
|
||||||
// SavePushNotificationRegistration saves a push notification option to the db
|
// SavePushNotificationRegistration saves a push notification option to the db
|
||||||
|
@ -39,8 +44,7 @@ func NewSQLitePersistence(db *sql.DB) Persistence {
|
||||||
|
|
||||||
func (p *SQLitePersistence) GetPushNotificationRegistrationByPublicKeyAndInstallationID(publicKey []byte, installationID string) (*protobuf.PushNotificationRegistration, error) {
|
func (p *SQLitePersistence) GetPushNotificationRegistrationByPublicKeyAndInstallationID(publicKey []byte, installationID string) (*protobuf.PushNotificationRegistration, error) {
|
||||||
var marshaledRegistration []byte
|
var marshaledRegistration []byte
|
||||||
var version uint64
|
err := p.db.QueryRow(`SELECT registration FROM push_notification_server_registrations WHERE public_key = ? AND installation_id = ? AND registration IS NOT NULL`, publicKey, installationID).Scan(&marshaledRegistration)
|
||||||
err := p.db.QueryRow(`SELECT version,registration FROM push_notification_server_registrations WHERE public_key = ? AND installation_id = ?`, publicKey, installationID).Scan(&version, &marshaledRegistration)
|
|
||||||
|
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -48,21 +52,25 @@ func (p *SQLitePersistence) GetPushNotificationRegistrationByPublicKeyAndInstall
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
registration := &protobuf.PushNotificationRegistration{
|
registration := &protobuf.PushNotificationRegistration{}
|
||||||
InstallationId: installationID,
|
|
||||||
Version: version,
|
|
||||||
}
|
|
||||||
|
|
||||||
if marshaledRegistration == nil {
|
|
||||||
return registration, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := proto.Unmarshal(marshaledRegistration, registration); err != nil {
|
if err := proto.Unmarshal(marshaledRegistration, registration); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return registration, nil
|
return registration, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *SQLitePersistence) GetPushNotificationRegistrationVersion(publicKey []byte, installationID string) (uint64, error) {
|
||||||
|
registration, err := p.GetPushNotificationRegistrationByPublicKeyAndInstallationID(publicKey, installationID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if registration == nil {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
return registration.Version, nil
|
||||||
|
}
|
||||||
|
|
||||||
type PushNotificationIDAndRegistration struct {
|
type PushNotificationIDAndRegistration struct {
|
||||||
ID []byte
|
ID []byte
|
||||||
Registration *protobuf.PushNotificationRegistration
|
Registration *protobuf.PushNotificationRegistration
|
||||||
|
@ -78,7 +86,7 @@ func (p *SQLitePersistence) GetPushNotificationRegistrationByPublicKeys(publicKe
|
||||||
|
|
||||||
inVector := strings.Repeat("?, ", len(publicKeys)-1) + "?"
|
inVector := strings.Repeat("?, ", len(publicKeys)-1) + "?"
|
||||||
|
|
||||||
rows, err := p.db.Query(`SELECT public_key,registration FROM push_notification_server_registrations WHERE public_key IN (`+inVector+`)`, publicKeyArgs...) // nolint: gosec
|
rows, err := p.db.Query(`SELECT public_key,registration FROM push_notification_server_registrations WHERE registration IS NOT NULL AND public_key IN (`+inVector+`)`, publicKeyArgs...) // nolint: gosec
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -138,6 +146,11 @@ func (p *SQLitePersistence) SavePushNotificationRegistration(publicKey []byte, r
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *SQLitePersistence) UnregisterPushNotificationRegistration(publicKey []byte, installationID string, version uint64) error {
|
||||||
|
_, err := p.db.Exec(`UPDATE push_notification_server_registrations SET registration = NULL, version = ? WHERE public_key = ? AND installation_id = ?`, version, publicKey, installationID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (p *SQLitePersistence) DeletePushNotificationRegistration(publicKey []byte, installationID string) error {
|
func (p *SQLitePersistence) DeletePushNotificationRegistration(publicKey []byte, installationID string) error {
|
||||||
_, err := p.db.Exec(`DELETE FROM push_notification_server_registrations WHERE public_key = ? AND installation_id = ?`, publicKey, installationID)
|
_, err := p.db.Exec(`DELETE FROM push_notification_server_registrations WHERE public_key = ? AND installation_id = ?`, publicKey, installationID)
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -233,12 +233,12 @@ func (s *Server) validateRegistration(publicKey *ecdsa.PublicKey, payload []byte
|
||||||
return nil, ErrMalformedPushNotificationRegistrationInstallationID
|
return nil, ErrMalformedPushNotificationRegistrationInstallationID
|
||||||
}
|
}
|
||||||
|
|
||||||
previousRegistration, err := s.persistence.GetPushNotificationRegistrationByPublicKeyAndInstallationID(common.HashPublicKey(publicKey), registration.InstallationId)
|
previousVersion, err := s.persistence.GetPushNotificationRegistrationVersion(common.HashPublicKey(publicKey), registration.InstallationId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if previousRegistration != nil && registration.Version <= previousRegistration.Version {
|
if registration.Version <= previousVersion {
|
||||||
return nil, ErrInvalidPushNotificationRegistrationVersion
|
return nil, ErrInvalidPushNotificationRegistrationVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,6 +290,7 @@ func (s *Server) buildPushNotificationQueryResponse(query *protobuf.PushNotifica
|
||||||
for _, idAndResponse := range registrations {
|
for _, idAndResponse := range registrations {
|
||||||
|
|
||||||
registration := idAndResponse.Registration
|
registration := idAndResponse.Registration
|
||||||
|
|
||||||
info := &protobuf.PushNotificationQueryInfo{
|
info := &protobuf.PushNotificationQueryInfo{
|
||||||
PublicKey: idAndResponse.ID,
|
PublicKey: idAndResponse.ID,
|
||||||
Grant: registration.Grant,
|
Grant: registration.Grant,
|
||||||
|
@ -336,7 +337,7 @@ func (s *Server) buildPushNotificationRequestResponseAndSendNotification(request
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.config.Logger.Error("failed to retrieve registration", zap.Error(err))
|
s.config.Logger.Error("failed to retrieve registration", zap.Error(err))
|
||||||
report.Error = protobuf.PushNotificationReport_UNKNOWN_ERROR_TYPE
|
report.Error = protobuf.PushNotificationReport_UNKNOWN_ERROR_TYPE
|
||||||
} else if registration == nil || registration.AccessToken == "" {
|
} else if registration == nil {
|
||||||
s.config.Logger.Warn("empty registration")
|
s.config.Logger.Warn("empty registration")
|
||||||
report.Error = protobuf.PushNotificationReport_NOT_REGISTERED
|
report.Error = protobuf.PushNotificationReport_NOT_REGISTERED
|
||||||
} else if registration.AccessToken != pn.AccessToken {
|
} else if registration.AccessToken != pn.AccessToken {
|
||||||
|
@ -404,11 +405,7 @@ func (s *Server) buildPushNotificationRegistrationResponse(publicKey *ecdsa.Publ
|
||||||
if registration.Unregister {
|
if registration.Unregister {
|
||||||
s.config.Logger.Info("unregistering client")
|
s.config.Logger.Info("unregistering client")
|
||||||
// We save an empty registration, only keeping version and installation-id
|
// We save an empty registration, only keeping version and installation-id
|
||||||
emptyRegistration := &protobuf.PushNotificationRegistration{
|
if err := s.persistence.UnregisterPushNotificationRegistration(common.HashPublicKey(publicKey), registration.InstallationId, registration.Version); err != nil {
|
||||||
Version: registration.Version,
|
|
||||||
InstallationId: registration.InstallationId,
|
|
||||||
}
|
|
||||||
if err := s.persistence.SavePushNotificationRegistration(common.HashPublicKey(publicKey), emptyRegistration); err != nil {
|
|
||||||
response.Error = protobuf.PushNotificationRegistrationResponse_INTERNAL_ERROR
|
response.Error = protobuf.PushNotificationRegistrationResponse_INTERNAL_ERROR
|
||||||
s.config.Logger.Error("failed to unregister ", zap.Error(err))
|
s.config.Logger.Error("failed to unregister ", zap.Error(err))
|
||||||
return response
|
return response
|
||||||
|
|
Loading…
Reference in New Issue