Skip to content

Commit

Permalink
fix: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Oct 23, 2023
1 parent 76fa5b0 commit 5cb2ea8
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 154 deletions.
2 changes: 1 addition & 1 deletion cmd/clidoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func init() {
"NewErrorValidationRecoveryStateFailure": text.NewErrorValidationRecoveryStateFailure(),
"NewInfoNodeInputEmail": text.NewInfoNodeInputEmail(),
"NewInfoNodeResendOTP": text.NewInfoNodeResendOTP(),
"NewInfoNodeLoginAndLinkCredential": text.NewInfoNodeLoginAndLinkCredential(),
"NewInfoNodeLabelContinue": text.NewInfoNodeLabelContinue(),
"NewInfoSelfServiceSettingsRegisterWebAuthn": text.NewInfoSelfServiceSettingsRegisterWebAuthn(),
"NewInfoLoginWebAuthnPasswordless": text.NewInfoLoginWebAuthnPasswordless(),
Expand All @@ -163,7 +164,6 @@ func init() {
"NewInfoSelfServiceLoginCode": text.NewInfoSelfServiceLoginCode(),
"NewErrorValidationRegistrationRetrySuccessful": text.NewErrorValidationRegistrationRetrySuccessful(),
"NewInfoSelfServiceRegistrationRegisterCode": text.NewInfoSelfServiceRegistrationRegisterCode(),
"NewInfoSelfServiceLoginLinkCredentials": text.NewInfoSelfServiceLoginLinkCredentials(),
"NewErrorValidationLoginLinkedCredentialsDoNotMatch": text.NewErrorValidationLoginLinkedCredentialsDoNotMatch(),
}
}
Expand Down
1 change: 0 additions & 1 deletion selfservice/flow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
)

const InternalContextDuplicateCredentialsPath = "registration_duplicate_credentials"
const InternalContextLinkCredentialsPath = "link_credentials"

type RegistrationDuplicateCredentials struct {
CredentialsType identity.CredentialsType
Expand Down
2 changes: 1 addition & 1 deletion selfservice/flow/login/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (f *Flow) EnsureInternalContext() {
}

func (f *Flow) duplicateCredentials() (*flow.RegistrationDuplicateCredentials, error) {
raw := gjson.GetBytes(f.InternalContext, flow.InternalContextLinkCredentialsPath)
raw := gjson.GetBytes(f.InternalContext, flow.InternalContextDuplicateCredentialsPath)
if !raw.IsObject() {
return nil, nil
}
Expand Down
55 changes: 29 additions & 26 deletions selfservice/flow/login/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ package login

import (
_ "embed"
"encoding/json"
"net/http"
"net/url"
"time"

"github.com/gofrs/uuid"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"

"github.com/ory/herodot"
hydraclientgo "github.com/ory/hydra-client-go/v2"
Expand Down Expand Up @@ -104,6 +101,12 @@ func WithFlowReturnTo(returnTo string) FlowOption {
}
}

func WithInternalContext(internalContext []byte) FlowOption {
return func(f *Flow) {
f.InternalContext = internalContext
}
}

func WithFormErrorMessage(messages []text.Message) FlowOption {
return func(f *Flow) {
for i := range messages {
Expand Down Expand Up @@ -776,35 +779,35 @@ 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
}
/* 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)
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
}
})
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
}
http.Redirect(w, r, loginFlow.AppendTo(h.d.Config().SelfServiceFlowLoginUI(r.Context())).String(), http.StatusSeeOther)
return
} */

var i *identity.Identity
var group node.UiNodeGroup
Expand Down
34 changes: 0 additions & 34 deletions selfservice/flow/registration/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
package registration

import (
"encoding/json"
"net/http"
"net/url"
"time"

"github.com/gofrs/uuid"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"

"github.com/ory/herodot"
"github.com/ory/kratos/driver/config"
Expand Down Expand Up @@ -602,37 +599,6 @@ func (h *Handler) updateRegistrationFlow(w http.ResponseWriter, r *http.Request,
return
}

internalContextDuplicateCredentials := gjson.GetBytes(f.InternalContext, flow.InternalContextDuplicateCredentialsPath)
if internalContextDuplicateCredentials.IsObject() {
// If return_to was set before, we need to preserve it.
var opts []login.FlowOption
if len(f.ReturnTo) > 0 {
opts = append(opts, login.WithFlowReturnTo(f.ReturnTo))
}
opts = append(opts, func(newFlow *login.Flow) {
newFlow.UI.Messages.Add(text.NewInfoSelfServiceLoginLinkCredentials())
var linkCredentials flow.RegistrationDuplicateCredentials
if err := json.Unmarshal([]byte(internalContextDuplicateCredentials.Raw), &linkCredentials); err != nil {
h.d.RegistrationFlowErrorHandler().WriteFlowError(w, r, f, node.DefaultGroup, err)
return
}
newFlow.InternalContext, err = sjson.SetBytes(newFlow.InternalContext, flow.InternalContextLinkCredentialsPath,
linkCredentials)
if err != nil {
h.d.RegistrationFlowErrorHandler().WriteFlowError(w, r, f, node.DefaultGroup, err)
return
}
})
loginFlow, _, err := h.d.LoginHandler().NewLoginFlow(w, r, flow.TypeBrowser, opts...)
if err != nil {
h.d.RegistrationFlowErrorHandler().WriteFlowError(w, r, f, node.DefaultGroup, err)
return
}

http.Redirect(w, r, loginFlow.AppendTo(h.d.Config().SelfServiceFlowLoginUI(r.Context())).String(), http.StatusSeeOther)
return
}

i := identity.NewIdentity(h.d.Config().DefaultIdentityTraitsSchemaID(r.Context()))
var s Strategy
for _, ss := range h.d.AllRegistrationStrategies() {
Expand Down
29 changes: 2 additions & 27 deletions selfservice/flow/registration/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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"
Expand Down Expand Up @@ -175,29 +176,6 @@ func (e *HookExecutor) PostRegistrationHook(w http.ResponseWriter, r *http.Reque
CredentialsConfig: i.Credentials[ct].Config,
DuplicateIdentifier: duplicateIdentifier,
}
loginFlowID, err := a.GetOuterLoginFlowID()
if err != nil {
return err
}
if loginFlowID != nil {
loginFlow, err := e.d.LoginFlowPersister().GetLoginFlow(r.Context(), *loginFlowID)
if err != nil {
return err
}
loginFlow.InternalContext, err = sjson.SetBytes(loginFlow.InternalContext, flow.InternalContextDuplicateCredentialsPath,
registrationDuplicateCredentials)
if err != nil {
return err
}
loginFlow.UI.SetNode(node.NewInputField(
"method",
node.LoginAndLinkCredentials,
node.DefaultGroup,
node.InputAttributeTypeSubmit))
if err := e.d.LoginFlowPersister().UpdateLoginFlow(r.Context(), loginFlow); err != nil {
return err
}
}

a.InternalContext, err = sjson.SetBytes(a.InternalContext, flow.InternalContextDuplicateCredentialsPath,
registrationDuplicateCredentials)
Expand All @@ -208,10 +186,7 @@ func (e *HookExecutor) PostRegistrationHook(w http.ResponseWriter, r *http.Reque
"method",
node.LoginAndLinkCredentials,
node.DefaultGroup,
node.InputAttributeTypeSubmit))
if err := e.d.RegistrationFlowPersister().UpdateRegistrationFlow(r.Context(), a); err != nil {
return err
}
node.InputAttributeTypeSubmit).WithMetaLabel(text.NewInfoNodeLoginAndLinkCredential()))
}
}
return err
Expand Down
18 changes: 10 additions & 8 deletions selfservice/strategy/oidc/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,17 +543,19 @@ func (s *Strategy) handleError(w http.ResponseWriter, r *http.Request, f flow.Fl
// Reset all nodes to not confuse users.
// This is kinda hacky and will probably need to be updated at some point.

var loginAndLinkCredentialsNode *node.Node
for _, n := range rf.UI.Nodes {
if n.ID() == "method" && n.GetValue() == node.LoginAndLinkCredentials {
loginAndLinkCredentialsNode = n
break
if dup := new(identity.ErrDuplicateCredentials); errors.As(err, &dup) {
rf.UI.Messages.Add(text.NewErrorValidationDuplicateCredentialsOnOIDCLink())
lf, err := s.registrationToLogin(w, r, rf, provider)
if err != nil {
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())
// ensure the function does not continue to execute
return registration.ErrHookAbortFlow
}

rf.UI.Nodes = node.Nodes{}
if loginAndLinkCredentialsNode != nil {
rf.UI.Nodes.Upsert(loginAndLinkCredentialsNode)
}

// Adds the "Continue" button
rf.UI.SetCSRF(s.d.GenerateCSRFToken(r))
Expand Down
4 changes: 4 additions & 0 deletions selfservice/strategy/oidc/strategy_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ func (s *Strategy) registrationToLogin(w http.ResponseWriter, r *http.Request, r
opts = append(opts, login.WithFormErrorMessage(rf.UI.Messages))
}

if len(rf.InternalContext) > 0 {
opts = append(opts, login.WithInternalContext(rf.InternalContext))
}

lf, _, err := s.d.LoginHandler().NewLoginFlow(w, r, rf.Type, opts...)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 5cb2ea8

Please sign in to comment.