2022-03-18 08:46:07 +00:00
|
|
|
package temporary
|
2020-08-24 15:50:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
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"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
2020-08-24 15:50:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *TokenStore) Create(ctx context.Context, body *session.CreateRequestBody) (*session.CreateResponseBody, error) {
|
2020-11-16 10:26:35 +00:00
|
|
|
ownerBytes, err := owner.NewIDFromV2(body.GetOwnerID()).Marshal()
|
2020-08-24 15:50:57 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
ownerID: base58.Encode(ownerBytes),
|
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
|
|
|
|
}
|