Skip to content

Commit

Permalink
test(totp): ensure context is cleaned up after use
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Oct 19, 2021
1 parent 8a210c4 commit 1905883
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions selfservice/strategy/totp/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/ory/x/decoderx"
)

const internalContextKeyURL = "url"
const InternalContextKeyURL = "url"

func (s *Strategy) RegisterSettingsRoutes(_ *x.RouterPublic) {
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func (s *Strategy) continueSettingsFlow(
}

func (s *Strategy) continueSettingsFlowAddTOTP(w http.ResponseWriter, r *http.Request, ctxUpdate *settings.UpdateContext, p *submitSelfServiceSettingsFlowWithTotpMethodBody) (*identity.Identity, error) {
keyURL := gjson.GetBytes(ctxUpdate.Flow.InternalContext, flow.PrefixInternalContextKey(s.ID(), internalContextKeyURL)).String()
keyURL := gjson.GetBytes(ctxUpdate.Flow.InternalContext, flow.PrefixInternalContextKey(s.ID(), InternalContextKeyURL)).String()
if len(keyURL) == 0 {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Could not find they TOTP key in the internal context. This is a code bug and should be reported to https://github.com/ory/kratos/."))
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func (s *Strategy) continueSettingsFlowAddTOTP(w http.ResponseWriter, r *http.Re
i.SetCredentials(s.ID(), *c)

// Remove the TOTP URL from the internal context now that it is set!
ctxUpdate.Flow.InternalContext, err = sjson.DeleteBytes(ctxUpdate.Flow.InternalContext, flow.PrefixInternalContextKey(s.ID(), internalContextKeyURL))
ctxUpdate.Flow.InternalContext, err = sjson.DeleteBytes(ctxUpdate.Flow.InternalContext, flow.PrefixInternalContextKey(s.ID(), InternalContextKeyURL))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -254,7 +254,7 @@ func (s *Strategy) PopulateSettingsMethod(r *http.Request, id *identity.Identity
return err
}

f.InternalContext, err = sjson.SetBytes(f.InternalContext, flow.PrefixInternalContextKey(s.ID(), internalContextKeyURL), key.URL())
f.InternalContext, err = sjson.SetBytes(f.InternalContext, flow.PrefixInternalContextKey(s.ID(), InternalContextKeyURL), key.URL())
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions selfservice/strategy/totp/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package totp_test
import (
"context"
"encoding/json"
"github.com/gofrs/uuid"
"github.com/ory/kratos/selfservice/flow"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -328,6 +330,10 @@ func TestCompleteSettings(t *testing.T) {
assert.EqualValues(t, settings.StateSuccess, gjson.Get(actual, "state").String(), actual)
}

actualFlow, err:= reg.SettingsFlowPersister().GetSettingsFlow(context.Background(),uuid.FromStringOrNil(f.Id))
require.NoError(t, err)
assert.Empty(t, gjson.GetBytes(actualFlow.InternalContext,flow.PrefixInternalContextKey(identity.CredentialsTypeTOTP, totp.InternalContextKeyURL)))

checkIdentity(t, id, key)
}

Expand Down

0 comments on commit 1905883

Please sign in to comment.