Skip to content

Commit

Permalink
Delete session if prekey upload returns 422
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 31, 2024
1 parent 6ab4669 commit 79d42dd
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/signalmeow/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"
Expand Down Expand Up @@ -342,6 +343,8 @@ func KyberPreKeyToJSON(kyberPreKey *libsignalgo.KyberPreKeyRecord) (map[string]i
return kyberPreKeyJson, nil
}

var errPrekeyUpload422 = errors.New("http 422 while registering prekeys")

func RegisterPreKeys(ctx context.Context, generatedPreKeys *GeneratedPreKeys, pni bool, username string, password string) error {
log := zerolog.Ctx(ctx).With().Str("action", "register prekeys").Logger()
// Convert generated prekeys to JSON
Expand Down Expand Up @@ -383,7 +386,9 @@ func RegisterPreKeys(ctx context.Context, generatedPreKeys *GeneratedPreKeys, pn
}
defer resp.Body.Close()
// status code not 2xx
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
if resp.StatusCode == 422 {
return errPrekeyUpload422
} else if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("error registering prekeys: %v", resp.Status)
}
return err
Expand Down Expand Up @@ -619,6 +624,14 @@ func (cli *Client) StartKeyCheckLoop(ctx context.Context) {
}
err = cli.CheckAndUploadNewPreKeys(ctx, cli.Store.PNIPreKeyStore)
if err != nil {
if errors.Is(err, errPrekeyUpload422) {
log.Err(err).Msg("Got 422 error while uploading PNI prekeys, deleting session")
disconnectErr := cli.ClearKeysAndDisconnect(ctx)
if disconnectErr != nil {
log.Err(disconnectErr).Msg("ClearKeysAndDisconnect error")
}
return
}
log.Err(err).Msg("Error checking and uploading new prekeys for PNI identity")
// Retry within half an hour
window_start = 5
Expand Down

0 comments on commit 79d42dd

Please sign in to comment.