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"
|
2020-05-18 10:14:18 +00:00
|
|
|
|
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
2020-04-29 08:52:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-18 10:14:18 +00:00
|
|
|
// PublicSessionToken returns a binary representation of session public key.
|
|
|
|
//
|
|
|
|
// If passed PrivateToken is nil, ErrNilPrivateToken returns.
|
|
|
|
// If passed PrivateToken carries nil private key, crypto.ErrEmptyPrivateKey returns.
|
|
|
|
func PublicSessionToken(pToken PrivateToken) ([]byte, error) {
|
|
|
|
if pToken == nil {
|
|
|
|
return nil, ErrNilPrivateToken
|
|
|
|
}
|
|
|
|
|
|
|
|
sk := pToken.PrivateKey()
|
|
|
|
if sk == nil {
|
|
|
|
return nil, crypto.ErrEmptyPrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return crypto.MarshalPublicKey(&sk.PublicKey), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrivateKey is a session private key getter.
|
2020-05-18 10:11:39 +00:00
|
|
|
func (t *pToken) PrivateKey() *ecdsa.PrivateKey {
|
|
|
|
return t.sessionKey
|
2020-04-29 08:52:05 +00:00
|
|
|
}
|
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
|
|
|
|
}
|