-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokeninfo.go
128 lines (116 loc) · 4.37 KB
/
tokeninfo.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package mytokenlib
import (
"github.com/oidc-mytoken/api/v0"
)
// TokeninfoEndpoint is type representing a mytoken server's Revocation Endpoint and the actions that can be
// performed there.
type TokeninfoEndpoint struct {
endpoint string
}
func newTokeninfoEndpoint(endpoint string) *TokeninfoEndpoint {
return &TokeninfoEndpoint{
endpoint: endpoint,
}
}
// DoHTTPRequest performs an http request to the tokeninfo endpoint
func (info TokeninfoEndpoint) DoHTTPRequest(method string, req, resp interface{}) error {
return doHTTPRequest(method, info.endpoint, req, resp)
}
// Introspect introspects the passed mytoken
func (info TokeninfoEndpoint) Introspect(mytoken string) (*api.TokeninfoIntrospectResponse, error) {
req := api.TokenInfoRequest{
Action: api.TokeninfoActionIntrospect,
Mytoken: mytoken,
}
var resp api.TokeninfoIntrospectResponse
if err := info.DoHTTPRequest("POST", req, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// APIHistory obtains the event history for the passed mytoken or mom ids.
// If the used mytoken changes (due to token rotation), the new mytoken is included in the api.TokeninfoHistoryResponse
func (info TokeninfoEndpoint) APIHistory(mytoken string, momIDs ...string) (
resp api.TokeninfoHistoryResponse, err error,
) {
req := api.TokenInfoRequest{
Action: api.TokeninfoActionEventHistory,
Mytoken: mytoken,
MOMIDs: momIDs,
}
err = info.DoHTTPRequest("POST", req, &resp)
return
}
// History obtains the event history for the passed mytoken.
// If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
func (info TokeninfoEndpoint) History(mytoken *string) ([]api.EventEntry, error) {
resp, err := info.APIHistory(*mytoken)
if err != nil {
return nil, err
}
if resp.TokenUpdate != nil {
*mytoken = resp.TokenUpdate.Mytoken
}
return resp.EventHistory.Events, nil
}
// HistoryForOtherMytoken obtains the event history for mytoken with the passed mom id and uses the passed mytoken as
// authorization.
// If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
func (info TokeninfoEndpoint) HistoryForOtherMytoken(mytoken *string, momID string) (*api.EventHistory, error) {
resp, err := info.APIHistory(*mytoken, momID)
if err != nil {
return nil, err
}
if resp.TokenUpdate != nil {
*mytoken = resp.TokenUpdate.Mytoken
}
return &resp.EventHistory, nil
}
// APISubtokens returns an api.TokeninfoTreeResponse listing metadata about the passed mytoken and its children (
// recursively)
// If the used mytoken changes (due to token rotation), the new mytoken is included in the api.TokeninfoTreeResponse
func (info TokeninfoEndpoint) APISubtokens(mytoken string) (resp api.TokeninfoSubtokensResponse, err error) {
req := api.TokenInfoRequest{
Action: api.TokeninfoActionSubtokens,
Mytoken: mytoken,
}
err = info.DoHTTPRequest("POST", req, &resp)
return
}
// Subtokens returns an api.MytokenEntryTree listing metadata about the passed mytoken and its children (
// recursively)
// If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
func (info TokeninfoEndpoint) Subtokens(mytoken *string) (*api.MytokenEntryTree, error) {
resp, err := info.APISubtokens(*mytoken)
if err != nil {
return nil, err
}
if resp.TokenUpdate != nil {
*mytoken = resp.TokenUpdate.Mytoken
}
return &resp.Tokens, nil
}
// APIListMytokens returns an api.TokeninfoListResponse listing metadata about all the user's mytoken and their
// children (recursively)
// If the used mytoken changes (due to token rotation), the new mytoken is included in the api.TokeninfoListResponse
func (info TokeninfoEndpoint) APIListMytokens(mytoken string) (resp api.TokeninfoListResponse, err error) {
req := api.TokenInfoRequest{
Action: api.TokeninfoActionListMytokens,
Mytoken: mytoken,
}
err = info.DoHTTPRequest("POST", req, &resp)
return
}
// ListMytokens returns a slice of api.MytokenEntryTree listing metadata about all the user's mytoken and their
// children (recursively)
// If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
func (info TokeninfoEndpoint) ListMytokens(mytoken *string) ([]api.MytokenEntryTree, error) {
resp, err := info.APIListMytokens(*mytoken)
if err != nil {
return nil, err
}
if resp.TokenUpdate != nil {
*mytoken = resp.TokenUpdate.Mytoken
}
return resp.Tokens, nil
}