-
Notifications
You must be signed in to change notification settings - Fork 0
/
saml_handler.go
50 lines (46 loc) · 1.59 KB
/
saml_handler.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
package kitwalk
import (
"io"
"net/url"
"github.com/PuerkitoBio/goquery"
)
func isContinueRequired(body io.Reader) bool {
doc, err := goquery.NewDocumentFromReader(body)
if err != nil {
panic("Cannot parse response")
}
unameInput := doc.Find("input[id='username']").First()
passwdInput := doc.Find("input[id='password']").First()
if unameInput.Length() == 0 && passwdInput.Length() == 0 {
return true
}
return false
}
func parseSamlResp(body io.Reader) (string, url.Values, error) {
doc, err := goquery.NewDocumentFromReader(body)
if err != nil {
panic("Cannot parse response")
}
// When invalid auth info is posted
errorForm := doc.Find("p[class=\"form-error\"]").First()
if errorForm.Length() != 0 {
return "", nil, &ShibbolethAuthError{errMsg: errorForm.Text()}
}
// Parse SAML response form.
// When you use a normal browser such as Chrome or FireFox, this form will be submitted automatically.
form := doc.Find("form")
actionURL, actionURLExists := form.Attr("action")
if !actionURLExists {
return "", nil, &ShibbolethAuthError{errMsg: "Could not find action url"}
}
relayState, rStateExists := form.Find("input[name=\"" + DefaultRelayStateKey + "\"]").First().Attr("value")
samlResponse, sRespExists := form.Find("input[name=\"" + DefaultSAMLResponseKey + "\"]").First().Attr("value")
if !rStateExists || !sRespExists {
return "", nil, &ShibbolethAuthError{errMsg: "Could not parse response"}
}
// Create post data
authData := url.Values{}
authData.Add(DefaultRelayStateKey, relayState)
authData.Add(DefaultSAMLResponseKey, samlResponse)
return actionURL, authData, nil
}