Skip to content

Commit 1d54dc1

Browse files
author
aforge
committed
Added a couple todos, changed phone lib dep.
1 parent 91b16d2 commit 1d54dc1

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

go.mod

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ require (
1313
github.com/gorilla/handlers v1.4.2
1414
github.com/gorilla/websocket v1.4.2
1515
github.com/jmoiron/sqlx v1.2.0
16+
github.com/nyaruka/phonenumbers v1.0.56
1617
github.com/prometheus/client_golang v1.5.1
1718
github.com/prometheus/common v0.9.1
1819
github.com/tinode/jsonco v1.0.0
1920
github.com/tinode/snowflake v1.0.0
20-
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 // indirect
21-
github.com/ttacon/libphonenumber v1.1.0
2221
go.mongodb.org/mongo-driver v1.3.1
2322
golang.org/x/crypto v0.0.0-20200320181102-891825fb96df
2423
golang.org/x/net v0.0.0-20200320220750-118fecf932d8

go.sum

+2-4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
138138
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
139139
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
140140
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
141+
github.com/nyaruka/phonenumbers v1.0.56 h1:WdOfLJMyhXibLTBHu1MIrPmZ5eylfGaXZ9vl9h9SB08=
142+
github.com/nyaruka/phonenumbers v1.0.56/go.mod h1:sDaTZ/KPX5f8qyV9qN+hIm+4ZBARJrupC6LuhshJq1U=
141143
github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg=
142144
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
143145
github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo=
@@ -187,10 +189,6 @@ github.com/tinode/jsonco v1.0.0 h1:zVcpjzDvjuA1G+HLrckI5EiiRyq9jgV3x37OQl6e5FE=
187189
github.com/tinode/jsonco v1.0.0/go.mod h1:Bnavu3302Qfn2pILMNwASkelodgeew3IvDrbdzU84u8=
188190
github.com/tinode/snowflake v1.0.0 h1:YciQ9ZKn1TrnvpS8yZErt044XJaxWVtR9aMO9rOZVOE=
189191
github.com/tinode/snowflake v1.0.0/go.mod h1:5JiaCe3o7QdDeyRcAeZBGVghwRS+ygt2CF/hxmAoptQ=
190-
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 h1:5u+EJUQiosu3JFX0XS0qTf5FznsMOzTjGqavBGuCbo0=
191-
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2/go.mod h1:4kyMkleCiLkgY6z8gK5BkI01ChBtxR0ro3I1ZDcGM3w=
192-
github.com/ttacon/libphonenumber v1.1.0 h1:tC6kE4t8UI4OqQVQjW5q8gSWhG2wnY5moEpSEORdYm4=
193-
github.com/ttacon/libphonenumber v1.1.0/go.mod h1:E0TpmdVMq5dyVlQ7oenAkhsLu86OkUl+yR4OAxyEg/M=
194192
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk=
195193
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
196194
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc h1:n+nNi93yXLkJvKwXNP9d55HC7lGK4H/SRcwB5IaUZLo=

server/utils.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"net"
11+
"net/mail"
1112
"path/filepath"
1213
"reflect"
1314
"regexp"
@@ -20,13 +21,12 @@ import (
2021

2122
"github.com/tinode/chat/server/auth"
2223
"github.com/tinode/chat/server/store/types"
23-
"github.com/ttacon/libphonenumber"
24+
"github.com/nyaruka/phonenumbers"
2425

2526
"golang.org/x/crypto/acme/autocert"
2627
)
2728

2829
var tagPrefixRegexp = regexp.MustCompile(`^([a-z]\w{0,5}):\S`)
29-
var emailRegexp = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
3030

3131
const nullValue = "\u2421"
3232

@@ -403,20 +403,25 @@ func versionToString(vers int) string {
403403
// against the email, telephone number or login patterns.
404404
// On success, prepends the token with the corresponding prefix.
405405
func rewriteToken(orig string) string {
406-
if orig == "" || strings.HasPrefix(orig, "basic:") ||
407-
strings.HasPrefix(orig, "email:") || strings.HasPrefix(orig, "tel:") {
406+
if orig == "" || tagPrefixRegexp.MatchString(orig) {
407+
// It either empty or already has a prefix. E.g. basic:alice.
408408
return orig
409409
}
410410
// Is it email?
411-
if emailRegexp.MatchString(orig) {
411+
if r, err := mail.ParseAddress(orig); err == nil && r.Address == orig {
412412
return "email:" + orig
413413
}
414414
// TODO: pass region as a param.
415-
if num, err := libphonenumber.Parse(orig, "US"); err == nil {
415+
// 1. As provided by the client (e.g. as specified or inferred
416+
// from client's phone number or location).
417+
// 2. Use value from .conf file.
418+
// 3. Fallback to US as a last resort.
419+
if num, err := phonenumbers.Parse(orig, "US"); err == nil {
416420
// It's a phone number.
417-
return "tel:" + libphonenumber.Format(num, libphonenumber.E164)
421+
return "tel:" + phonenumbers.Format(num, phonenumbers.E164)
418422
}
419423
// Does it look like a username/login?
424+
// TODO: use configured authenticators to check if orig may a valid user name.
420425
runes := []rune(orig)
421426
if len(runes) >= 3 && unicode.IsLetter(runes[0]) {
422427
// Check if the remaining chars are letters or digits.

0 commit comments

Comments
 (0)