Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
787 lines (643 loc) · 20 KB
/
Copy pathclient.go
File metadata and controls
787 lines (643 loc) · 20 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
// Package turnkey provides a client for interacting with the Turnkey API.
package turnkey
import (
"bytes"
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
)
//go:embed VERSION
var embeddedVersion string
var DefaultClientVersion = "go-sdk/" + strings.TrimSpace(embeddedVersion)
const (
defaultBaseURL = "https://api.turnkey.com"
defaultAuthProxyURL = "https://authproxy.turnkey.com"
defaultHTTPTimeout = 30 * time.Second
)
type urlConfig struct {
baseURL string
authProxyBaseURL string
}
type httpConfig struct {
maxRetries int
retryDelay time.Duration
}
type pollConfig struct {
interval time.Duration
timeout time.Duration
}
type config struct {
organizationID string
authProxyConfigID string
stamper Stamper
httpClient *http.Client
logger Logger
clientVersion string
urls urlConfig
http httpConfig
activityPoll pollConfig
mfaPoll pollConfig
consensusPoll pollConfig
}
type activityTyper interface {
ActivityType() string
}
type defaultLogger struct{}
func (d *defaultLogger) Printf(format string, v ...any) {
fmt.Printf(format+"\n", v...)
}
// Logger defines a minimal logging interface.
type Logger interface {
Printf(format string, v ...any)
}
type loggingRoundTripper struct {
inner http.RoundTripper
logger Logger
}
// RoundTrip implements http.RoundTripper, logging any errors from the request.
func (lrt *loggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := lrt.inner.RoundTrip(req)
if err != nil {
lrt.logger.Printf("Request failed: %v", err)
return nil, err
}
return resp, nil
}
// OptionFunc defines a function which sets configuration options for a Client.
type OptionFunc func(c *config) error
// WithLogger sets a custom logger for the SDK.
func WithLogger(logger Logger) OptionFunc {
return func(c *config) error {
c.logger = logger
return nil
}
}
// WithBaseURL overrides the Turnkey API base URL.
func WithBaseURL(baseURL string) OptionFunc {
return func(c *config) error {
c.urls.baseURL = strings.TrimRight(baseURL, "/")
return nil
}
}
// WithAuthProxyBaseURL overrides the Turnkey Auth Proxy base URL.
func WithAuthProxyBaseURL(baseURL string) OptionFunc {
return func(c *config) error {
c.urls.authProxyBaseURL = strings.TrimRight(baseURL, "/")
return nil
}
}
// WithAuthProxyConfigID sets the Auth Proxy config ID header value.
func WithAuthProxyConfigID(configID string) OptionFunc {
return func(c *config) error {
c.authProxyConfigID = configID
return nil
}
}
// DefaultAuthProxyConfigID returns the configured Auth Proxy config ID, nil if not set.
func (c *Client) DefaultAuthProxyConfigID() *string {
if c.config.authProxyConfigID != "" {
return &c.config.authProxyConfigID
}
return nil
}
// WithHTTPClient sets the HTTP client used by the SDK.
func WithHTTPClient(httpClient *http.Client) OptionFunc {
return func(c *config) error {
c.httpClient = httpClient
return nil
}
}
// WithHTTPRetries sets the number of retries for transient HTTP errors (5xx, network failures).
func WithHTTPRetries(n int) OptionFunc {
return func(c *config) error {
if n < 0 {
return fmt.Errorf("httpMaxRetries must be >= 0, got %d", n)
}
c.http.maxRetries = n
return nil
}
}
// WithHTTPRetryDelay sets the base delay for HTTP retry backoff.
func WithHTTPRetryDelay(d time.Duration) OptionFunc {
return func(c *config) error {
c.http.retryDelay = d
return nil
}
}
// WithActivityPollInterval sets the activity polling interval.
func WithActivityPollInterval(interval time.Duration) OptionFunc {
return func(c *config) error {
c.activityPoll.interval = interval
return nil
}
}
// WithActivityPollTimeout sets the activity polling timeout.
func WithActivityPollTimeout(timeout time.Duration) OptionFunc {
return func(c *config) error {
c.activityPoll.timeout = timeout
return nil
}
}
// WithMFAPollInterval sets the MFA polling interval.
func WithMFAPollInterval(interval time.Duration) OptionFunc {
return func(c *config) error {
c.mfaPoll.interval = interval
return nil
}
}
// WithMFAPollTimeout sets the MFA polling timeout.
func WithMFAPollTimeout(timeout time.Duration) OptionFunc {
return func(c *config) error {
c.mfaPoll.timeout = timeout
return nil
}
}
func WithMFAPolling(interval, timeout time.Duration) OptionFunc {
return func(c *config) error {
if interval <= 0 || timeout <= 0 {
return fmt.Errorf("MFA interval and timeout must both be > 0")
}
c.mfaPoll.interval = interval
c.mfaPoll.timeout = timeout
return nil
}
}
func WithConsensusPolling(interval, timeout time.Duration) OptionFunc {
return func(c *config) error {
if interval <= 0 || timeout <= 0 {
return fmt.Errorf("consensus interval and timeout must both be > 0")
}
c.consensusPoll.interval = interval
c.consensusPoll.timeout = timeout
return nil
}
}
// NewClient returns a new Turnkey API client.
// stamper signs each authenticated request; pass nil only if the client will not make signed requests.
// organizationID sets the default organization; it may be overridden per-request.
func NewClient(stamper Stamper, organizationID string, options ...OptionFunc) (*Client, error) {
cfg := &config{
organizationID: organizationID,
stamper: stamper,
httpClient: http.DefaultClient,
logger: &defaultLogger{},
clientVersion: DefaultClientVersion,
urls: urlConfig{
baseURL: defaultBaseURL,
authProxyBaseURL: defaultAuthProxyURL,
},
http: httpConfig{
maxRetries: 3,
retryDelay: 100 * time.Millisecond,
},
activityPoll: pollConfig{
interval: time.Second,
timeout: 2 * time.Minute,
},
mfaPoll: pollConfig{
interval: 0,
timeout: 0,
},
consensusPoll: pollConfig{
interval: 0,
timeout: 0,
},
}
for _, option := range options {
if err := option(cfg); err != nil {
return nil, err
}
}
if cfg.urls.baseURL == "" {
return nil, errors.New("base URL is required")
}
if cfg.urls.authProxyBaseURL == "" {
return nil, errors.New("auth proxy base URL is required")
}
if cfg.httpClient == nil {
cfg.httpClient = http.DefaultClient
}
if cfg.logger == nil {
cfg.logger = &defaultLogger{}
}
transport := cfg.httpClient.Transport
if transport == nil {
transport = http.DefaultTransport
}
timeout := cfg.httpClient.Timeout
if timeout == 0 {
timeout = defaultHTTPTimeout
}
cfg.httpClient = &http.Client{
Transport: &loggingRoundTripper{inner: transport, logger: cfg.logger},
Timeout: timeout,
}
return &Client{config: cfg}, nil
}
// Client provides a handle by which to interact with the Turnkey API.
type Client struct {
config *config
}
// BaseURL returns the configured Turnkey API base URL.
func (c *Client) BaseURL() string {
return c.config.urls.baseURL
}
// AuthProxyBaseURL returns the configured Turnkey Auth Proxy base URL.
func (c *Client) AuthProxyBaseURL() string {
return c.config.urls.authProxyBaseURL
}
// ClientVersion returns the configured client version header value.
func (c *Client) ClientVersion() string {
return c.config.clientVersion
}
// DefaultOrganization returns the configured organization ID, or nil if none was set.
func (c *Client) DefaultOrganization() *string {
if c.config.organizationID != "" {
return &c.config.organizationID
}
return nil
}
func (c *Client) organizationID(inputOrganizationID string) (string, error) {
if inputOrganizationID != "" {
return inputOrganizationID, nil
}
if org := c.DefaultOrganization(); org != nil {
return *org, nil
}
return "", errors.New("organizationId is required; set OrganizationID on the request or initialize the client with a default organization ID")
}
// classifyActivity categorizes an activity type (consensus needed, failed, completed, etc.) and extracts the typed result if available.
func classifyActivity(activity *Activity) (*Activity, bool, error) {
switch activity.Status {
case ActivityStatusCompleted:
return activity, true, nil
case ActivityStatusFailed, ActivityStatusRejected:
return nil, true, &ActivityFailedError{
ActivityID: activity.ID,
Status: activity.Status,
Failure: activity.Failure,
}
case ActivityStatusConsensusNeeded, ActivityStatusAuthenticatorsNeeded:
return nil, true, &ActivityRequiresApprovalError{ActivityID: activity.ID, Activity: *activity}
case ActivityStatusPending, ActivityStatusCreated:
return nil, false, nil
default:
return nil, true, fmt.Errorf("unexpected activity status: %s", activity.Status)
}
}
func activityAndWait[T any](ctx context.Context, c *Client, reply *ActivityResult[T], extract func(Result) *T) (*T, *Activity, error) {
if _, done, err := classifyActivity(&reply.Activity); err != nil {
return nil, nil, err // failed / rejected / consensus-needed
} else if done {
result := extract(reply.Activity.Result)
if result == nil {
return nil, nil, fmt.Errorf("activity %s completed with nil result", reply.Activity.ID)
}
return result, &reply.Activity, nil
}
// Pending or created -> poll
activity, err := c.waitActivity(ctx, reply.Activity.ID)
if err != nil {
return nil, nil, err
}
result := extract(activity.Result)
if result == nil {
return nil, nil, fmt.Errorf("activity %s completed with nil result", activity.ID)
}
return result, activity, nil
}
// activityAndWaitNoResult polls an activity with no typed result until it completes, returning the final Activity.
func activityAndWaitNoResult(ctx context.Context, c *Client, reply *ActivityResult[map[string]any]) (*Activity, error) {
if reply.Activity.Status == ActivityStatusCompleted {
return &reply.Activity, nil
}
return c.waitActivity(ctx, reply.Activity.ID)
}
// postJSON sends a POST request with a JSON body and decodes the JSON response.
func (c *Client) postJSON(ctx context.Context, baseURL string, path string, body any, out any, sign bool, extraHeaders map[string]string) error {
data, err := json.Marshal(body)
if err != nil {
return err
}
var stamp *Stamp
if sign {
stamp, err = c.stamp(ctx, data)
if err != nil {
return err
}
}
respBody, err := c.postWithRetry(ctx, baseURL, path, data, stamp, extraHeaders)
if err != nil {
return err
}
if out == nil || len(respBody) == 0 {
return nil
}
return json.Unmarshal(respBody, out)
}
// postWithRetry sends a POST request with the given body and retries with exponential backoff.
func (c *Client) postWithRetry(ctx context.Context, baseURL, path string, data []byte, stamp *Stamp, extraHeaders map[string]string) ([]byte, error) {
const maxRetryDelay = 5 * time.Second
delay := c.config.http.retryDelay
var lastErr error
for attempt := 0; attempt <= c.config.http.maxRetries; attempt++ {
if attempt > 0 {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(delay):
delay = min(delay*2, maxRetryDelay)
}
}
respBody, retryable, err := c.postAttempt(ctx, baseURL, path, data, stamp, extraHeaders)
if err == nil {
return respBody, nil
}
if !retryable {
return nil, err
}
lastErr = err
}
return nil, fmt.Errorf("after %d attempts: %w", c.config.http.maxRetries+1, lastErr)
}
// postAttempt sends a single POST request and returns whether the error is retryable.
func (c *Client) postAttempt(ctx context.Context, baseURL, path string, data []byte, stamp *Stamp, extraHeaders map[string]string) ([]byte, bool, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(baseURL, "/")+path, bytes.NewReader(data))
if err != nil {
return nil, false, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Client-Version", c.config.clientVersion)
for key, value := range extraHeaders {
req.Header.Set(key, value)
}
if stamp != nil {
req.Header.Set(stamp.HeaderName, stamp.HeaderValue)
}
resp, err := c.config.httpClient.Do(req)
if err != nil {
return nil, true, err
}
respBody, readErr := io.ReadAll(resp.Body)
if closeErr := resp.Body.Close(); closeErr != nil && readErr == nil {
readErr = closeErr
}
if readErr != nil {
return nil, true, readErr
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
reqErr := newRequestError(resp.StatusCode, respBody)
return nil, resp.StatusCode >= 500, reqErr
}
return respBody, false, nil
}
// signedRequest creates a SignedRequest for the given path and body, signing it if requested.
func (c *Client) signedRequest(ctx context.Context, baseURL string, path string, body any, sign bool) (*SignedRequest, error) {
data, err := json.Marshal(body)
if err != nil {
return nil, err
}
var stamp *Stamp
if sign {
stamp, err = c.stamp(ctx, data)
if err != nil {
return nil, err
}
}
sr := &SignedRequest{
URL: strings.TrimRight(baseURL, "/") + path,
Body: string(data),
Stamp: stamp,
Type: RequestTypeActivity,
}
return sr, nil
}
// newRequestError creates a RequestError by parsing the Turnkey API error response body.
func newRequestError(statusCode int, body []byte) error {
var parsed errorResponse
if err := json.Unmarshal(body, &parsed); err != nil {
return &RequestError{StatusCode: statusCode, Body: body}
}
return &RequestError{
StatusCode: statusCode,
Status: &RPCStatus{
Code: parsed.Code,
Message: parsed.Message,
Details: parsed.Details,
},
Body: body,
}
}
// decodeActivityResponse decodes the raw JSON response from an activity request into an ActivityResult with a typed result.
func decodeActivityResponse[T any](raw map[string]any, resultType string, out *ActivityResult[T]) error {
data, err := json.Marshal(raw)
if err != nil {
return err
}
var envelope ActivityResponse
if err := json.Unmarshal(data, &envelope); err != nil {
return err
}
out.Activity = envelope.Activity
if len(resultType) == 0 {
return nil
}
result, err := extractActivityResult[T](raw, resultType)
if err != nil {
return err
}
out.Result = result
return nil
}
// extractActivityResult extracts the typed result from the raw JSON response of an activity request based on the result type.
func extractActivityResult[T any](raw map[string]any, resultType string) (*T, error) {
activityRaw, ok := raw["activity"].(map[string]any)
if !ok {
return nil, nil
}
resultRaw, ok := activityRaw["result"].(map[string]any)
if !ok {
return nil, nil
}
resultField := strings.ToLower(resultType[:1]) + resultType[1:]
value, ok := resultRaw[resultField]
if !ok || value == nil {
return nil, nil
}
resultData, err := json.Marshal(value)
if err != nil {
return nil, err
}
var result T
if err := json.Unmarshal(resultData, &result); err != nil {
return nil, err
}
return &result, nil
}
// stamp generates a Stamp for the given request body by signing it with the configured Stamper.
func (c *Client) stamp(ctx context.Context, body []byte) (*Stamp, error) {
if c.config.stamper == nil {
return nil, errors.New("stamper is required for signed Turnkey requests")
}
return c.config.stamper.Stamp(ctx, body)
}
// activityEnvelope creates the JSON envelope for an activity request, extracting the organization ID and timestamp from the input if available.
func (c *Client) activityEnvelope(input activityTyper) (map[string]any, error) {
body, err := toMap(input)
if err != nil {
return nil, err
}
var inputOrgID string
if v, ok := body["organizationId"].(string); ok {
inputOrgID = v
}
organizationID, err := c.organizationID(inputOrgID)
if err != nil {
return nil, err
}
var timestampMs string
if v, ok := body["timestampMs"].(string); ok {
timestampMs = v
}
if timestampMs == "" {
timestampMs = fmt.Sprintf("%d", time.Now().UnixMilli())
}
delete(body, "organizationId")
delete(body, "timestampMs")
delete(body, "parameters")
return map[string]any{
"type": input.ActivityType(),
"timestampMs": timestampMs,
"organizationId": organizationID,
"parameters": body,
}, nil
}
// topMap converts a struct to a map[string]any by JSON-marshaling and unmarshaling it.
func toMap(input any) (map[string]any, error) {
data, err := json.Marshal(input)
if err != nil {
return nil, err
}
var out map[string]any
if err := json.Unmarshal(data, &out); err != nil {
return nil, err
}
for key, value := range out {
if value == nil {
delete(out, key)
}
}
return out, nil
}
// waitActivity polls an activity until it reaches a final status.
// If consensus polling is configured and the activity hits CONSENSUS_NEEDED,
// it transitions to pollConsensus until terminal or consensus timeout.
// If the activity hits AUTHENTICATORS_NEEDED, it transitions to pollMFA.
func (c *Client) waitActivity(ctx context.Context, activityID string) (*Activity, error) {
activity, err := c.pollActivity(ctx, activityID)
if err != nil {
return activity, err
}
switch activity.Status {
case ActivityStatusConsensusNeeded:
if c.config.consensusPoll.interval == 0 || c.config.consensusPoll.timeout == 0 {
return nil, &ActivityRequiresApprovalError{ActivityID: activity.ID, Activity: *activity}
}
return c.pollConsensus(ctx, activity)
case ActivityStatusAuthenticatorsNeeded:
if c.config.mfaPoll.interval == 0 || c.config.mfaPoll.timeout == 0 {
return nil, &ActivityRequiresApprovalError{ActivityID: activity.ID, Activity: *activity}
}
return c.pollMFA(ctx, activity)
default:
return activity, nil
}
}
// pollConsensus polls an activity in CONSENSUS_NEEDED status until it reaches a terminal status,
// returning an error if the consensus timeout is reached first.
func (c *Client) pollConsensus(ctx context.Context, latest *Activity) (*Activity, error) {
deadline := time.NewTimer(c.config.consensusPoll.timeout)
defer deadline.Stop()
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-deadline.C:
return nil, &ActivityRequiresApprovalError{ActivityID: latest.ID, Activity: *latest}
case <-time.After(c.config.consensusPoll.interval):
}
activity, err := c.GetActivity(ctx, GetActivityRequest{ActivityID: latest.ID})
if err != nil {
return nil, err
}
if activity.Activity.Status != ActivityStatusConsensusNeeded {
result, _, err := classifyActivity(&activity.Activity)
return result, err
}
latest = &activity.Activity
}
}
// pollActivity polls at the activity-poll cadence with exponential backoff.
// Returns when the activity reaches any terminal status, CONSENSUS_NEEDED is
// or AUTHENTICATORS_NEEDED are returned to the caller so they can transition
// to their dedicated polling phases.
func (c *Client) pollActivity(ctx context.Context, activityID string) (*Activity, error) {
deadline := time.NewTimer(c.config.activityPoll.timeout)
defer deadline.Stop()
delay := c.config.activityPoll.interval
const maxDelay = 5 * time.Second
for {
activity, err := c.GetActivity(ctx, GetActivityRequest{ActivityID: activityID})
if err != nil {
return nil, err
}
if activity.Activity.Status == ActivityStatusConsensusNeeded ||
activity.Activity.Status == ActivityStatusAuthenticatorsNeeded {
return &activity.Activity, nil
}
if result, done, err := classifyActivity(&activity.Activity); done {
return result, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-deadline.C:
return nil, fmt.Errorf("timed out waiting for activity %s", activityID)
case <-time.After(delay):
}
delay = min(delay*2, maxDelay)
}
}
// pollMFA polls an activity in AUTHENTICATORS_NEEDED status until it reaches a terminal status,
// returning an approval-required error if the MFA timeout is reached first.
func (c *Client) pollMFA(ctx context.Context, latest *Activity) (*Activity, error) {
deadline := time.NewTimer(c.config.mfaPoll.timeout)
defer deadline.Stop()
delay := c.config.mfaPoll.interval
const maxDelay = 5 * time.Second
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-deadline.C:
return nil, &ActivityRequiresApprovalError{ActivityID: latest.ID, Activity: *latest}
case <-time.After(delay):
}
activity, err := c.GetActivity(ctx, GetActivityRequest{ActivityID: latest.ID})
if err != nil {
return nil, err
}
if activity.Activity.Status != ActivityStatusAuthenticatorsNeeded {
result, _, err := classifyActivity(&activity.Activity)
return result, err
}
latest = &activity.Activity
delay = min(delay*2, maxDelay)
}
}