Skip to content

Commit

Permalink
simplify, coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Dec 10, 2024
1 parent 3d50fc3 commit fbfced1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 47 deletions.
3 changes: 1 addition & 2 deletions selfservice/flow/registration/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ func (h *Handler) NewRegistrationFlow(w http.ResponseWriter, r *http.Request, ft
} else {
f.OrganizationID = uuid.NullUUID{UUID: orgID, Valid: true}
strategyFilters = []StrategyFilter{func(s Strategy) bool {
return s.ID() == identity.CredentialsTypeOIDC ||
s.ID() == identity.CredentialsTypeSAML
return s.ID() == identity.CredentialsTypeOIDC || s.ID() == identity.CredentialsTypeSAML
}}
}
}
Expand Down
51 changes: 6 additions & 45 deletions selfservice/strategy/oidc/provider_jackson.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ package oidc

import (
"context"
"net/url"
"strings"

gooidc "github.com/coreos/go-oidc/v3/oidc"
"github.com/pkg/errors"
"github.com/coreos/go-oidc/v3/oidc"
"golang.org/x/oauth2"

"github.com/ory/herodot"
"github.com/ory/x/urlx"
)

Expand All @@ -33,13 +31,14 @@ func NewProviderJackson(

func (j *ProviderJackson) setProvider(ctx context.Context) {
if j.ProviderGenericOIDC.p == nil {
config := gooidc.ProviderConfig{
internalHost := strings.TrimSuffix(j.config.TokenURL, "/api/oauth/token")
config := oidc.ProviderConfig{
IssuerURL: j.config.IssuerURL,
AuthURL: j.config.AuthURL,
TokenURL: j.config.TokenURL,
DeviceAuthURL: "",
UserInfoURL: j.config.IssuerURL + "/api/oauth/userinfo",
JWKSURL: j.config.IssuerURL + "/oauth/jwks",
UserInfoURL: internalHost + "/api/oauth/userinfo",
JWKSURL: internalHost + "/oauth/jwks",
Algorithms: []string{"RS256"},
}
j.ProviderGenericOIDC.p = config.NewProvider(j.withHTTPClientContext(ctx))
Expand All @@ -56,41 +55,3 @@ func (j *ProviderJackson) OAuth2(ctx context.Context) (*oauth2.Config, error) {

return config, nil
}

func (j *ProviderJackson) Claims(ctx context.Context, exchange *oauth2.Token, query url.Values) (*Claims, error) {
j.setProvider(ctx)
return j.claimsFromIDToken(ctx, exchange)
}

func (j *ProviderJackson) claimsFromIDToken(ctx context.Context, exchange *oauth2.Token) (*Claims, error) {
p, raw, err := j.idTokenAndProvider(ctx, exchange)
if err != nil {
return nil, err
}

return j.verifyAndDecodeClaimsWithProvider(ctx, p, raw)
}

func (j *ProviderJackson) verifyAndDecodeClaimsWithProvider(ctx context.Context, provider *gooidc.Provider, raw string) (*Claims, error) {
verifier := provider.VerifierContext(j.withHTTPClientContext(ctx), &gooidc.Config{
ClientID: j.config.ClientID,
SkipIssuerCheck: true,
})
token, err := verifier.Verify(ctx, raw)
if err != nil {
return nil, errors.WithStack(herodot.ErrBadRequest.WithReasonf("%s", err))
}

var claims Claims
if err := token.Claims(&claims); err != nil {
return nil, errors.WithStack(herodot.ErrBadRequest.WithReasonf("%s", err))
}

var rawClaims map[string]interface{}
if err := token.Claims(&rawClaims); err != nil {
return nil, errors.WithStack(herodot.ErrBadRequest.WithReasonf("%s", err))
}
claims.RawClaims = rawClaims

return &claims, nil
}
33 changes: 33 additions & 0 deletions selfservice/strategy/oidc/provider_jackson_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package oidc_test

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ory/kratos/internal"
"github.com/ory/kratos/selfservice/strategy/oidc"
)

func TestProviderJackson(t *testing.T) {
_, reg := internal.NewVeryFastRegistryWithoutDB(t)

j := oidc.NewProviderJackson(&oidc.Configuration{
Provider: "jackson",
IssuerURL: "https://www.jackson.com/oauth",
AuthURL: "https://www.jackson.com/oauth/auth",
TokenURL: "https://www.jackson.com/api/oauth/token",
Mapper: "file://./stub/hydra.schema.json",
Scope: []string{"email", "profile"},
ID: "some-id",
}, reg)
assert.NotNil(t, j)

c, err := j.(oidc.OAuth2Provider).OAuth2(context.Background())
require.NoError(t, err)

assert.True(t, strings.HasSuffix(c.RedirectURL, "/self-service/methods/saml/callback/some-id"))
}

0 comments on commit fbfced1

Please sign in to comment.