Skip to content

Commit

Permalink
fix: error when merging oidc credentials to existing user on mobile (…
Browse files Browse the repository at this point in the history
…PS-147)
  • Loading branch information
splaunov committed Nov 24, 2023
1 parent c06f46b commit a2074e2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
26 changes: 26 additions & 0 deletions persistence/sql/persister_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package sql
import (
"context"
"fmt"
"github.com/ory/x/sqlxx"
"time"

"github.com/gobuffalo/pop/v6"
Expand Down Expand Up @@ -83,3 +84,28 @@ func (p *Persister) DeleteExpiredLoginFlows(ctx context.Context, expiresAt time.
}
return nil
}

func (p *Persister) GetInternalContext(ctx context.Context, id uuid.UUID) (sqlxx.JSONRawMessage, error) {
ctx, span := p.r.Tracer(ctx).Tracer().Start(ctx, "persistence.sql.GetLoginFlow")
defer span.End()

conn := p.GetConnection(ctx)

type Flow struct {
InternalContext sqlxx.JSONRawMessage `db:"internal_context" json:"-" faker:"-"`
}

var r Flow
if err := conn.RawQuery(fmt.Sprintf(
"SELECT internal_context FROM %s WHERE id = ? AND nid = ? UNION SELECT internal_context FROM %s WHERE id = ? AND nid = ?",
new(login.Flow).TableName(ctx),
"selfservice_registration_flows",
),
id, p.NetworkID(ctx),
id, p.NetworkID(ctx),
).First(&r); err != nil {
return nil, sqlcon.HandleError(err)
}

return r.InternalContext, nil
}
11 changes: 6 additions & 5 deletions selfservice/flow/login/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gofrs/uuid"
"github.com/ory/kratos/schema"
"github.com/ory/x/decoderx"
"github.com/ory/x/sqlxx"
"github.com/tidwall/gjson"
"net/http"
"time"
Expand Down Expand Up @@ -283,19 +284,19 @@ func (e *HookExecutor) linkCredentials(r *http.Request, s *session.Session, i *i
if innerErr != nil {
return innerErr
}
linkCredentialsFlow, innerErr := e.d.LoginFlowPersister().GetLoginFlow(r.Context(), linkCredentialsFlowID)
internalContext, innerErr := e.d.LoginFlowPersister().GetInternalContext(r.Context(), linkCredentialsFlowID)
if innerErr != nil {
return innerErr
}
innerErr = e.getInternalContextLinkCredentials(linkCredentialsFlow, flow.InternalContextDuplicateCredentialsPath, &lc)
innerErr = e.getInternalContextLinkCredentials(internalContext, flow.InternalContextDuplicateCredentialsPath, &lc)
if innerErr != nil {
return innerErr
}
}
}

if lc.CredentialsType == "" {
err := e.getInternalContextLinkCredentials(f, flow.InternalContextLinkCredentialsPath, &lc)
err := e.getInternalContextLinkCredentials(f.InternalContext, flow.InternalContextLinkCredentialsPath, &lc)
if err != nil {
return err
}
Expand Down Expand Up @@ -326,8 +327,8 @@ func (e *HookExecutor) linkCredentials(r *http.Request, s *session.Session, i *i
return nil
}

func (e *HookExecutor) getInternalContextLinkCredentials(f *Flow, internalContextPath string, lc *flow.RegistrationDuplicateCredentials) error {
internalContextLinkCredentials := gjson.GetBytes(f.InternalContext, internalContextPath)
func (e *HookExecutor) getInternalContextLinkCredentials(internalContext sqlxx.JSONRawMessage, internalContextPath string, lc *flow.RegistrationDuplicateCredentials) error {
internalContextLinkCredentials := gjson.GetBytes(internalContext, internalContextPath)
if internalContextLinkCredentials.IsObject() {
if err := json.Unmarshal([]byte(internalContextLinkCredentials.Raw), lc); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions selfservice/flow/login/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package login

import (
"context"
"github.com/ory/x/sqlxx"
"time"

"github.com/gofrs/uuid"
Expand All @@ -17,6 +18,7 @@ type (
GetLoginFlow(context.Context, uuid.UUID) (*Flow, error)
ForceLoginFlow(ctx context.Context, id uuid.UUID) error
DeleteExpiredLoginFlows(context.Context, time.Time, int) error
GetInternalContext(context.Context, uuid.UUID) (sqlxx.JSONRawMessage, error)
}
FlowPersistenceProvider interface {
LoginFlowPersister() FlowPersister
Expand Down

0 comments on commit a2074e2

Please sign in to comment.