/
gln_refdata.go
160 lines (147 loc) · 5.1 KB
/
gln_refdata.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package business
import (
"context"
"encoding/xml"
"fmt"
"regexp"
"strings"
"time"
"unicode/utf8"
"github.com/cloudtrust/httpclient"
"github.com/cloudtrust/keycloak-bridge/internal/keycloakb"
"github.com/cloudtrust/keycloak-client"
"github.com/pkg/errors"
"gopkg.in/h2non/gentleman.v2/plugin"
"gopkg.in/h2non/gentleman.v2/plugins/body"
"gopkg.in/h2non/gentleman.v2/plugins/headers"
)
var (
soapRequest = strings.Trim(`
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<DownloadPartnerInput xmlns="http://refdatabase.refdata.ch/">
<TYPE xmlns="http://refdatabase.refdata.ch/Partner_in">GLN</TYPE>
<PTYPE xmlns="http://refdatabase.refdata.ch/Partner_in">ALL</PTYPE>
<TERM xmlns="http://refdatabase.refdata.ch/Partner_in">##GLN##</TERM>
</DownloadPartnerInput>
</soap:Body>
</soap:Envelope>
`, " \r\n\t")
)
type soapPartnerResponse struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
XSI *string `xml:"xmlns:xsi,attr"`
XSD *string `xml:"xmlns:xsd,attr"`
Header interface{} `xml:"http://schemas.xmlsoap.org/soap/envelope/ Header"`
Body struct {
Partner struct {
Creation *string `xml:"CREATION_DATETIME,attr"`
Namespace *string `xml:"xmlns,attr"`
Result struct {
Namespace *string `xml:"xmlns,attr"`
OkError *string `xml:"OK_ERROR"`
NbrRecord *int `xml:"NBR_RECORD"`
} `xml:"RESULT"`
Items []soapItem `xml:"ITEM"`
} `xml:"PARTNER"`
} `xml:"Body"`
}
type soapItem struct {
Date *string `xml:"DT,attr"`
Namespace *string `xml:"xmlns,attr"`
PartnerType *string `xml:"PTYPE,omitempty"`
GLN *string `xml:"GLN,omitempty"`
Status *string `xml:"STATUS,omitempty"`
StartDate *string `xml:"STDATE,omitempty"`
Language *string `xml:"LANG,omitempty"`
LastName *string `xml:"DESCR1,omitempty"`
FirstName *string `xml:"DESCR2,omitempty"`
Role struct {
Type *string `xml:"TYPE,omitempty"`
ZipCode *string `xml:"ZIP,omitempty"`
City *string `xml:"CITY,omitempty"`
Canton *string `xml:"CTN,omitempty"`
Country *string `xml:"CNTRY,omitempty"`
} `xml:"ROLE,omitempty"`
}
type glnRefData struct {
client *httpclient.Client
soapAction string
logger keycloakb.Logger
}
// NewRefDataLookup creates a GLN lookup tool using refdata.ch
func NewRefDataLookup(baseURL string, httpTimeout time.Duration, logger keycloakb.Logger) (GlnLookupProvider, error) {
var client, err = httpclient.New(baseURL, httpTimeout)
if err != nil {
return nil, err
}
var soapAction = "http://refdatabase.refdata.ch/Download"
re := regexp.MustCompile(`/`)
idx := re.FindAllIndex([]byte(baseURL), 3)
if len(idx) > 2 {
// First 2 slashes are the ones between the protocol and the server name. We split starting of the third one
soapAction = strings.ReplaceAll(baseURL[0:idx[2][0]], "https", "http") + "/Download"
}
return &glnRefData{
client: client,
soapAction: soapAction,
logger: logger,
}, nil
}
func (l *glnRefData) Lookup(gln string) GlnSearchResult {
var bytes, err = l.request(gln)
if err != nil {
return GlnSearchResult{Error: err}
}
return l.xmlToDetails(gln, bytes)
}
func (l *glnRefData) request(gln string) ([]byte, error) {
var bodyValue = strings.ReplaceAll(soapRequest, "##GLN##", gln)
var plugins []plugin.Plugin
plugins = append(plugins,
headers.Set("Content-Type", "text/xml; charset=utf-8"),
headers.Set("Content-Length", fmt.Sprintf("%d", utf8.RuneCountInString(bodyValue))),
headers.Set("SOAPAction", l.soapAction),
body.String(bodyValue),
)
var response []byte
var _, err = l.client.Post(&response, plugins...)
if err != nil {
l.logger.Warn(context.Background(), "msg", "Can't get response from refData", "err", err.Error(), "gln", gln)
return nil, errors.Wrap(err, keycloak.MsgErrCannotObtain+"."+keycloak.Response)
}
return response, nil
}
func (l *glnRefData) xmlToDetails(gln string, xmlContent []byte) GlnSearchResult {
var response soapPartnerResponse
if err := xml.Unmarshal(xmlContent, &response); err != nil {
return GlnSearchResult{Error: ErrGLNCantParse}
}
if response.Body.Partner.Result.OkError == nil || *response.Body.Partner.Result.OkError != "OK" {
l.logger.Info(context.Background(), "msg", "Failed to request GLN through refdata", "gln", gln)
return GlnSearchResult{Error: ErrGLNNotFound}
}
if response.Body.Partner.Result.NbrRecord == nil || *response.Body.Partner.Result.NbrRecord == 0 {
return GlnSearchResult{Error: ErrGLNNotFound}
}
var result []GlnPerson
for _, item := range response.Body.Partner.Items {
var active = item.Status != nil && *item.Status == "A"
result = append(result, GlnPerson{
Active: &active,
Number: item.GLN,
FirstName: item.FirstName,
LastName: item.LastName,
ZipCode: item.Role.ZipCode,
City: item.Role.City,
Canton: item.Role.Canton,
Country: item.Role.Country,
Profession: item.Role.Type,
})
}
return GlnSearchResult{
Persons: result,
Error: nil,
}
}