forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
75 lines (68 loc) · 2.49 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package emails
import (
"context"
"net/mail"
"strings"
"github.com/keybase/client/go/externals"
"github.com/keybase/client/go/libkb"
)
// splitBulk splits on newline or comma.
func splitBulk(s string) []string {
f := func(c rune) bool {
return c == '\n' || c == ','
}
split := strings.FieldsFunc(s, f)
for i, s := range split {
split[i] = strings.TrimSpace(s)
}
return split
}
// ParseSeparatedEmails parses a comma-or-new-line-separated email list that
// comes in a string. It can extract emails that conform to RFC 5322 and RFC
// 6532 (see net/mail documentation for more info).
//
// Examples of a valid e-mail entities would be:
// - Jan Smith <jan@example.com>
// - alice@example.org
//
// The "name" portion is ignored, caller will always get the raw e-mails, in
// this case: { "jan@example.com", "alice@example.org" }.
//
// Individual e-mails have to be separated by comma or newline, and they can be
// surrounded by any amount of whitespace characters that will be ignored.
//
// `malformed` is an optional pointer to string list to which this function can
// append malformed e-mails for the caller. It can be nil.
func ParseSeparatedEmails(mctx libkb.MetaContext, emails string, malformed *[]string) (ret []string) {
emailList := splitBulk(emails)
mctx.Debug("ParseSeparatedEmails: bulk email invite count: %d", len(emailList))
for _, email := range emailList {
addr, parseErr := mail.ParseAddress(email)
if parseErr != nil {
mctx.Debug("ParseSeparatedEmails: skipping malformed email %q: %s", email, parseErr)
if malformed != nil {
*malformed = append(*malformed, email)
}
continue
}
// API server side of this only accepts x.yy domain name:
parts := strings.Split(addr.Address, ".")
if len(parts[len(parts)-1]) < 2 {
mctx.Debug("ParseSeparatedEmails: skipping malformed email (domain) %q", email)
if malformed != nil {
*malformed = append(*malformed, email)
}
continue
}
ret = append(ret, addr.Address)
}
return ret
}
// CreateAssertionFromEmail creates AssertionURL from an e-mail address. E-mail
// address is not validated apart from the minimal validation that assertion
// code does, so pretty much anything that has '@' in it will pass.
func CreateAssertionFromEmail(ctx context.Context, email string) (libkb.AssertionURL, error) {
actx := externals.MakeStaticAssertionContext(ctx)
// `strict` argument here doesn't actually do anything for "email" assertions.
return libkb.ParseAssertionURLKeyValue(actx, "email", email, false /* strict */)
}