-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathokta.go
More file actions
182 lines (157 loc) · 5.08 KB
/
Copy pathokta.go
File metadata and controls
182 lines (157 loc) · 5.08 KB
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
package connector
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
// OktaListUsersConnector lists users from an Okta organization.
type OktaListUsersConnector struct {
Client *http.Client
baseURL string
}
func (c *OktaListUsersConnector) apiURL(domain, path string) string {
if c.baseURL != "" {
return c.baseURL + path
}
return "https://" + domain + path
}
func (c *OktaListUsersConnector) Execute(ctx context.Context, params map[string]any) (map[string]any, error) {
domain, token, err := extractOktaCredential(params)
if err != nil {
return nil, fmt.Errorf("okta/list_users: %w", err)
}
query := url.Values{}
if q, ok := params["q"].(string); ok && q != "" {
query.Set("q", q)
}
if filter, ok := params["filter"].(string); ok && filter != "" {
query.Set("filter", filter)
}
if limit, ok := extractInt(params["limit"]); ok && limit > 0 {
query.Set("limit", fmt.Sprintf("%d", limit))
}
endpoint := c.apiURL(domain, "/api/v1/users")
if len(query) > 0 {
endpoint += "?" + query.Encode()
}
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
if err != nil {
return nil, fmt.Errorf("okta/list_users: creating request: %w", err)
}
req.Header.Set("Authorization", "SSWS "+token)
req.Header.Set("Accept", "application/json")
resp, err := httpClient(c.Client).Do(req)
if err != nil {
return nil, fmt.Errorf("okta/list_users: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, DefaultMaxResponseBytes))
if err != nil {
return nil, fmt.Errorf("okta/list_users: reading response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("okta/list_users: Okta API returned %d: %s", resp.StatusCode, truncate(string(body), 500))
}
var users []any
if err := json.Unmarshal(body, &users); err != nil {
return nil, fmt.Errorf("okta/list_users: parsing response: %w", err)
}
return map[string]any{"users": users, "count": len(users)}, nil
}
// OktaCreateUserConnector creates a new user in an Okta organization.
type OktaCreateUserConnector struct {
Client *http.Client
baseURL string
}
func (c *OktaCreateUserConnector) apiURL(domain, path string) string {
if c.baseURL != "" {
return c.baseURL + path
}
return "https://" + domain + path
}
func (c *OktaCreateUserConnector) Execute(ctx context.Context, params map[string]any) (map[string]any, error) {
domain, token, err := extractOktaCredential(params)
if err != nil {
return nil, fmt.Errorf("okta/create_user: %w", err)
}
profile, _ := params["profile"].(map[string]any)
if profile == nil {
return nil, fmt.Errorf("okta/create_user: profile is required")
}
if _, ok := profile["login"]; !ok {
return nil, fmt.Errorf("okta/create_user: profile.login is required")
}
if _, ok := profile["email"]; !ok {
return nil, fmt.Errorf("okta/create_user: profile.email is required")
}
body := map[string]any{"profile": profile}
if credentials, ok := params["credentials"].(map[string]any); ok {
body["credentials"] = credentials
}
if groupIDs, ok := params["group_ids"].([]any); ok && len(groupIDs) > 0 {
body["groupIds"] = groupIDs
}
activate := true
if a, ok := params["activate"].(bool); ok {
activate = a
}
reqJSON, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("okta/create_user: marshaling request: %w", err)
}
endpoint := fmt.Sprintf("%s?activate=%v", c.apiURL(domain, "/api/v1/users"), activate)
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewReader(reqJSON))
if err != nil {
return nil, fmt.Errorf("okta/create_user: creating request: %w", err)
}
req.Header.Set("Authorization", "SSWS "+token)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := httpClient(c.Client).Do(req)
if err != nil {
return nil, fmt.Errorf("okta/create_user: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(io.LimitReader(resp.Body, DefaultMaxResponseBytes))
if err != nil {
return nil, fmt.Errorf("okta/create_user: reading response: %w", err)
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("okta/create_user: Okta API returned %d: %s", resp.StatusCode, truncate(string(respBody), 500))
}
return parseJSONBody(respBody, "okta/create_user")
}
func extractOktaCredential(params map[string]any) (domain, token string, err error) {
raw, ok := params["_credential"]
if !ok || raw == nil {
return "", "", fmt.Errorf("credential is required")
}
delete(params, "_credential")
var cred map[string]string
switch v := raw.(type) {
case map[string]string:
cred = v
case map[string]any:
cred = make(map[string]string, len(v))
for k, val := range v {
if s, ok := val.(string); ok {
cred[k] = s
}
}
default:
return "", "", fmt.Errorf("credential is required")
}
domain = cred["domain"]
if domain == "" {
return "", "", fmt.Errorf("credential must contain a 'domain' field (e.g. dev-xxx.okta.com)")
}
token = cred["token"]
if token == "" {
return "", "", fmt.Errorf("credential must contain a 'token' field")
}
return domain, token, nil
}