-
Notifications
You must be signed in to change notification settings - Fork 0
/
grants.go
96 lines (84 loc) · 3.33 KB
/
grants.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
package mytokenlib
import (
"github.com/oidc-mytoken/api/v0"
)
// GrantsEndpoint is type representing a mytoken server's grants Endpoint and the actions that can be
// performed there.
type GrantsEndpoint struct {
endpoint string
SSH *SSHGrantEndpoint
}
func newGrantsEndpoint(endpoint string) *GrantsEndpoint {
return &GrantsEndpoint{
endpoint: endpoint,
SSH: newSSHGrantEndpoint(endpoint),
}
}
// DoHTTPRequest performs an http request to the grants endpoint
func (g GrantsEndpoint) DoHTTPRequest(method string, req, resp interface{}) error {
return doHTTPRequest(method, g.endpoint, req, resp)
}
// DoHTTPRequestWithAuth performs an http request to the grants endpoint
func (g GrantsEndpoint) DoHTTPRequestWithAuth(method string, req, resp interface{}, mytoken string) error {
return doHTTPRequestWithAuth(method, g.endpoint, req, resp, mytoken)
}
// APIGet returns the api.GrantTypeInfoResponse about the enabled grant types for this user.
// If the used mytoken changes (due to token rotation), the new mytoken is included in the api.GrantTypeInfoResponse
func (g GrantsEndpoint) APIGet(mytoken string) (resp api.GrantTypeInfoResponse, err error) {
err = g.DoHTTPRequestWithAuth("GET", nil, &resp, mytoken)
return
}
// Get returns a slice of api.GrantTypeInfo about the enabled grant types for this user.
// If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
func (g GrantsEndpoint) Get(mytoken *string) ([]api.GrantTypeInfo, error) {
resp, err := g.APIGet(*mytoken)
if err != nil {
return nil, err
}
if resp.TokenUpdate != nil {
*mytoken = resp.TokenUpdate.Mytoken
}
return resp.GrantTypes, nil
}
func (g GrantsEndpoint) changeGrant(method, mytoken, grant string) (resp api.OnlyTokenUpdateResponse, err error) {
req := api.GrantTypeRequest{
GrantType: grant,
Mytoken: mytoken,
}
err = g.DoHTTPRequest(method, req, &resp)
return
}
// APIEnableGrant enables the passed grant for this user.
// If the used mytoken changes (due to token rotation), the new mytoken is included in the api.OnlyTokenUpdateResponse
func (g GrantsEndpoint) APIEnableGrant(mytoken, grant string) (resp api.OnlyTokenUpdateResponse, err error) {
return g.changeGrant("POST", mytoken, grant)
}
// EnableGrant enables the passed grant for this user.
// If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
func (g GrantsEndpoint) EnableGrant(mytoken *string, grant string) (err error) {
res, err := g.APIEnableGrant(*mytoken, grant)
if err != nil {
return err
}
if res.TokenUpdate != nil {
*mytoken = res.TokenUpdate.Mytoken
}
return nil
}
// APIDisableGrant disables the passed grant for this user.
// If the used mytoken changes (due to token rotation), the new mytoken is included in the api.OnlyTokenUpdateResponse
func (g GrantsEndpoint) APIDisableGrant(mytoken, grant string) (resp api.OnlyTokenUpdateResponse, err error) {
return g.changeGrant("DELETE", mytoken, grant)
}
// DisableGrant disables the passed grant for this user.
// If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
func (g GrantsEndpoint) DisableGrant(mytoken *string, grant string) (err error) {
res, err := g.APIDisableGrant(*mytoken, grant)
if err != nil {
return err
}
if res.TokenUpdate != nil {
*mytoken = res.TokenUpdate.Mytoken
}
return nil
}