1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
17 reObsoleteToken = regexp.MustCompile(`^[0-9a-z]{41,}$`)
18 ErrObsoleteToken = errors.New("obsolete token format")
19 ErrTokenFormat = errors.New("badly formatted token")
20 ErrSalted = errors.New("token already salted")
23 func SaltToken(token, remote string) (string, error) {
24 parts := strings.Split(token, "/")
25 if len(parts) < 3 || parts[0] != "v2" {
26 if reObsoleteToken.MatchString(token) {
27 return "", ErrObsoleteToken
29 return "", ErrTokenFormat
34 if len(secret) != 40 {
36 secret = fmt.Sprintf("%x", hmac.New(sha1.New, []byte(secret)).Sum([]byte(remote)))
37 return "v2/" + uuid + "/" + secret, nil
38 } else if strings.HasPrefix(uuid, remote) {
39 // already salted for the desired remote
42 // salted for a different remote, can't be used