-
Notifications
You must be signed in to change notification settings - Fork 3
/
binding.go
141 lines (127 loc) · 3.94 KB
/
binding.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
// Copyright 2023 Cisco Systems, Inc. and its affiliates
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
package samltest
import (
"bytes"
"compress/flate"
"encoding/base64"
"encoding/xml"
"fmt"
"github.com/beevik/etree"
samlutils "github.com/cisco-open/go-lanai/pkg/security/saml/utils"
"github.com/cisco-open/go-lanai/test/webtest"
"github.com/crewjam/saml"
"io"
"net/http"
"net/url"
"strings"
)
type BindableSamlTypes interface {
saml.LogoutRequest | saml.LogoutResponse | saml.AuthnRequest | saml.Response
}
// xmlSerializer is a common interface of saml.LogoutRequest, saml.LogoutResponse, saml.AuthnRequest, saml.Response
type xmlSerializer interface {
Element() *etree.Element
}
// RequestWithSAMLPostBinding returns a webtest.RequestOptions that inject given SAML Request/Response using Post binding.
// Note: request need to be POST
func RequestWithSAMLPostBinding[T BindableSamlTypes](samlObj *T, relayState string) webtest.RequestOptions {
return func(req *http.Request) {
var i interface{} = samlObj
serializer, ok := i.(xmlSerializer)
if !ok {
panic(fmt.Sprintf("%T doess not have Element() *etree.Element", i))
}
doc := etree.NewDocument()
doc.SetRoot(serializer.Element())
decoded, e := doc.WriteToBytes()
if e != nil {
panic(e)
}
encoded := base64.StdEncoding.EncodeToString(decoded)
values := url.Values{}
values.Set("SAMLRequest", encoded)
values.Add("RelayState", relayState)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Body = io.NopCloser(strings.NewReader(values.Encode()))
}
}
type BindingParseResult struct {
Binding string
Values url.Values
Encoded string
Decoded []byte
}
// ParseBinding parse redirect/post binding from given HTTP response
func ParseBinding[T samlutils.ParsableSamlTypes](resp *http.Response, dest *T) (ret BindingParseResult, err error) {
param := samlutils.HttpParamSAMLResponse
var i interface{} = dest
switch i.(type) {
case *saml.LogoutRequest, *saml.AuthnRequest:
param = samlutils.HttpParamSAMLRequest
}
switch {
case resp.StatusCode < 300:
ret.Binding = saml.HTTPPostBinding
ret.Values, err = extractPostBindingValues(resp)
default:
ret.Binding = saml.HTTPRedirectBinding
ret.Values, err = extractRedirectBindingValues(resp)
}
if err != nil {
return
}
ret.Encoded = ret.Values.Get(param)
if len(ret.Encoded) == 0 {
return ret, fmt.Errorf("unable to find %s in http response", param)
}
ret.Decoded, err = base64.StdEncoding.DecodeString(ret.Encoded)
if err != nil {
return
}
// try de-compress
r := flate.NewReader(bytes.NewReader(ret.Decoded))
if data, e := io.ReadAll(r); e == nil {
ret.Decoded = data
}
err = xml.Unmarshal(ret.Decoded, dest)
return
}
func extractRedirectBindingValues(resp *http.Response) (url.Values, error) {
loc := resp.Header.Get("Location")
locUri, e := url.Parse(loc)
if e != nil {
return nil, e
}
return locUri.Query(), nil
}
func extractPostBindingValues(resp *http.Response) (url.Values, error) {
htmlDoc := etree.NewDocument()
if _, e := htmlDoc.ReadFrom(resp.Body); e != nil {
return nil, e
}
values := url.Values{}
elems := htmlDoc.FindElements("//input")
for _, el := range elems {
if typ := el.SelectAttrValue("type", ""); typ == "submit" {
continue
}
name := el.SelectAttrValue("name", "unknown")
value := el.SelectAttrValue("value", "")
values.Add(name, value)
}
return values, nil
}