forked from auth0/go-auth0
/
hook.go
186 lines (162 loc) · 6.29 KB
/
hook.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package management
import "context"
// Hook is a secure, self-contained function that
// allows the behavior of Auth0 to be customized.
//
// See: https://auth0.com/docs/customize/hooks
type Hook struct {
// The hook's identifier.
ID *string `json:"id,omitempty"`
// The name of the hook. Can only contain alphanumeric characters, spaces
// and '-'. Can neither start nor end with '-' or spaces.
Name *string `json:"name,omitempty"`
// A script that contains the hook's code.
Script *string `json:"script,omitempty"`
// The extensibility point name
// Can currently be any of the following:
// "credentials-exchange", "pre-user-registration",
// "post-user-registration", "post-change-password"
TriggerID *string `json:"triggerId,omitempty"`
// Used to store additional metadata
Dependencies *map[string]string `json:"dependencies,omitempty"`
// Enabled should be set to true if the hook is enabled, false otherwise.
Enabled *bool `json:"enabled,omitempty"`
}
// HookList is a list of Hooks.
type HookList struct {
List
Hooks []*Hook `json:"hooks"`
}
// HookSecrets are the secret keys and values associated with a Hook.
type HookSecrets map[string]string
// Keys gets the configured hook secret keys.
func (s HookSecrets) Keys() []string {
keys := make([]string, len(s))
i := 0
for k := range s {
keys[i] = k
i++
}
return keys
}
// Difference returns a new map containing only keys which
// are present in s that are missing from other.
func (s HookSecrets) difference(other HookSecrets) HookSecrets {
d := make(HookSecrets)
for k, v := range s {
if _, ok := other[k]; !ok {
d[k] = v
}
}
return d
}
// Intersection returns a new map containing only
// keys which are present in both s and other.
func (s HookSecrets) intersection(other HookSecrets) HookSecrets {
i := make(HookSecrets)
for k, v := range s {
if _, ok := other[k]; ok {
i[k] = v
}
}
return i
}
// HookManager manages Auth0 Hook resources.
type HookManager manager
// Create a new hook.
//
// Note: Changing a hook's trigger changes the signature of the script and should be done with caution.
//
// See: https://auth0.com/docs/api/management/v2#!/Hooks/post_hooks
func (m *HookManager) Create(ctx context.Context, h *Hook, opts ...RequestOption) error {
return m.management.Request(ctx, "POST", m.management.URI("hooks"), h, opts...)
}
// Read hook details. Accepts a list of fields to include or exclude in the result.
//
// See: https://auth0.com/docs/api/management/v2/#!/Hooks/get_hooks_by_id
func (m *HookManager) Read(ctx context.Context, id string, opts ...RequestOption) (h *Hook, err error) {
err = m.management.Request(ctx, "GET", m.management.URI("hooks", id), &h, opts...)
return
}
// Update an existing hook.
//
// See: https://auth0.com/docs/api/management/v2/#!/Hooks/patch_hooks_by_id
func (m *HookManager) Update(ctx context.Context, id string, h *Hook, opts ...RequestOption) error {
return m.management.Request(ctx, "PATCH", m.management.URI("hooks", id), h, opts...)
}
// Delete a hook.
//
// See: https://auth0.com/docs/api/management/v2/#!/Hooks/delete_hooks_by_id
func (m *HookManager) Delete(ctx context.Context, id string, opts ...RequestOption) error {
return m.management.Request(ctx, "DELETE", m.management.URI("hooks", id), nil, opts...)
}
// List all hooks.
//
// See: https://auth0.com/docs/api/management/v2/#!/Hooks/get_hooks
func (m *HookManager) List(ctx context.Context, opts ...RequestOption) (l *HookList, err error) {
err = m.management.Request(ctx, "GET", m.management.URI("hooks"), &l, applyListDefaults(opts))
return
}
// CreateSecrets adds one or more secrets to an existing hook. A hook can have a
// maximum of 20 secrets.
//
// See: https://auth0.com/docs/api/management/v2#!/Hooks/post_secrets
func (m *HookManager) CreateSecrets(ctx context.Context, hookID string, s HookSecrets, opts ...RequestOption) (err error) {
return m.management.Request(ctx, "POST", m.management.URI("hooks", hookID, "secrets"), &s, opts...)
}
// UpdateSecrets updates one or more existing secrets for an existing hook.
//
// See: https://auth0.com/docs/api/management/v2#!/Hooks/patch_secrets
func (m *HookManager) UpdateSecrets(ctx context.Context, hookID string, s HookSecrets, opts ...RequestOption) (err error) {
return m.management.Request(ctx, "PATCH", m.management.URI("hooks", hookID, "secrets"), &s, opts...)
}
// ReplaceSecrets replaces existing secrets with the provided ones.
//
// Note: ReplaceSecrets is a wrapper method and will internally call Secrets,
// CreateSecrets, UpdateSecrets or RemoveSecrets as needed in order to replicate
// PUT semantics.
func (m *HookManager) ReplaceSecrets(ctx context.Context, hookID string, s HookSecrets, opts ...RequestOption) (err error) {
o, err := m.Secrets(ctx, hookID, opts...)
if err != nil {
return err
}
if add := s.difference(o); len(add) > 0 {
err = m.CreateSecrets(ctx, hookID, add, opts...)
}
if update := s.intersection(o); len(update) > 0 {
err = m.UpdateSecrets(ctx, hookID, update, opts...)
}
if rm := o.difference(s); len(rm) > 0 {
err = m.RemoveSecrets(ctx, hookID, rm.Keys(), opts...)
}
return err
}
// Secrets retrieves a hook's secrets by the ID of the hook.
//
// Note: For security, hook secret values cannot be retrieved outside rule
// execution (they all appear as "_VALUE_NOT_SHOWN_").
//
// See: https://auth0.com/docs/api/management/v2/#!/Hooks/get_secrets
func (m *HookManager) Secrets(ctx context.Context, hookID string, opts ...RequestOption) (s HookSecrets, err error) {
err = m.management.Request(ctx, "GET", m.management.URI("hooks", hookID, "secrets"), &s, opts...)
return
}
// RemoveSecrets deletes one or more existing secrets for a given hook. Accepts
// an array of secret names to delete.
//
// See: https://auth0.com/docs/api/management/v2/#!/Hooks/delete_secrets
func (m *HookManager) RemoveSecrets(ctx context.Context, hookID string, keys []string, opts ...RequestOption) (err error) {
return m.management.Request(ctx, "DELETE", m.management.URI("hooks", hookID, "secrets"), keys, opts...)
}
// RemoveAllSecrets removes all secrets associated with a given hook.
func (m *HookManager) RemoveAllSecrets(ctx context.Context, hookID string, opts ...RequestOption) (err error) {
s, err := m.Secrets(ctx, hookID, opts...)
if err != nil {
return err
}
keys := s.Keys()
if len(keys) > 0 {
err = m.RemoveSecrets(ctx, hookID, keys, opts...)
}
return err
}