Skip to content

Commit

Permalink
chore: cleanup and test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Oct 24, 2023
1 parent 5cb2ea8 commit c540231
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 94 deletions.
2 changes: 1 addition & 1 deletion selfservice/flow/login/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (f *Flow) EnsureInternalContext() {
}
}

func (f *Flow) duplicateCredentials() (*flow.RegistrationDuplicateCredentials, error) {
func (f *Flow) DuplicateCredentials() (*flow.RegistrationDuplicateCredentials, error) {
raw := gjson.GetBytes(f.InternalContext, flow.InternalContextDuplicateCredentialsPath)
if !raw.IsObject() {
return nil, nil
Expand Down
31 changes: 0 additions & 31 deletions selfservice/flow/login/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package login

import (
_ "embed"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -779,36 +778,6 @@ continueLogin:
return
}

/* internalContextDuplicateCredentials := gjson.GetBytes(f.InternalContext, flow.InternalContextDuplicateCredentialsPath)
if internalContextDuplicateCredentials.IsObject() {
var linkCredentials flow.RegistrationDuplicateCredentials
if err := json.Unmarshal([]byte(internalContextDuplicateCredentials.Raw), &linkCredentials); err != nil {
h.d.LoginFlowErrorHandler().WriteFlowError(w, r, f, node.DefaultGroup, err)
return
}
loginFlow, _, err := h.NewLoginFlow(w, r, f.Type, func(newFlow *Flow) {
newFlow.ReturnTo = f.ReturnTo
newFlow.HydraLoginRequest = f.HydraLoginRequest
newFlow.OAuth2LoginChallenge = f.OAuth2LoginChallenge
newFlow.OrganizationID = f.OrganizationID
newFlow.UI.Messages.Add(text.NewInfoSelfServiceLoginLinkCredentials())
newFlow.InternalContext, err = sjson.SetBytes(
newFlow.InternalContext, flow.InternalContextLinkCredentialsPath, linkCredentials)
if err != nil {
h.d.LoginFlowErrorHandler().WriteFlowError(w, r, f, node.DefaultGroup, err)
return
}
})
if err != nil {
h.d.LoginFlowErrorHandler().WriteFlowError(w, r, f, node.DefaultGroup, err)
return
}
http.Redirect(w, r, loginFlow.AppendTo(h.d.Config().SelfServiceFlowLoginUI(r.Context())).String(), http.StatusSeeOther)
return
} */

var i *identity.Identity
var group node.UiNodeGroup
for _, ss := range h.d.AllLoginStrategies() {
Expand Down
2 changes: 1 addition & 1 deletion selfservice/flow/login/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (e *HookExecutor) PreLoginHook(w http.ResponseWriter, r *http.Request, a *F

// maybeLinkCredentials links the identity with the credentials of the inner context of the login flow.
func (e *HookExecutor) maybeLinkCredentials(r *http.Request, s *session.Session, i *identity.Identity, f *Flow) error {
lc, err := f.duplicateCredentials()
lc, err := f.DuplicateCredentials()
if err != nil {
return err
} else if lc == nil {
Expand Down
7 changes: 2 additions & 5 deletions selfservice/flow/login/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ import (
"context"
"net/http"

"github.com/ory/x/sqlxx"

"github.com/gofrs/uuid"

"github.com/ory/kratos/session"

"github.com/pkg/errors"

"github.com/ory/kratos/identity"
"github.com/ory/kratos/session"
"github.com/ory/kratos/ui/node"
"github.com/ory/kratos/x"
"github.com/ory/x/sqlxx"
)

type Strategy interface {
Expand Down
32 changes: 0 additions & 32 deletions selfservice/flow/registration/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/gofrs/uuid"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"

hydraclientgo "github.com/ory/hydra-client-go/v2"
"github.com/ory/kratos/driver/config"
Expand Down Expand Up @@ -262,34 +261,3 @@ func (f *Flow) GetFlowName() flow.FlowName {
func (f *Flow) SetState(state State) {
f.State = state
}

const internalContextOuterLoginFlowPath = "outer_flow"

type OuterLoginFlow struct {
ID string
}

func (f *Flow) GetOuterLoginFlowID() (*uuid.UUID, error) {
la := gjson.GetBytes(f.InternalContext, internalContextOuterLoginFlowPath)
if !la.IsObject() {
return nil, nil
}
var internalContextOuterFlow OuterLoginFlow
if err := json.Unmarshal([]byte(la.Raw), &internalContextOuterFlow); err != nil {
return nil, err
}
id, err := uuid.FromString(internalContextOuterFlow.ID)
if err != nil {
return nil, err
}
return &id, nil
}

func (f *Flow) SetOuterLoginFlowID(flowID uuid.UUID) error {
var err error = nil
f.InternalContext, err = sjson.SetBytes(f.InternalContext, internalContextOuterLoginFlowPath,
OuterLoginFlow{
ID: flowID.String(),
})
return err
}
8 changes: 0 additions & 8 deletions selfservice/flow/registration/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/ory/kratos/schema"
"github.com/ory/kratos/selfservice/errorx"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/selfservice/flow/login"
"github.com/ory/kratos/selfservice/flow/logout"
"github.com/ory/kratos/selfservice/sessiontokenexchange"
"github.com/ory/kratos/session"
Expand All @@ -45,7 +44,6 @@ type (
config.Provider
errorx.ManagementProvider
hydra.Provider
login.HandlerProvider
session.HandlerProvider
session.ManagementProvider
x.WriterProvider
Expand Down Expand Up @@ -115,12 +113,6 @@ func WithFlowOAuth2LoginChallenge(loginChallenge string) FlowOption {
}
}

func WithOuterFlow(flowId uuid.UUID) FlowOption {
return func(f *Flow) {
_ = f.SetOuterLoginFlowID(flowId)
}
}

func (h *Handler) NewRegistrationFlow(w http.ResponseWriter, r *http.Request, ft flow.Type, opts ...FlowOption) (*Flow, error) {
if !h.d.Config().SelfServiceFlowRegistrationEnabled(r.Context()) {
return nil, errors.WithStack(ErrRegistrationDisabled)
Expand Down
7 changes: 0 additions & 7 deletions selfservice/flow/registration/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"github.com/ory/kratos/selfservice/flow/login"
"github.com/ory/kratos/selfservice/sessiontokenexchange"
"github.com/ory/kratos/session"
"github.com/ory/kratos/text"
"github.com/ory/kratos/ui/node"
"github.com/ory/kratos/x"
"github.com/ory/kratos/x/events"
"github.com/ory/x/otelx"
Expand Down Expand Up @@ -182,11 +180,6 @@ func (e *HookExecutor) PostRegistrationHook(w http.ResponseWriter, r *http.Reque
if err != nil {
return err
}
a.UI.SetNode(node.NewInputField(
"method",
node.LoginAndLinkCredentials,
node.DefaultGroup,
node.InputAttributeTypeSubmit).WithMetaLabel(text.NewInfoNodeLoginAndLinkCredential()))
}
}
return err
Expand Down
9 changes: 8 additions & 1 deletion selfservice/strategy/oidc/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,13 @@ func (s *Strategy) handleError(w http.ResponseWriter, r *http.Request, f flow.Fl
return err
}
// return a new login flow with the error message embedded in the login flow.
x.AcceptToRedirectOrJSON(w, r, s.d.Writer(), lf, lf.AppendTo(s.d.Config().SelfServiceFlowLoginUI(r.Context())).String())
redirectURL := lf.AppendTo(s.d.Config().SelfServiceFlowLoginUI(r.Context()))
if err, dc := lf.DuplicateCredentials(); err == nil && dc != nil {
q := redirectURL.Query()
q.Set("skip_org_ui", "true")
redirectURL.RawQuery = q.Encode()
}
x.AcceptToRedirectOrJSON(w, r, s.d.Writer(), lf, redirectURL.String())
// ensure the function does not continue to execute
return registration.ErrHookAbortFlow
}
Expand Down Expand Up @@ -663,5 +669,6 @@ func (s *Strategy) linkCredentials(ctx context.Context, i *identity.Identity, id
}
}
i.Credentials[s.ID()] = *creds

return nil
}
2 changes: 0 additions & 2 deletions selfservice/strategy/oidc/strategy_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ func (s *Strategy) processLogin(w http.ResponseWriter, r *http.Request, loginFlo
opts = append(opts, registration.WithFlowOAuth2LoginChallenge(loginFlow.OAuth2LoginChallenge.String()))
}

opts = append(opts, registration.WithOuterFlow(loginFlow.ID))

registrationFlow, err := s.d.RegistrationHandler().NewRegistrationFlow(w, r, loginFlow.Type, opts...)
if err != nil {
return nil, s.handleError(w, r, loginFlow, provider.Config().ID, nil, err)
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ func TestStrategy(t *testing.T) {
action := assertFormValues(t, r.ID, "valid")
res, body := makeRequest(t, "valid", action, url.Values{})
assertUIError(t, res, body, "An account with the same identifier (email, phone, username, ...) exists already.")
require.Contains(t, gjson.GetBytes(body, "ui.action").String(), "/self-service/registration")
require.Contains(t, gjson.GetBytes(body, "ui.action").String(), "/self-service/login")
})

t.Run("case=should fail login", func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ context("Account Verification Settings Success", () => {
.clear()
.type(email)
cy.get('[value="profile"]').click()
cy.expectSettingsSaved()
cy.get('input[name="traits.email"]').should("contain.value", email)
cy.getSession().then(
assertVerifiableAddress({ isVerified: false, email }),
)

if (app == "express") {
cy.expectSettingsSaved()
cy.get('input[name="traits.email"]').should("contain.value", email)
cy.getSession().then(
assertVerifiableAddress({ isVerified: false, email }),
)
}
cy.verifyEmail({ expect: { email } })
})

Expand Down

0 comments on commit c540231

Please sign in to comment.