2022-03-24 08:05:41 +00:00
|
|
|
package bearer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
2022-04-05 11:13:34 +00:00
|
|
|
"fmt"
|
2022-03-24 08:05:41 +00:00
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/acl"
|
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
frostfscrypto "github.com/TrueCloudLab/frostfs-sdk-go/crypto"
|
|
|
|
frostfsecdsa "github.com/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/eacl"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/user"
|
2022-03-24 08:05:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Token represents bearer token for object service operations.
|
|
|
|
//
|
2022-12-13 14:36:35 +00:00
|
|
|
// Token is mutually compatible with github.com/TrueCloudLab/frostfs-api-go/v2/acl.BearerToken
|
2022-03-24 08:05:41 +00:00
|
|
|
// message. See ReadFromV2 / WriteToV2 methods.
|
|
|
|
//
|
|
|
|
// Instances can be created using built-in var declaration.
|
2022-06-01 10:39:33 +00:00
|
|
|
type Token struct {
|
|
|
|
targetUserSet bool
|
|
|
|
targetUser user.ID
|
|
|
|
|
|
|
|
eaclTableSet bool
|
|
|
|
eaclTable eacl.Table
|
|
|
|
|
|
|
|
lifetimeSet bool
|
|
|
|
iat, nbf, exp uint64
|
|
|
|
|
|
|
|
sigSet bool
|
|
|
|
sig refs.Signature
|
2023-02-21 08:29:14 +00:00
|
|
|
|
|
|
|
impersonate bool
|
2022-06-01 10:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// reads Token from the acl.BearerToken message. If checkFieldPresence is set,
|
|
|
|
// returns an error on absence of any protocol-required field.
|
|
|
|
func (b *Token) readFromV2(m acl.BearerToken, checkFieldPresence bool) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
body := m.GetBody()
|
|
|
|
if checkFieldPresence && body == nil {
|
|
|
|
return errors.New("missing token body")
|
|
|
|
}
|
|
|
|
|
|
|
|
eaclTable := body.GetEACL()
|
|
|
|
if b.eaclTableSet = eaclTable != nil; b.eaclTableSet {
|
|
|
|
b.eaclTable = *eacl.NewTableFromV2(eaclTable)
|
|
|
|
} else if checkFieldPresence {
|
|
|
|
return errors.New("missing eACL table")
|
|
|
|
}
|
|
|
|
|
|
|
|
targetUser := body.GetOwnerID()
|
|
|
|
if b.targetUserSet = targetUser != nil; b.targetUserSet {
|
|
|
|
err = b.targetUser.ReadFromV2(*targetUser)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid target user: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lifetime := body.GetLifetime()
|
|
|
|
if b.lifetimeSet = lifetime != nil; b.lifetimeSet {
|
|
|
|
b.iat = lifetime.GetIat()
|
|
|
|
b.nbf = lifetime.GetNbf()
|
|
|
|
b.exp = lifetime.GetExp()
|
|
|
|
} else if checkFieldPresence {
|
|
|
|
return errors.New("missing token lifetime")
|
|
|
|
}
|
|
|
|
|
2023-02-21 08:29:14 +00:00
|
|
|
b.impersonate = body.GetImpersonate()
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
sig := m.GetSignature()
|
|
|
|
if b.sigSet = sig != nil; sig != nil {
|
|
|
|
b.sig = *sig
|
|
|
|
} else if checkFieldPresence {
|
|
|
|
return errors.New("missing body signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-24 08:05:41 +00:00
|
|
|
|
|
|
|
// ReadFromV2 reads Token from the acl.BearerToken message.
|
|
|
|
//
|
|
|
|
// See also WriteToV2.
|
2022-06-01 10:39:33 +00:00
|
|
|
func (b *Token) ReadFromV2(m acl.BearerToken) error {
|
|
|
|
return b.readFromV2(m, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b Token) fillBody() *acl.BearerTokenBody {
|
|
|
|
if !b.eaclTableSet && !b.targetUserSet && !b.lifetimeSet {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var body acl.BearerTokenBody
|
|
|
|
|
|
|
|
if b.eaclTableSet {
|
|
|
|
body.SetEACL(b.eaclTable.ToV2())
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.targetUserSet {
|
|
|
|
var targetUser refs.OwnerID
|
|
|
|
b.targetUser.WriteToV2(&targetUser)
|
|
|
|
|
|
|
|
body.SetOwnerID(&targetUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.lifetimeSet {
|
|
|
|
var lifetime acl.TokenLifetime
|
|
|
|
lifetime.SetIat(b.iat)
|
|
|
|
lifetime.SetNbf(b.nbf)
|
|
|
|
lifetime.SetExp(b.exp)
|
|
|
|
|
|
|
|
body.SetLifetime(&lifetime)
|
|
|
|
}
|
|
|
|
|
2023-02-21 08:29:14 +00:00
|
|
|
body.SetImpersonate(b.impersonate)
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
return &body
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b Token) signedData() []byte {
|
|
|
|
return b.fillBody().StableMarshal(nil)
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToV2 writes Token to the acl.BearerToken message.
|
|
|
|
// The message must not be nil.
|
|
|
|
//
|
|
|
|
// See also ReadFromV2.
|
|
|
|
func (b Token) WriteToV2(m *acl.BearerToken) {
|
2022-06-01 10:39:33 +00:00
|
|
|
m.SetBody(b.fillBody())
|
2022-03-24 08:05:41 +00:00
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
var sig *refs.Signature
|
2022-03-24 08:05:41 +00:00
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
if b.sigSet {
|
|
|
|
sig = &b.sig
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
m.SetSignature(sig)
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
// SetExp sets "exp" (expiration time) claim which identifies the
|
2022-12-29 10:46:18 +00:00
|
|
|
// expiration time (in FrostFS epochs) after which the Token MUST NOT be
|
2022-12-23 11:58:18 +00:00
|
|
|
// accepted for processing. The processing of the "exp" claim requires
|
|
|
|
// that the current epoch MUST be before or equal to the expiration epoch
|
|
|
|
// listed in the "exp" claim.
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
2022-06-01 10:39:33 +00:00
|
|
|
// Naming is inspired by https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4.
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
2022-06-01 10:39:33 +00:00
|
|
|
// See also InvalidAt.
|
|
|
|
func (b *Token) SetExp(exp uint64) {
|
|
|
|
b.exp = exp
|
|
|
|
b.lifetimeSet = true
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
// SetNbf sets "nbf" (not before) claim which identifies the time (in
|
2022-12-29 10:46:18 +00:00
|
|
|
// FrostFS epochs) before which the Token MUST NOT be accepted for processing. The
|
2022-03-24 08:05:41 +00:00
|
|
|
// processing of the "nbf" claim requires that the current epoch MUST be
|
|
|
|
// after or equal to the not-before epoch listed in the "nbf" claim.
|
|
|
|
//
|
|
|
|
// Naming is inspired by https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5.
|
|
|
|
//
|
2022-06-01 10:39:33 +00:00
|
|
|
// See also InvalidAt.
|
|
|
|
func (b *Token) SetNbf(nbf uint64) {
|
|
|
|
b.nbf = nbf
|
|
|
|
b.lifetimeSet = true
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetIat sets "iat" (issued at) claim which identifies the time (in FrostFS
|
2022-03-24 08:05:41 +00:00
|
|
|
// epochs) at which the Token was issued. This claim can be used to determine
|
|
|
|
// the age of the Token.
|
|
|
|
//
|
|
|
|
// Naming is inspired by https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.6.
|
|
|
|
//
|
2022-06-01 10:39:33 +00:00
|
|
|
// See also InvalidAt.
|
|
|
|
func (b *Token) SetIat(iat uint64) {
|
|
|
|
b.iat = iat
|
|
|
|
b.lifetimeSet = true
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
// InvalidAt asserts "exp", "nbf" and "iat" claims for the given epoch.
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
2022-06-01 10:39:33 +00:00
|
|
|
// Zero Container is invalid in any epoch.
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
2022-06-01 10:39:33 +00:00
|
|
|
// See also SetExp, SetNbf, SetIat.
|
|
|
|
func (b Token) InvalidAt(epoch uint64) bool {
|
2022-12-23 11:58:18 +00:00
|
|
|
return !b.lifetimeSet || b.nbf > epoch || b.iat > epoch || b.exp < epoch
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
// SetEACLTable sets eacl.Table that replaces the one from the issuer's
|
|
|
|
// container. If table has specified container, bearer token can be used only
|
|
|
|
// for operations within this specific container. Otherwise, Token can be used
|
|
|
|
// within any issuer's container.
|
|
|
|
//
|
|
|
|
// SetEACLTable MUST be called if Token is going to be transmitted over
|
2022-12-29 10:46:18 +00:00
|
|
|
// FrostFS API V2 protocol.
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
2022-06-01 10:39:33 +00:00
|
|
|
// See also EACLTable, AssertContainer.
|
2022-03-24 08:05:41 +00:00
|
|
|
func (b *Token) SetEACLTable(table eacl.Table) {
|
2022-06-01 10:39:33 +00:00
|
|
|
b.eaclTable = table
|
|
|
|
b.eaclTableSet = true
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
// EACLTable returns extended ACL table set by SetEACLTable.
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
2022-06-01 10:39:33 +00:00
|
|
|
// Zero Token has zero eacl.Table.
|
2022-03-24 08:05:41 +00:00
|
|
|
func (b Token) EACLTable() eacl.Table {
|
2022-06-01 10:39:33 +00:00
|
|
|
if b.eaclTableSet {
|
|
|
|
return b.eaclTable
|
|
|
|
}
|
|
|
|
|
|
|
|
return eacl.Table{}
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 08:29:14 +00:00
|
|
|
func (b *Token) SetImpersonate(v bool) {
|
|
|
|
b.impersonate = v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b Token) Impersonate() bool {
|
|
|
|
return b.impersonate
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
// AssertContainer checks if the token is valid within the given container.
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
2022-06-01 10:39:33 +00:00
|
|
|
// Note: cnr is assumed to refer to the issuer's container, otherwise the check
|
|
|
|
// is meaningless.
|
|
|
|
//
|
|
|
|
// Zero Token is valid in any container.
|
|
|
|
//
|
|
|
|
// See also SetEACLTable.
|
|
|
|
func (b Token) AssertContainer(cnr cid.ID) bool {
|
|
|
|
if !b.eaclTableSet {
|
|
|
|
return true
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
cnrTable, set := b.eaclTable.CID()
|
|
|
|
return !set || cnrTable.Equals(cnr)
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
// ForUser specifies ID of the user who can use the Token for the operations
|
|
|
|
// within issuer's container(s).
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
2022-06-01 10:39:33 +00:00
|
|
|
// Optional: by default, any user has access to Token usage.
|
|
|
|
//
|
|
|
|
// See also AssertUser.
|
|
|
|
func (b *Token) ForUser(id user.ID) {
|
|
|
|
b.targetUser = id
|
|
|
|
b.targetUserSet = true
|
|
|
|
}
|
2022-04-11 06:30:22 +00:00
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
// AssertUser checks if the Token is issued to the given user.
|
|
|
|
//
|
|
|
|
// Zero Token is available to any user.
|
|
|
|
//
|
|
|
|
// See also ForUser.
|
|
|
|
func (b Token) AssertUser(id user.ID) bool {
|
|
|
|
return !b.targetUserSet || b.targetUser.Equals(id)
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
// Sign calculates and writes signature of the Token data using issuer's secret.
|
|
|
|
// Returns signature calculation errors.
|
|
|
|
//
|
|
|
|
// Sign MUST be called if Token is going to be transmitted over
|
2022-12-29 10:46:18 +00:00
|
|
|
// FrostFS API V2 protocol.
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
2022-06-01 10:39:33 +00:00
|
|
|
// Note that any Token mutation is likely to break the signature, so it is
|
|
|
|
// expected to be calculated as a final stage of Token formation.
|
|
|
|
//
|
|
|
|
// See also VerifySignature, Issuer.
|
2022-03-24 08:05:41 +00:00
|
|
|
func (b *Token) Sign(key ecdsa.PrivateKey) error {
|
2022-12-13 14:36:35 +00:00
|
|
|
var sig frostfscrypto.Signature
|
2022-04-05 11:13:34 +00:00
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
err := sig.Calculate(frostfsecdsa.Signer(key), b.signedData())
|
2022-04-05 11:13:34 +00:00
|
|
|
if err != nil {
|
2022-06-01 10:39:33 +00:00
|
|
|
return err
|
2022-04-05 11:13:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
sig.WriteToV2(&b.sig)
|
|
|
|
b.sigSet = true
|
2022-04-05 11:13:34 +00:00
|
|
|
|
|
|
|
return nil
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
// VerifySignature checks if Token signature is presented and valid.
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
2022-06-01 10:39:33 +00:00
|
|
|
// Zero Token fails the check.
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
|
|
|
// See also Sign.
|
2022-06-01 10:39:33 +00:00
|
|
|
func (b Token) VerifySignature() bool {
|
|
|
|
if !b.sigSet {
|
|
|
|
return false
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
var sig frostfscrypto.Signature
|
2022-03-24 08:05:41 +00:00
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
// TODO: (#233) check owner<->key relation
|
2022-07-07 10:51:05 +00:00
|
|
|
return sig.ReadFromV2(b.sig) == nil && sig.Verify(b.signedData())
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Marshal encodes Token into a binary format of the FrostFS API protocol
|
2022-06-01 10:39:33 +00:00
|
|
|
// (Protocol Buffers V3 with direct field order).
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
|
|
|
// See also Unmarshal.
|
|
|
|
func (b Token) Marshal() []byte {
|
2022-06-01 10:39:33 +00:00
|
|
|
var m acl.BearerToken
|
|
|
|
b.WriteToV2(&m)
|
|
|
|
|
|
|
|
return m.StableMarshal(nil)
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Unmarshal decodes FrostFS API protocol binary data into the Token
|
2022-06-01 10:39:33 +00:00
|
|
|
// (Protocol Buffers V3 with direct field order). Returns an error describing
|
|
|
|
// a format violation.
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
|
|
|
// See also Marshal.
|
|
|
|
func (b *Token) Unmarshal(data []byte) error {
|
2022-06-01 10:39:33 +00:00
|
|
|
var m acl.BearerToken
|
|
|
|
|
|
|
|
err := m.Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.readFromV2(m, false)
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// MarshalJSON encodes Token into a JSON format of the FrostFS API protocol
|
2022-06-01 10:39:33 +00:00
|
|
|
// (Protocol Buffers V3 JSON).
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
|
|
|
// See also UnmarshalJSON.
|
|
|
|
func (b Token) MarshalJSON() ([]byte, error) {
|
2022-06-01 10:39:33 +00:00
|
|
|
var m acl.BearerToken
|
|
|
|
b.WriteToV2(&m)
|
|
|
|
|
|
|
|
return m.MarshalJSON()
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// UnmarshalJSON decodes FrostFS API protocol JSON data into the Token
|
2022-06-01 10:39:33 +00:00
|
|
|
// (Protocol Buffers V3 JSON). Returns an error describing a format violation.
|
2022-03-24 08:05:41 +00:00
|
|
|
//
|
|
|
|
// See also MarshalJSON.
|
|
|
|
func (b *Token) UnmarshalJSON(data []byte) error {
|
2022-06-01 10:39:33 +00:00
|
|
|
var m acl.BearerToken
|
|
|
|
|
|
|
|
err := m.UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.readFromV2(m, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SigningKeyBytes returns issuer's public key in a binary format of
|
2022-12-29 10:46:18 +00:00
|
|
|
// FrostFS API protocol.
|
2022-06-01 10:39:33 +00:00
|
|
|
//
|
|
|
|
// Unsigned Token has empty key.
|
|
|
|
//
|
|
|
|
// See also ResolveIssuer.
|
|
|
|
func (b Token) SigningKeyBytes() []byte {
|
|
|
|
if b.sigSet {
|
|
|
|
return b.sig.GetKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-03-24 08:05:41 +00:00
|
|
|
}
|
2022-04-12 15:19:00 +00:00
|
|
|
|
2022-06-01 10:39:33 +00:00
|
|
|
// ResolveIssuer resolves issuer's user.ID from the key used for Token signing.
|
|
|
|
// Returns zero user.ID if Token is unsigned or key has incorrect format.
|
|
|
|
//
|
|
|
|
// See also SigningKeyBytes.
|
|
|
|
func ResolveIssuer(b Token) (usr user.ID) {
|
|
|
|
binKey := b.SigningKeyBytes()
|
|
|
|
|
|
|
|
if len(binKey) != 0 {
|
2022-12-13 14:36:35 +00:00
|
|
|
var key frostfsecdsa.PublicKey
|
2022-06-01 10:39:33 +00:00
|
|
|
if key.Decode(binKey) == nil {
|
|
|
|
user.IDFromKey(&usr, ecdsa.PublicKey(key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2022-04-12 15:19:00 +00:00
|
|
|
}
|