Skip to content

Commit

Permalink
feat: add internal context to settings flow
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Oct 19, 2021
1 parent b12bf52 commit afb6895
Show file tree
Hide file tree
Showing 48 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "selfservice_settings_flows" DROP COLUMN "internal_context";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "selfservice_settings_flows" ADD COLUMN "internal_context" json;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `selfservice_settings_flows` DROP COLUMN `internal_context`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `selfservice_settings_flows` ADD COLUMN `internal_context` JSON;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "selfservice_settings_flows" DROP COLUMN "internal_context";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "selfservice_settings_flows" ADD COLUMN "internal_context" jsonb;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "_selfservice_settings_flows_tmp" RENAME TO "selfservice_settings_flows";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "selfservice_settings_flows" ADD COLUMN "internal_context" TEXT;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE selfservice_settings_flows SET internal_context='{}';
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE selfservice_settings_flows SET internal_context='{}';
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE selfservice_settings_flows SET internal_context='{}';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

DROP TABLE "selfservice_settings_flows";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE selfservice_settings_flows SET internal_context='{}';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "selfservice_settings_flows" RENAME COLUMN "internal_context" TO "_internal_context_tmp";
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `selfservice_settings_flows` MODIFY `internal_context` JSON NOT NULL;
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "selfservice_settings_flows" ALTER COLUMN "internal_context" TYPE jsonb, ALTER COLUMN "internal_context" SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO "_selfservice_settings_flows_tmp" (id, request_url, issued_at, expires_at, identity_id, created_at, updated_at, active_method, state, type, ui, nid) SELECT id, request_url, issued_at, expires_at, identity_id, created_at, updated_at, active_method, state, type, ui, nid FROM "selfservice_settings_flows";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS "selfservice_settings_flows_nid_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "selfservice_settings_flows" ADD COLUMN "internal_context" json;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX "selfservice_settings_flows_nid_idx" ON "_selfservice_settings_flows_tmp" (id, nid);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE "_selfservice_settings_flows_tmp" (
"id" TEXT PRIMARY KEY,
"request_url" TEXT NOT NULL,
"issued_at" DATETIME NOT NULL DEFAULT 'CURRENT_TIMESTAMP',
"expires_at" DATETIME NOT NULL,
"identity_id" char(36) NOT NULL,
"created_at" DATETIME NOT NULL,
"updated_at" DATETIME NOT NULL,
"active_method" TEXT,
"state" TEXT NOT NULL DEFAULT 'show_form',
"type" TEXT NOT NULL DEFAULT 'browser',
"ui" TEXT,
"nid" char(36),
"internal_context" TEXT NOT NULL,
FOREIGN KEY (identity_id) REFERENCES identities (id) ON UPDATE NO ACTION ON DELETE CASCADE
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "selfservice_settings_flows" SET "internal_context" = "_internal_context_tmp";
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE "_selfservice_settings_flows_tmp" (
"id" TEXT PRIMARY KEY,
"request_url" TEXT NOT NULL,
"issued_at" DATETIME NOT NULL DEFAULT 'CURRENT_TIMESTAMP',
"expires_at" DATETIME NOT NULL,
"identity_id" char(36) NOT NULL,
"created_at" DATETIME NOT NULL,
"updated_at" DATETIME NOT NULL,
"active_method" TEXT,
"state" TEXT NOT NULL DEFAULT 'show_form',
"type" TEXT NOT NULL DEFAULT 'browser',
"ui" TEXT,
"nid" char(36),
FOREIGN KEY (identity_id) REFERENCES identities (id) ON UPDATE NO ACTION ON DELETE CASCADE
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX "selfservice_settings_flows_nid_idx" ON "_selfservice_settings_flows_tmp" (id, nid);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "selfservice_settings_flows" ALTER COLUMN "internal_context" SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS "selfservice_settings_flows_nid_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO "_selfservice_settings_flows_tmp" (id, request_url, issued_at, expires_at, identity_id, created_at, updated_at, active_method, state, type, ui, nid, internal_context) SELECT id, request_url, issued_at, expires_at, identity_id, created_at, updated_at, active_method, state, type, ui, nid, internal_context FROM "selfservice_settings_flows";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "selfservice_settings_flows" DROP COLUMN "_internal_context_tmp";
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE "selfservice_settings_flows";
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "_selfservice_settings_flows_tmp" RENAME TO "selfservice_settings_flows";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop_column("selfservice_settings_flows", "internal_context")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_column("selfservice_settings_flows", "internal_context", "json", { "null": true })
sql("UPDATE selfservice_settings_flows SET internal_context='{}'")
change_column("selfservice_settings_flows", "internal_context", "json")
2 changes: 2 additions & 0 deletions persistence/sql/persister_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var _ settings.FlowPersister = new(Persister)

func (p *Persister) CreateSettingsFlow(ctx context.Context, r *settings.Flow) error {
r.NID = corp.ContextualizeNID(ctx, p.nid)
r.EnsureInternalContext()
return sqlcon.HandleError(p.GetConnection(ctx).Create(r))
}

Expand All @@ -38,5 +39,6 @@ func (p *Persister) GetSettingsFlow(ctx context.Context, id uuid.UUID) (*setting
func (p *Persister) UpdateSettingsFlow(ctx context.Context, r *settings.Flow) error {
cp := *r
cp.NID = corp.ContextualizeNID(ctx, p.nid)
cp.EnsureInternalContext()
return p.update(ctx, cp)
}
3 changes: 3 additions & 0 deletions selfservice/flow/internal_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package flow

const InternalContextKeyTOTPURL = "totp_url"
13 changes: 13 additions & 0 deletions selfservice/flow/settings/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package settings

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

"github.com/tidwall/gjson"

"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/ui/container"
"github.com/ory/x/urlx"
Expand Down Expand Up @@ -86,6 +89,9 @@ type Flow struct {
// required: true
State State `json:"state" faker:"-" db:"state"`

// InternalContext stores internal context used by internals - for example MFA keys.
InternalContext json.RawMessage `db:"internal_context" json:"-" faker:"-"`

// IdentityID is a helper struct field for gobuffalo.pop.
IdentityID uuid.UUID `json:"-" faker:"-" db:"identity_id"`
// CreatedAt is a helper struct field for gobuffalo.pop.
Expand Down Expand Up @@ -128,6 +134,7 @@ func NewFlow(conf *config.Config, exp time.Duration, r *http.Request, i *identit
Method: "POST",
Action: flow.AppendFlowTo(urlx.AppendPaths(conf.SelfPublicURL(r), RouteSubmitFlow), id).String(),
},
InternalContext: []byte("{}"),
}
}

Expand Down Expand Up @@ -167,3 +174,9 @@ func (f *Flow) Valid(s *session.Session) error {

return nil
}

func (f *Flow) EnsureInternalContext() {
if !gjson.ParseBytes(f.InternalContext).IsObject() {
f.InternalContext = []byte("{}")
}
}
11 changes: 11 additions & 0 deletions selfservice/flow/settings/test/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ func TestRequestPersister(ctx context.Context, conf *config.Config, p interface
})
})

t.Run("case=should ensure that internal context is an object", func(t *testing.T) {
r := newFlow(t)
r.InternalContext = []byte("null")
require.NoError(t, p.CreateSettingsFlow(ctx, r))
assert.Equal(t, "{}", string(r.InternalContext))

r.InternalContext = nil
require.NoError(t, p.UpdateSettingsFlow(ctx, r))
assert.Equal(t, "{}", string(r.InternalContext))
})

t.Run("case=should create with set ids", func(t *testing.T) {
var r settings.Flow
require.NoError(t, faker.FakeData(&r))
Expand Down

0 comments on commit afb6895

Please sign in to comment.