2021-10-27 10:00:15 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2022-04-05 11:13:34 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-04-05 11:13:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-10-27 10:00:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2022-04-05 11:13:34 +00:00
|
|
|
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
|
|
|
|
neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa"
|
2021-10-27 10:00:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Token represents NeoFS API v2-compatible
|
|
|
|
// session token.
|
2022-02-25 08:21:19 +00:00
|
|
|
type Token session.Token
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-02-25 08:21:19 +00:00
|
|
|
// NewTokenFromV2 wraps session.Token message structure
|
2021-10-27 10:00:15 +00:00
|
|
|
// into Token.
|
|
|
|
//
|
2022-02-25 08:21:19 +00:00
|
|
|
// Nil session.Token converts to nil.
|
|
|
|
func NewTokenFromV2(tV2 *session.Token) *Token {
|
2021-10-27 10:00:15 +00:00
|
|
|
return (*Token)(tV2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewToken creates and returns blank Token.
|
|
|
|
//
|
|
|
|
// Defaults:
|
|
|
|
// - body: nil;
|
|
|
|
// - id: nil;
|
|
|
|
// - ownerId: nil;
|
|
|
|
// - sessionKey: nil;
|
|
|
|
// - exp: 0;
|
|
|
|
// - iat: 0;
|
|
|
|
// - nbf: 0;
|
|
|
|
func NewToken() *Token {
|
2022-02-25 08:21:19 +00:00
|
|
|
return NewTokenFromV2(new(session.Token))
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 08:21:19 +00:00
|
|
|
// ToV2 converts Token to session.Token message structure.
|
2021-10-27 10:00:15 +00:00
|
|
|
//
|
|
|
|
// Nil Token converts to nil.
|
2022-02-25 08:21:19 +00:00
|
|
|
func (t *Token) ToV2() *session.Token {
|
|
|
|
return (*session.Token)(t)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 08:21:19 +00:00
|
|
|
func (t *Token) setBodyField(setter func(*session.TokenBody)) {
|
|
|
|
token := (*session.Token)(t)
|
2021-10-27 10:00:15 +00:00
|
|
|
body := token.GetBody()
|
|
|
|
|
|
|
|
if body == nil {
|
2022-02-25 08:21:19 +00:00
|
|
|
body = new(session.TokenBody)
|
2021-10-27 10:00:15 +00:00
|
|
|
token.SetBody(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
setter(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns Token identifier.
|
|
|
|
func (t *Token) ID() []byte {
|
2022-02-25 08:21:19 +00:00
|
|
|
return (*session.Token)(t).
|
2021-10-27 10:00:15 +00:00
|
|
|
GetBody().
|
|
|
|
GetID()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetID sets Token identifier.
|
|
|
|
func (t *Token) SetID(v []byte) {
|
2022-02-25 08:21:19 +00:00
|
|
|
t.setBodyField(func(body *session.TokenBody) {
|
2021-10-27 10:00:15 +00:00
|
|
|
body.SetID(v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// OwnerID returns Token's owner identifier.
|
|
|
|
func (t *Token) OwnerID() *owner.ID {
|
|
|
|
return owner.NewIDFromV2(
|
2022-02-25 08:21:19 +00:00
|
|
|
(*session.Token)(t).
|
2021-10-27 10:00:15 +00:00
|
|
|
GetBody().
|
|
|
|
GetOwnerID(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOwnerID sets Token's owner identifier.
|
|
|
|
func (t *Token) SetOwnerID(v *owner.ID) {
|
2022-02-25 08:21:19 +00:00
|
|
|
t.setBodyField(func(body *session.TokenBody) {
|
2021-10-27 10:00:15 +00:00
|
|
|
body.SetOwnerID(v.ToV2())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionKey returns public key of the session
|
|
|
|
// in a binary format.
|
|
|
|
func (t *Token) SessionKey() []byte {
|
2022-02-25 08:21:19 +00:00
|
|
|
return (*session.Token)(t).
|
2021-10-27 10:00:15 +00:00
|
|
|
GetBody().
|
|
|
|
GetSessionKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSessionKey sets public key of the session
|
|
|
|
// in a binary format.
|
|
|
|
func (t *Token) SetSessionKey(v []byte) {
|
2022-02-25 08:21:19 +00:00
|
|
|
t.setBodyField(func(body *session.TokenBody) {
|
2021-10-27 10:00:15 +00:00
|
|
|
body.SetSessionKey(v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Token) setLifetimeField(f func(*session.TokenLifetime)) {
|
2022-02-25 08:21:19 +00:00
|
|
|
t.setBodyField(func(body *session.TokenBody) {
|
2021-10-27 10:00:15 +00:00
|
|
|
lt := body.GetLifetime()
|
|
|
|
if lt == nil {
|
|
|
|
lt = new(session.TokenLifetime)
|
|
|
|
body.SetLifetime(lt)
|
|
|
|
}
|
|
|
|
|
|
|
|
f(lt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exp returns epoch number of the token expiration.
|
|
|
|
func (t *Token) Exp() uint64 {
|
2022-02-25 08:21:19 +00:00
|
|
|
return (*session.Token)(t).
|
2021-10-27 10:00:15 +00:00
|
|
|
GetBody().
|
|
|
|
GetLifetime().
|
|
|
|
GetExp()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetExp sets epoch number of the token expiration.
|
|
|
|
func (t *Token) SetExp(exp uint64) {
|
|
|
|
t.setLifetimeField(func(lt *session.TokenLifetime) {
|
|
|
|
lt.SetExp(exp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nbf returns starting epoch number of the token.
|
|
|
|
func (t *Token) Nbf() uint64 {
|
2022-02-25 08:21:19 +00:00
|
|
|
return (*session.Token)(t).
|
2021-10-27 10:00:15 +00:00
|
|
|
GetBody().
|
|
|
|
GetLifetime().
|
|
|
|
GetNbf()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNbf sets starting epoch number of the token.
|
|
|
|
func (t *Token) SetNbf(nbf uint64) {
|
|
|
|
t.setLifetimeField(func(lt *session.TokenLifetime) {
|
|
|
|
lt.SetNbf(nbf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iat returns starting epoch number of the token.
|
|
|
|
func (t *Token) Iat() uint64 {
|
2022-02-25 08:21:19 +00:00
|
|
|
return (*session.Token)(t).
|
2021-10-27 10:00:15 +00:00
|
|
|
GetBody().
|
|
|
|
GetLifetime().
|
|
|
|
GetIat()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIat sets the number of the epoch in which the token was issued.
|
|
|
|
func (t *Token) SetIat(iat uint64) {
|
|
|
|
t.setLifetimeField(func(lt *session.TokenLifetime) {
|
|
|
|
lt.SetIat(iat)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign calculates and writes signature of the Token data.
|
|
|
|
//
|
|
|
|
// Returns signature calculation errors.
|
|
|
|
func (t *Token) Sign(key *ecdsa.PrivateKey) error {
|
2022-04-05 11:13:34 +00:00
|
|
|
if key == nil {
|
|
|
|
return errors.New("nil private key")
|
|
|
|
}
|
|
|
|
|
2022-02-25 08:21:19 +00:00
|
|
|
tV2 := (*session.Token)(t)
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-04-05 11:13:34 +00:00
|
|
|
digest, err := tV2.GetBody().StableMarshal(nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unexpected error from Token.StableMarshal: %v", err))
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 11:13:34 +00:00
|
|
|
var sig neofscrypto.Signature
|
|
|
|
var signer neofsecdsa.Signer
|
|
|
|
|
|
|
|
signer.SetKey(*key)
|
|
|
|
|
|
|
|
err = sig.Calculate(signer, digest)
|
2022-02-24 11:45:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-05 11:13:34 +00:00
|
|
|
var sigV2 refs.Signature
|
|
|
|
sig.WriteToV2(&sigV2)
|
|
|
|
|
|
|
|
tV2.SetSignature(&sigV2)
|
|
|
|
|
2022-02-24 11:45:52 +00:00
|
|
|
return nil
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// VerifySignature checks if token signature is
|
|
|
|
// presented and valid.
|
|
|
|
func (t *Token) VerifySignature() bool {
|
2022-02-25 08:21:19 +00:00
|
|
|
tV2 := (*session.Token)(t)
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-04-05 11:13:34 +00:00
|
|
|
digest, err := tV2.GetBody().StableMarshal(nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unexpected error from Token.StableMarshal: %v", err))
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 11:13:34 +00:00
|
|
|
sigV2 := tV2.GetSignature()
|
|
|
|
if sigV2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
2021-10-27 10:00:15 +00:00
|
|
|
|
2022-04-05 11:13:34 +00:00
|
|
|
var sig neofscrypto.Signature
|
|
|
|
sig.ReadFromV2(*sigV2)
|
|
|
|
|
|
|
|
return sig.Verify(digest)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetContext sets context of the Token.
|
|
|
|
//
|
|
|
|
// Supported contexts:
|
2022-03-02 12:49:03 +00:00
|
|
|
// - *ContainerContext,
|
|
|
|
// - *ObjectContext.
|
2021-10-27 10:00:15 +00:00
|
|
|
//
|
|
|
|
// Resets context if it is not supported.
|
|
|
|
func (t *Token) SetContext(v interface{}) {
|
2022-02-25 08:21:19 +00:00
|
|
|
var cV2 session.TokenContext
|
2021-10-27 10:00:15 +00:00
|
|
|
|
|
|
|
switch c := v.(type) {
|
|
|
|
case *ContainerContext:
|
|
|
|
cV2 = c.ToV2()
|
2022-03-02 12:49:03 +00:00
|
|
|
case *ObjectContext:
|
|
|
|
cV2 = c.ToV2()
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 08:21:19 +00:00
|
|
|
t.setBodyField(func(body *session.TokenBody) {
|
2021-10-27 10:00:15 +00:00
|
|
|
body.SetContext(cV2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context returns context of the Token.
|
|
|
|
//
|
|
|
|
// Supports same contexts as SetContext.
|
|
|
|
//
|
|
|
|
// Returns nil if context is not supported.
|
|
|
|
func (t *Token) Context() interface{} {
|
2022-02-25 08:21:19 +00:00
|
|
|
switch v := (*session.Token)(t).
|
2021-10-27 10:00:15 +00:00
|
|
|
GetBody().
|
|
|
|
GetContext(); c := v.(type) {
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
case *session.ContainerSessionContext:
|
2022-02-01 10:21:25 +00:00
|
|
|
return NewContainerContextFromV2(c)
|
2022-01-25 16:21:35 +00:00
|
|
|
case *session.ObjectSessionContext:
|
2022-02-01 10:21:25 +00:00
|
|
|
return NewObjectContextFromV2(c)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContainerContext is a helper function that casts
|
|
|
|
// Token context to ContainerContext.
|
|
|
|
//
|
|
|
|
// Returns nil if context is not a ContainerContext.
|
|
|
|
func GetContainerContext(t *Token) *ContainerContext {
|
|
|
|
c, _ := t.Context().(*ContainerContext)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal marshals Token into a protobuf binary form.
|
2021-11-08 12:25:13 +00:00
|
|
|
func (t *Token) Marshal() ([]byte, error) {
|
2022-02-25 08:21:19 +00:00
|
|
|
return (*session.Token)(t).
|
2021-11-08 12:25:13 +00:00
|
|
|
StableMarshal(nil)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of Token.
|
|
|
|
func (t *Token) Unmarshal(data []byte) error {
|
2022-02-25 08:21:19 +00:00
|
|
|
return (*session.Token)(t).
|
2021-10-27 10:00:15 +00:00
|
|
|
Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes Token to protobuf JSON format.
|
|
|
|
func (t *Token) MarshalJSON() ([]byte, error) {
|
2022-02-25 08:21:19 +00:00
|
|
|
return (*session.Token)(t).
|
2021-10-27 10:00:15 +00:00
|
|
|
MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes Token from protobuf JSON format.
|
|
|
|
func (t *Token) UnmarshalJSON(data []byte) error {
|
2022-02-25 08:21:19 +00:00
|
|
|
return (*session.Token)(t).
|
2021-10-27 10:00:15 +00:00
|
|
|
UnmarshalJSON(data)
|
|
|
|
}
|