X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/5e2bf20f75a532625719f0f779d1e26f9d4466c4..6ab526bb7fef3d7d42ff728fa30444e75de7be38:/sdk/go/keepclient/perms.go diff --git a/sdk/go/keepclient/perms.go b/sdk/go/keepclient/perms.go index a73db76bbb..d650f0d7ad 100644 --- a/sdk/go/keepclient/perms.go +++ b/sdk/go/keepclient/perms.go @@ -29,13 +29,15 @@ var ( // makePermSignature generates a SHA-1 HMAC digest for the given blob, // token, expiry, and site secret. -func makePermSignature(blobHash, apiToken, expiry string, permissionSecret []byte) string { +func makePermSignature(blobHash, apiToken, expiry, blobSignatureTTL string, permissionSecret []byte) string { hmac := hmac.New(sha1.New, permissionSecret) hmac.Write([]byte(blobHash)) hmac.Write([]byte("@")) hmac.Write([]byte(apiToken)) hmac.Write([]byte("@")) hmac.Write([]byte(expiry)) + hmac.Write([]byte("@")) + hmac.Write([]byte(blobSignatureTTL)) digest := hmac.Sum(nil) return fmt.Sprintf("%x", digest) } @@ -46,15 +48,16 @@ func makePermSignature(blobHash, apiToken, expiry string, permissionSecret []byt // // This function is intended to be used by system components and admin // utilities: userland programs do not know the permissionSecret. -func SignLocator(blobLocator, apiToken string, expiry time.Time, permissionSecret []byte) string { +func SignLocator(blobLocator, apiToken string, expiry time.Time, blobSignatureTTL time.Duration, permissionSecret []byte) string { if len(permissionSecret) == 0 || apiToken == "" { return blobLocator } // Strip off all hints: only the hash is used to sign. blobHash := strings.Split(blobLocator, "+")[0] timestampHex := fmt.Sprintf("%08x", expiry.Unix()) + blobSignatureTTLHex := strconv.FormatInt(int64(blobSignatureTTL.Seconds()), 16) return blobLocator + - "+A" + makePermSignature(blobHash, apiToken, timestampHex, permissionSecret) + + "+A" + makePermSignature(blobHash, apiToken, timestampHex, blobSignatureTTLHex, permissionSecret) + "@" + timestampHex } @@ -70,20 +73,21 @@ var signedLocatorRe = regexp.MustCompile(`^([[:xdigit:]]{32}).*\+A([[:xdigit:]]{ // // This function is intended to be used by system components and admin // utilities: userland programs do not know the permissionSecret. -func VerifySignature(signedLocator, apiToken string, permissionSecret []byte) error { +func VerifySignature(signedLocator, apiToken string, blobSignatureTTL time.Duration, permissionSecret []byte) error { matches := signedLocatorRe.FindStringSubmatch(signedLocator) if matches == nil { return ErrSignatureMissing } blobHash := matches[1] - sigHex := matches[2] - expHex := matches[3] - if expTime, err := parseHexTimestamp(expHex); err != nil { + signatureHex := matches[2] + expiryHex := matches[3] + if expiryTime, err := parseHexTimestamp(expiryHex); err != nil { return ErrSignatureInvalid - } else if expTime.Before(time.Now()) { + } else if expiryTime.Before(time.Now()) { return ErrSignatureExpired } - if sigHex != makePermSignature(blobHash, apiToken, expHex, permissionSecret) { + blobSignatureTTLHex := strconv.FormatInt(int64(blobSignatureTTL.Seconds()), 16) + if signatureHex != makePermSignature(blobHash, apiToken, expiryHex, blobSignatureTTLHex, permissionSecret) { return ErrSignatureInvalid } return nil