github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/engine/pgp_keyfinder.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 engine
     5  
     6  import (
     7  	"github.com/keybase/client/go/libkb"
     8  )
     9  
    10  // PGPKeyfinder is an engine to find PGP Keys for users (loaded by
    11  // assertions), possibly tracking them if necessary.
    12  type PGPKeyfinder struct {
    13  	arg    *PGPKeyfinderArg
    14  	uplus  []*UserPlusKeys
    15  	runerr error
    16  	libkb.Contextified
    17  }
    18  
    19  type PGPKeyfinderArg struct {
    20  	Usernames []string // must be keybase usernames
    21  }
    22  
    23  // NewPGPKeyfinder creates a PGPKeyfinder engine.
    24  func NewPGPKeyfinder(g *libkb.GlobalContext, arg *PGPKeyfinderArg) *PGPKeyfinder {
    25  	return &PGPKeyfinder{
    26  		arg:          arg,
    27  		Contextified: libkb.NewContextified(g),
    28  	}
    29  }
    30  
    31  // Name is the unique engine name.
    32  func (e *PGPKeyfinder) Name() string {
    33  	return "PGPKeyfinder"
    34  }
    35  
    36  // GetPrereqs returns the engine prereqs.
    37  func (e *PGPKeyfinder) Prereqs() Prereqs {
    38  	return Prereqs{}
    39  }
    40  
    41  // RequiredUIs returns the required UIs.
    42  func (e *PGPKeyfinder) RequiredUIs() []libkb.UIKind {
    43  	return []libkb.UIKind{}
    44  }
    45  
    46  // SubConsumers returns the other UI consumers for this engine.
    47  func (e *PGPKeyfinder) SubConsumers() []libkb.UIConsumer {
    48  	return []libkb.UIConsumer{}
    49  }
    50  
    51  // Run starts the engine.
    52  func (e *PGPKeyfinder) Run(m libkb.MetaContext) error {
    53  	e.loadUsers(m)
    54  	e.loadKeys(m)
    55  	return e.runerr
    56  }
    57  
    58  // UsersPlusKeys returns the users found while running the engine,
    59  // plus their pgp keys.
    60  func (e *PGPKeyfinder) UsersPlusKeys() []*UserPlusKeys {
    61  	return e.uplus
    62  }
    63  
    64  // don't identify or track, just load the users
    65  func (e *PGPKeyfinder) loadUsers(m libkb.MetaContext) {
    66  	if e.runerr != nil {
    67  		return
    68  	}
    69  
    70  	for _, u := range e.arg.Usernames {
    71  		arg := libkb.NewLoadUserByNameArg(e.G(), u)
    72  		user, err := libkb.LoadUser(arg)
    73  		if err != nil {
    74  			e.runerr = err
    75  			return
    76  		}
    77  		e.addUser(user, false)
    78  	}
    79  
    80  }
    81  
    82  func (e *PGPKeyfinder) loadKeys(m libkb.MetaContext) {
    83  	if e.runerr != nil {
    84  		return
    85  	}
    86  
    87  	// get the pgp keys for all the users
    88  	for _, x := range e.uplus {
    89  		keys := x.User.GetActivePGPKeys(true)
    90  		if len(keys) == 0 {
    91  			e.runerr = libkb.NoPGPEncryptionKeyError{
    92  				User:                    x.User.GetName(),
    93  				HasKeybaseEncryptionKey: x.User.HasEncryptionSubkey() || (x.User.GetComputedKeyFamily().GetLatestPerUserKey() != nil),
    94  			}
    95  			return
    96  		}
    97  		x.Keys = keys
    98  	}
    99  }
   100  
   101  type UserPlusKeys struct {
   102  	User      *libkb.User
   103  	IsTracked bool
   104  	Keys      []*libkb.PGPKeyBundle
   105  }
   106  
   107  func (e *PGPKeyfinder) addUser(user *libkb.User, tracked bool) {
   108  	e.uplus = append(e.uplus, &UserPlusKeys{User: user, IsTracked: tracked})
   109  }