2022-03-18 08:46:07 +00:00
|
|
|
package temporary
|
2020-08-24 15:50:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-17 13:59:46 +00:00
|
|
|
"errors"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-08-24 15:50:57 +00:00
|
|
|
|
|
|
|
"github.com/mr-tron/base58"
|
2021-05-31 08:55:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-08-24 15:50:57 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2022-03-18 08:46:07 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/session/storage"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2020-08-24 15:50:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *TokenStore) Create(ctx context.Context, body *session.CreateRequestBody) (*session.CreateResponseBody, error) {
|
2022-05-17 13:59:46 +00:00
|
|
|
idV2 := body.GetOwnerID()
|
|
|
|
if idV2 == nil {
|
|
|
|
return nil, errors.New("missing owner")
|
|
|
|
}
|
|
|
|
|
|
|
|
var id user.ID
|
|
|
|
|
|
|
|
err := id.ReadFromV2(*idV2)
|
2020-08-24 15:50:57 +00:00
|
|
|
if err != nil {
|
2022-05-17 13:59:46 +00:00
|
|
|
return nil, fmt.Errorf("invalid owner: %w", err)
|
2020-08-24 15:50:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 19:55:47 +00:00
|
|
|
uidBytes, err := storage.NewTokenID()
|
2020-08-24 15:50:57 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not generate token ID: %w", err)
|
2020-08-24 15:50:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 08:55:40 +00:00
|
|
|
sk, err := keys.NewPrivateKey()
|
2020-08-24 15:50:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mtx.Lock()
|
|
|
|
s.tokens[key{
|
|
|
|
tokenID: base58.Encode(uidBytes),
|
2022-05-17 13:59:46 +00:00
|
|
|
ownerID: base58.Encode(id.WalletBytes()),
|
2022-03-20 19:55:47 +00:00
|
|
|
}] = storage.NewPrivateToken(&sk.PrivateKey, body.GetExpiration())
|
2020-08-24 15:50:57 +00:00
|
|
|
s.mtx.Unlock()
|
|
|
|
|
|
|
|
res := new(session.CreateResponseBody)
|
|
|
|
res.SetID(uidBytes)
|
2021-05-31 08:55:40 +00:00
|
|
|
res.SetSessionKey(sk.PublicKey().Bytes())
|
2020-08-24 15:50:57 +00:00
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2022-03-21 14:56:56 +00:00
|
|
|
|
|
|
|
func (s *TokenStore) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|