Skip to content

Commit

Permalink
pair-code: update docs and return error if phone number is too short
Browse files Browse the repository at this point in the history
Fixes #647
  • Loading branch information
tulir committed Aug 28, 2024
1 parent 3d63c6f commit 8acde1b
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pair-code.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"fmt"
"regexp"
"strconv"
"strings"

"go.mau.fi/util/random"
"golang.org/x/crypto/curve25519"
Expand Down Expand Up @@ -72,7 +73,9 @@ func generateCompanionEphemeralKey() (ephemeralKeyPair *keys.KeyPair, ephemeralK
// PairPhone generates a pairing code that can be used to link to a phone without scanning a QR code.
//
// You must connect the client normally before calling this (which means you'll also receive a QR code
// event, but that can be ignored when doing code pairing).
// event, but that can be ignored when doing code pairing). You should also wait for `*events.QR` before
// calling this to ensure the connection is fully established. If using [Client.GetQRChannel], wait for
// the first item in the channel. Alternatively, sleeping for a second after calling Connect will probably work too.
//
// The exact expiry of pairing codes is unknown, but QR codes are always generated and the login websocket is closed
// after the QR codes run out, which means there's a 160-second time limit. It is recommended to generate the pairing
Expand All @@ -86,6 +89,11 @@ func generateCompanionEphemeralKey() (ephemeralKeyPair *keys.KeyPair, ephemeralK
func (cli *Client) PairPhone(phone string, showPushNotification bool, clientType PairClientType, clientDisplayName string) (string, error) {
ephemeralKeyPair, ephemeralKey, encodedLinkingCode := generateCompanionEphemeralKey()
phone = notNumbers.ReplaceAllString(phone, "")
if len(phone) <= 6 {
return "", fmt.Errorf("phone number too short")
} else if strings.HasPrefix(phone, "0") {
return "", fmt.Errorf("international phone number required (must not start with 0)")
}
jid := types.NewJID(phone, types.DefaultUserServer)
resp, err := cli.sendIQ(infoQuery{
Namespace: "md",
Expand Down

0 comments on commit 8acde1b

Please sign in to comment.