2020-04-29 08:52:05 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2020-04-29 09:46:05 +00:00
|
|
|
"crypto/elliptic"
|
2020-04-29 08:52:05 +00:00
|
|
|
"crypto/rand"
|
|
|
|
|
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pToken struct {
|
|
|
|
// private session token
|
|
|
|
sessionKey *ecdsa.PrivateKey
|
2020-04-29 11:11:19 +00:00
|
|
|
// last epoch of the lifetime
|
|
|
|
validUntil uint64
|
2020-04-29 08:52:05 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 11:11:19 +00:00
|
|
|
// NewPrivateToken creates PrivateToken instance that expires after passed epoch.
|
2020-04-29 08:52:05 +00:00
|
|
|
//
|
|
|
|
// Returns non-nil error on key generation error.
|
2020-04-29 11:11:19 +00:00
|
|
|
func NewPrivateToken(validUntil uint64) (PrivateToken, error) {
|
2020-04-29 09:46:05 +00:00
|
|
|
sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
2020-04-29 08:52:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pToken{
|
|
|
|
sessionKey: sk,
|
2020-04-29 11:11:19 +00:00
|
|
|
validUntil: validUntil,
|
2020-04-29 08:52:05 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign signs data with session private key.
|
|
|
|
func (t *pToken) Sign(data []byte) ([]byte, error) {
|
|
|
|
return crypto.Sign(t.sessionKey, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublicKey returns a binary representation of the session public key.
|
|
|
|
func (t *pToken) PublicKey() []byte {
|
|
|
|
return crypto.MarshalPublicKey(&t.sessionKey.PublicKey)
|
|
|
|
}
|
2020-04-29 11:11:19 +00:00
|
|
|
|
|
|
|
func (t *pToken) Expired(epoch uint64) bool {
|
|
|
|
return t.validUntil < epoch
|
|
|
|
}
|
2020-05-08 10:20:12 +00:00
|
|
|
|
|
|
|
// SetOwnerID is an owner ID field setter.
|
|
|
|
func (s *PrivateTokenKey) SetOwnerID(id OwnerID) {
|
|
|
|
s.owner = id
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTokenID is a token ID field setter.
|
|
|
|
func (s *PrivateTokenKey) SetTokenID(id TokenID) {
|
|
|
|
s.token = id
|
|
|
|
}
|