-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
83 lines (72 loc) · 1.97 KB
/
Copy pathclient.go
File metadata and controls
83 lines (72 loc) · 1.97 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package manage
import (
"log"
"github.com/fident/go-manage/authenticate"
"github.com/fident/go-manage/key"
"github.com/fident/go-manage/tls"
"github.com/fident/go-manage/token"
"google.golang.org/grpc/metadata"
)
// Instance is go-manage client instance
type Instance struct {
authenticated bool
key key.Key
activeToken token.Token
fidentEndpoint string
}
// New client instance
func New(keyfilePath, fidentInstanceAddress string) (Instance, error) {
in := Instance{}
err := in.Init(keyfilePath, fidentInstanceAddress)
if err != nil {
return in, err
}
return in, nil
}
// IsAuthenticated checks clients authentication status
func (i *Instance) IsAuthenticated() bool {
return i.authenticated
}
// Init client with a new keyfile and endpoint
func (i *Instance) Init(keyfilePath, fidentInstanceAddress string) error {
// Get fident instance
if fidentInstanceAddress == FidentInstanceAddressSharedLocal {
fidentInstanceAddress = getAddressFromLocal()
}
// Read key file
key, err := key.FromFile(keyfilePath)
if err != nil {
log.Fatalf("failed to read keyfile: %v", err)
return err
}
i.key = key
i.fidentEndpoint = fidentInstanceAddress
tls.InitTLS()
return nil
}
func (i *Instance) updateToken() error {
// Use key to get token
tok, err := authenticate.GetToken(i.key, i.fidentEndpoint)
if err != nil {
return err
}
// Parse token for local management
res, err := token.Parse(tok)
if err != nil {
return err
}
i.activeToken = res
i.authenticated = true
return nil
}
// preflightAuth checks current active token status before making request handling token expiry & refresh
func (i *Instance) preflightAuth() (metadata.MD, error) {
if i.authenticated == false || i.activeToken.GetValue() == "" || i.activeToken.WithinExpiryRange() {
err := i.updateToken()
if err != nil {
return metadata.MD{}, err
}
}
meta := metadata.New(map[string]string{projectIDMetaKey: i.key.ProjectID, tokenMetaKey: i.activeToken.GetValue()})
return meta, nil
}