2020-09-10 14:51:58 +00:00
|
|
|
package eacl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
|
|
|
v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl"
|
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Target is a group of request senders to match EACL. Defined by role enum
|
|
|
|
// and set of public keys.
|
|
|
|
type Target struct {
|
|
|
|
role Role
|
|
|
|
keys []ecdsa.PublicKey
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:40:57 +00:00
|
|
|
func (t *Target) SetKeys(keys ...ecdsa.PublicKey) {
|
|
|
|
t.keys = keys
|
|
|
|
}
|
|
|
|
|
2020-09-10 14:51:58 +00:00
|
|
|
func (t Target) Keys() []ecdsa.PublicKey {
|
|
|
|
return t.keys
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:40:57 +00:00
|
|
|
func (t *Target) SetRole(r Role) {
|
|
|
|
t.role = r
|
|
|
|
}
|
|
|
|
|
2020-09-10 14:51:58 +00:00
|
|
|
func (t Target) Role() Role {
|
|
|
|
return t.role
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Target) ToV2() *v2acl.Target {
|
|
|
|
keys := make([][]byte, 0, len(t.keys))
|
|
|
|
for i := range t.keys {
|
|
|
|
key := crypto.MarshalPublicKey(&t.keys[i])
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
target := new(v2acl.Target)
|
|
|
|
|
|
|
|
target.SetRole(t.role.ToV2())
|
2020-10-19 12:39:13 +00:00
|
|
|
target.SetKeys(keys)
|
2020-09-10 14:51:58 +00:00
|
|
|
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:40:57 +00:00
|
|
|
func NewTarget() *Target {
|
|
|
|
return NewTargetFromV2(new(v2acl.Target))
|
|
|
|
}
|
|
|
|
|
2020-09-10 14:51:58 +00:00
|
|
|
func NewTargetFromV2(target *v2acl.Target) *Target {
|
|
|
|
t := new(Target)
|
|
|
|
|
|
|
|
if target == nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
t.role = RoleFromV2(target.GetRole())
|
2020-10-19 12:39:13 +00:00
|
|
|
v2keys := target.GetKeys()
|
2020-09-10 14:51:58 +00:00
|
|
|
t.keys = make([]ecdsa.PublicKey, 0, len(v2keys))
|
|
|
|
for i := range v2keys {
|
|
|
|
key := crypto.UnmarshalPublicKey(v2keys[i])
|
|
|
|
t.keys = append(t.keys, *key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
2020-11-13 11:40:57 +00:00
|
|
|
|
|
|
|
// Marshal marshals Target into a protobuf binary form.
|
|
|
|
//
|
|
|
|
// Buffer is allocated when the argument is empty.
|
|
|
|
// Otherwise, the first buffer is used.
|
|
|
|
func (t *Target) Marshal(b ...[]byte) ([]byte, error) {
|
|
|
|
var buf []byte
|
|
|
|
if len(b) > 0 {
|
|
|
|
buf = b[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.ToV2().
|
|
|
|
StableMarshal(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of Target.
|
|
|
|
func (t *Target) Unmarshal(data []byte) error {
|
|
|
|
fV2 := new(v2acl.Target)
|
|
|
|
if err := fV2.Unmarshal(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*t = *NewTargetFromV2(fV2)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes Target to protobuf JSON format.
|
|
|
|
func (t *Target) MarshalJSON() ([]byte, error) {
|
|
|
|
return t.ToV2().
|
|
|
|
MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes Target from protobuf JSON format.
|
|
|
|
func (t *Target) UnmarshalJSON(data []byte) error {
|
|
|
|
tV2 := new(v2acl.Target)
|
|
|
|
if err := tV2.UnmarshalJSON(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*t = *NewTargetFromV2(tV2)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|