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/google/uuid"
|
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
|
|
|
uid, err := uuid.NewRandom()
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
uidBytes, err := uid.MarshalBinary()
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not marshal 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
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:46:07 +00:00
|
|
|
privateToken := new(storage.PrivateToken)
|
|
|
|
privateToken.SetSessionKey(&sk.PrivateKey)
|
|
|
|
privateToken.SetExpiredAt(body.GetExpiration())
|
|
|
|
|
2020-08-24 15:50:57 +00:00
|
|
|
s.mtx.Lock()
|
|
|
|
s.tokens[key{
|
|
|
|
tokenID: base58.Encode(uidBytes),
|
|
|
|
ownerID: base58.Encode(ownerBytes),
|
2022-03-18 08:46:07 +00:00
|
|
|
}] = privateToken
|
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
|
|
|
|
}
|