| 1 |
package element |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"sort" |
| 7 |
"strconv" |
| 8 |
|
| 9 |
"github.com/mitch000001/go-hbci/charset" |
| 10 |
"github.com/mitch000001/go-hbci/domain" |
| 11 |
) |
| 12 |
|
| 13 |
// NewSupportedSecurityMethod returns a new SupportedSecurityMethodDataElement |
| 14 |
func NewSupportedSecurityMethod(methodCode string, versions ...int) *SupportedSecurityMethodDataElement { |
| 15 |
s := &SupportedSecurityMethodDataElement{ |
| 16 |
MethodCode: NewAlphaNumeric(methodCode, 3), |
| 17 |
Versions: NewSecurityMethodVersions(1, 9, versions...), |
| 18 |
} |
| 19 |
s.DataElement = NewDataElementGroup(supportedSecurityMethodDEG, 2, s) |
| 20 |
return s |
| 21 |
} |
| 22 |
|
| 23 |
// SupportedSecurityMethodDataElement defines a DataElement for supported |
| 24 |
// security methods |
| 25 |
type SupportedSecurityMethodDataElement struct { |
| 26 |
DataElement |
| 27 |
// Code | Bedeutung |
| 28 |
// ------------------------------ |
| 29 |
// DDV | DES-DES-Verfahren |
| 30 |
// RDH | RSA-DES-Hybridverfahren |
| 31 |
MethodCode *AlphaNumericDataElement |
| 32 |
// At the moment only "1" is allowed |
| 33 |
Versions *SecurityMethodVersionsDataElement |
| 34 |
} |
| 35 |
|
| 36 |
// GroupDataElements returns the grouped DataElements within the |
| 37 |
// SupportedSecurityMethodDataElement |
| 38 |
func (s *SupportedSecurityMethodDataElement) GroupDataElements() []DataElement { |
| 39 |
return []DataElement{ |
| 40 |
s.MethodCode, |
| 41 |
s.Versions, |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// UnmarshalHBCI unmarshals the value to a SupportedSecurityMethodDataElement |
| 46 |
func (s *SupportedSecurityMethodDataElement) UnmarshalHBCI(value []byte) error { |
| 47 |
s.DataElement = NewDataElementGroup(supportedSecurityMethodDEG, 2, s) |
| 48 |
return s.DataElement.UnmarshalHBCI(value) |
| 49 |
} |
| 50 |
|
| 51 |
// NewSecurityMethodVersions returns a new SecurityMethodVersionsDataElement |
| 52 |
func NewSecurityMethodVersions(min, max int, versions ...int) *SecurityMethodVersionsDataElement { |
| 53 |
versionDEs := make([]DataElement, len(versions)) |
| 54 |
for i, version := range versions { |
| 55 |
versionDEs[i] = NewNumber(version, 3) |
| 56 |
} |
| 57 |
s := &SecurityMethodVersionsDataElement{} |
| 58 |
s.arrayElementGroup = newArrayElementGroup(securityMethodVersionGDEG, min, max, versionDEs) |
| 59 |
return s |
| 60 |
} |
| 61 |
|
| 62 |
// SecurityMethodVersionsDataElement represents the possible versions of a |
| 63 |
// security method. |
| 64 |
type SecurityMethodVersionsDataElement struct { |
| 65 |
*arrayElementGroup |
| 66 |
} |
| 67 |
|
| 68 |
// Elements returns the elements within the SecurityMethodVersionsDataElement |
| 69 |
func (s *SecurityMethodVersionsDataElement) Elements() []DataElement { |
| 70 |
return s.arrayElementGroup.array |
| 71 |
} |
| 72 |
|
| 73 |
// UnmarshalHBCI unmarshals the value to a SecurityMethodVersionsDataElement |
| 74 |
func (s *SecurityMethodVersionsDataElement) UnmarshalHBCI(value []byte) error { |
| 75 |
dataElements := make([]DataElement, 9) |
| 76 |
s.arrayElementGroup = newArrayElementGroup(securityMethodVersionGDEG, 1, 9, dataElements) |
| 77 |
return s.arrayElementGroup.UnmarshalHBCI(value) |
| 78 |
} |
| 79 |
|
| 80 |
// Versions returns the possible versions packaged in a slice of |
| 81 |
// NumberDataElements |
| 82 |
func (s *SecurityMethodVersionsDataElement) Versions() []*NumberDataElement { |
| 83 |
versions := make([]*NumberDataElement, len(s.arrayElementGroup.array)) |
| 84 |
for i, version := range s.arrayElementGroup.array { |
| 85 |
versions[i] = version.(*NumberDataElement) |
| 86 |
} |
| 87 |
return versions |
| 88 |
} |
| 89 |
|
| 90 |
// NewSupportedHBCIVersions returns a new SupportedHBCIVersionsDataElement |
| 91 |
func NewSupportedHBCIVersions(versions ...int) *SupportedHBCIVersionsDataElement { |
| 92 |
versionDEs := make([]DataElement, len(versions)) |
| 93 |
for i, version := range versions { |
| 94 |
versionDEs[i] = NewNumber(version, 3) |
| 95 |
} |
| 96 |
s := &SupportedHBCIVersionsDataElement{} |
| 97 |
s.arrayElementGroup = newArrayElementGroup(supportedHBCIVersionDEG, 1, 9, versionDEs) |
| 98 |
return s |
| 99 |
} |
| 100 |
|
| 101 |
var validHBCIVersions = []int{201, 210, 220, 300} |
| 102 |
|
| 103 |
// SupportedHBCIVersionsDataElement represents a DataElement for supported HBCI |
| 104 |
// versions |
| 105 |
type SupportedHBCIVersionsDataElement struct { |
| 106 |
*arrayElementGroup |
| 107 |
} |
| 108 |
|
| 109 |
// UnmarshalHBCI unmarshals the value to a SupportedHBCIVersionsDataElement |
| 110 |
func (s *SupportedHBCIVersionsDataElement) UnmarshalHBCI(value []byte) error { |
| 111 |
elements, err := ExtractElements(value) |
| 112 |
if err != nil { |
| 113 |
return err |
| 114 |
} |
| 115 |
if len(elements) == 0 || len(elements) > 9 { |
| 116 |
return fmt.Errorf("Malformed marshaled value") |
| 117 |
} |
| 118 |
versions := make([]DataElement, len(elements)) |
| 119 |
for i, elem := range elements { |
| 120 |
version, err := strconv.Atoi(charset.ToUTF8(elem)) |
| 121 |
if err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
versions[i] = NewNumber(version, 3) |
| 125 |
} |
| 126 |
s.arrayElementGroup = newArrayElementGroup(supportedHBCIVersionDEG, 1, 9, versions) |
| 127 |
return nil |
| 128 |
} |
| 129 |
|
| 130 |
// NewSupportedLanguages returns a new SupportedLanguagesDataElement |
| 131 |
func NewSupportedLanguages(languages ...int) *SupportedLanguagesDataElement { |
| 132 |
languageDEs := make([]DataElement, len(languages)) |
| 133 |
for i, lang := range languages { |
| 134 |
languageDEs[i] = NewNumber(lang, 3) |
| 135 |
} |
| 136 |
s := &SupportedLanguagesDataElement{} |
| 137 |
s.arrayElementGroup = newArrayElementGroup(supportedLanguagesDEG, 1, 9, languageDEs) |
| 138 |
return s |
| 139 |
} |
| 140 |
|
| 141 |
var validLanguages = []int{1, 2, 3} |
| 142 |
|
| 143 |
// SupportedLanguagesDataElement represents the supported languages by the bank |
| 144 |
// institute |
| 145 |
type SupportedLanguagesDataElement struct { |
| 146 |
*arrayElementGroup |
| 147 |
} |
| 148 |
|
| 149 |
// Languages returns the supported languages packaged within a slice of |
| 150 |
// NumberDataElements |
| 151 |
func (s *SupportedLanguagesDataElement) Languages() []*NumberDataElement { |
| 152 |
languages := make([]*NumberDataElement, len(s.arrayElementGroup.array)) |
| 153 |
for i, lang := range s.arrayElementGroup.array { |
| 154 |
languages[i] = lang.(*NumberDataElement) |
| 155 |
} |
| 156 |
return languages |
| 157 |
} |
| 158 |
|
| 159 |
// UnmarshalHBCI unmarshals the value into the SupportedLanguagesDataElement |
| 160 |
func (s *SupportedLanguagesDataElement) UnmarshalHBCI(value []byte) error { |
| 161 |
elements, err := ExtractElements(value) |
| 162 |
if err != nil { |
| 163 |
return err |
| 164 |
} |
| 165 |
if len(elements) == 0 || len(elements) > 9 { |
| 166 |
return fmt.Errorf("Malformed marshaled value") |
| 167 |
} |
| 168 |
languages := make([]DataElement, len(elements)) |
| 169 |
for i, elem := range elements { |
| 170 |
lang, err := strconv.Atoi(charset.ToUTF8(elem)) |
| 171 |
if err != nil { |
| 172 |
return err |
| 173 |
} |
| 174 |
if sort.SearchInts(validLanguages, lang) >= len(validLanguages) { |
| 175 |
return fmt.Errorf("Unsupported language code: %d", lang) |
| 176 |
} |
| 177 |
languages[i] = NewNumber(lang, 3) |
| 178 |
} |
| 179 |
s.arrayElementGroup = newArrayElementGroup(supportedLanguagesDEG, 1, 9, languages) |
| 180 |
return nil |
| 181 |
} |
| 182 |
|
| 183 |
// SupportedCompressionMethodsDataElement represents the compression methods |
| 184 |
// supported by the bank institute |
| 185 |
type SupportedCompressionMethodsDataElement struct { |
| 186 |
*arrayElementGroup |
| 187 |
} |
| 188 |
|
| 189 |
// A BusinessTransactionParameter defines parameters for a specific business |
| 190 |
// transaction. |
| 191 |
type BusinessTransactionParameter struct { |
| 192 |
DataElement |
| 193 |
DataElements []DataElement |
| 194 |
} |
| 195 |
|
| 196 |
// GroupDataElements returns the grouped DataElements. |
| 197 |
func (b *BusinessTransactionParameter) GroupDataElements() []DataElement { |
| 198 |
return b.DataElements |
| 199 |
} |
| 200 |
|
| 201 |
// NewPinTanBusinessTransactionParameters returns a new |
| 202 |
// PinTanBusinessTransactionParameters DataElement |
| 203 |
func NewPinTanBusinessTransactionParameters(pinTanTransactions []domain.PinTanBusinessTransaction) *PinTanBusinessTransactionParameters { |
| 204 |
transactionsDEs := make([]DataElement, len(pinTanTransactions)) |
| 205 |
for i, transaction := range pinTanTransactions { |
| 206 |
pinTanBusinessTransaction := &PinTanBusinessTransactionParameter{ |
| 207 |
SegmentID: NewAlphaNumeric(transaction.SegmentID, 6), |
| 208 |
NeedsTAN: NewBoolean(transaction.NeedsTan), |
| 209 |
} |
| 210 |
pinTanBusinessTransaction.DataElement = NewGroupDataElementGroup(pinTanBusinessTransactionParameterGDEG, 2, pinTanBusinessTransaction) |
| 211 |
transactionsDEs[i] = pinTanBusinessTransaction |
| 212 |
} |
| 213 |
p := &PinTanBusinessTransactionParameters{} |
| 214 |
p.arrayElementGroup = newArrayElementGroup(pinTanBusinessTransactionParameterGDEG, len(transactionsDEs), len(transactionsDEs), transactionsDEs) |
| 215 |
return p |
| 216 |
} |
| 217 |
|
| 218 |
// PinTanBusinessTransactionParameters represents a slice of |
| 219 |
// PinTanBusinessTransactionParameter DataElements |
| 220 |
type PinTanBusinessTransactionParameters struct { |
| 221 |
*arrayElementGroup |
| 222 |
} |
| 223 |
|
| 224 |
// Val returns the underlying PinTanBusinessTransactions |
| 225 |
func (p *PinTanBusinessTransactionParameters) Val() []domain.PinTanBusinessTransaction { |
| 226 |
transactions := make([]domain.PinTanBusinessTransaction, len(p.array)) |
| 227 |
for i, elem := range p.array { |
| 228 |
transactions[i] = elem.(*PinTanBusinessTransactionParameter).Val() |
| 229 |
} |
| 230 |
return transactions |
| 231 |
} |
| 232 |
|
| 233 |
// UnmarshalHBCI unmarshals value into the PinTanBusinessTransactionParameters |
| 234 |
func (p *PinTanBusinessTransactionParameters) UnmarshalHBCI(value []byte) error { |
| 235 |
elements, err := ExtractElements(value) |
| 236 |
if err != nil { |
| 237 |
return err |
| 238 |
} |
| 239 |
if len(elements)%2 != 0 { |
| 240 |
return fmt.Errorf("Malformed marshaled value: value pairs not even") |
| 241 |
} |
| 242 |
dataElements := make([]DataElement, len(elements)/2) |
| 243 |
for i := 0; i < len(elements); i += 2 { |
| 244 |
elem := bytes.Join(elements[i:i+2], []byte(":")) |
| 245 |
pinTanTransaction := &PinTanBusinessTransactionParameter{} |
| 246 |
err := pinTanTransaction.UnmarshalHBCI(elem) |
| 247 |
if err != nil { |
| 248 |
return err |
| 249 |
} |
| 250 |
dataElements[i/2] = pinTanTransaction |
| 251 |
} |
| 252 |
p.arrayElementGroup = newArrayElementGroup(pinTanBusinessTransactionParameterGDEG, len(dataElements), len(dataElements), dataElements) |
| 253 |
return nil |
| 254 |
} |
| 255 |
|
| 256 |
// PinTanBusinessTransactionParameter defines a specific |
| 257 |
// PinTanBusinessTransactionParameter DataElement |
| 258 |
type PinTanBusinessTransactionParameter struct { |
| 259 |
DataElement `yaml:"-"` |
| 260 |
SegmentID *AlphaNumericDataElement `yaml:"segmentID"` |
| 261 |
NeedsTAN *BooleanDataElement `yaml:"needsTan"` |
| 262 |
} |
| 263 |
|
| 264 |
// Elements returns the elements of this DataElement. |
| 265 |
func (p *PinTanBusinessTransactionParameter) Elements() []DataElement { |
| 266 |
return []DataElement{ |
| 267 |
p.SegmentID, |
| 268 |
p.NeedsTAN, |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
// Val returns the underlying PinTanBusinessTransaction |
| 273 |
func (p *PinTanBusinessTransactionParameter) Val() domain.PinTanBusinessTransaction { |
| 274 |
return domain.PinTanBusinessTransaction{ |
| 275 |
SegmentID: p.SegmentID.Val(), |
| 276 |
NeedsTan: p.NeedsTAN.Val(), |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
// UnmarshalHBCI unmarshals value into p |
| 281 |
func (p *PinTanBusinessTransactionParameter) UnmarshalHBCI(value []byte) error { |
| 282 |
elements, err := ExtractElements(value) |
| 283 |
if err != nil { |
| 284 |
return err |
| 285 |
} |
| 286 |
p.SegmentID = NewAlphaNumeric(charset.ToUTF8(elements[0]), 6) |
| 287 |
needsTan := &BooleanDataElement{} |
| 288 |
err = needsTan.UnmarshalHBCI(elements[1]) |
| 289 |
if err != nil { |
| 290 |
return err |
| 291 |
} |
| 292 |
p.NeedsTAN = needsTan |
| 293 |
p.DataElement = NewGroupDataElementGroup(pinTanBusinessTransactionParameterGDEG, 2, p) |
| 294 |
return nil |
| 295 |
} |