1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
18 reObsoleteToken = regexp.MustCompile(`^[0-9a-z]{41,}$`)
19 ErrObsoleteToken = errors.New("obsolete token format")
20 ErrTokenFormat = errors.New("badly formatted token")
21 ErrSalted = errors.New("token already salted")
24 func SaltToken(token, remote string) (string, error) {
25 parts := strings.Split(token, "/")
26 if len(parts) < 3 || parts[0] != "v2" {
27 if reObsoleteToken.MatchString(token) {
28 return "", ErrObsoleteToken
30 return "", ErrTokenFormat
34 if strings.HasPrefix(uuid, remote) {
35 // target cluster issued this token -- send the real
38 } else if len(secret) != 40 {
40 hmac := hmac.New(sha1.New, []byte(secret))
41 io.WriteString(hmac, remote)
42 secret = fmt.Sprintf("%x", hmac.Sum(nil))
43 return "v2/" + uuid + "/" + secret, nil
45 // already salted, and not issued by target cluster --