github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/idutil/test/kbpki_util.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package test
     6  
     7  import (
     8  	"sync"
     9  
    10  	"github.com/keybase/client/go/kbfs/idutil"
    11  	"github.com/keybase/client/go/kbfs/tlf"
    12  	kbname "github.com/keybase/client/go/kbun"
    13  	"github.com/keybase/client/go/protocol/keybase1"
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  // DaemonKBPKI is a hacky way to make a KBPKI instance that uses some
    18  // methods from KeybaseService.
    19  type DaemonKBPKI struct {
    20  	idutil.KBPKI
    21  	Daemon *idutil.DaemonLocal
    22  }
    23  
    24  // GetCurrentSession implements the idutil.DaemonLocal interface for
    25  // DaemonKBPKI.
    26  func (d *DaemonKBPKI) GetCurrentSession(ctx context.Context) (
    27  	idutil.SessionInfo, error) {
    28  	const sessionID = 0
    29  	return d.Daemon.CurrentSession(ctx, sessionID)
    30  }
    31  
    32  // Resolve implements the idutil.DaemonLocal interface for
    33  // DaemonKBPKI.
    34  func (d *DaemonKBPKI) Resolve(
    35  	ctx context.Context, assertion string,
    36  	offline keybase1.OfflineAvailability) (
    37  	kbname.NormalizedUsername, keybase1.UserOrTeamID, error) {
    38  	return d.Daemon.Resolve(ctx, assertion, offline)
    39  }
    40  
    41  // NormalizeSocialAssertion implements the idutil.DaemonLocal
    42  // interface for DaemonKBPKI.
    43  func (d *DaemonKBPKI) NormalizeSocialAssertion(
    44  	ctx context.Context, assertion string) (
    45  	keybase1.SocialAssertion, error) {
    46  	return d.Daemon.NormalizeSocialAssertion(ctx, assertion)
    47  }
    48  
    49  // Identify implements the idutil.DaemonLocal interface for
    50  // DaemonKBPKI.
    51  func (d *DaemonKBPKI) Identify(
    52  	ctx context.Context, assertion, reason string,
    53  	offline keybase1.OfflineAvailability) (
    54  	kbname.NormalizedUsername, keybase1.UserOrTeamID, error) {
    55  	return d.Daemon.Identify(ctx, assertion, reason, offline)
    56  }
    57  
    58  // ResolveImplicitTeam implements the idutil.DaemonLocal interface
    59  // for DaemonKBPKI.
    60  func (d *DaemonKBPKI) ResolveImplicitTeam(
    61  	ctx context.Context, assertions, suffix string, tlfType tlf.Type,
    62  	offline keybase1.OfflineAvailability) (
    63  	idutil.ImplicitTeamInfo, error) {
    64  	return d.Daemon.ResolveIdentifyImplicitTeam(
    65  		ctx, assertions, suffix, tlfType, false, "", offline)
    66  }
    67  
    68  // GetNormalizedUsername implements the idutil.DaemonLocal interface
    69  // for DaemonKBPKI.
    70  func (d *DaemonKBPKI) GetNormalizedUsername(
    71  	ctx context.Context, id keybase1.UserOrTeamID,
    72  	offline keybase1.OfflineAvailability) (kbname.NormalizedUsername, error) {
    73  	asUser, err := id.AsUser()
    74  	if err != nil {
    75  		return kbname.NormalizedUsername(""), err
    76  	}
    77  	userInfo, err := d.Daemon.LoadUserPlusKeys(
    78  		ctx, asUser, "", keybase1.OfflineAvailability_NONE)
    79  	if err != nil {
    80  		return kbname.NormalizedUsername(""), err
    81  	}
    82  	return userInfo.Name, nil
    83  }
    84  
    85  // ResolveTeamTLFID implements the idutil.DaemonLocal interface for
    86  // DaemonKBPKI.
    87  func (d *DaemonKBPKI) ResolveTeamTLFID(
    88  	ctx context.Context, teamID keybase1.TeamID,
    89  	offline keybase1.OfflineAvailability) (tlf.ID, error) {
    90  	settings, err := d.Daemon.GetTeamSettings(ctx, teamID, offline)
    91  	if err != nil {
    92  		return tlf.NullID, err
    93  	}
    94  	tlfID, err := tlf.ParseID(settings.TlfID.String())
    95  	if err != nil {
    96  		return tlf.NullID, err
    97  	}
    98  	return tlfID, nil
    99  }
   100  
   101  // IdentifyCountingKBPKI is a KBPKI instance that counts calls to
   102  // Identify.
   103  type IdentifyCountingKBPKI struct {
   104  	idutil.KBPKI
   105  	identifyLock  sync.RWMutex
   106  	identifyCalls int
   107  }
   108  
   109  func (ik *IdentifyCountingKBPKI) addIdentifyCall() {
   110  	ik.identifyLock.Lock()
   111  	defer ik.identifyLock.Unlock()
   112  	ik.identifyCalls++
   113  }
   114  
   115  // GetIdentifyCalls returns the number of times Identify has been
   116  // called.
   117  func (ik *IdentifyCountingKBPKI) GetIdentifyCalls() int {
   118  	ik.identifyLock.RLock()
   119  	defer ik.identifyLock.RUnlock()
   120  	return ik.identifyCalls
   121  }
   122  
   123  // Identify implements the idutil.Identifier interface for
   124  // IdentifyCountingKBPKI.
   125  func (ik *IdentifyCountingKBPKI) Identify(
   126  	ctx context.Context, assertion, reason string,
   127  	offline keybase1.OfflineAvailability) (
   128  	kbname.NormalizedUsername, keybase1.UserOrTeamID, error) {
   129  	ik.addIdentifyCall()
   130  	return ik.KBPKI.Identify(ctx, assertion, reason, offline)
   131  }