-
Notifications
You must be signed in to change notification settings - Fork 8
/
xml.go
197 lines (167 loc) · 4.47 KB
/
xml.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
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
package xml
import (
"bufio"
"bytes"
"compress/flate"
"encoding/base64"
"encoding/xml"
"fmt"
"net/http"
"strings"
"github.com/zitadel/saml/pkg/provider/xml/samlp"
"github.com/zitadel/saml/pkg/provider/xml/soap"
"github.com/zitadel/saml/pkg/provider/xml/xml_dsig"
)
const (
EncodingDeflate = "urn:oasis:names:tc:SAML:2.0:bindings:URL-Encoding:DEFLATE"
)
func Marshal(data interface{}) (string, error) {
var xmlbuff bytes.Buffer
memWriter := bufio.NewWriter(&xmlbuff)
_, err := memWriter.Write([]byte(xml.Header))
if err != nil {
return "", err
}
encoder := xml.NewEncoder(memWriter)
err = encoder.Encode(data)
if err != nil {
return "", err
}
err = memWriter.Flush()
if err != nil {
return "", err
}
return xmlbuff.String(), nil
}
func DeflateAndBase64(data []byte) ([]byte, error) {
b := &bytes.Buffer{}
w1 := base64.NewEncoder(base64.StdEncoding, b)
defer w1.Close()
w2, _ := flate.NewWriter(w1, 1)
defer w2.Close()
bw := bufio.NewWriter(w1)
if _, err := bw.Write(data); err != nil {
return nil, err
}
if err := bw.Flush(); err != nil {
return nil, err
}
if err := w2.Flush(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func WriteXMLMarshalled(w http.ResponseWriter, body interface{}) error {
_, err := w.Write([]byte(xml.Header))
if err != nil {
return err
}
encoder := xml.NewEncoder(w)
err = encoder.Encode(body)
if err != nil {
return err
}
err = encoder.Flush()
return err
}
func Write(w http.ResponseWriter, body []byte) error {
_, err := w.Write(body)
return err
}
func DecodeAuthNRequest(encoding string, message string) (*samlp.AuthnRequestType, error) {
reqBytes, err := base64.StdEncoding.DecodeString(message)
if err != nil {
return nil, fmt.Errorf("failed to base64 decode: %w", err)
}
req := &samlp.AuthnRequestType{}
switch encoding {
case EncodingDeflate:
reader := flate.NewReader(bytes.NewReader(reqBytes))
decoder := xml.NewDecoder(reader)
if err = decoder.Decode(req); err != nil {
return nil, fmt.Errorf("failed to defalte decode: %w", err)
}
default:
reader := flate.NewReader(bytes.NewReader(reqBytes))
decoder := xml.NewDecoder(reader)
if err = decoder.Decode(req); err != nil {
if err := xml.Unmarshal(reqBytes, req); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
}
}
return req, nil
}
func DecodeSignature(encoding string, message string) (*xml_dsig.SignatureType, error) {
retBytes := []byte(message)
ret := &xml_dsig.SignatureType{}
switch encoding {
case EncodingDeflate:
reader := flate.NewReader(bytes.NewReader(retBytes))
decoder := xml.NewDecoder(reader)
if err := decoder.Decode(ret); err != nil {
return nil, fmt.Errorf("failed to defalte decode: %w", err)
}
default:
reader := flate.NewReader(bytes.NewReader(retBytes))
decoder := xml.NewDecoder(reader)
if err := decoder.Decode(ret); err != nil {
if err := xml.Unmarshal(retBytes, ret); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
}
}
return ret, nil
}
func DecodeAttributeQuery(request string) (*samlp.AttributeQueryType, error) {
decoder := xml.NewDecoder(strings.NewReader(request))
var attrEnv soap.AttributeQueryEnvelope
err := decoder.Decode(&attrEnv)
if err != nil {
return nil, err
}
return attrEnv.Body.AttributeQuery, nil
}
func DecodeLogoutRequest(encoding string, message string) (*samlp.LogoutRequestType, error) {
reqBytes, err := base64.StdEncoding.DecodeString(message)
if err != nil {
return nil, err
}
req := &samlp.LogoutRequestType{}
switch encoding {
case "":
reader := flate.NewReader(bytes.NewReader(reqBytes))
decoder := xml.NewDecoder(reader)
if err = decoder.Decode(req); err != nil {
return nil, err
}
case EncodingDeflate:
reader := flate.NewReader(bytes.NewReader(reqBytes))
decoder := xml.NewDecoder(reader)
if err = decoder.Decode(req); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unknown encoding")
}
return req, nil
}
func DecodeResponse(encoding string, message string) (*samlp.ResponseType, error) {
req := &samlp.ResponseType{}
switch encoding {
case "":
decoder := xml.NewDecoder(bytes.NewReader([]byte(message)))
if err := decoder.Decode(req); err != nil {
return nil, err
}
case EncodingDeflate:
reader := flate.NewReader(bytes.NewReader([]byte(message)))
decoder := xml.NewDecoder(reader)
if err := decoder.Decode(req); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unknown encoding")
}
return req, nil
}