-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.go
More file actions
358 lines (326 loc) · 10.7 KB
/
client.go
File metadata and controls
358 lines (326 loc) · 10.7 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package kps
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha1" // #nosec G505: KPS uçları HMAC-SHA1 istiyor
"encoding/base64"
"encoding/xml"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/beevik/etree"
"github.com/google/uuid"
dsig "github.com/russellhaering/goxmldsig"
)
const (
stsURL = "https://kimlikdogrulama.nvi.gov.tr/Services/Issuer.svc/IWSTrust13"
queryURL = "https://kpsv2.nvi.gov.tr/Services/RoutingService.svc"
soapNS12 = "http://www.w3.org/2003/05/soap-envelope"
wsuNS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
wsseNS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
wsaNS = "http://www.w3.org/2005/08/addressing"
dsigNS = "http://www.w3.org/2000/09/xmldsig#"
trustNS = "http://docs.oasis-open.org/ws-sx/ws-trust/200512"
methodURI = "http://kps.nvi.gov.tr/2025/08/01/TumKutukDogrulaServis/Sorgula"
)
type Client struct {
Username string
Password string
HTTPClient *http.Client
}
// New: basit kurucu. httpClient nil ise makul timeout’lu varsayılan kullanılır.
func New(username, password string, httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = &http.Client{Timeout: 30 * time.Second}
}
return &Client{
Username: username,
Password: password,
HTTPClient: httpClient,
}
}
// DoQuery: STS → imzalı servis → parse akışını yürütür.
func (c *Client) DoQuery(ctx context.Context, req QueryRequest) (Result, error) {
arts, rawSTS, err := callSTS(c.Username, c.Password)
if err != nil {
return Result{Status: false, Code: 2, Aciklama: fmt.Sprintf("STS hatası: %v", err), Raw: rawSTS}, err
}
body := BuildTumKutukBody(req)
rawSvc, err := c.callSignedService(ctx, arts, body)
if err != nil {
return Result{Status: false, Code: 2, Aciklama: fmt.Sprintf("Servis hatası: %v", err), Raw: rawSvc}, err
}
parsed, pErr := ParseTumKutukResponse(rawSvc)
if pErr != nil {
parsed.Raw = rawSvc
}
return parsed, pErr
}
type stsArtifacts struct {
BinarySecretB64 string // HMAC anahtarı (base64)
TokenXML string // RequestedSecurityToken içindeki EncryptedData (inner XML)
AssertionID string // KeyIdentifier (SAML Assertion ID)
}
func callSTS(username, password string) (stsArtifacts, string, error) {
msgID := "urn:uuid:" + uuid.New().String()
now := time.Now().UTC()
created := tsISO(now)
expires := tsISO(now.Add(5 * time.Minute))
rst := fmt.Sprintf(`<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="%s" xmlns:a="%s" xmlns:wst="%s" xmlns:wsse="%s" xmlns:wsu="%s" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
<s:Header>
<a:MessageID>%s</a:MessageID>
<a:To>%s</a:To>
<a:Action>http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue</a:Action>
<wsse:Security s:mustUnderstand="1">
<wsu:Timestamp wsu:Id="_0">
<wsu:Created>%s</wsu:Created>
<wsu:Expires>%s</wsu:Expires>
</wsu:Timestamp>
<wsse:UsernameToken wsu:Id="Me">
<wsse:Username>%s</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">%s</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</s:Header>
<s:Body>
<wst:RequestSecurityToken>
<wst:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1</wst:TokenType>
<wst:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</wst:RequestType>
<wsp:AppliesTo>
<a:EndpointReference>
<a:Address>%s</a:Address>
</a:EndpointReference>
</wsp:AppliesTo>
<wst:KeyType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey</wst:KeyType>
</wst:RequestSecurityToken>
</s:Body>
</s:Envelope>`,
soapNS12, wsaNS, trustNS, wsseNS, wsuNS,
msgID, stsURL,
created, expires,
xmlEscape(username), xmlEscape(password),
queryURL,
)
req, _ := http.NewRequest("POST", stsURL, bytes.NewReader([]byte(rst)))
req.Header.Set("Content-Type", "application/soap+xml; charset=utf-8")
cli := &http.Client{Timeout: 30 * time.Second}
resp, err := cli.Do(req)
if err != nil {
return stsArtifacts{}, "", err
}
defer resp.Body.Close()
b, _ := io.ReadAll(resp.Body)
raw := string(b)
if resp.StatusCode != http.StatusOK {
return stsArtifacts{}, raw, fmt.Errorf("sts http %d", resp.StatusCode)
}
arts, perr := parseSTSResponse(raw)
if perr != nil {
return stsArtifacts{}, raw, perr
}
return arts, raw, nil
}
func parseSTSResponse(respXML string) (stsArtifacts, error) {
dec := xml.NewDecoder(strings.NewReader(respXML))
var secret, keyID string
for {
tok, err := dec.Token()
if err == io.EOF {
break
}
if err != nil {
return stsArtifacts{}, err
}
switch t := tok.(type) {
case xml.StartElement:
switch t.Name.Local {
case "BinarySecret":
var v string
if err := dec.DecodeElement(&v, &t); err == nil {
if s := strings.TrimSpace(v); s != "" {
secret = s
}
}
case "KeyIdentifier":
var v string
if err := dec.DecodeElement(&v, &t); err == nil {
if s := strings.TrimSpace(v); s != "" {
keyID = s
}
}
}
}
}
tokenInner := extractInnerXMLOfTagAnyNS(respXML, "RequestedSecurityToken")
if strings.TrimSpace(secret) == "" || strings.TrimSpace(keyID) == "" || strings.TrimSpace(tokenInner) == "" {
return stsArtifacts{}, fmt.Errorf("STS parse error (secret/keyID/token missing)")
}
return stsArtifacts{
BinarySecretB64: strings.TrimSpace(secret),
TokenXML: strings.TrimSpace(tokenInner),
AssertionID: strings.TrimSpace(keyID),
}, nil
}
func (c *Client) callSignedService(ctx context.Context, art stsArtifacts, bodyXML string) (string, error) {
now := time.Now().UTC()
created := tsISO(now)
expires := tsISO(now.Add(5 * time.Minute))
msgID := "urn:uuid:" + uuid.New().String()
// (1) Timestamp (Id = _0)
timestamp := fmt.Sprintf(
`<wsu:Timestamp xmlns:wsu="%s" wsu:Id="_0"><wsu:Created>%s</wsu:Created><wsu:Expires>%s</wsu:Expires></wsu:Timestamp>`,
wsuNS, created, expires,
)
// (2) Canonicalize & digest
tsC14N, err := c14nExclusive(timestamp)
if err != nil {
return "", fmt.Errorf("c14n timestamp: %w", err)
}
tsSha1 := sha1.Sum(tsC14N)
digestValue := base64.StdEncoding.EncodeToString(tsSha1[:])
// (3) SignedInfo
signedInfo := fmt.Sprintf(
`<dsig:SignedInfo xmlns:dsig="%s">
<dsig:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<dsig:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#hmac-sha1"/>
<dsig:Reference URI="#_0">
<dsig:Transforms>
<dsig:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<dsig:DigestValue>%s</dsig:DigestValue>
</dsig:Reference>
</dsig:SignedInfo>`,
dsigNS, digestValue,
)
// (4) HMAC-SHA1(SignatureValue)
siC14N, err := c14nExclusive(signedInfo)
if err != nil {
return "", fmt.Errorf("c14n signedinfo: %w", err)
}
key, err := base64.StdEncoding.DecodeString(strings.TrimSpace(art.BinarySecretB64))
if err != nil {
return "", fmt.Errorf("decode secret: %w", err)
}
mac := hmac.New(sha1.New, key)
_, _ = mac.Write(siC14N)
sigB64 := base64.StdEncoding.EncodeToString(mac.Sum(nil))
// (5) Signature bloğu
signature := fmt.Sprintf(
`<dsig:Signature xmlns:dsig="%s">
%s
<dsig:SignatureValue>%s</dsig:SignatureValue>
<dsig:KeyInfo>
<wsse:SecurityTokenReference xmlns:wsse="%s">
<wsse:KeyIdentifier ValueType="http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.0#SAMLAssertionID">%s</wsse:KeyIdentifier>
</wsse:SecurityTokenReference>
</dsig:KeyInfo>
</dsig:Signature>`,
dsigNS, signedInfo, sigB64, wsseNS, xmlEscape(art.AssertionID),
)
// (6) Header (WS-Addressing + WS-Security)
header := fmt.Sprintf(`
<a:MessageID xmlns:a="%s">%s</a:MessageID>
<a:To xmlns:a="%s" s:mustUnderstand="1">%s</a:To>
<a:Action xmlns:a="%s" s:mustUnderstand="1">%s</a:Action>
<wsse:Security xmlns:wsse="%s" xmlns:wsu="%s" s:mustUnderstand="1">
%s
%s
%s
</wsse:Security>`,
wsaNS, msgID, wsaNS, queryURL, wsaNS, methodURI,
wsseNS, wsuNS,
timestamp, // imza bunun üzerinden alındı
art.TokenXML, // STS’ten gelen EncryptedData inner XML
signature,
)
// (7) Envelope
envelope := fmt.Sprintf(`<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="%s">
<s:Header>%s</s:Header>
<s:Body>%s</s:Body>
</s:Envelope>`, soapNS12, header, bodyXML)
// (8) POST
raw, status, err := httpPost(queryURL, "application/soap+xml; charset=utf-8", []byte(envelope))
if err != nil {
return raw, err
}
if status != http.StatusOK {
return raw, fmt.Errorf("service http %d", status)
}
return raw, nil
}
func httpPost(url, contentType string, body []byte) (string, int, error) {
req, _ := http.NewRequest("POST", url, bytes.NewReader(body))
req.Header.Set("Content-Type", contentType)
cli := &http.Client{Timeout: 30 * time.Second}
resp, err := cli.Do(req)
if err != nil {
return "", 0, err
}
defer resp.Body.Close()
b, _ := io.ReadAll(resp.Body)
return string(b), resp.StatusCode, nil
}
func c14nExclusive(fragment string) ([]byte, error) {
// etree yalnızca C14N için kullanılıyor
s := bytes.TrimSpace([]byte(fragment))
doc := etree.NewDocument()
if err := doc.ReadFromBytes(s); err != nil {
return nil, err
}
el := doc.Root()
if el == nil {
return nil, fmt.Errorf("c14nExclusive: empty root element")
}
canon := dsig.MakeC14N10ExclusiveCanonicalizerWithPrefixList("")
return canon.Canonicalize(el)
}
func xmlEscape(s string) string {
var buf bytes.Buffer
_ = xml.EscapeText(&buf, []byte(s))
return buf.String()
}
func tsISO(t time.Time) string {
return t.UTC().Format("2006-01-02T15:04:05Z")
}
func extractInnerXMLOfTagAnyNS(xmlStr, local string) string {
low := strings.ToLower(xmlStr)
needleOpen := "<" + strings.ToLower(local)
idx := strings.Index(low, needleOpen)
if idx < 0 {
needleOpen = ":" + strings.ToLower(local)
idx = strings.Index(low, needleOpen)
if idx < 0 {
return ""
}
idx = strings.LastIndex(low[:idx], "<")
if idx < 0 {
return ""
}
}
gt := strings.Index(low[idx:], ">")
if gt < 0 {
return ""
}
start := idx + gt + 1
closeTag := "</" + strings.ToLower(local) + ">"
end := strings.Index(low[start:], closeTag)
if end < 0 {
cand := strings.Index(low[start:], strings.ToLower(local)+">")
if cand >= 0 {
pre := strings.LastIndex(low[:start+cand+len(local)+1], "</")
if pre >= 0 {
end = pre - start
}
}
if end < 0 {
return ""
}
}
return xmlStr[start : start+end]
}