github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/downgrade_leases.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 libkb
     5  
     6  import (
     7  	"strings"
     8  
     9  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    10  	context "golang.org/x/net/context"
    11  )
    12  
    13  func kidsToString(kids []keybase1.KID) string {
    14  	var tmp []string
    15  	for _, k := range kids {
    16  		tmp = append(tmp, string(k))
    17  	}
    18  	return strings.Join(tmp, ",")
    19  }
    20  
    21  func sigIDsToString(sigIDs []keybase1.SigID) string {
    22  	var tmp []string
    23  	for _, k := range sigIDs {
    24  		tmp = append(tmp, string(k))
    25  	}
    26  	return strings.Join(tmp, ",")
    27  }
    28  
    29  func UidsToString(uids []keybase1.UID) string {
    30  	s := make([]string, len(uids))
    31  	for i, uid := range uids {
    32  		s[i] = string(uid)
    33  	}
    34  	return strings.Join(s, ",")
    35  }
    36  
    37  type Lease struct {
    38  	MerkleSeqno keybase1.Seqno    `json:"merkle_seqno"`
    39  	LeaseID     keybase1.LeaseID  `json:"downgrade_lease_id"`
    40  	HashMeta    keybase1.HashMeta `json:"hash_meta"`
    41  }
    42  
    43  type leaseReply struct {
    44  	Lease
    45  	Status AppStatus `json:"status"`
    46  }
    47  
    48  func (r *leaseReply) GetAppStatus() *AppStatus {
    49  	return &r.Status
    50  }
    51  
    52  func RequestDowngradeLeaseByKID(ctx context.Context, g *GlobalContext, kids []keybase1.KID) (lease *Lease, mr *MerkleRoot, err error) {
    53  	var res leaseReply
    54  	mctx := NewMetaContext(ctx, g)
    55  	err = g.API.PostDecode(mctx, APIArg{
    56  		Endpoint:    "downgrade/key",
    57  		SessionType: APISessionTypeREQUIRED,
    58  		Args: HTTPArgs{
    59  			"kids": S{kidsToString(kids)},
    60  		},
    61  	}, &res)
    62  	if err != nil {
    63  		return nil, nil, err
    64  	}
    65  	return leaseWithMerkleRoot(ctx, g, res)
    66  }
    67  
    68  func CancelDowngradeLease(ctx context.Context, g *GlobalContext, l keybase1.LeaseID) error {
    69  	mctx := NewMetaContext(ctx, g)
    70  	_, err := g.API.Post(mctx, APIArg{
    71  		Endpoint:    "downgrade/cancel",
    72  		SessionType: APISessionTypeREQUIRED,
    73  		Args: HTTPArgs{
    74  			"downgrade_lease_id": S{string(l)},
    75  		},
    76  	})
    77  	return err
    78  }
    79  
    80  func RequestDowngradeLeaseBySigIDs(ctx context.Context, g *GlobalContext, sigIDs []keybase1.SigID) (lease *Lease, mr *MerkleRoot, err error) {
    81  	var res leaseReply
    82  	mctx := NewMetaContext(ctx, g)
    83  	err = g.API.PostDecode(mctx, APIArg{
    84  		Endpoint:    "downgrade/sig",
    85  		SessionType: APISessionTypeREQUIRED,
    86  		Args: HTTPArgs{
    87  			"sig_ids": S{sigIDsToString(sigIDs)},
    88  		},
    89  	}, &res)
    90  	if err != nil {
    91  		return nil, nil, err
    92  	}
    93  	return leaseWithMerkleRoot(ctx, g, res)
    94  }
    95  
    96  func RequestDowngradeLeaseByTeam(ctx context.Context, g *GlobalContext, teamID keybase1.TeamID, uids []keybase1.UID) (lease *Lease, mr *MerkleRoot, err error) {
    97  	var res leaseReply
    98  	mctx := NewMetaContext(ctx, g)
    99  	err = g.API.PostDecode(mctx, APIArg{
   100  		Endpoint:    "downgrade/team",
   101  		SessionType: APISessionTypeREQUIRED,
   102  		Args: HTTPArgs{
   103  			"team_id":     S{string(teamID)},
   104  			"member_uids": S{UidsToString(uids)},
   105  		},
   106  	}, &res)
   107  	if err != nil {
   108  		return nil, nil, err
   109  	}
   110  	return leaseWithMerkleRoot(ctx, g, res)
   111  }
   112  
   113  func leaseWithMerkleRoot(ctx context.Context, g *GlobalContext, res leaseReply) (lease *Lease, mr *MerkleRoot, err error) {
   114  	mr, err = g.MerkleClient.FetchRootFromServerByMinSeqno(NewMetaContext(ctx, g), res.Lease.MerkleSeqno)
   115  	if err != nil {
   116  		return nil, nil, err
   117  	}
   118  	return &res.Lease, mr, nil
   119  }