lotus/node/modules/core.go

92 lines
2.2 KiB
Go
Raw Normal View History

package modules
import (
2019-10-11 00:31:06 +00:00
"context"
2019-07-23 20:37:06 +00:00
"crypto/rand"
"errors"
"io"
"io/ioutil"
2020-05-20 18:23:51 +00:00
"github.com/gbrlsnchs/jwt/v3"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p-core/peerstore"
record "github.com/libp2p/go-libp2p-record"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-jsonrpc/auth"
2020-05-20 18:23:51 +00:00
"github.com/filecoin-project/lotus/api/apistruct"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/addrutil"
"github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/repo"
)
2019-07-03 17:39:07 +00:00
var log = logging.Logger("modules")
type Genesis func() (*types.BlockHeader, error)
2019-07-08 13:36:43 +00:00
// RecordValidator provides namesys compatible routing record validator
func RecordValidator(ps peerstore.Peerstore) record.Validator {
return record.NamespacedValidator{
"pk": record.PublicKeyValidator{},
}
}
2019-07-08 13:36:43 +00:00
const JWTSecretName = "auth-jwt-private" //nolint:gosec
2019-07-23 20:23:44 +00:00
2019-07-23 20:37:06 +00:00
type jwtPayload struct {
2020-05-20 18:23:51 +00:00
Allow []auth.Permission
2019-07-23 20:37:06 +00:00
}
func APISecret(keystore types.KeyStore, lr repo.LockedRepo) (*dtypes.APIAlg, error) {
2019-07-23 20:23:44 +00:00
key, err := keystore.Get(JWTSecretName)
if errors.Is(err, types.ErrKeyInfoNotFound) {
2019-07-23 20:37:06 +00:00
log.Warn("Generating new API secret")
sk, err := ioutil.ReadAll(io.LimitReader(rand.Reader, 32))
if err != nil {
return nil, err
}
key = types.KeyInfo{
Type: "jwt-hmac-secret",
PrivateKey: sk,
}
if err := keystore.Put(JWTSecretName, key); err != nil {
return nil, xerrors.Errorf("writing API secret: %w", err)
}
// TODO: make this configurable
p := jwtPayload{
2019-12-09 17:08:32 +00:00
Allow: apistruct.AllPermissions,
2019-07-23 20:37:06 +00:00
}
cliToken, err := jwt.Sign(&p, jwt.NewHS256(key.PrivateKey))
if err != nil {
return nil, err
}
if err := lr.SetAPIToken(cliToken); err != nil {
return nil, err
}
} else if err != nil {
return nil, xerrors.Errorf("could not get JWT Token: %w", err)
2019-07-23 20:23:44 +00:00
}
2019-07-23 20:37:06 +00:00
return (*dtypes.APIAlg)(jwt.NewHS256(key.PrivateKey)), nil
2019-07-23 20:23:44 +00:00
}
2019-10-11 00:31:06 +00:00
func ConfigBootstrap(peers []string) func() (dtypes.BootstrapPeers, error) {
return func() (dtypes.BootstrapPeers, error) {
return addrutil.ParseAddresses(context.TODO(), peers)
}
}
func BuiltinBootstrap() (dtypes.BootstrapPeers, error) {
return build.BuiltinBootstrap()
}