@@ -19,14 +19,12 @@ import (
19
19
)
20
20
21
21
type Client struct {
22
- conn * websocket.Conn
23
- config * Config
24
- baseURL string
25
- handlers map [string ]MessageHandler
26
- done chan struct {}
27
- handlersMux sync.RWMutex
28
- tlsConfig * tls.Config
29
-
22
+ conn * websocket.Conn
23
+ config * Config
24
+ baseURL string
25
+ handlers map [string ]MessageHandler
26
+ done chan struct {}
27
+ handlersMux sync.RWMutex
30
28
reconnectInterval time.Duration
31
29
isConnected bool
32
30
reconnectMux sync.RWMutex
@@ -45,9 +43,9 @@ func WithBaseURL(url string) ClientOption {
45
43
}
46
44
}
47
45
48
- func WithTLSConfig (tlsConfig * tls. Config ) ClientOption {
46
+ func WithTLSConfig (tlsClientCertPath string ) ClientOption {
49
47
return func (c * Client ) {
50
- c .tlsConfig = tlsConfig
48
+ c .config . TlsClientCert = tlsClientCertPath
51
49
}
52
50
}
53
51
@@ -73,8 +71,13 @@ func NewClient(newtID, secret string, endpoint string, opts ...ClientOption) (*C
73
71
}
74
72
75
73
// Apply options before loading config
76
- for _ , opt := range opts {
77
- opt (client )
74
+ if opts != nil {
75
+ for _ , opt := range opts {
76
+ if opt == nil {
77
+ continue
78
+ }
79
+ opt (client )
80
+ }
78
81
}
79
82
80
83
// Load existing config if available
@@ -187,10 +190,13 @@ func (c *Client) getToken() (string, error) {
187
190
188
191
// Make the request
189
192
client := & http.Client {}
190
- if c .tlsConfig != nil {
191
- logger .Info ("Adding tls to req" )
193
+ if c .config .TlsClientCert != "" {
194
+ tlsConfig , err := LoadClientCertificate (c .config .TlsClientCert )
195
+ if err != nil {
196
+ return "" , fmt .Errorf ("failed to load certificate %s: %w" , c .config .TlsClientCert , err )
197
+ }
192
198
client .Transport = & http.Transport {
193
- TLSClientConfig : c . tlsConfig ,
199
+ TLSClientConfig : tlsConfig ,
194
200
}
195
201
}
196
202
resp , err := client .Do (req )
@@ -236,9 +242,13 @@ func (c *Client) getToken() (string, error) {
236
242
237
243
// Make the request
238
244
client := & http.Client {}
239
- if c .tlsConfig != nil {
245
+ if c .config .TlsClientCert != "" {
246
+ tlsConfig , err := LoadClientCertificate (c .config .TlsClientCert )
247
+ if err != nil {
248
+ return "" , fmt .Errorf ("failed to load certificate %s: %w" , c .config .TlsClientCert , err )
249
+ }
240
250
client .Transport = & http.Transport {
241
- TLSClientConfig : c . tlsConfig ,
251
+ TLSClientConfig : tlsConfig ,
242
252
}
243
253
}
244
254
resp , err := client .Do (req )
@@ -317,8 +327,13 @@ func (c *Client) establishConnection() error {
317
327
318
328
// Connect to WebSocket
319
329
dialer := websocket .DefaultDialer
320
- if c .tlsConfig != nil {
321
- dialer .TLSClientConfig = c .tlsConfig
330
+ if c .config .TlsClientCert != "" {
331
+ logger .Info ("Adding tls to req" )
332
+ tlsConfig , err := LoadClientCertificate (c .config .TlsClientCert )
333
+ if err != nil {
334
+ return fmt .Errorf ("failed to load certificate %s: %w" , c .config .TlsClientCert , err )
335
+ }
336
+ dialer .TLSClientConfig = tlsConfig
322
337
}
323
338
conn , _ , err := dialer .Dial (u .String (), nil )
324
339
if err != nil {
@@ -381,6 +396,7 @@ func (c *Client) setConnected(status bool) {
381
396
382
397
// LoadClientCertificate Helper method to load client certificates
383
398
func LoadClientCertificate (p12Path string ) (* tls.Config , error ) {
399
+ logger .Info ("Loading tls-client-cert %s" , p12Path )
384
400
// Read the PKCS12 file
385
401
p12Data , err := os .ReadFile (p12Path )
386
402
if err != nil {
@@ -392,7 +408,7 @@ func LoadClientCertificate(p12Path string) (*tls.Config, error) {
392
408
if err != nil {
393
409
return nil , fmt .Errorf ("failed to decode PKCS12: %w" , err )
394
410
}
395
-
411
+
396
412
// Create certificate
397
413
cert := tls.Certificate {
398
414
Certificate : [][]byte {certificate .Raw },
0 commit comments