-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.go
More file actions
149 lines (124 loc) · 4.62 KB
/
Copy pathauth.go
File metadata and controls
149 lines (124 loc) · 4.62 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
package idrx
import (
"context"
"fmt"
"net/http"
"strconv"
"time"
)
// AuthProvider defines the interface for authentication strategies.
// This follows the strategy pattern to support different auth mechanisms.
type AuthProvider interface {
// Authenticate adds authentication headers to the HTTP request
Authenticate(ctx context.Context, req *http.Request, body any) error
// GetAuthType returns the type of authentication (business, user, etc.)
GetAuthType() string
}
// BusinessAuth provides authentication for business-level API operations.
// This is used for general endpoints and organization management.
type BusinessAuth struct {
apiKey string
secretKey string
}
// NewBusinessAuth creates a new business-level authentication provider.
func NewBusinessAuth(apiKey, secretKey string) AuthProvider {
return &BusinessAuth{
apiKey: apiKey,
secretKey: secretKey,
}
}
// Authenticate implements the AuthProvider interface for business auth.
func (b *BusinessAuth) Authenticate(ctx context.Context, req *http.Request, body any) error {
// Check if context is already canceled
if err := ctx.Err(); err != nil {
return fmt.Errorf("context canceled during authentication: %w", err)
}
return b.addAuthHeaders(ctx, req, body)
}
// GetAuthType returns the authentication type.
func (b *BusinessAuth) GetAuthType() string {
return "business"
}
// addAuthHeaders adds the required authentication headers for IDRX API.
func (b *BusinessAuth) addAuthHeaders(ctx context.Context, req *http.Request, body any) error {
// Check context before proceeding with potentially time-consuming operations
if err := ctx.Err(); err != nil {
return fmt.Errorf("context canceled before signature generation: %w", err)
}
timestamp := strconv.FormatInt(time.Now().UnixMilli(), 10)
// Generate signature using existing CreateSignature function
signature, err := CreateSignature(req.Method, req.URL.Path, body, timestamp, b.secretKey)
if err != nil {
return fmt.Errorf("failed to create signature: %w", err)
}
// Check context again before setting headers
if err := ctx.Err(); err != nil {
return fmt.Errorf("context canceled during header setup: %w", err)
}
// Add required IDRX authentication headers
req.Header.Set("idrx-api-key", b.apiKey)
req.Header.Set("idrx-api-sig", signature)
req.Header.Set("idrx-api-ts", timestamp)
return nil
}
// UserAuth provides authentication for user-level API operations.
// This is used for user-specific operations and is obtained from onboarding response.
type UserAuth struct {
apiKey string
secretKey string
}
// NewUserAuth creates a new user-level authentication provider.
func NewUserAuth(apiKey, secretKey string) AuthProvider {
return &UserAuth{
apiKey: apiKey,
secretKey: secretKey,
}
}
// Authenticate implements the AuthProvider interface for user auth.
func (u *UserAuth) Authenticate(ctx context.Context, req *http.Request, body any) error {
// Check if context is already canceled
if err := ctx.Err(); err != nil {
return fmt.Errorf("context canceled during authentication: %w", err)
}
return u.addAuthHeaders(ctx, req, body)
}
// GetAuthType returns the authentication type.
func (u *UserAuth) GetAuthType() string {
return "user"
}
// addAuthHeaders adds the required authentication headers for IDRX API.
func (u *UserAuth) addAuthHeaders(ctx context.Context, req *http.Request, body any) error {
// Check context before proceeding with potentially time-consuming operations
if err := ctx.Err(); err != nil {
return fmt.Errorf("context canceled before signature generation: %w", err)
}
timestamp := strconv.FormatInt(time.Now().UnixMilli(), 10)
// Generate signature using existing CreateSignature function
signature, err := CreateSignature(req.Method, req.URL.Path, body, timestamp, u.secretKey)
if err != nil {
return fmt.Errorf("failed to create signature: %w", err)
}
// Check context again before setting headers
if err := ctx.Err(); err != nil {
return fmt.Errorf("context canceled during header setup: %w", err)
}
// Add required IDRX authentication headers
req.Header.Set("idrx-api-key", u.apiKey)
req.Header.Set("idrx-api-sig", signature)
req.Header.Set("idrx-api-ts", timestamp)
return nil
}
// NoAuth provides a no-op authentication provider for testing or public endpoints.
type NoAuth struct{}
// NewNoAuth creates a new no-authentication provider.
func NewNoAuth() AuthProvider {
return &NoAuth{}
}
// Authenticate implements the AuthProvider interface with no-op.
func (n *NoAuth) Authenticate(_ context.Context, _ *http.Request, _ any) error {
return nil
}
// GetAuthType returns the authentication type.
func (n *NoAuth) GetAuthType() string {
return "none"
}