Skip to content

Commit

Permalink
fix: pw migration hook in handler
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Jul 15, 2024
1 parent 630c487 commit 27280a9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"credentials": {
"password": {
"type": "password",
"identifiers": [
"[email protected]"
],
"config": {
"use_password_migration_hook": true
},
"version": 0
}
},
"schema_id": "default",
"state": "active",
"traits": {
"email": "[email protected]"
},
"metadata_public": null,
"metadata_admin": null,
"organization_id": null
}
3 changes: 3 additions & 0 deletions identity/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ type AdminIdentityImportCredentialsPasswordConfig struct {

// The password in plain text if no hash is available.
Password string `json:"password"`

// If set to true, the password will be migrated using the password migration hook.
UsePasswordMigrationHook bool `json:"use_password_migration_hook,omitempty"`
}

// Create Identity and Import Social Sign In Credentials
Expand Down
4 changes: 4 additions & 0 deletions identity/handler_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (h *Handler) importCredentials(ctx context.Context, i *Identity, creds *Ide
}

func (h *Handler) importPasswordCredentials(ctx context.Context, i *Identity, creds *AdminIdentityImportCredentialsPassword) (err error) {
if creds.Config.UsePasswordMigrationHook {
return i.SetCredentialsWithConfig(CredentialsTypePassword, Credentials{}, CredentialsPassword{UsePasswordMigrationHook: true})
}

// In here we deliberately ignore any password policies as the point here is to import passwords, even if they
// are not matching the policy, as the user needs to able to sign in with their old password.
hashed := []byte(creds.Config.HashedPassword)
Expand Down
15 changes: 15 additions & 0 deletions identity/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,21 @@ func TestHandler(t *testing.T) {
}
})

t.Run("with password migration hook enabled", func(t *testing.T) {
res := send(t, adminTS, "POST", "/identities", http.StatusCreated, identity.CreateIdentityBody{
Traits: []byte(`{"email": "[email protected]"}`),
Credentials: &identity.IdentityWithCredentials{Password: &identity.AdminIdentityImportCredentialsPassword{
Config: identity.AdminIdentityImportCredentialsPasswordConfig{UsePasswordMigrationHook: true},
}},
})
actual, err := reg.PrivilegedIdentityPool().GetIdentityConfidential(ctx, uuid.FromStringOrNil(res.Get("id").String()))
require.NoError(t, err)

snapshotx.SnapshotT(t, identity.WithCredentialsAndAdminMetadataInJSON(*actual), snapshotx.ExceptNestedKeys(ignoreDefault...), snapshotx.ExceptNestedKeys("hashed_password"))

assert.True(t, gjson.GetBytes(actual.Credentials[identity.CredentialsTypePassword].Config, "use_password_migration_hook").Bool())
})

t.Run("with not-normalized email", func(t *testing.T) {
res := send(t, adminTS, "POST", "/identities", http.StatusCreated, identity.CreateIdentityBody{
SchemaID: "customer",
Expand Down

0 comments on commit 27280a9

Please sign in to comment.