Commit 6e92286e authored by Sybren A. Stüvel's avatar Sybren A. Stüvel

More stuff and stuff

parent 5293b708
OUT := bunqtest.exe
OUT := bunqtest
PKG := gitlab.com/dr.sybren/bunqapi
VERSION := $(shell git describe --tags --dirty --always)
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/)
......
......@@ -7,6 +7,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/sirupsen/logrus"
......@@ -126,8 +127,22 @@ func (c *Client) DoRequest(method, endpoint string, payload interface{}, respons
}
if err := json.Unmarshal(respBody, response); err != nil {
logger.WithError(err).Fatal("unable to parse response JSON")
logger.WithFields(logrus.Fields{
logrus.ErrorKey: err,
"body": string(respBody),
}).Fatal("unable to parse response JSON")
}
fmt.Println("\nBODY:\n" + string(respBody))
fmt.Println("\nParsed and then as JSON:")
asJson, err := json.MarshalIndent(response, "", " ")
if err != nil {
panic(err)
}
os.Stdout.Write(asJson)
fmt.Println()
logger.WithField("body", string(respBody)).Info("response")
return nil
}
......@@ -4,8 +4,8 @@ const libraryVersion = "0.0.1"
// URLs of the bunq API.
const (
// urlSandbox = "https://public-api.sandbox.bunq.com/v1/"
urlSandbox = "http://localhost:4444/v1/"
urlSandbox = "https://public-api.sandbox.bunq.com/v1/"
// urlSandbox = "http://localhost:4444/v1/"
urlProduction = "https://api.bunq.com/v1/"
)
......
......@@ -4,6 +4,8 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"github.com/sirupsen/logrus"
)
type installationRequest struct {
......@@ -11,13 +13,20 @@ type installationRequest struct {
}
type installationResponse struct {
ID BunqID `json:"Id"`
Token struct {
ID int `json:"id"`
Created string `json:"created"`
Updated string `json:"updated"`
Token string `json:"token"`
} `json:"Token"`
Response []struct {
ID BunqID `json:"Id"`
Token struct {
ID int `json:"id"`
Created string `json:"created"`
Updated string `json:"updated"`
Token string `json:"token"`
} `json:"Token"`
ServerPublicKey struct {
Key string `json:"server_public_key"`
} `json:"ServerPublicKey"`
} `json:"Response"`
}
func publicKeyString(privateKey *rsa.PrivateKey) string {
......@@ -47,4 +56,9 @@ func (c *Client) PostInstallation() {
if errResp != nil {
log.WithFields(errResp.LogFields()).Fatal("error performing installation request")
}
log.WithFields(logrus.Fields{
"installationToken": response.Response[0].Token.Token,
"serverPublicKey": response.Response[0].ServerPublicKey.Key,
}).Info("installation token obtained")
}
package bunqapi
import "github.com/sirupsen/logrus"
import (
"fmt"
"github.com/sirupsen/logrus"
)
type BunqID struct {
ID string `json:"id"`
ID int `json:"id"`
}
type ErrorResponse struct {
Error struct {
Error []struct {
Description string `json:"error_description"`
Translated string `json:"error_description_translated"`
} `json:"Error"`
}
func (er *ErrorResponse) LogFields() logrus.Fields {
return logrus.Fields{
"errorDescription": er.Error.Description,
"errorTranslated": er.Error.Translated,
switch len(er.Error) {
case 0:
return logrus.Fields{"errorDescription": "-missing-"}
case 1:
return logrus.Fields{
"errorDescription": er.Error[0].Description,
"errorTranslated": er.Error[0].Translated,
}
default:
f := logrus.Fields{}
for index, err := range er.Error {
f[fmt.Sprintf("errorDescription%d", index)] = err.Description
f[fmt.Sprintf("errorTranslated%d", index)] = err.Translated
}
return f
}
}
package bunqapi
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"net/http"
"sort"
"strings"
......@@ -54,10 +55,12 @@ func (c *Client) SignRequest(r *http.Request) error {
}
hash("\n")
io.Copy(hasher, r.Body)
if seeker, ok := r.Body.(io.Seeker); ok {
seeker.Seek(0, io.SeekStart)
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
log.WithError(err).Panic("unable to read request body for signing")
}
hasher.Write(bodyBytes)
r.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
sum := hasher.Sum(nil)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment