Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
casperbiering authored Nov 5, 2020
2 parents 2a544fa + ce9de80 commit c547c9f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
1 change: 1 addition & 0 deletions pkg/provider/keycloak/example/assertion.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<BODY Onload="document.forms[0].submit()">
<FORM METHOD="POST" ACTION="https://signin.aws.amazon.com/saml">
<INPUT TYPE="BUTTON" VALUE="Click"/>
<INPUT TYPE="HIDDEN" NAME="SAMLResponse" VALUE="abc123"
/>
<NOSCRIPT>
Expand Down
39 changes: 22 additions & 17 deletions pkg/provider/keycloak/keycloak.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,7 @@ func (kc *Client) Authenticate(loginDetails *creds.LoginDetails) (string, error)
}
}

var samlAssertion string

doc.Find("input").Each(func(i int, s *goquery.Selection) {
name, ok := s.Attr("name")
if !ok {
log.Fatalf("unable to locate IDP authentication form submit URL")
}
if name == "SAMLResponse" {
val, ok := s.Attr("value")
if !ok {
log.Fatalf("unable to locate saml assertion value")
}
samlAssertion = val
}
})

return samlAssertion, nil
return extractSamlResponse(doc), nil
}

func (kc *Client) getLoginForm(loginDetails *creds.LoginDetails) (string, url.Values, error) {
Expand Down Expand Up @@ -197,6 +181,27 @@ func extractSubmitURL(doc *goquery.Document) (string, error) {
return submitURL, nil
}

func extractSamlResponse(doc *goquery.Document) string {
var samlAssertion string

doc.Find("input").Each(func(i int, s *goquery.Selection) {
name, ok := s.Attr("name")
if ( ok && name == "SAMLResponse" ) {
val, ok := s.Attr("value")
if !ok {
log.Fatalf("unable to locate saml assertion value")
}
samlAssertion = val
}
})

if samlAssertion == "" {
log.Fatalf("unable to locate saml response field")
}

return samlAssertion
}

func containsTotpForm(doc *goquery.Document) bool {
// search totp field at Keycloak < 8.0.1
totpIndex := doc.Find("input#totp").Index()
Expand Down
10 changes: 10 additions & 0 deletions pkg/provider/keycloak/keycloak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ func TestClient_postTotpFormWithProvidedMFAToken(t *testing.T) {
pr.Mock.AssertNumberOfCalls(t, "RequestSecurityCode", 0)
}

func TestClient_extractSamlResponse(t *testing.T) {
data, err := ioutil.ReadFile("example/assertion.html")
require.Nil(t, err)

doc, err := goquery.NewDocumentFromReader(bytes.NewReader(data))
require.Nil(t, err)

require.Equal(t, extractSamlResponse(doc), "abc123")
}

func TestClient_containsTotpForm(t *testing.T) {
data, err := ioutil.ReadFile("example/mfapage.html")
require.Nil(t, err)
Expand Down

0 comments on commit c547c9f

Please sign in to comment.