2021-10-27 10:00:15 +00:00
|
|
|
package sessiontest
|
|
|
|
|
|
|
|
import (
|
2021-11-22 12:38:15 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
crand "crypto/rand"
|
2021-10-27 10:00:15 +00:00
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
|
|
|
)
|
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
var p *ecdsa.PrivateKey
|
2021-10-27 10:00:15 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
p, err = ecdsa.GenerateKey(elliptic.P256(), crand.Reader)
|
2021-10-27 10:00:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate returns random session.Token.
|
|
|
|
//
|
|
|
|
// Resulting token is unsigned.
|
|
|
|
func Generate() *session.Token {
|
|
|
|
tok := session.NewToken()
|
|
|
|
|
|
|
|
uid, err := uuid.New().MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := new(owner.NEO3Wallet)
|
|
|
|
rand.Read(w.Bytes())
|
|
|
|
|
|
|
|
ownerID := owner.NewID()
|
|
|
|
ownerID.SetNeo3Wallet(w)
|
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
keyBin := elliptic.MarshalCompressed(p.PublicKey.Curve, p.PublicKey.X, p.PublicKey.Y)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateSigned returns signed random session.Token.
|
|
|
|
//
|
|
|
|
// Panics if token could not be signed (actually unexpected).
|
|
|
|
func GenerateSigned() *session.Token {
|
|
|
|
tok := Generate()
|
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
err := tok.Sign(p)
|
2021-10-27 10:00:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok
|
|
|
|
}
|