forked from charlires/go-auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_grant.go
93 lines (81 loc) · 2.71 KB
/
client_grant.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
package management
// ClientGrant is a method through which applications can gain Access Tokens.
//
// See: https://auth0.com/docs/get-started/applications/application-grant-types
type ClientGrant struct {
// A generated string identifying the client grant.
ID *string `json:"id,omitempty"`
// The identifier of the client.
ClientID *string `json:"client_id,omitempty"`
// The audience.
Audience *string `json:"audience,omitempty"`
Scope []interface{} `json:"scope"`
}
// ClientGrantList is a list of ClientGrants.
type ClientGrantList struct {
List
ClientGrants []*ClientGrant `json:"client_grants"`
}
// ClientGrantManager manages Auth0 ClientGrant resources.
type ClientGrantManager struct {
*Management
}
func newClientGrantManager(m *Management) *ClientGrantManager {
return &ClientGrantManager{m}
}
// Create a client grant.
//
// See: https://auth0.com/docs/api/management/v2#!/Client_Grants/post_client_grants
func (m *ClientGrantManager) Create(g *ClientGrant, opts ...RequestOption) (err error) {
return m.Request("POST", m.URI("client-grants"), g, opts...)
}
// Read a client grant by its ID.
//
// The Auth0 Management API does not offer a method to retrieve a client grant
// by id, we fake this by listing all client grants and matching by id on the
// client side. For this reason this method should be used with caution.
func (m *ClientGrantManager) Read(id string, opts ...RequestOption) (*ClientGrant, error) {
var page int
for {
l, err := m.List(append(opts, Page(page))...)
if err != nil {
return nil, err
}
for _, g := range l.ClientGrants {
if g.GetID() == id {
return g, nil
}
}
if !l.HasNext() {
break
}
page++
}
return nil, &managementError{
StatusCode: 404,
Err: "Not Found",
Message: "Client grant not found",
}
}
// Update a client grant.
//
// See: https://auth0.com/docs/api/management/v2#!/Client_Grants/patch_client_grants_by_id
func (m *ClientGrantManager) Update(id string, g *ClientGrant, opts ...RequestOption) (err error) {
return m.Request("PATCH", m.URI("client-grants", id), g, opts...)
}
// Delete a client grant.
//
// See: https://auth0.com/docs/api/management/v2#!/Client_Grants/delete_client_grants_by_id
func (m *ClientGrantManager) Delete(id string, opts ...RequestOption) (err error) {
return m.Request("DELETE", m.URI("client-grants", id), nil, opts...)
}
// List all client grants.
//
// This method forces the `include_totals=true` and defaults to `per_page=50` if
// not provided.
//
// See: https://auth0.com/docs/api/management/v2#!/Client_Grants/get_client_grants
func (m *ClientGrantManager) List(opts ...RequestOption) (gs *ClientGrantList, err error) {
err = m.Request("GET", m.URI("client-grants"), &gs, applyListDefaults(opts))
return
}