From f67bd2b59b8b7a59538ad84c1c3732fe42bce065 Mon Sep 17 00:00:00 2001 From: Max Hille Date: Tue, 6 Feb 2018 16:38:28 +0100 Subject: [PATCH] make http client pluggable --- client/client.go | 12 ++++++++++- dialog/dialog_test.go | 12 +++++++---- dialog/pin_tan_dialog.go | 30 +++++++++++++++++++------- dialog/pin_tan_dialog_examples_test.go | 16 +++++++------- transport/https/https.go | 8 +++++++ 5 files changed, 57 insertions(+), 21 deletions(-) diff --git a/client/client.go b/client/client.go index d201033..58712f2 100644 --- a/client/client.go +++ b/client/client.go @@ -11,6 +11,7 @@ import ( "github.com/mitch000001/go-hbci/element" "github.com/mitch000001/go-hbci/message" "github.com/mitch000001/go-hbci/segment" + "github.com/mitch000001/go-hbci/transport" ) // Config defines the basic configuration needed for a Client to work. @@ -20,6 +21,7 @@ type Config struct { PIN string `json:"pin"` URL string `json:"url"` HBCIVersion int `json:"hbci_version"` + Transport transport.Transport } func (c Config) hbciVersion() (segment.HBCIVersion, error) { @@ -64,7 +66,15 @@ func New(config Config) (*Client, error) { } hbciVersion = version } - d := dialog.NewPinTanDialog(bankID, url, config.AccountID, hbciVersion) + dcfg := dialog.Config{ + BankID: bankID, + HBCIURL: url, + UserID: config.AccountID, + HBCIVersion: hbciVersion, + Transport: config.Transport, + } + + d := dialog.NewPinTanDialog(dcfg) d.SetPin(config.PIN) client := &Client{ config: config, diff --git a/dialog/dialog_test.go b/dialog/dialog_test.go index fb191d4..b4539ff 100644 --- a/dialog/dialog_test.go +++ b/dialog/dialog_test.go @@ -244,10 +244,14 @@ func TestPinTanDialogInit(t *testing.T) { } func newTestPinTanDialog(transport *mockHTTPSTransport) *PinTanDialog { - url := "http://localhost" - clientID := "12345" - bankID := domain.BankID{CountryCode: 280, ID: "10000000"} - d := NewPinTanDialog(bankID, url, clientID, segment.HBCI220) + cfg := Config{ + BankID: domain.BankID{CountryCode: 280, ID: "10000000"}, + HBCIURL: "http://localhost", + UserID: "12345", + HBCIVersion: segment.HBCI220, + } + + d := NewPinTanDialog(cfg) d.SetPin("abcde") d.SetClientSystemID("xyz") d.transport = transport diff --git a/dialog/pin_tan_dialog.go b/dialog/pin_tan_dialog.go index 1fb141d..f35fa89 100644 --- a/dialog/pin_tan_dialog.go +++ b/dialog/pin_tan_dialog.go @@ -12,24 +12,38 @@ import ( middleware "github.com/mitch000001/go-hbci/transport/middleware" ) +// Config contains the configuration of a PinTanDialog +type Config struct { + BankID domain.BankID + HBCIURL string + UserID string + HBCIVersion segment.HBCIVersion + Transport transport.Transport +} + // NewPinTanDialog creates a new dialog to use for pin/tan transport -func NewPinTanDialog(bankID domain.BankID, hbciURL string, userID string, hbciVersion segment.HBCIVersion) *PinTanDialog { - pinKey := domain.NewPinKey("", domain.NewPinTanKeyName(bankID, userID, "S")) +func NewPinTanDialog(config Config) *PinTanDialog { + pinKey := domain.NewPinKey("", domain.NewPinTanKeyName(config.BankID, config.UserID, "S")) signatureProvider := message.NewPinTanSignatureProvider(pinKey, "0") - pinKey = domain.NewPinKey("", domain.NewPinTanKeyName(bankID, userID, "V")) + pinKey = domain.NewPinKey("", domain.NewPinTanKeyName(config.BankID, config.UserID, "V")) cryptoProvider := message.NewPinTanCryptoProvider(pinKey, "0") d := &PinTanDialog{ dialog: newDialog( - bankID, - hbciURL, - userID, - hbciVersion, + config.BankID, + config.HBCIURL, + config.UserID, + config.HBCIVersion, signatureProvider, cryptoProvider, ), } + var dialogTransport transport.Transport - dialogTransport = https.New() + if config.Transport == nil { + dialogTransport = https.New() + } else { + dialogTransport = config.Transport + } dialogTransport = middleware.Base64Encoding(base64.StdEncoding)(dialogTransport) dialogTransport = middleware.Logging(internal.Debug)(dialogTransport) d.transport = dialogTransport diff --git a/dialog/pin_tan_dialog_examples_test.go b/dialog/pin_tan_dialog_examples_test.go index fc698a2..5da554e 100644 --- a/dialog/pin_tan_dialog_examples_test.go +++ b/dialog/pin_tan_dialog_examples_test.go @@ -2,18 +2,18 @@ package dialog_test import ( "github.com/mitch000001/go-hbci/domain" - "github.com/mitch000001/go-hbci/segment" ) import "github.com/mitch000001/go-hbci/dialog" func ExamplePinTanDialog() { - url := "https://bank.de/hbci" - userId := "100000000" - blz := "1000000" - bankId := domain.BankID{ - CountryCode: 280, - ID: blz, + cfg := dialog.Config{ + HBCIURL: "https://bank.de/hbci", + UserID: "100000000", + BankID: domain.BankID{ + CountryCode: 280, + ID: "1000000", + }, } - d := dialog.NewPinTanDialog(bankId, url, userId, segment.HBCI220) + d := dialog.NewPinTanDialog(cfg) d.SetPin("12345") } diff --git a/transport/https/https.go b/transport/https/https.go index c170820..d6eb573 100644 --- a/transport/https/https.go +++ b/transport/https/https.go @@ -67,6 +67,14 @@ func New() *HTTPSTransport { } } +// NewNonDefault returns a HTTPSTransport which uses the given http.Client to +// perform requests to the HBCO server +func NewNonDefault(h *http.Client) transport.Transport { + return &HTTPSTransport{ + HTTPClient: h, + } +} + // A HTTPSTransport implements transport.Transport and performs request over HTTPS type HTTPSTransport struct { HTTPClient *http.Client