forked from shoenig/vaultapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
48 lines (37 loc) · 967 Bytes
/
token.go
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
// Author hoenig
package vaultapi
import (
"io/ioutil"
"strings"
)
// A Tokener provides a token that can be used to
// authenticate with vault.
type Tokener interface {
Token() (string, error)
}
type staticToken struct {
// todo: maybe mlock this
token string
}
var _ Tokener = (*staticToken)(nil)
// NewStaticToken creates a Tokener that will only
// ever return the one provided value for token.
func NewStaticToken(token string) Tokener {
return &staticToken{token: token}
}
func (t *staticToken) Token() (string, error) {
return t.token, nil
}
type fileToken struct {
filename string
}
var _ Tokener = (*fileToken)(nil)
// NewFileToken will create a Tokener that will always
// reload the token value from the specified file.
func NewFileToken(filename string) Tokener {
return &fileToken{filename: filename}
}
func (t *fileToken) Token() (string, error) {
bs, err := ioutil.ReadFile(t.filename)
return strings.TrimSpace(string(bs)), err
}