Skip to content

Commit

Permalink
fix: better const handling for internal context
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Oct 19, 2021
1 parent dac4f75 commit 1e457e3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 9 deletions.
22 changes: 22 additions & 0 deletions schema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ func NewTOTPVerifierWrongError(instancePtr string) error {
})
}

func NewLookupAlreadyUsed() error {
t := text.NewErrorValidationLookupAlreadyUsed()
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: t.Text,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(t),
})
}

func NewErrorValidationLookupInvalid() error {
t := text.NewErrorValidationLookupInvalid()
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: t.Text,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(t),
})
}

type ValidationErrorContextPasswordPolicyViolation struct {
Reason string
}
Expand Down
5 changes: 3 additions & 2 deletions selfservice/flow/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

var (
ErrStrategyNotResponsible = errors.New("strategy is not responsible for this request")
ErrCompletedByStrategy = errors.New("flow response completed by strategy")
ErrStrategyNotResponsible = errors.New("strategy is not responsible for this request")
ErrCompletedByStrategy = errors.New("flow response completed by strategy")
ErrStrategyAsksToReturnToUI = errors.New("flow strategy is redirecting to the ui")
)
6 changes: 5 additions & 1 deletion selfservice/flow/internal_context.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package flow

const InternalContextKeyTOTPURL = "totp_url"
import "github.com/ory/kratos/identity"

func PrefixInternalContextKey(t identity.CredentialsType, suffix string) string {
return string(t) + "_" + suffix
}
18 changes: 15 additions & 3 deletions selfservice/strategy/totp/.schema/settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@
"type": "string"
},
"totp_code": {
"type": "string",
"maxLength": 6,
"minLength": 6
"type": "string"
},
"totp_unlink": {
"type": "boolean"
}
},
"if": {
"properties": {
"method": {
"const": "totp"
}
}
},
"then": {
"totp_code": {
"type": "string",
"maxLength": 6,
"minLength": 6
}
}
}
2 changes: 1 addition & 1 deletion selfservice/strategy/totp/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *Strategy) PopulateLoginMethod(r *http.Request, requestedAAL identity.Au

sr.UI.SetCSRF(s.d.GenerateCSRFToken(r))
sr.UI.SetNode(node.NewInputField("totp_code", "", node.TOTPGroup, node.InputAttributeTypeText, node.WithRequiredInputAttribute).WithMetaLabel(text.NewInfoLoginTOTPLabel()))
sr.UI.GetNodes().Append(node.NewInputField("method", "totp", node.TOTPGroup, node.InputAttributeTypeSubmit).WithMetaLabel(text.NewInfoLogin()))
sr.UI.GetNodes().Append(node.NewInputField("method", s.ID(), node.TOTPGroup, node.InputAttributeTypeSubmit).WithMetaLabel(text.NewInfoLogin()))

return nil
}
Expand Down
17 changes: 15 additions & 2 deletions selfservice/strategy/totp/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"github.com/ory/x/decoderx"
)

const internalContextKeyURL = "url"

func (s *Strategy) RegisterSettingsRoutes(_ *x.RouterPublic) {
}

Expand Down Expand Up @@ -156,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.InternalContextKeyTOTPURL).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 @@ -192,6 +194,17 @@ func (s *Strategy) continueSettingsFlowAddTOTP(w http.ResponseWriter, r *http.Re
c := &identity.Credentials{Type: s.ID(), Identifiers: []string{i.ID.String()}, Config: co}
c.Config = co
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))
if err != nil {
return nil, err
}

if err := s.d.SettingsFlowPersister().UpdateSettingsFlow(r.Context(), ctxUpdate.Flow); err != nil {
return nil, err
}

return i, nil
}

Expand Down Expand Up @@ -241,7 +254,7 @@ func (s *Strategy) PopulateSettingsMethod(r *http.Request, id *identity.Identity
return err
}

f.InternalContext, err = sjson.SetBytes(f.InternalContext, flow.InternalContextKeyTOTPURL, key.URL())
f.InternalContext, err = sjson.SetBytes(f.InternalContext, flow.PrefixInternalContextKey(s.ID(), internalContextKeyURL), key.URL())
if err != nil {
return err
}
Expand Down

0 comments on commit 1e457e3

Please sign in to comment.