Skip to content
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

Add an AssertionHandler to the middleware #509

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ jobs:
name: Run golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: v1.46.2
17 changes: 17 additions & 0 deletions samlsp/basic_assertion_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package samlsp

import (
"github.com/crewjam/saml"
)

var _ SamlAssertionHandler = BasicSamlAssertionHandler{}

// BasicSamlAssertionHandler is an implementation of SamlAssertionHandler that has
// an empty HandleAssertion function to retain useability.
type BasicSamlAssertionHandler struct{}

// HandleAssertion is called and passed saml assertion
// this can add extra functionality and should return any error that occurs.
func (as BasicSamlAssertionHandler) HandleAssertion(assertion *saml.Assertion) error {
return nil
}
19 changes: 13 additions & 6 deletions samlsp/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ import (
// SAML service provider already has a private key, we borrow that key
// to sign the JWTs as well.
type Middleware struct {
ServiceProvider saml.ServiceProvider
OnError func(w http.ResponseWriter, r *http.Request, err error)
Binding string // either saml.HTTPPostBinding or saml.HTTPRedirectBinding
ResponseBinding string // either saml.HTTPPostBinding or saml.HTTPArtifactBinding
RequestTracker RequestTracker
Session SessionProvider
ServiceProvider saml.ServiceProvider
OnError func(w http.ResponseWriter, r *http.Request, err error)
Binding string // either saml.HTTPPostBinding or saml.HTTPRedirectBinding
ResponseBinding string // either saml.HTTPPostBinding or saml.HTTPArtifactBinding
RequestTracker RequestTracker
Session SessionProvider
AssertionHandler SamlAssertionHandler
}

// ServeHTTP implements http.Handler and serves the SAML-specific HTTP endpoints
Expand Down Expand Up @@ -92,6 +93,12 @@ func (m *Middleware) ServeACS(w http.ResponseWriter, r *http.Request) {
return
}

assertionErr := m.AssertionHandler.HandleAssertion(assertion)
if assertionErr != nil {
m.OnError(w, r, assertionErr)
return
}

m.CreateSessionFromAssertion(w, r, assertion, m.ServiceProvider.DefaultRedirectURI)
return
}
Expand Down
17 changes: 12 additions & 5 deletions samlsp/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ func DefaultServiceProvider(opts Options) saml.ServiceProvider {
}
}

// DefaultSamlAssertionHandler returns the default SamlAssertionHandler for the provided options,
// a BasicSamlAssertionHandler configured to do nothing.
func DefaultSamlAssertionHandler(opts Options) BasicSamlAssertionHandler {
return BasicSamlAssertionHandler{}
}

// New creates a new Middleware with the default providers for the
// given options.
//
Expand All @@ -139,11 +145,12 @@ func DefaultServiceProvider(opts Options) saml.ServiceProvider {
// in the returned Middleware.
func New(opts Options) (*Middleware, error) {
m := &Middleware{
ServiceProvider: DefaultServiceProvider(opts),
Binding: "",
ResponseBinding: saml.HTTPPostBinding,
OnError: DefaultOnError,
Session: DefaultSessionProvider(opts),
ServiceProvider: DefaultServiceProvider(opts),
Binding: "",
ResponseBinding: saml.HTTPPostBinding,
OnError: DefaultOnError,
Session: DefaultSessionProvider(opts),
AssertionHandler: DefaultSamlAssertionHandler(opts),
}
m.RequestTracker = DefaultRequestTracker(opts, &m.ServiceProvider)
if opts.UseArtifactResponse {
Expand Down
9 changes: 9 additions & 0 deletions samlsp/saml_assertion_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package samlsp

import "github.com/crewjam/saml"

// SamlAssertionHandler is an interface implemented by types that can handle
// assertions and add extra functionality
type SamlAssertionHandler interface {
HandleAssertion(assertion *saml.Assertion) error
}