-
Notifications
You must be signed in to change notification settings - Fork 1
/
token_lookup.go
92 lines (87 loc) · 2.57 KB
/
token_lookup.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
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
84
85
86
87
88
89
90
91
92
package forest
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
)
// LookupToken token lookup
type LookupToken struct {
Data struct {
Accessor string `json:"accessor"`
CreationTime int64 `json:"creation_time"`
CreationTTL int64 `json:"creation_ttl"`
DisplayName string `json:"display_name"`
EntityID string `json:"entity_id"`
ExpireTime interface{} `json:"expire_time"`
ExplicitMaxTTL int64 `json:"explicit_max_ttl"`
ID string `json:"id"` // This is the token auth
Meta map[string]string `json:"meta"`
NumUses int64 `json:"num_uses"`
Orphan bool `json:"orphan"`
Path string `json:"path"`
Policies []string `json:"policies"`
TTL int64 `json:"ttl"`
Type string `json:"type"`
} `json:"data"`
RequestID string `json:"request_id"`
LeaseID string `json:"lease_id"`
Renewable bool `json:"renewable"`
LeaseDuration int64 `json:"lease_duration"`
WrapInfo interface{} `json:"wrap_info"`
Warnings []string `json:"warnings"`
Auth interface{} `json:"auth"`
}
type sLookupOther struct {
Token string `json:"token"`
}
// LookupSelf lookup information on current token used in the instance
func (v *Vault) LookupSelf(ctx context.Context) (lookup LookupToken, err error) {
req, err := v.requestGen(ctx, http.MethodGet, "/auth/token/lookup-self", nil)
if err != nil {
return
}
res, err := v.Config.HTTPClient.Do(req)
if err != nil {
return
}
defer res.Body.Close()
if err := checkErrorResponse(res); err != nil {
return lookup, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return lookup, err
}
err = json.Unmarshal(body, &lookup)
return lookup, nil
}
// LookupOther lookup information on passed token
func (v *Vault) LookupOther(ctx context.Context, token string) (lookup LookupToken, err error) {
body := sLookupOther{
Token: token,
}
b, err := json.Marshal(body)
if err != nil {
return
}
req, err := v.requestGen(ctx, http.MethodPost, "/auth/token/lookup", bytes.NewBuffer(b))
if err != nil {
return
}
res, err := v.Config.HTTPClient.Do(req)
if err != nil {
return
}
defer res.Body.Close()
if err := checkErrorResponse(res); err != nil {
return lookup, err
}
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return lookup, err
}
err = json.Unmarshal(resBody, &lookup)
return lookup, nil
}