/
kbpki_util.go
131 lines (116 loc) · 3.92 KB
/
kbpki_util.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package test
import (
"sync"
"github.com/adamwalz/keybase-client/go/kbfs/idutil"
"github.com/adamwalz/keybase-client/go/kbfs/tlf"
kbname "github.com/adamwalz/keybase-client/go/kbun"
"github.com/adamwalz/keybase-client/go/protocol/keybase1"
"golang.org/x/net/context"
)
// DaemonKBPKI is a hacky way to make a KBPKI instance that uses some
// methods from KeybaseService.
type DaemonKBPKI struct {
idutil.KBPKI
Daemon *idutil.DaemonLocal
}
// GetCurrentSession implements the idutil.DaemonLocal interface for
// DaemonKBPKI.
func (d *DaemonKBPKI) GetCurrentSession(ctx context.Context) (
idutil.SessionInfo, error) {
const sessionID = 0
return d.Daemon.CurrentSession(ctx, sessionID)
}
// Resolve implements the idutil.DaemonLocal interface for
// DaemonKBPKI.
func (d *DaemonKBPKI) Resolve(
ctx context.Context, assertion string,
offline keybase1.OfflineAvailability) (
kbname.NormalizedUsername, keybase1.UserOrTeamID, error) {
return d.Daemon.Resolve(ctx, assertion, offline)
}
// NormalizeSocialAssertion implements the idutil.DaemonLocal
// interface for DaemonKBPKI.
func (d *DaemonKBPKI) NormalizeSocialAssertion(
ctx context.Context, assertion string) (
keybase1.SocialAssertion, error) {
return d.Daemon.NormalizeSocialAssertion(ctx, assertion)
}
// Identify implements the idutil.DaemonLocal interface for
// DaemonKBPKI.
func (d *DaemonKBPKI) Identify(
ctx context.Context, assertion, reason string,
offline keybase1.OfflineAvailability) (
kbname.NormalizedUsername, keybase1.UserOrTeamID, error) {
return d.Daemon.Identify(ctx, assertion, reason, offline)
}
// ResolveImplicitTeam implements the idutil.DaemonLocal interface
// for DaemonKBPKI.
func (d *DaemonKBPKI) ResolveImplicitTeam(
ctx context.Context, assertions, suffix string, tlfType tlf.Type,
offline keybase1.OfflineAvailability) (
idutil.ImplicitTeamInfo, error) {
return d.Daemon.ResolveIdentifyImplicitTeam(
ctx, assertions, suffix, tlfType, false, "", offline)
}
// GetNormalizedUsername implements the idutil.DaemonLocal interface
// for DaemonKBPKI.
func (d *DaemonKBPKI) GetNormalizedUsername(
ctx context.Context, id keybase1.UserOrTeamID,
offline keybase1.OfflineAvailability) (kbname.NormalizedUsername, error) {
asUser, err := id.AsUser()
if err != nil {
return kbname.NormalizedUsername(""), err
}
userInfo, err := d.Daemon.LoadUserPlusKeys(
ctx, asUser, "", keybase1.OfflineAvailability_NONE)
if err != nil {
return kbname.NormalizedUsername(""), err
}
return userInfo.Name, nil
}
// ResolveTeamTLFID implements the idutil.DaemonLocal interface for
// DaemonKBPKI.
func (d *DaemonKBPKI) ResolveTeamTLFID(
ctx context.Context, teamID keybase1.TeamID,
offline keybase1.OfflineAvailability) (tlf.ID, error) {
settings, err := d.Daemon.GetTeamSettings(ctx, teamID, offline)
if err != nil {
return tlf.NullID, err
}
tlfID, err := tlf.ParseID(settings.TlfID.String())
if err != nil {
return tlf.NullID, err
}
return tlfID, nil
}
// IdentifyCountingKBPKI is a KBPKI instance that counts calls to
// Identify.
type IdentifyCountingKBPKI struct {
idutil.KBPKI
identifyLock sync.RWMutex
identifyCalls int
}
func (ik *IdentifyCountingKBPKI) addIdentifyCall() {
ik.identifyLock.Lock()
defer ik.identifyLock.Unlock()
ik.identifyCalls++
}
// GetIdentifyCalls returns the number of times Identify has been
// called.
func (ik *IdentifyCountingKBPKI) GetIdentifyCalls() int {
ik.identifyLock.RLock()
defer ik.identifyLock.RUnlock()
return ik.identifyCalls
}
// Identify implements the idutil.Identifier interface for
// IdentifyCountingKBPKI.
func (ik *IdentifyCountingKBPKI) Identify(
ctx context.Context, assertion, reason string,
offline keybase1.OfflineAvailability) (
kbname.NormalizedUsername, keybase1.UserOrTeamID, error) {
ik.addIdentifyCall()
return ik.KBPKI.Identify(ctx, assertion, reason, offline)
}