mirror of
https://github.com/lightningnetwork/lnd.git
synced 2024-11-19 01:43:16 +01:00
045765111a
Fixes #4383 by adding a new SafeCopyMacaroon function that correctly clones all caveats and prevents modifications on the copy from affecting the original.
54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package macaroons
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
|
|
macaroon "gopkg.in/macaroon.v2"
|
|
)
|
|
|
|
// MacaroonCredential wraps a macaroon to implement the
|
|
// credentials.PerRPCCredentials interface.
|
|
type MacaroonCredential struct {
|
|
*macaroon.Macaroon
|
|
}
|
|
|
|
// RequireTransportSecurity implements the PerRPCCredentials interface.
|
|
func (m MacaroonCredential) RequireTransportSecurity() bool {
|
|
return true
|
|
}
|
|
|
|
// GetRequestMetadata implements the PerRPCCredentials interface. This method
|
|
// is required in order to pass the wrapped macaroon into the gRPC context.
|
|
// With this, the macaroon will be available within the request handling scope
|
|
// of the ultimate gRPC server implementation.
|
|
func (m MacaroonCredential) GetRequestMetadata(ctx context.Context,
|
|
uri ...string) (map[string]string, error) {
|
|
|
|
macBytes, err := m.MarshalBinary()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
md := make(map[string]string)
|
|
md["macaroon"] = hex.EncodeToString(macBytes)
|
|
return md, nil
|
|
}
|
|
|
|
// NewMacaroonCredential returns a copy of the passed macaroon wrapped in a
|
|
// MacaroonCredential struct which implements PerRPCCredentials.
|
|
func NewMacaroonCredential(m *macaroon.Macaroon) (MacaroonCredential, error) {
|
|
ms := MacaroonCredential{}
|
|
|
|
// The macaroon library's Clone() method has a subtle bug that doesn't
|
|
// correctly clone all caveats. We need to use our own, safe clone
|
|
// function instead.
|
|
var err error
|
|
ms.Macaroon, err = SafeCopyMacaroon(m)
|
|
if err != nil {
|
|
return ms, err
|
|
}
|
|
|
|
return ms, nil
|
|
}
|