2019-06-25 11:42:17 +00:00
|
|
|
package modules
|
2019-07-01 10:18:00 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-23 20:37:06 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2019-07-08 15:14:36 +00:00
|
|
|
|
2019-07-24 01:16:17 +00:00
|
|
|
"github.com/gbrlsnchs/jwt/v3"
|
2019-07-03 17:39:07 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-07-01 10:18:00 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peerstore"
|
|
|
|
record "github.com/libp2p/go-libp2p-record"
|
2019-07-24 01:16:17 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-08 15:14:36 +00:00
|
|
|
|
2019-07-24 01:16:17 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/api"
|
2019-07-18 16:10:53 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-07-10 15:38:35 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/node/repo"
|
2019-07-01 10:18:00 +00:00
|
|
|
)
|
|
|
|
|
2019-07-03 17:39:07 +00:00
|
|
|
var log = logging.Logger("modules")
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
type Genesis func() (*types.BlockHeader, error)
|
2019-07-08 13:36:43 +00:00
|
|
|
|
2019-07-01 10:18:00 +00:00
|
|
|
// RecordValidator provides namesys compatible routing record validator
|
|
|
|
func RecordValidator(ps peerstore.Peerstore) record.Validator {
|
|
|
|
return record.NamespacedValidator{
|
2019-07-01 20:00:22 +00:00
|
|
|
"pk": record.PublicKeyValidator{},
|
2019-07-01 10:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-08 13:36:43 +00:00
|
|
|
|
2019-07-23 20:23:44 +00:00
|
|
|
const JWTSecretName = "auth-jwt-private"
|
|
|
|
|
|
|
|
type APIAlg jwt.HMACSHA
|
|
|
|
|
2019-07-23 20:37:06 +00:00
|
|
|
type jwtPayload struct {
|
|
|
|
Allow []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func APISecret(keystore types.KeyStore, lr repo.LockedRepo) (*APIAlg, error) {
|
2019-07-23 20:23:44 +00:00
|
|
|
key, err := keystore.Get(JWTSecretName)
|
|
|
|
if err != nil {
|
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{
|
|
|
|
Allow: api.AllPermissions,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-07-23 20:23:44 +00:00
|
|
|
}
|
2019-07-23 20:37:06 +00:00
|
|
|
|
2019-07-23 20:23:44 +00:00
|
|
|
return (*APIAlg)(jwt.NewHS256(key.PrivateKey)), nil
|
|
|
|
}
|