-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support SAML HTTP redirect binding #1045
Open
easeway
wants to merge
1
commit into
dexidp:master
Choose a base branch
from
easeway:redirect-binding
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+224
−44
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,15 @@ | |
package saml | ||
|
||
import ( | ||
"bytes" | ||
"compress/flate" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"encoding/pem" | ||
"encoding/xml" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
"time" | ||
|
@@ -57,6 +60,10 @@ var ( | |
nameIDformatTransient, | ||
} | ||
nameIDFormatLookup = make(map[string]string) | ||
validBindings = map[string]bool{ | ||
connector.SAMLBindingPOST: true, | ||
connector.SAMLBindingRedirect: true, | ||
} | ||
) | ||
|
||
func init() { | ||
|
@@ -72,6 +79,14 @@ func init() { | |
} | ||
} | ||
|
||
// verifyBinding verifies binding from config | ||
func verifyBinding(binding string) error { | ||
if _, exist := validBindings[binding]; !exist { | ||
return fmt.Errorf("unsupported binding: %s", binding) | ||
} | ||
return nil | ||
} | ||
|
||
// Config represents configuration options for the SAML provider. | ||
type Config struct { | ||
// TODO(ericchiang): A bunch of these fields could be auto-filled if | ||
|
@@ -113,6 +128,10 @@ type Config struct { | |
// urn:oasis:names:tc:SAML:2.0:nameid-format:persistent | ||
// | ||
NameIDPolicyFormat string `json:"nameIDPolicyFormat"` | ||
|
||
// Specify which binding to use, default is post | ||
RequestBinding string `json:"requestBinding"` | ||
ResponseBinding string `json:"responseBinding"` | ||
} | ||
|
||
type certStore struct { | ||
|
@@ -165,6 +184,21 @@ func (c *Config) openConnector(logger logrus.FieldLogger) (*provider, error) { | |
logger: logger, | ||
|
||
nameIDPolicyFormat: c.NameIDPolicyFormat, | ||
|
||
requestBinding: c.RequestBinding, | ||
responseBinding: c.ResponseBinding, | ||
} | ||
|
||
if p.requestBinding == "" { | ||
p.requestBinding = connector.SAMLBindingPOST | ||
} else if err := verifyBinding(p.requestBinding); err != nil { | ||
return nil, err | ||
} | ||
|
||
if p.responseBinding == "" { | ||
p.responseBinding = connector.SAMLBindingPOST | ||
} else if err := verifyBinding(p.responseBinding); err != nil { | ||
return nil, err | ||
} | ||
|
||
if p.nameIDPolicyFormat == "" { | ||
|
@@ -236,11 +270,13 @@ type provider struct { | |
|
||
nameIDPolicyFormat string | ||
|
||
requestBinding string | ||
responseBinding string | ||
|
||
logger logrus.FieldLogger | ||
} | ||
|
||
func (p *provider) POSTData(s connector.Scopes, id string) (action, value string, err error) { | ||
|
||
func (p *provider) AuthnRequest(s connector.Scopes, id string) (binding, ssoURL, value string, err error) { | ||
r := &authnRequest{ | ||
ProtocolBinding: bindingPOST, | ||
ID: id, | ||
|
@@ -252,6 +288,11 @@ func (p *provider) POSTData(s connector.Scopes, id string) (action, value string | |
}, | ||
AssertionConsumerServiceURL: p.redirectURI, | ||
} | ||
|
||
if p.responseBinding == connector.SAMLBindingRedirect { | ||
r.ProtocolBinding = bindingRedirect | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we not set this if it's a POST? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's already set to POST in a few lines above when constructing the authnRequest. |
||
} | ||
|
||
if p.entityIssuer != "" { | ||
// Issuer for the request is optional. For example, okta always ignores | ||
// this value. | ||
|
@@ -260,15 +301,23 @@ func (p *provider) POSTData(s connector.Scopes, id string) (action, value string | |
|
||
data, err := xml.MarshalIndent(r, "", " ") | ||
if err != nil { | ||
return "", "", fmt.Errorf("marshal authn request: %v", err) | ||
return "", "", "", fmt.Errorf("marshal authn request: %v", err) | ||
} | ||
|
||
// for redirect binding, SAMLRequest must be deflated | ||
if p.requestBinding == connector.SAMLBindingRedirect { | ||
data, err = compressRequest(data) | ||
if err != nil { | ||
return "", "", "", fmt.Errorf("deflate request: %v", err) | ||
} | ||
} | ||
|
||
// See: https://docs.oasis-open.org/security/saml/v2.0/saml-bindings-2.0-os.pdf | ||
// "3.5.4 Message Encoding" | ||
return p.ssoURL, base64.StdEncoding.EncodeToString(data), nil | ||
return p.requestBinding, p.ssoURL, base64.StdEncoding.EncodeToString(data), nil | ||
} | ||
|
||
// HandlePOST interprets a request from a SAML provider attempting to verify a | ||
// HandleResponse interprets a request from a SAML provider attempting to verify a | ||
// user's identity. | ||
// | ||
// The steps taken are: | ||
|
@@ -277,12 +326,24 @@ func (p *provider) POSTData(s connector.Scopes, id string) (action, value string | |
// * Verify various parts of the Assertion element. Conditions, audience, etc. | ||
// * Map the Assertion's attribute elements to user info. | ||
// | ||
func (p *provider) HandlePOST(s connector.Scopes, samlResponse, inResponseTo string) (ident connector.Identity, err error) { | ||
func (p *provider) HandleResponse(s connector.Scopes, | ||
binding, samlResponse, inResponseTo string) (ident connector.Identity, err error) { | ||
// enforce responseBinding | ||
if binding != p.responseBinding { | ||
return ident, fmt.Errorf("unexpected response binding %s, expect %s as configured", binding, p.responseBinding) | ||
} | ||
rawResp, err := base64.StdEncoding.DecodeString(samlResponse) | ||
if err != nil { | ||
return ident, fmt.Errorf("decode response: %v", err) | ||
} | ||
|
||
// with HTTP redirect binding, response is compressed | ||
if p.responseBinding == connector.SAMLBindingRedirect { | ||
if rawResp, err = decompressResponse(rawResp); err != nil { | ||
return ident, fmt.Errorf("inflate response: %v", err) | ||
} | ||
} | ||
|
||
// Root element is allowed to not be signed if the Assertion element is. | ||
rootElementSigned := true | ||
if p.validator != nil { | ||
|
@@ -293,7 +354,7 @@ func (p *provider) HandlePOST(s connector.Scopes, samlResponse, inResponseTo str | |
} | ||
|
||
var resp response | ||
if err := xml.Unmarshal(rawResp, &resp); err != nil { | ||
if err = xml.Unmarshal(rawResp, &resp); err != nil { | ||
return ident, fmt.Errorf("unmarshal response: %v", err) | ||
} | ||
|
||
|
@@ -598,3 +659,32 @@ func before(now, notBefore time.Time) bool { | |
func after(now, notOnOrAfter time.Time) bool { | ||
return now.After(notOnOrAfter.Add(allowedClockDrift)) | ||
} | ||
|
||
func compressRequest(data []byte) ([]byte, error) { | ||
var buf bytes.Buffer | ||
zw, err := flate.NewWriter(&buf, flate.DefaultCompression) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer zw.Close() | ||
if _, err := zw.Write(data); err != nil { | ||
return nil, err | ||
} | ||
if err := zw.Flush(); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func decompressResponse(data []byte) ([]byte, error) { | ||
zr := flate.NewReader(bytes.NewReader(data)) | ||
defer zr.Close() | ||
result, err := ioutil.ReadAll(zr) | ||
// the compressed data has no frame, so we don't know the length | ||
// of decompressed data. according to impl of compress/flate, | ||
// treat io.EOF/io.ErrUnexpectedEOF as completion of decompression. | ||
if err == io.EOF || err == io.ErrUnexpectedEOF { | ||
err = nil | ||
} | ||
return result, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,9 @@ type responseTest struct { | |
emailAttr string | ||
groupsAttr string | ||
|
||
// use redirect binding for response | ||
responseUseRedirect bool | ||
|
||
// Expected outcome of the test. | ||
wantErr bool | ||
wantIdent connector.Identity | ||
|
@@ -262,6 +265,27 @@ func TestTwoAssertionFirstSigned(t *testing.T) { | |
test.run(t) | ||
} | ||
|
||
// TestResponseRedirectBinding provides a response for HTTP redirect binding | ||
func TestResponseRedirectBinding(t *testing.T) { | ||
test := responseTest{ | ||
caFile: "testdata/ca.crt", | ||
respFile: "testdata/good-resp.xml", | ||
now: "2017-04-04T04:34:59.330Z", | ||
usernameAttr: "Name", | ||
emailAttr: "email", | ||
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m", | ||
redirectURI: "http://127.0.0.1:5556/dex/callback", | ||
wantIdent: connector.Identity{ | ||
UserID: "[email protected]", | ||
Username: "Eric", | ||
Email: "[email protected]", | ||
EmailVerified: true, | ||
}, | ||
responseUseRedirect: true, | ||
} | ||
test.run(t) | ||
} | ||
|
||
func loadCert(ca string) (*x509.Certificate, error) { | ||
data, err := ioutil.ReadFile(ca) | ||
if err != nil { | ||
|
@@ -283,8 +307,13 @@ func (r responseTest) run(t *testing.T) { | |
RedirectURI: r.redirectURI, | ||
EntityIssuer: r.entityIssuer, | ||
// Never logging in, don't need this. | ||
SSOURL: "http://foo.bar/", | ||
SSOURL: "http://foo.bar/", | ||
ResponseBinding: connector.SAMLBindingPOST, | ||
} | ||
if r.responseUseRedirect { | ||
c.ResponseBinding = connector.SAMLBindingRedirect | ||
} | ||
|
||
now, err := time.Parse(timeFormat, r.now) | ||
if err != nil { | ||
t.Fatalf("parse test time: %v", err) | ||
|
@@ -299,13 +328,18 @@ func (r responseTest) run(t *testing.T) { | |
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if r.responseUseRedirect { | ||
if resp, err = compressRequest(resp); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
samlResp := base64.StdEncoding.EncodeToString(resp) | ||
|
||
scopes := connector.Scopes{ | ||
OfflineAccess: false, | ||
Groups: true, | ||
} | ||
ident, err := conn.HandlePOST(scopes, samlResp, r.inResponseTo) | ||
ident, err := conn.HandleResponse(scopes, c.ResponseBinding, samlResp, r.inResponseTo) | ||
if err != nil { | ||
if !r.wantErr { | ||
t.Fatalf("handle response: %v", err) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this should probably be httpPost and httpRedirect, right? Since the other ones are SOAP based.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean the const name should be "SAMLHTTPPOST" or the value should be "httpPost"? I'm using the same value from config option. I'm OK if we decide to use "http-post", "http-redirect" in values of config option "binding". But I'd like to use the same value internally in code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant the "post" and "redirect" values.
Actually, I think the way it currently is is fine. Let's keep it as is.