summaryrefslogtreecommitdiff
path: root/order.go
blob: eea8d5caaf4c4f6aaf34d2389e239face1deb53c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package libebics

import (
	"bytes"
	"compress/zlib"
	"encoding/base64"
	"encoding/xml"
	"fmt"
)

type EbicsUnsecuredRequest struct {
	XMLName  xml.Name    `xml:"ebicsUnsecuredRequest"`
	Xmlns    string      `xml:"xmlns,attr"`
	Version  string      `xml:"Version,attr"`
	Revision string      `xml:"Revision,attr"`
	Header   EbicsHeader `xml:"header"`
	Body     EbicsBody   `xml:"body"`
}

type EbicsHeader struct {
	XMLName      xml.Name           `xml:"header"`
	Authenticate bool               `xml:"authenticate,attr"`
	Static       EbicsHeaderStatic  `xml:"static"`
	Mutable      EbicsHeaderMutable `xml:"mutable"`
}

type EbicsHeaderStatic struct {
	XMLName        xml.Name          `xml:"static"`
	HostID         string            `xml:"HostID"`
	PartnerID      string            `xml:"PartnerID"`
	UserID         string            `xml:"UserID"`
	Product        EbicsProduct      `xml:"Product"`
	OrderDetails   EbicsOrderDetails `xml:"OrderDetails"`
	SecurityMedium string            `xml:"SecurityMedium"`
}

type EbicsProduct struct {
	XMLName  xml.Name `xml:"Product"`
	Language string   `xml:"Language,attr"`
	Name     string   `xml:",chardata"`
}

type EbicsOrderDetails struct {
	XMLName        xml.Name `xml:"OrderDetails"`
	AdminOrderType string   `xml:"AdminOrderType"`
}

type EbicsHeaderMutable struct {
	XMLName xml.Name `xml:"mutable"`
}

type EbicsBody struct {
	XMLName      xml.Name          `xml:"body"`
	DataTransfer EbicsDataTransfer `xml:"DataTransfer"`
}

type EbicsDataTransfer struct {
	XMLName   xml.Name `xml:"DataTransfer"`
	OrderData string   `xml:"OrderData"` // TODO Properly marshal this
}

type EbicsSignaturePubKeyOrderData struct {
	XMLName             xml.Name                 `xml:"esig:SignaturePubKeyOrderData"`
	Esig                string                   `xml:"xmlns:esi,attr"`
	Ds                  string                   `xml:"xmlns:ds,attr"`
	SignaturePubKeyInfo EbicsSignaturePubKeyInfo `xml:"esig:SignaturePubKeyInfo"`
	PartnerID           string                   `xml:"esig:PartnerID"`
	UserID              string                   `xml:"esig:UserID"`
}

type EbicsSignaturePubKeyInfo struct {
	XMLName          xml.Name      `xml:"esig:SignaturePubKeyInfo"`
	X509Data         EbicsX509Data `xml:"ds:X509Data"`
	SignatureVersion string        `xml:"esig:SignatureVersion"` // TODO Use ENUM
}

type EbicsX509Data struct {
	XMLName         xml.Name `xml:"ds:X509Data"`
	X509Certificate string   `xml:"ds:X509Certificate"` // TODO Properly marshal this
}

func GenerateIniRequest(signatureCertificate EbicsCertificate) (string, error) {

	if signatureCertificate.ebicsCertificateType != A005 &&
		signatureCertificate.ebicsCertificateType != A006 {
		return "", fmt.Errorf("signature certificate has invalid certificate type %s", signatureCertificate.ebicsCertificateType.String())
	}

	var zlibWriterBuffer bytes.Buffer
	zlibWriter := zlib.NewWriter(&zlibWriterBuffer)

	orderData := EbicsSignaturePubKeyOrderData{
		SignaturePubKeyInfo: EbicsSignaturePubKeyInfo{
			X509Data: EbicsX509Data{
				X509Certificate: base64.StdEncoding.EncodeToString(
					signatureCertificate.x509Certificate,
				),
			},
			SignatureVersion: signatureCertificate.ebicsCertificateType.String(),
		},
		PartnerID: "MV008078",
		UserID:    "SCHOLZ01",
	}

	orderData_xml, err := xml.Marshal(orderData)
	if err != nil {
		return "", err
	}

	zlibWriter.Write(orderData_xml)
	zlibWriter.Close()

	ini := EbicsUnsecuredRequest{
		Xmlns:    "urn:org:ebics:H005",
		Version:  "H005",
		Revision: "1",
		Header: EbicsHeader{
			Authenticate: true,
			Static: EbicsHeaderStatic{
				HostID:    "MULTIVIA",
				PartnerID: "MV008078",
				UserID:    "SCHOLZ01",
				Product: EbicsProduct{
					Language: "en",
					Name:     "libebics",
				},
				OrderDetails: EbicsOrderDetails{
					AdminOrderType: "INI",
				},
				SecurityMedium: "0000",
			},
		},
		Body: EbicsBody{
			DataTransfer: EbicsDataTransfer{
				OrderData: base64.StdEncoding.EncodeToString(
					zlibWriterBuffer.Bytes(),
				),
			},
		},
	}

	marshal, err := xml.MarshalIndent(ini, "", "  ")

	return (xml.Header + string(marshal)), err
}