Skip to content

Commit

Permalink
make http client pluggable
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhille committed Feb 15, 2018
1 parent 53967c9 commit f67bd2b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
12 changes: 11 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions dialog/dialog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 22 additions & 8 deletions dialog/pin_tan_dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions dialog/pin_tan_dialog_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
8 changes: 8 additions & 0 deletions transport/https/https.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f67bd2b

Please sign in to comment.