This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
71 lines (59 loc) · 1.46 KB
/
identity.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"fmt"
"regexp"
"strings"
"github.com/keybase/go-crypto/openpgp/packet"
)
type Identity struct {
Username string
Comment string
Email string
}
var idRE = regexp.MustCompile("" +
`^"?([^(<]*?)"?` + // The beginning name of the user (no comment or key)
`(?:\s*\((.*?)\)"?)?` + // The optional comment
`(?:\s*<(.*?)>)?$`) // The optional email address
func ParseIdentity(s string) (*Identity, error) {
v := idRE.FindStringSubmatch(s)
if v == nil {
return nil, fmt.Errorf("Bad PGP-style identity: %s", s)
}
ret := &Identity{
Username: v[1],
Comment: v[2],
Email: v[3],
}
return ret, nil
}
func (i Identity) Format() string {
var parts []string
if len(i.Username) > 0 {
parts = append(parts, i.Username)
}
if len(i.Comment) > 0 {
parts = append(parts, "("+i.Comment+")")
}
if len(i.Email) > 0 {
parts = append(parts, "<"+i.Email+">")
}
return strings.Join(parts, " ")
}
func (i Identity) String() string {
return i.Format()
}
func (i Identity) ToPGPUserID() *packet.UserId {
return packet.NewUserId(i.Username, i.Comment, i.Email)
}
func KeybaseIdentity(g *GlobalContext, un NormalizedUsername) Identity {
if un.IsNil() {
un = g.Env.GetUsername()
}
return Identity{
Username: CanonicalHost + "/" + un.String(),
Email: un.String() + "@" + CanonicalHost,
}
}
type Identities []Identity