2017-08-18 04:50:15 +03:00
|
|
|
package macaroons
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"gopkg.in/macaroon-bakery.v1/bakery"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// dbFileName is the filename within the data directory which contains
|
|
|
|
// the macaroon stores.
|
|
|
|
dbFilename = "macaroons.db"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewService returns a service backed by the macaroon Bolt DB stored in the
|
|
|
|
// passed directory.
|
|
|
|
func NewService(dir string) (*bakery.Service, error) {
|
2017-08-22 09:18:19 +03:00
|
|
|
// Open the database that we'll use to store the primary macaroon key,
|
|
|
|
// and all generated macaroons+caveats.
|
2017-08-18 04:50:15 +03:00
|
|
|
macaroonDB, err := bolt.Open(path.Join(dir, dbFilename), 0600,
|
|
|
|
bolt.DefaultOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-22 09:18:19 +03:00
|
|
|
|
2017-08-18 04:50:15 +03:00
|
|
|
rootKeyStore, err := NewRootKeyStorage(macaroonDB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
macaroonStore, err := NewStorage(macaroonDB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-22 09:18:19 +03:00
|
|
|
|
2017-08-18 04:50:15 +03:00
|
|
|
macaroonParams := bakery.NewServiceParams{
|
|
|
|
Location: "lnd",
|
|
|
|
Store: macaroonStore,
|
|
|
|
RootKeyStore: rootKeyStore,
|
|
|
|
// No third-party caveat support for now.
|
|
|
|
// TODO(aakselrod): Add third-party caveat support.
|
|
|
|
Locator: nil,
|
|
|
|
Key: nil,
|
|
|
|
}
|
|
|
|
return bakery.NewService(macaroonParams)
|
|
|
|
}
|