File: token.go

package info (click to toggle)
golang-github-scaleway-scaleway-sdk-go 1.0.0~beta32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,444 kB
  • sloc: sh: 70; makefile: 3
file content (51 lines) | stat: -rw-r--r-- 1,298 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package auth

import "net/http"

// Token is the pair accessKey + secretKey.
// This type is public because it's an internal package.
type Token struct {
	AccessKey string
	SecretKey string
}

// XAuthTokenHeader is Scaleway standard auth header
const XAuthTokenHeader = "X-Auth-Token" // #nosec G101

// NewToken create a token authentication from an
// access key and a secret key
func NewToken(accessKey, secretKey string) *Token {
	return &Token{AccessKey: accessKey, SecretKey: secretKey}
}

// Headers returns headers that must be add to the http request
func (t *Token) Headers() http.Header {
	headers := http.Header{}
	headers.Set(XAuthTokenHeader, t.SecretKey)
	return headers
}

func AnonymizeTokenHeaders(headers http.Header) http.Header {
	key := headers.Get(XAuthTokenHeader)
	if key != "" {
		headers.Set(XAuthTokenHeader, HideSecretKey(key))
	}
	return headers
}

// AnonymizedHeaders returns an anonymized version of Headers()
// This method could be use for logging purpose.
func (t *Token) AnonymizedHeaders() http.Header {
	return AnonymizeTokenHeaders(t.Headers())
}

func HideSecretKey(k string) string {
	switch {
	case len(k) == 0:
		return ""
	case len(k) > 8:
		return k[0:8] + "-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
	default:
		return "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
	}
}