-
Notifications
You must be signed in to change notification settings - Fork 13
/
macaroons.go
58 lines (42 loc) · 1.34 KB
/
macaroons.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package rpcserver
import (
"github.com/BoltzExchange/boltz-lnd/database"
"github.com/BoltzExchange/boltz-lnd/logger"
"github.com/BoltzExchange/boltz-lnd/macaroons"
"github.com/BoltzExchange/boltz-lnd/utils"
"gopkg.in/macaroon-bakery.v2/bakery"
"io/ioutil"
)
func (server *RpcServer) generateMacaroons(database *database.Database) (*macaroons.Service, error) {
logger.Info("Enabling Macaroon authentication")
service := macaroons.Service{
Database: database,
}
service.Init()
if utils.FileExists(server.AdminMacaroonPath) && utils.FileExists(server.ReadonlyMacaroonPath) {
// TODO: check if the macaroons on the disk are still up to date
return &service, nil
}
logger.Warning("Could not find Macaroons")
logger.Info("Generating new Macaroons")
err := writeMacaroon(service, macaroons.AdminPermissions(), server.AdminMacaroonPath)
if err != nil {
return nil, err
}
err = writeMacaroon(service, macaroons.ReadPermissions, server.ReadonlyMacaroonPath)
if err != nil {
return nil, err
}
return &service, nil
}
func writeMacaroon(service macaroons.Service, permissions []bakery.Op, path string) error {
macaroon, err := service.NewMacaroon(permissions...)
if err != nil {
return err
}
macaroonBytes, err := macaroon.M().MarshalBinary()
if err != nil {
return err
}
return ioutil.WriteFile(path, macaroonBytes, 0600)
}