25
"github.com/cubefs/cubefs/blobstore/common/proto"
31
TokenSize = 4 + 4 + 10 + 5 + 5 + 5
39
type UploadToken struct {
45
func (t *UploadToken) IsValidBid(bid proto.BlobID) bool {
46
minBid, n := binary.Uvarint(t.Data[8:])
50
count, n := binary.Uvarint(t.Data[8+n:])
54
return minBid <= uint64(bid) && uint64(bid) < minBid+count
58
func (t *UploadToken) IsValid(clusterID proto.ClusterID, vid proto.Vid, bid proto.BlobID,
59
size uint32, secretKey []byte) bool {
61
minBid, count, expiredTime uint64
67
next := func() (uint64, bool) {
68
val, n := binary.Uvarint(data[offset:])
76
if minBid, ok = next(); !ok {
79
if count, ok = next(); !ok {
82
if !(minBid <= uint64(bid) && uint64(bid) < minBid+count) {
86
if expiredTime, ok = next(); !ok {
89
if expiredTime != 0 && time.Now().UTC().Unix() > int64(expiredTime) {
93
token := newUploadToken(clusterID, vid, proto.BlobID(minBid),
94
uint32(count), size, uint32(expiredTime), secretKey)
95
return bytes.Equal(token.Data[0:8], t.Data[0:8])
100
func NewUploadToken(clusterID proto.ClusterID, vid proto.Vid, bid proto.BlobID,
101
count, size uint32, expiration time.Duration, secretKey []byte) UploadToken {
102
expiredTime := uint32(0)
103
if expiration != time.Duration(0) {
104
expiredTime = uint32(time.Now().Add(expiration).UTC().Unix())
106
return newUploadToken(clusterID, vid, bid, count, size, expiredTime, secretKey)
109
func newUploadToken(clusterID proto.ClusterID, vid proto.Vid, bid proto.BlobID,
110
count, size uint32, expiredTime uint32, secretKey []byte) UploadToken {
111
var token UploadToken
112
data := token.Data[:]
113
binary.BigEndian.PutUint32(data[0:4], uint32(clusterID))
114
binary.BigEndian.PutUint32(data[4:8], uint32(vid))
117
offset += binary.PutUvarint(data[offset:], uint64(bid))
118
offset += binary.PutUvarint(data[offset:], uint64(count))
119
offset += binary.PutUvarint(data[offset:], uint64(expiredTime))
120
token.Offset = uint8(offset)
121
offset += binary.PutUvarint(data[offset:], uint64(size))
123
h := hmac.New(sha1.New, secretKey)
124
h.Write(data[:offset])
128
copy(data[0:8], sum[0:8])
133
func EncodeToken(token UploadToken) string {
134
return hex.EncodeToString(token.Data[:token.Offset])
138
func DecodeToken(s string) UploadToken {
139
var token UploadToken
140
src, _ := hex.DecodeString(s)
141
token.Offset = uint8(copy(token.Data[:], src))