forked from RobotsAndPencils/go-saml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthnrequest.go
295 lines (269 loc) · 8.08 KB
/
authnrequest.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
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
// Copyright 2014 Matthew Baird, Andrew Mussey
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package saml
import (
"crypto"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/xml"
"errors"
"fmt"
"net/url"
"time"
"github.com/maditya/go-saml/util"
)
func ParseCompressedEncodedRequest(b64RequestXML string) (*AuthnRequest, error) {
var authnRequest AuthnRequest
compressedXML, err := base64.StdEncoding.DecodeString(b64RequestXML)
if err != nil {
return nil, err
}
bXML := util.Decompress(compressedXML)
err = xml.Unmarshal(bXML, &authnRequest)
if err != nil {
return nil, err
}
// There is a bug with XML namespaces in Go that's causing XML attributes with colons to not be roundtrip
// marshal and unmarshaled so we'll keep the original string around for validation.
authnRequest.originalString = string(bXML)
return &authnRequest, nil
}
func ParseEncodedRequest(b64RequestXML string) (*AuthnRequest, error) {
authnRequest := AuthnRequest{}
bytesXML, err := base64.StdEncoding.DecodeString(b64RequestXML)
if err != nil {
return nil, err
}
err = xml.Unmarshal(bytesXML, &authnRequest)
if err != nil {
return nil, err
}
// There is a bug with XML namespaces in Go that's causing XML attributes with colons to not be roundtrip
// marshal and unmarshaled so we'll keep the original string around for validation.
authnRequest.originalString = string(bytesXML)
return &authnRequest, nil
}
func (r *AuthnRequest) Validate(cert []byte) error {
if r.Version != "2.0" {
return errors.New("unsupported SAML Version")
}
if len(r.ID) == 0 {
return errors.New("missing ID attribute on SAML Response")
}
// TODO more validation
err := VerifyRequestSignature(r.originalString, cert)
if err != nil {
return err
}
return nil
}
// GetSignedAuthnRequest returns a singed XML document that represents a AuthnRequest SAML document
func (s *ServiceProviderConfig) GetAuthnRequest() (*AuthnRequest, error) {
if s.Cert == nil {
return nil, fmt.Errorf("saml:GetAuthnRequest:invalid cert provided")
}
r := NewAuthnRequest()
r.AssertionConsumerServiceURL = s.AssertionConsumerServiceURL
r.Issuer.Url = s.AssertionConsumerServiceURL
r.Destination = s.IDPSSOURL
r.Signature.KeyInfo.X509Data.X509Certificate.Cert = base64.StdEncoding.EncodeToString(s.Cert.Raw)
return r, nil
}
// GetAuthnRequestURL generate a URL for the AuthnRequest to the IdP with the SAMLRequst parameter encoded
func GetAuthnRequestURL(baseURL string, b64XML string, state string) (string, error) {
u, err := url.Parse(baseURL)
if err != nil {
return "", err
}
q := u.Query()
q.Add("SAMLRequest", b64XML)
q.Add("RelayState", state)
u.RawQuery = q.Encode()
return u.String(), nil
}
func NewAuthnRequest() *AuthnRequest {
id := util.ID()
return &AuthnRequest{
XMLName: xml.Name{
Local: "saml2p:AuthnRequest",
},
SAMLP: "urn:oasis:names:tc:SAML:2.0:protocol",
SAML: "urn:oasis:names:tc:SAML:2.0:assertion",
SAMLSIG: "http://www.w3.org/2000/09/xmldsig#",
ID: id,
ProtocolBinding: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST",
Version: "2.0",
ForceAuthn: "false",
AssertionConsumerServiceURL: "", // caller must populate ar.AppSettings.AssertionConsumerServiceURL,
Issuer: Issuer{
XMLName: xml.Name{
Local: "saml2:Issuer",
},
Url: "", // caller must populate ar.AppSettings.Issuer
SAML: "urn:oasis:names:tc:SAML:2.0:assertion",
},
IssueInstant: time.Now().UTC().Format(time.RFC3339Nano),
NameIDPolicy: NameIDPolicy{
XMLName: xml.Name{
Local: "saml2p:NameIDPolicy",
},
AllowCreate: true,
Format: "urn:oasis:names:tc:SAML:2.0:nameid-format:transient",
},
RequestedAuthnContext: RequestedAuthnContext{
XMLName: xml.Name{
Local: "saml2p:RequestedAuthnContext",
},
SAMLP: "urn:oasis:names:tc:SAML:2.0:protocol",
Comparison: "exact",
AuthnContextClassRef: AuthnContextClassRef{
XMLName: xml.Name{
Local: "saml:AuthnContextClassRef",
},
SAML: "urn:oasis:names:tc:SAML:2.0:assertion",
Transport: "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport",
},
},
Signature: Signature{
XMLName: xml.Name{
Local: "samlsig:Signature",
},
Id: "Signature1",
SignedInfo: SignedInfo{
XMLName: xml.Name{
Local: "samlsig:SignedInfo",
},
CanonicalizationMethod: CanonicalizationMethod{
XMLName: xml.Name{
Local: "samlsig:CanonicalizationMethod",
},
Algorithm: "http://www.w3.org/2001/10/xml-exc-c14n#",
},
SignatureMethod: SignatureMethod{
XMLName: xml.Name{
Local: "samlsig:SignatureMethod",
},
Algorithm: "http://www.w3.org/2000/09/xmldsig#rsa-sha1",
},
SamlsigReference: SamlsigReference{
XMLName: xml.Name{
Local: "samlsig:Reference",
},
URI: "#" + id,
Transforms: Transforms{
XMLName: xml.Name{
Local: "samlsig:Transforms",
},
Transform: Transform{
XMLName: xml.Name{
Local: "samlsig:Transform",
},
Algorithm: "http://www.w3.org/2000/09/xmldsig#enveloped-signature",
},
},
DigestMethod: DigestMethod{
XMLName: xml.Name{
Local: "samlsig:DigestMethod",
},
Algorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
},
DigestValue: DigestValue{
XMLName: xml.Name{
Local: "samlsig:DigestValue",
},
},
},
},
SignatureValue: SignatureValue{
XMLName: xml.Name{
Local: "samlsig:SignatureValue",
},
},
KeyInfo: KeyInfo{
XMLName: xml.Name{
Local: "samlsig:KeyInfo",
},
X509Data: X509Data{
XMLName: xml.Name{
Local: "samlsig:X509Data",
},
X509Certificate: X509Certificate{
XMLName: xml.Name{
Local: "samlsig:X509Certificate",
},
Cert: "", // caller must populate cert,
},
},
},
},
}
}
func (r *AuthnRequest) String() (string, error) {
b, err := xml.MarshalIndent(r, "", " ")
if err != nil {
return "", err
}
return string(b), nil
}
func (r *AuthnRequest) SignedString(privateKey crypto.PrivateKey) (string, error) {
s, err := r.String()
if err != nil {
return "", err
}
if privateKey == nil {
return "", fmt.Errorf("saml:SignedString:private key cannot be nil")
}
key, ok := privateKey.(*rsa.PrivateKey)
if !ok {
return "", fmt.Errorf("saml:SignedString:private key type not supported")
}
keyBytes := x509.MarshalPKCS1PrivateKey(key)
return SignRequest(s, keyBytes)
}
// GetAuthnRequestURL generate a URL for the AuthnRequest to the IdP with the SAMLRequst parameter encoded
func (r *AuthnRequest) EncodedSignedString(privateKey crypto.PrivateKey) (string, error) {
signed, err := r.SignedString(privateKey)
if err != nil {
return "", err
}
b64XML := base64.StdEncoding.EncodeToString([]byte(signed))
return b64XML, nil
}
func (r *AuthnRequest) CompressedEncodedSignedString(privateKey crypto.PrivateKey) (string, error) {
signed, err := r.SignedString(privateKey)
if err != nil {
return "", err
}
compressed := util.Compress([]byte(signed))
b64XML := base64.StdEncoding.EncodeToString(compressed)
return b64XML, nil
}
func (r *AuthnRequest) EncodedString() (string, error) {
saml, err := r.String()
if err != nil {
return "", err
}
b64XML := base64.StdEncoding.EncodeToString([]byte(saml))
return b64XML, nil
}
func (r *AuthnRequest) CompressedEncodedString() (string, error) {
saml, err := r.String()
if err != nil {
return "", err
}
compressed := util.Compress([]byte(saml))
b64XML := base64.StdEncoding.EncodeToString(compressed)
return b64XML, nil
}