2021-10-27 10:00:15 +00:00
|
|
|
package sessiontest
|
|
|
|
|
|
|
|
import (
|
2022-01-13 13:16:35 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
2021-10-27 10:00:15 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2021-12-10 13:56:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-10-27 10:00:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
|
|
|
)
|
|
|
|
|
2021-12-10 13:56:04 +00:00
|
|
|
var p *keys.PrivateKey
|
2021-10-27 10:00:15 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
|
2021-12-10 13:56:04 +00:00
|
|
|
p, err = keys.NewPrivateKey()
|
2021-10-27 10:00:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:19:16 +00:00
|
|
|
// Token returns random session.Token.
|
2021-10-27 10:00:15 +00:00
|
|
|
//
|
|
|
|
// Resulting token is unsigned.
|
2021-11-12 11:19:16 +00:00
|
|
|
func Token() *session.Token {
|
2021-10-27 10:00:15 +00:00
|
|
|
tok := session.NewToken()
|
|
|
|
|
|
|
|
uid, err := uuid.New().MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-01-13 13:16:35 +00:00
|
|
|
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-10-27 10:00:15 +00:00
|
|
|
|
|
|
|
ownerID := owner.NewID()
|
2022-01-13 13:16:35 +00:00
|
|
|
ownerID.SetPublicKey(&priv.PublicKey)
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2021-12-10 13:56:04 +00:00
|
|
|
keyBin := p.PublicKey().Bytes()
|
2021-10-27 10:00:15 +00:00
|
|
|
|
|
|
|
tok.SetID(uid)
|
|
|
|
tok.SetOwnerID(ownerID)
|
|
|
|
tok.SetSessionKey(keyBin)
|
|
|
|
tok.SetExp(11)
|
|
|
|
tok.SetNbf(22)
|
|
|
|
tok.SetIat(33)
|
|
|
|
|
|
|
|
return tok
|
|
|
|
}
|
|
|
|
|
2021-11-12 11:19:16 +00:00
|
|
|
// SignedToken returns signed random session.Token.
|
2021-10-27 10:00:15 +00:00
|
|
|
//
|
|
|
|
// Panics if token could not be signed (actually unexpected).
|
2021-11-12 11:19:16 +00:00
|
|
|
func SignedToken() *session.Token {
|
|
|
|
tok := Token()
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2021-12-10 13:56:04 +00:00
|
|
|
err := tok.Sign(&p.PrivateKey)
|
2021-10-27 10:00:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok
|
|
|
|
}
|