Skip to content

Commit

Permalink
test: add credentials test
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Oct 19, 2021
1 parent 97e59e6 commit 58b388c
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 21 deletions.
16 changes: 11 additions & 5 deletions selfservice/strategy/lookup/credentials.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lookup

import (
"github.com/ory/kratos/text"
"github.com/ory/kratos/ui/node"
"time"

"github.com/ory/x/sqlxx"
Expand All @@ -12,16 +14,20 @@ type CredentialsConfig struct {
RecoveryCodes []RecoveryCode `json:"recovery_codes"`
}

func (c *CredentialsConfig) ToReadableList() []string {
codes := make([]string, len(c.RecoveryCodes))
func (c *CredentialsConfig) ToNode() *node.Node {
messages := make([]text.Message, len(c.RecoveryCodes))
formatted := make([]string, len(c.RecoveryCodes))
for k, code := range c.RecoveryCodes {
if time.Time(code.UsedAt).IsZero() {
codes[k] = code.Code
messages[k] = *text.NewInfoSelfServiceSettingsLookupSecret(code.Code)
formatted[k] = code.Code
} else {
codes[k] = "already used"
messages[k] = *text.NewInfoSelfServiceSettingsLookupSecretUsed(time.Time(code.UsedAt))
formatted[k] = "used"
}
}
return codes

return node.NewTextField(node.LookupCodes, text.NewInfoSelfServiceSettingsLookupSecretList(formatted, messages), node.LookupGroup).WithMetaLabel(text.NewInfoSelfServiceSettingsLookupSecretsLabel())
}

type RecoveryCode struct {
Expand Down
24 changes: 24 additions & 0 deletions selfservice/strategy/lookup/credentials_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lookup

import (
_ "embed"
"encoding/json"
"github.com/ory/x/assertx"
"github.com/ory/x/sqlxx"
"testing"
"time"
)

//go:embed fixtures/node.json
var fixtureNode []byte

func TestToNode(t *testing.T) {
c := CredentialsConfig{RecoveryCodes: []RecoveryCode{
{Code: "foo", UsedAt: sqlxx.NullTime(time.Unix(1629199958, 0))},
{Code: "bar"},
{Code: "baz"},
{Code: "oof", UsedAt: sqlxx.NullTime(time.Unix(1629199968, 0))},
}}

assertx.EqualAsJSON(t, json.RawMessage(fixtureNode), c.ToNode())
}
56 changes: 56 additions & 0 deletions selfservice/strategy/lookup/fixtures/node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"type": "text",
"group": "lookup_secret",
"attributes": {
"text": {
"id": 1050015,
"text": "used, bar, baz, used",
"type": "info",
"context": {
"secrets": [
{
"id": 1050014,
"text": "Secret was used at 2021-08-17 13:32:38 +0200 CEST",
"type": "info",
"context": {
"used_at": "2021-08-17T13:32:38+02:00"
}
},
{
"id": 1050009,
"text": "bar",
"type": "info",
"context": {
"secret": "bar"
}
},
{
"id": 1050009,
"text": "baz",
"type": "info",
"context": {
"secret": "baz"
}
},
{
"id": 1050014,
"text": "Secret was used at 2021-08-17 13:32:48 +0200 CEST",
"type": "info",
"context": {
"used_at": "2021-08-17T13:32:48+02:00"
}
}
]
}
},
"id": "lookup_secret_codes"
},
"messages": [],
"meta": {
"label": {
"id": 1050010,
"text": "These are your back up recovery codes. Please keep them in a safe place!",
"type": "info"
}
}
}
4 changes: 0 additions & 4 deletions selfservice/strategy/lookup/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,3 @@ func NewConfirmLookupNode() *node.Node {
node.InputAttributeTypeSubmit).
WithMetaLabel(text.NewInfoSelfServiceSettingsLookupConfirm())
}

func NewLookupNode(secrets []string) *node.Node {
return node.NewTextField(node.LookupCodes, text.NewInfoSelfServiceSettingsLookupSecrets(secrets), node.LookupGroup).WithMetaLabel(text.NewInfoSelfServiceSettingsLookupSecretsLabel())
}
8 changes: 4 additions & 4 deletions selfservice/strategy/lookup/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (s *Strategy) continueSettingsFlowReveal(w http.ResponseWriter, r *http.Req
return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Unable to decode lookup codes from JSON.").WithDebug(err.Error()))
}

ctxUpdate.Flow.UI.Nodes.Upsert(NewLookupNode(creds.ToReadableList()))
ctxUpdate.Flow.UI.Nodes.Upsert(creds.ToNode())
ctxUpdate.Flow.UI.Nodes.Remove(node.LookupReveal)
ctxUpdate.Flow.UI.Nodes.Upsert(NewRegenerateLookupNode())
ctxUpdate.Flow.InternalContext, err = sjson.SetBytes(ctxUpdate.Flow.InternalContext, flow.PrefixInternalContextKey(s.ID(), internalContextKeyRevealed), true)
Expand All @@ -193,12 +193,12 @@ func (s *Strategy) continueSettingsFlowReveal(w http.ResponseWriter, r *http.Req
}

func (s *Strategy) continueSettingsFlowRegenerate(w http.ResponseWriter, r *http.Request, ctxUpdate *settings.UpdateContext, p *submitSelfServiceSettingsFlowWithLookupMethodBody) error {
codes := make([]string, numCodes)
codes := make([]RecoveryCode, numCodes)
for k := range codes {
codes[k] = randx.MustString(8, randx.AlphaLowerNum)
codes[k] = RecoveryCode{Code:randx.MustString(8, randx.AlphaLowerNum)}
}

ctxUpdate.Flow.UI.Nodes.Upsert(NewLookupNode(codes))
ctxUpdate.Flow.UI.Nodes.Upsert((&CredentialsConfig{RecoveryCodes: codes}).ToNode())
ctxUpdate.Flow.UI.Nodes.Remove(node.LookupRegenerate)
ctxUpdate.Flow.UI.Nodes.Remove(node.LookupReveal)
ctxUpdate.Flow.UI.Nodes.Upsert(NewConfirmLookupNode())
Expand Down
4 changes: 2 additions & 2 deletions selfservice/strategy/lookup/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func TestCountActiveCredentials(t *testing.T) {
{
in: identity.CredentialsCollection{{
Type: strategy.ID(),
Config: []byte(`{"lookup_url": ""}`),
Config: []byte(`{"recovery_codes": []}`),
}},
expected: 0,
},
{
in: identity.CredentialsCollection{{
Type: strategy.ID(),
Identifiers: []string{"foo"},
Config: []byte(`{"lookup_url": ""}`),
Config: []byte(`{"recovery_codes": [{}]}`),
}},
expected: 1,
},
Expand Down
36 changes: 30 additions & 6 deletions text/message_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ const (
InfoSelfServiceSettingsTOTPSecret
InfoSelfServiceSettingsRevealLookup
InfoSelfServiceSettingsRegenerateLookup
InfoSelfServiceSettingsLookupSecrets
InfoSelfServiceSettingsLookupSecret
InfoSelfServiceSettingsLookupSecretLabel
InfoSelfServiceSettingsLookupConfirm
InfoSelfServiceSettingsRegisterWebAuthn
InfoSelfServiceSettingsRegisterWebAuthnDisplayName
InfoSelfServiceSettingsLookupSecretUsed
InfoSelfServiceSettingsLookupSecretList
)

const (
Expand Down Expand Up @@ -104,20 +107,41 @@ func NewInfoSelfServiceSettingsLookupConfirm() *Message {
}
}

func NewInfoSelfServiceSettingsLookupSecrets(secrets []string) *Message {
func NewInfoSelfServiceSettingsLookupSecretList(secrets []string, raw interface{}) *Message {
return &Message{
ID: InfoSelfServiceSettingsLookupSecrets,
Text: fmt.Sprintf("%s", strings.Join(secrets, " ")),
ID: InfoSelfServiceSettingsLookupSecretList,
Text: fmt.Sprintf("%s", strings.Join(secrets, ", ")),
Type: Info,
Context: context(map[string]interface{}{
"secrets": secrets,
"secrets": raw,
}),
}
}
func NewInfoSelfServiceSettingsLookupSecret(secret string) *Message {
return &Message{
ID: InfoSelfServiceSettingsLookupSecret,
Text: secret,
Type: Info,
Context: context(map[string]interface{}{
"secret": secret,
}),
}
}

func NewInfoSelfServiceSettingsLookupSecretUsed(usedAt time.Time) *Message {
return &Message{
ID: InfoSelfServiceSettingsLookupSecretUsed,
Text: fmt.Sprintf("Secret was used at %s", usedAt),
Type: Info,
Context: context(map[string]interface{}{
"used_at": usedAt,
}),
}
}

func NewInfoSelfServiceSettingsLookupSecretsLabel() *Message {
return &Message{
ID: InfoSelfServiceSettingsLookupSecrets,
ID: InfoSelfServiceSettingsLookupSecretLabel,
Text: "These are your back up recovery codes. Please keep them in a safe place!",
Type: Info,
}
Expand Down

0 comments on commit 58b388c

Please sign in to comment.