Skip to content

Commit

Permalink
feat(identity): add versioning to credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Mar 7, 2022
1 parent c9875a7 commit aaf779a
Show file tree
Hide file tree
Showing 24 changed files with 51 additions and 2 deletions.
5 changes: 4 additions & 1 deletion identity/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type Credentials struct {
// for passwordless authentication or access_token and refresh tokens from OpenID Connect flows.
Config sqlxx.JSONRawMessage `json:"config,omitempty" db:"config"`

// Version refers to the version of the credential. Useful when changing the config schema.
Version int `json:"version" db:"version"`

IdentityID uuid.UUID `json:"-" faker:"-" db:"identity_id"`

// CreatedAt is a helper struct field for gobuffalo.pop.
Expand Down Expand Up @@ -116,7 +119,7 @@ type (
// swagger:ignore
ActiveCredentialsCounter interface {
ID() CredentialsType
CountActiveCredentials(cc map[CredentialsType]Credentials) (int, error)
CountActiveFirstFactorCredentials(cc map[CredentialsType]Credentials) (int, error)
}

// swagger:ignore
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
{
"id": "5ff66179-c240-4703-b0d8-494592cefff5",
"credentials": {
"password": {
"type": "password",
"identifiers": [
"[email protected]"
],
"config": {
"hashed_password": "$argon2id$v=19$m=131072,t=2,p=1$lQFPaKxXqPL56/mU7vRi4w$6aldHyBnURt8sP8+xu41Ng"
},
"version": 0,
"created_at": "2013-10-07T08:23:19Z",
"updated_at": "2013-10-07T08:23:19Z"
}
},
"schema_id": "default",
"schema_url": "https://www.ory.sh/schemas/ZGVmYXVsdA",
"state": "active",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
{
"id": "a251ebc2-880c-4f76-a8f3-38e6940eab0e",
"credentials": {
"password": {
"type": "password",
"identifiers": [
"[email protected]"
],
"config": {
"hashed_password": "$argon2id$v=19$m=131072,t=2,p=1$lQFPaKxXqPL56/mU7vRi4w$6aldHyBnURt8sP8+xu41Ng"
},
"version": 0,
"created_at": "2013-10-07T08:23:19Z",
"updated_at": "2013-10-07T08:23:19Z"
}
},
"schema_id": "default",
"schema_url": "https://www.ory.sh/schemas/ZGVmYXVsdA",
"state": "active",
Expand Down
3 changes: 2 additions & 1 deletion persistence/sql/migratest/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/ory/kratos/identity"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -149,7 +150,7 @@ func TestMigrations(t *testing.T) {
// Prevents ordering to get in the way.
actual.VerifiableAddresses = nil
actual.RecoveryAddresses = nil
CompareWithFixture(t, actual, "identity", id.ID.String())
CompareWithFixture(t, identity.WithCredentialsInJSON(*actual), "identity", id.ID.String())
}

migratest.ContainsExpectedIds(t, filepath.Join("fixtures", "identity"), found)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO identity_credentials (id, config, identity_credential_type_id, identity_id, created_at, updated_at, version) VALUES ('4cefc264-4291-4abc-8f26-cc0217874f14', '{"hashed_password":"$argon2id$v=19$m=131072,t=2,p=1$lQFPaKxXqPL56/mU7vRi4w$6aldHyBnURt8sP8+xu41Ng"}', '22bff9ae-f5aa-45d7-803b-97ec0b4e7b32', '5ff66179-c240-4703-b0d8-494592cefff5', '2013-10-07 08:23:19', '2013-10-07 08:23:19', 0);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE identity_credentials ADD version INT NOT NULL DEFAULT '0';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE identity_credentials ADD version INT NOT NULL DEFAULT '0';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE identity_credentials ADD version INT NOT NULL DEFAULT '0';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE identity_credentials ADD version INT NOT NULL DEFAULT '0';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE identity_credentials DROP COLUMN version;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE identity_credentials DROP COLUMN version;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE identity_credentials DROP COLUMN version;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE identity_credentials DROP COLUMN version;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE identity_credentials DROP COLUMN version;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE identity_credentials ADD version INT NOT NULL DEFAULT '0';
6 changes: 6 additions & 0 deletions persistence/sql/persister_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

"github.com/ory/kratos/credentialmigrate"

"github.com/ory/kratos/corp"

"github.com/ory/jsonschema/v3"
Expand Down Expand Up @@ -386,6 +388,10 @@ func (p *Persister) GetIdentityConfidential(ctx context.Context, id uuid.UUID) (
i.Credentials[cred.Type] = *cred
}

if err := credentialmigrate.UpgradeCredentials(&i); err != nil {
return nil, err
}

if err := p.findRecoveryAddresses(ctx, &i); err != nil {
return nil, err
}
Expand Down

0 comments on commit aaf779a

Please sign in to comment.