auth: Load JWT secret once

This commit is contained in:
Łukasz Magiera
2019-07-24 00:38:52 +02:00
parent 57219c6126
commit 0aadddb6c8
3 changed files with 22 additions and 12 deletions
+14
View File
@@ -2,6 +2,8 @@ package modules
import (
"context"
"github.com/gbrlsnchs/jwt/v3"
"golang.org/x/xerrors"
"path/filepath"
"github.com/ipfs/go-bitswap"
@@ -70,6 +72,18 @@ func KeyStore(lr repo.LockedRepo) (types.KeyStore, error) {
return lr.KeyStore()
}
const JWTSecretName = "auth-jwt-private"
type APIAlg jwt.HMACSHA
func APISecret(keystore types.KeyStore) (*APIAlg, error) {
key, err := keystore.Get(JWTSecretName)
if err != nil {
return nil, xerrors.Errorf("couldn't get JWT secret: %w", err)
}
return (*APIAlg)(jwt.NewHS256(key.PrivateKey)), nil
}
func Datastore(r repo.LockedRepo) (datastore.Batching, error) {
return r.Datastore("/metadata")
}