vvauth 2.5.0 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cache.go DELETED
@@ -1,101 +0,0 @@
1
- package vvauth
2
-
3
- import (
4
- "crypto/sha256"
5
- "encoding/hex"
6
- "encoding/json"
7
- "fmt"
8
- "os"
9
- "path/filepath"
10
- "strings"
11
-
12
- sshagentcrypt "github.com/131/ssh-agent-crypt"
13
- )
14
-
15
- // tokenCache is the clear content of ~/.vauth/tokens/<key>.creds.
16
- type tokenCache struct {
17
- Token string `json:"token"`
18
- LeaseDuration int64 `json:"lease_duration"`
19
- Renewable bool `json:"renewable"`
20
- ExpiresAt *int64 `json:"expires_at"` // unix milliseconds, null when unlimited
21
- }
22
-
23
- // TokenCacheKey isolates cached tokens by Vault address and authentication
24
- // configuration. It matches the Node implementation byte for byte.
25
- func (v *Vauth) TokenCacheKey() string {
26
- key := `{"vault_addr":` + string(jsonString(strings.Trim(v.VaultAddr, "/")))
27
- if auth := v.RC.AuthKey(); auth != "" {
28
- key += "," + auth
29
- }
30
- digest := sha256.Sum256([]byte(key + "}"))
31
- return hex.EncodeToString(digest[:])
32
- }
33
-
34
- // TokenCacheFile is the armored cache file for this configuration.
35
- func (v *Vauth) TokenCacheFile() string {
36
- return filepath.Join(v.CacheDir, v.TokenCacheKey()+".creds")
37
- }
38
-
39
- func (v *Vauth) agentOptions() *sshagentcrypt.Options {
40
- return &sshagentcrypt.Options{AgentSocket: v.Agent}
41
- }
42
-
43
- func (v *Vauth) readTokenCache() *tokenCache {
44
- if !v.TokenCache {
45
- return nil
46
- }
47
- armor, err := os.ReadFile(v.TokenCacheFile())
48
- if err != nil {
49
- return nil
50
- }
51
- body, err := sshagentcrypt.Decrypt(armor, "", v.agentOptions())
52
- if err != nil {
53
- return nil
54
- }
55
- var cache tokenCache
56
- if json.Unmarshal(body, &cache) != nil || cache.Token == "" {
57
- return nil
58
- }
59
- return &cache
60
- }
61
-
62
- func (v *Vauth) saveTokenCache(token *token) {
63
- if !v.TokenCache {
64
- return
65
- }
66
- file := v.TokenCacheFile()
67
- temporary := file + ".next"
68
- defer os.Remove(temporary)
69
- cache := tokenCache{Token: token.Value, LeaseDuration: token.LeaseDuration, Renewable: token.Renewable}
70
- if token.TTL != 0 {
71
- expires := v.now().UnixMilli() + int64(token.TTL*1000)
72
- cache.ExpiresAt = &expires
73
- }
74
- err := v.writeTokenCache(file, temporary, &cache)
75
- if err != nil {
76
- // Cache failures must not prevent authentication or expose credentials.
77
- fmt.Fprintln(v.stderr(), "Could not encrypt/write token cache; token not persisted")
78
- }
79
- }
80
-
81
- func (v *Vauth) writeTokenCache(file, temporary string, cache *tokenCache) error {
82
- body, err := json.Marshal(cache)
83
- if err != nil {
84
- return err
85
- }
86
- if err := os.MkdirAll(filepath.Dir(file), 0o700); err != nil {
87
- return err
88
- }
89
- identity := v.Environ["VAUTH_USER_IDENTITY"]
90
- if identity == "" {
91
- identity = v.Environ["VAUTH_USER_MAIL"]
92
- }
93
- armor, err := sshagentcrypt.Encrypt(body, identity, v.agentOptions())
94
- if err != nil {
95
- return err
96
- }
97
- if err := os.WriteFile(temporary, []byte(armor), 0o600); err != nil {
98
- return err
99
- }
100
- return os.Rename(temporary, file)
101
- }
package/humandiff.go DELETED
@@ -1,50 +0,0 @@
1
- package vvauth
2
-
3
- import (
4
- "fmt"
5
- "math"
6
- "strings"
7
- )
8
-
9
- type diffStep struct {
10
- format string
11
- size float64
12
- }
13
-
14
- // nyks/date/humanDiff FORMAT_LIGHT steps.
15
- var diffSteps = []diffStep{
16
- {"%ds", 60}, {"%dm", 60}, {"%dh", 24}, {"%dd", 30}, {"%dmonth%s", 12}, {"%dy", 0},
17
- }
18
-
19
- // HumanDiff formats a duration in seconds with at most max units ("1d 3h"),
20
- // like nyks/date/humanDiff.
21
- func HumanDiff(seconds float64, max int) string {
22
- seconds = math.Ceil(seconds)
23
- if seconds == 0 {
24
- return "0s"
25
- }
26
- var parts []string
27
- for _, step := range diffSteps {
28
- var current float64
29
- if step.size == 0 {
30
- current = math.Floor(seconds)
31
- } else {
32
- current = math.Floor(math.Mod(seconds, step.size))
33
- seconds /= step.size
34
- }
35
- if current > 0 {
36
- plural := ""
37
- if current > 1 {
38
- plural = "s"
39
- }
40
- parts = append(parts, fmt.Sprintf(strings.Replace(step.format, "%s", plural, 1), int64(current)))
41
- }
42
- }
43
- for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 {
44
- parts[i], parts[j] = parts[j], parts[i]
45
- }
46
- if len(parts) > max {
47
- parts = parts[:max]
48
- }
49
- return strings.Join(parts, " ")
50
- }