Skip to content

Commit

Permalink
feat(saml): file reorganization
Browse files Browse the repository at this point in the history
Signed-off-by: ThibaultHerard <[email protected]>

Co-authored-by: sebferrer <[email protected]>
  • Loading branch information
ThibHrrd and sebferrer committed Oct 17, 2022
1 parent 6cf0778 commit 2366410
Show file tree
Hide file tree
Showing 37 changed files with 96 additions and 480 deletions.
5 changes: 2 additions & 3 deletions driver/registry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ import (
"github.com/ory/kratos/hash"
"github.com/ory/kratos/schema"
"github.com/ory/kratos/selfservice/flow/recovery"
"github.com/ory/kratos/selfservice/flow/saml"
"github.com/ory/kratos/selfservice/flow/settings"
"github.com/ory/kratos/selfservice/flow/verification"
"github.com/ory/kratos/selfservice/hook"
"github.com/ory/kratos/selfservice/strategy/link"
"github.com/ory/kratos/selfservice/strategy/profile"
samlstrategy "github.com/ory/kratos/selfservice/strategy/saml/strategy"
"github.com/ory/kratos/x"

"github.com/cenkalti/backoff"
Expand All @@ -63,6 +61,7 @@ import (
"github.com/ory/kratos/selfservice/flow/logout"
"github.com/ory/kratos/selfservice/flow/registration"
"github.com/ory/kratos/selfservice/strategy/oidc"
"github.com/ory/kratos/selfservice/strategy/saml"

"github.com/ory/herodot"

Expand Down Expand Up @@ -292,7 +291,7 @@ func (m *RegistryDefault) selfServiceStrategies() []interface{} {
m.selfserviceStrategies = []interface{}{
password2.NewStrategy(m),
oidc.NewStrategy(m),
samlstrategy.NewStrategy(m),
saml.NewStrategy(m),
profile.NewStrategy(m),
link.NewStrategy(m),
totp.NewStrategy(m),
Expand Down
2 changes: 1 addition & 1 deletion driver/registry_default_saml.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package driver

import "github.com/ory/kratos/selfservice/flow/saml"
import "github.com/ory/kratos/selfservice/strategy/saml"

func (m *RegistryDefault) SAMLHandler() *saml.Handler {
if m.selfserviceSAMLHandler == nil {
Expand Down
118 changes: 0 additions & 118 deletions selfservice/flow/saml/test/testdata/idp_saml_metadata.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package strategy
package saml

const (
sessionName = "ory_kratos_saml_auth_code_session"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (

samlidp "github.com/crewjam/saml"

samlstrategy "github.com/ory/kratos/selfservice/strategy/saml"

"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
"github.com/ory/x/decoderx"
Expand Down Expand Up @@ -144,7 +142,7 @@ func DestroyMiddlewareIfExists() {

func (h *Handler) instantiateMiddleware(ctx context.Context, config config.Config) error {
// Create a SAMLProvider object from the config file
var c samlstrategy.ConfigurationCollection
var c ConfigurationCollection
conf := config.SelfServiceStrategy(ctx, "saml").Config
if err := jsonx.
NewStrictDecoder(bytes.NewBuffer(conf)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"io/ioutil"
"testing"

samlhandler "github.com/ory/kratos/selfservice/flow/saml"
"github.com/ory/kratos/selfservice/strategy/saml"
"github.com/stretchr/testify/require"

helpertest "github.com/ory/kratos/selfservice/flow/saml/helpertest"
"gotest.tools/assert"
)

Expand All @@ -16,10 +15,10 @@ func TestInitMiddleWareWithMetadata(t *testing.T) {
t.Skip()
}

samlhandler.DestroyMiddlewareIfExists()
saml.DestroyMiddlewareIfExists()

middleWare, _, _, err := helpertest.InitMiddlewareWithMetadata(t,
"file://testdata/idp_saml_metadata.xml")
middleWare, _, _, err := InitTestMiddlewareWithMetadata(t,
"file://testdata/SP_IDPMetadata.xml")

require.NoError(t, err)
assert.Check(t, middleWare != nil)
Expand All @@ -33,9 +32,9 @@ func TestInitMiddleWareWithoutMetadata(t *testing.T) {
t.Skip()
}

samlhandler.DestroyMiddlewareIfExists()
saml.DestroyMiddlewareIfExists()

middleWare, _, _, err := helpertest.InitMiddlewareWithoutMetadata(t,
middleWare, _, _, err := InitTestMiddlewareWithoutMetadata(t,
"https://samltest.id/idp/profile/SAML2/Redirect/SSO",
"https://samltest.id/saml/idp",
"file://testdata/samlkratos.crt",
Expand All @@ -53,12 +52,12 @@ func TestGetMiddleware(t *testing.T) {
t.Skip()
}

samlhandler.DestroyMiddlewareIfExists()
saml.DestroyMiddlewareIfExists()

helpertest.InitMiddlewareWithMetadata(t,
"file://testdata/idp_saml_metadata.xml")
InitTestMiddlewareWithMetadata(t,
"file://testdata/SP_IDPMetadata.xml")

middleWare, err := samlhandler.GetMiddleware()
middleWare, err := saml.GetMiddleware()

require.NoError(t, err)
assert.Check(t, middleWare != nil)
Expand All @@ -72,12 +71,12 @@ func TestMustParseCertificate(t *testing.T) {
t.Skip()
}

samlhandler.DestroyMiddlewareIfExists()
saml.DestroyMiddlewareIfExists()

certificate, err := ioutil.ReadFile("testdata/samlkratos.crt")
require.NoError(t, err)

cert, err := samlhandler.MustParseCertificate(certificate)
cert, err := saml.MustParseCertificate(certificate)

require.NoError(t, err)
assert.Check(t, cert.Issuer.Country[0] == "AU")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import (
"reflect"
"testing"

samlhandler "github.com/ory/kratos/selfservice/flow/saml"
helpertest "github.com/ory/kratos/selfservice/flow/saml/helpertest"

samltesthelpers "github.com/ory/kratos/selfservice/flow/saml/helpertest"

"github.com/ory/kratos/selfservice/strategy/saml"
"github.com/stretchr/testify/require"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
Expand Down Expand Up @@ -71,12 +67,12 @@ func TestXmlMetadataExist(t *testing.T) {
t.Skip()
}

samlhandler.DestroyMiddlewareIfExists()
saml.DestroyMiddlewareIfExists()

_, _, ts, err := helpertest.InitMiddlewareWithMetadata(t,
"file://testdata/idp_saml_metadata.xml")
_, _, ts, err := InitTestMiddlewareWithMetadata(t,
"file://testdata/SP_IDPMetadata.xml")
assert.NilError(t, err)
res, _ := samltesthelpers.NewClient(t, nil).Get(ts.URL + "/self-service/methods/saml/metadata")
res, _ := NewTestClient(t, nil).Get(ts.URL + "/self-service/methods/saml/metadata")

assert.Check(t, is.Equal(http.StatusOK, res.StatusCode))
assert.Check(t, is.Equal("application/samlmetadata+xml",
Expand All @@ -88,11 +84,11 @@ func TestXmlMetadataValues(t *testing.T) {
t.Skip()
}

samlhandler.DestroyMiddlewareIfExists()
saml.DestroyMiddlewareIfExists()

_, _, ts, err := helpertest.InitMiddlewareWithMetadata(t,
"file://testdata/idp_saml_metadata.xml")
res, _ := samltesthelpers.NewClient(t, nil).Get(ts.URL + "/self-service/methods/saml/metadata")
_, _, ts, err := InitTestMiddlewareWithMetadata(t,
"file://testdata/SP_IDPMetadata.xml")
res, _ := NewTestClient(t, nil).Get(ts.URL + "/self-service/methods/saml/metadata")
body, _ := io.ReadAll(res.Body)

assert.Check(t, is.Equal(http.StatusOK, res.StatusCode))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package strategy
package saml

import (
_ "embed"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package strategy
package saml

import (
"bytes"
Expand Down Expand Up @@ -35,10 +35,8 @@ import (
"github.com/ory/kratos/selfservice/flow/login"

"github.com/ory/kratos/selfservice/flow/registration"
samlflow "github.com/ory/kratos/selfservice/flow/saml"
"github.com/ory/kratos/selfservice/flow/settings"
"github.com/ory/kratos/selfservice/strategy"
samlstrategy "github.com/ory/kratos/selfservice/strategy/saml"
"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
)
Expand Down Expand Up @@ -186,7 +184,7 @@ func (s *Strategy) validateFlow(ctx context.Context, r *http.Request, rid uuid.U

if ar, err := s.d.RegistrationFlowPersister().GetRegistrationFlow(ctx, rid); err == nil {
if ar.Type != flow.TypeBrowser {
return ar, samlstrategy.ErrAPIFlowNotSupported
return ar, ErrAPIFlowNotSupported
}

if err := ar.Valid(); err != nil {
Expand All @@ -197,7 +195,7 @@ func (s *Strategy) validateFlow(ctx context.Context, r *http.Request, rid uuid.U

if ar, err := s.d.LoginFlowPersister().GetLoginFlow(ctx, rid); err == nil {
if ar.Type != flow.TypeBrowser {
return ar, samlstrategy.ErrAPIFlowNotSupported
return ar, ErrAPIFlowNotSupported
}

if err := ar.Valid(); err != nil {
Expand All @@ -209,7 +207,7 @@ func (s *Strategy) validateFlow(ctx context.Context, r *http.Request, rid uuid.U
ar, err := s.d.SettingsFlowPersister().GetSettingsFlow(ctx, rid)
if err == nil {
if ar.Type != flow.TypeBrowser {
return ar, samlstrategy.ErrAPIFlowNotSupported
return ar, ErrAPIFlowNotSupported
}

sess, err := s.d.SessionManager().FetchFromRequest(ctx, r)
Expand Down Expand Up @@ -279,7 +277,7 @@ func (s *Strategy) handleCallback(w http.ResponseWriter, r *http.Request, ps htt
return
}

m, err := samlflow.GetMiddleware()
m, err := GetMiddleware()
if err != nil {
s.forwardError(w, r, err)
}
Expand Down Expand Up @@ -330,7 +328,7 @@ func (s *Strategy) forwardError(w http.ResponseWriter, r *http.Request, err erro
}

// Return the SAML Provider
func (s *Strategy) Provider(ctx context.Context) (samlstrategy.Provider, error) {
func (s *Strategy) Provider(ctx context.Context) (Provider, error) {
c, err := s.Config(ctx)
if err != nil {
return nil, err
Expand All @@ -345,8 +343,8 @@ func (s *Strategy) Provider(ctx context.Context) (samlstrategy.Provider, error)
}

// Translate YAML Config file into a SAML Provider struct
func (s *Strategy) Config(ctx context.Context) (*samlstrategy.ConfigurationCollection, error) {
var c samlstrategy.ConfigurationCollection
func (s *Strategy) Config(ctx context.Context) (*ConfigurationCollection, error) {
var c ConfigurationCollection

conf := s.d.Config().SelfServiceStrategy(ctx, string(s.ID())).Config
if err := jsonx.
Expand Down
13 changes: 0 additions & 13 deletions selfservice/strategy/saml/strategy/test/testdata/cert.pem

This file was deleted.

Loading

0 comments on commit 2366410

Please sign in to comment.