github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/externalapikeys.go (about) 1 package chat 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/keybase/client/go/chat/globals" 8 "github.com/keybase/client/go/chat/utils" 9 "github.com/keybase/client/go/protocol/chat1" 10 ) 11 12 type RemoteExternalAPIKeySource struct { 13 globals.Contextified 14 utils.DebugLabeler 15 16 ri func() chat1.RemoteInterface 17 } 18 19 func NewRemoteExternalAPIKeySource(g *globals.Context, ri func() chat1.RemoteInterface) *RemoteExternalAPIKeySource { 20 return &RemoteExternalAPIKeySource{ 21 Contextified: globals.NewContextified(g), 22 DebugLabeler: utils.NewDebugLabeler(g.ExternalG(), "RemoteExternalAPIKeySource", false), 23 ri: ri, 24 } 25 } 26 27 func (r *RemoteExternalAPIKeySource) GetKey(ctx context.Context, typ chat1.ExternalAPIKeyTyp) (res chat1.ExternalAPIKey, err error) { 28 defer r.Trace(ctx, &err, "GetKey")() 29 keys, err := r.ri().GetExternalAPIKeys(ctx, []chat1.ExternalAPIKeyTyp{typ}) 30 if err != nil { 31 return res, err 32 } 33 if len(keys) != 1 { 34 return res, fmt.Errorf("wrong number of keys returned: %d", len(keys)) 35 } 36 rtyp, err := keys[0].Typ() 37 if err != nil { 38 return res, err 39 } 40 if rtyp != typ { 41 return res, fmt.Errorf("server returned wrong key: %v != %v", typ, rtyp) 42 } 43 return keys[0], nil 44 } 45 46 func (r *RemoteExternalAPIKeySource) GetAllKeys(ctx context.Context) (res []chat1.ExternalAPIKey, err error) { 47 defer r.Trace(ctx, &err, "GetAllKeys")() 48 return r.ri().GetExternalAPIKeys(ctx, nil) 49 }