|
1 | 1 | package playwright
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/base64" |
4 | 5 | "encoding/json"
|
5 | 6 | "errors"
|
6 | 7 | "fmt"
|
@@ -46,6 +47,14 @@ func (b *browserImpl) NewContext(options ...BrowserNewContextOptions) (BrowserCo
|
46 | 47 | overrides["extraHTTPHeaders"] = serializeMapToNameAndValue(options[0].ExtraHttpHeaders)
|
47 | 48 | options[0].ExtraHttpHeaders = nil
|
48 | 49 | }
|
| 50 | + if option.ClientCertificates != nil { |
| 51 | + certs, err := transformClientCertificate(option.ClientCertificates) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + overrides["clientCertificates"] = certs |
| 56 | + options[0].ClientCertificates = nil |
| 57 | + } |
49 | 58 | if option.StorageStatePath != nil {
|
50 | 59 | var storageState *OptionalStorageState
|
51 | 60 | storageString, err := os.ReadFile(*options[0].StorageStatePath)
|
@@ -217,3 +226,49 @@ func newBrowser(parent *channelOwner, objectType string, guid string, initialize
|
217 | 226 | b.channel.On("close", b.onClose)
|
218 | 227 | return b
|
219 | 228 | }
|
| 229 | + |
| 230 | +func transformClientCertificate(clientCertificates []ClientCertificate) ([]map[string]interface{}, error) { |
| 231 | + results := make([]map[string]interface{}, 0) |
| 232 | + |
| 233 | + for _, cert := range clientCertificates { |
| 234 | + data := map[string]interface{}{ |
| 235 | + "origin": cert.Origin, |
| 236 | + "passphrase": cert.Passphrase, |
| 237 | + } |
| 238 | + if len(cert.Cert) > 0 { |
| 239 | + data["cert"] = base64.StdEncoding.EncodeToString(cert.Cert) |
| 240 | + } else if cert.CertPath != nil { |
| 241 | + content, err := os.ReadFile(*cert.CertPath) |
| 242 | + if err != nil { |
| 243 | + return nil, err |
| 244 | + } |
| 245 | + data["cert"] = base64.StdEncoding.EncodeToString(content) |
| 246 | + } |
| 247 | + |
| 248 | + if len(cert.Key) > 0 { |
| 249 | + data["key"] = base64.StdEncoding.EncodeToString(cert.Key) |
| 250 | + } else if cert.KeyPath != nil { |
| 251 | + content, err := os.ReadFile(*cert.KeyPath) |
| 252 | + if err != nil { |
| 253 | + return nil, err |
| 254 | + } |
| 255 | + data["key"] = base64.StdEncoding.EncodeToString(content) |
| 256 | + } |
| 257 | + |
| 258 | + if len(cert.Pfx) > 0 { |
| 259 | + data["pfx"] = base64.StdEncoding.EncodeToString(cert.Pfx) |
| 260 | + } else if cert.PfxPath != nil { |
| 261 | + content, err := os.ReadFile(*cert.PfxPath) |
| 262 | + if err != nil { |
| 263 | + return nil, err |
| 264 | + } |
| 265 | + data["pfx"] = base64.StdEncoding.EncodeToString(content) |
| 266 | + } |
| 267 | + |
| 268 | + results = append(results, data) |
| 269 | + } |
| 270 | + if len(results) == 0 { |
| 271 | + return nil, nil |
| 272 | + } |
| 273 | + return results, nil |
| 274 | +} |
0 commit comments