Skip to content

Commit

Permalink
feat: add versioning and improve compatibility for credential migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Mar 7, 2022
1 parent b75a639 commit 78ce668
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
},
"display_name": "asdf",
"added_at": "2022-02-28T16:40:39Z",
"is_passwordless": false,
"user_handle": "4d64fa08-20fc-450d-bebd-ebd7c7b6e249"
"is_passwordless": false
}
]
],
"user_handle": "TWT6CCD8RQ2+vevXx7biSQ=="
},
"version": 1,
"created_at": "0001-01-01T00:00:00Z",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"id": "4d64fa08-20fc-450d-bebd-ebd7c7b6e249",
"credentials": {
"webauthn": {
"type": "webauthn",
"identifiers": null,
"config": {
"credentials": [
{
"id": "HQ4LaIJ9NiqS1r0CQpWY+K0gMvhOq4yk5BHuO/YlitcurSpBK7weDXOvBcuN4lvn6DAmjGfmj/J/6bpOmtdT8Q==",
"public_key": "pQECAyYgASFYILAYFLoH1T8bQMSbPrNBCMMS5U7OFWRwv2U+GkAoiBADIlggBv+8ni7XVZYBB8ufMbP/d9fDxbmOkVVHOgcJifnoOR4=",
"attestation_type": "none",
"authenticator": {
"aaguid": "AAAAAAAAAAAAAAAAAAAAAA==",
"sign_count": 4,
"clone_warning": false
},
"display_name": "asdf",
"added_at": "2022-02-28T16:40:39Z",
"is_passwordless": true
}
],
"user_handle": "2gZaSs9fTEeGmsBlC4gfgg=="
},
"version": 1,
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z"
}
},
"schema_id": "",
"schema_url": "",
"state": "",
"traits": null,
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z"
}
12 changes: 7 additions & 5 deletions credentialmigrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ package credentialmigrate

import (
"encoding/json"

"github.com/pkg/errors"

"github.com/ory/kratos/identity"
"github.com/ory/kratos/selfservice/strategy/webauthn"
"github.com/pkg/errors"
)

// UpgradeWebAuthnCredential migrates a webauthn credential from an older version to a newer version.
func UpgradeWebAuthnCredential(i *identity.Identity, ic *identity.Credentials, c *webauthn.CredentialsConfig) {
if ic.Version == 0 {
for k := range c.Credentials {
c.Credentials[k].UserHandle = i.ID.String()

// We do not set c.IsPasswordless as it defaults to false anyways, which is the correct migration .
if len(c.UserHandle) == 0 {
c.UserHandle = i.ID[:]
}

// We do not set c.IsPasswordless as it defaults to false anyways, which is the correct migration .

ic.Version = 1
}
}
Expand Down
8 changes: 5 additions & 3 deletions credentialmigrate/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package credentialmigrate

import (
_ "embed"
"testing"

"github.com/gofrs/uuid"
"github.com/ory/kratos/identity"
"github.com/ory/x/snapshotx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"

"github.com/ory/kratos/identity"
"github.com/ory/x/snapshotx"
)

//go:embed stub/webauthn/v0.json
Expand Down
6 changes: 3 additions & 3 deletions credentialmigrate/stub/webauthn/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
},
"display_name": "asdf",
"added_at": "2022-02-28T16:40:39Z",
"is_passwordless": true,
"user_handle": "4d64fa08-20fc-450d-bebd-ebd7c7b6e249"
"is_passwordless": true
}
]
],
"user_handle":"2gZaSs9fTEeGmsBlC4gfgg=="
}

0 comments on commit 78ce668

Please sign in to comment.