github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/libkb/identity.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package libkb 5 6 import ( 7 "fmt" 8 "regexp" 9 "strings" 10 11 "github.com/keybase/go-crypto/openpgp/packet" 12 ) 13 14 type Identity struct { 15 Username string 16 Comment string 17 Email string 18 } 19 20 var idRE = regexp.MustCompile("" + 21 `^"?([^(<]*?)"?` + // The beginning name of the user (no comment or key) 22 `(?:\s*\((.*?)\)"?)?` + // The optional comment 23 `(?:\s*<(.*?)>)?$`) // The optional email address 24 25 func ParseIdentity(s string) (*Identity, error) { 26 v := idRE.FindStringSubmatch(s) 27 if v == nil { 28 return nil, fmt.Errorf("Bad PGP-style identity: %s", s) 29 } 30 ret := &Identity{ 31 Username: v[1], 32 Comment: v[2], 33 Email: v[3], 34 } 35 return ret, nil 36 } 37 38 func (i Identity) Format() string { 39 var parts []string 40 if len(i.Username) > 0 { 41 parts = append(parts, i.Username) 42 } 43 if len(i.Comment) > 0 { 44 parts = append(parts, "("+i.Comment+")") 45 } 46 if len(i.Email) > 0 { 47 parts = append(parts, "<"+i.Email+">") 48 } 49 return strings.Join(parts, " ") 50 } 51 52 func (i Identity) String() string { 53 return i.Format() 54 } 55 56 func (i Identity) ToPGPUserID() *packet.UserId { 57 return packet.NewUserId(i.Username, i.Comment, i.Email) 58 59 } 60 61 func KeybaseIdentity(g *GlobalContext, un NormalizedUsername) Identity { 62 if un.IsNil() { 63 un = g.Env.GetUsername() 64 } 65 return Identity{ 66 Username: CanonicalHost + "/" + un.String(), 67 Email: un.String() + "@" + CanonicalHost, 68 } 69 } 70 71 type Identities []Identity