red-machine-69654
08/16/2022, 4:10 PMred-machine-69654
08/16/2022, 4:10 PM{
"$id": "<https://schemas.ory.sh/presets/kratos/identity.email.schema.json>",
"title": "Person",
"type": "object",
"properties": {
"traits": {
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "email",
"title": "E-Mail",
"<http://ory.sh/kratos|ory.sh/kratos>": {
"credentials": {
"password": {
"identifier": true
},
"totp": {
"account_name": true
}
},
"recovery": {
"via": "email"
},
"verification": {
"via": "email"
}
},
"maxLength": 320
},
"name": {
"type": "object",
"properties": {
"first": {
"type": "string",
"title": "First"
},
"last": {
"type": "string",
"title": "last"
}
}
},
"username": {
"type": "string",
"title": "username",
"maxLength": 30,
"<http://ory.sh/kratos|ory.sh/kratos>": {
"credentials": {
"password": {
"identifier": true
}
}
}
}
},
"required": [
"email",
"username"
],
"additionalProperties": false
}
}
}
(we started off with a schema which had only email and password (and a few other fields).)proud-plumber-24205
08/16/2022, 4:12 PMred-machine-69654
08/16/2022, 5:52 PMred-machine-69654
08/16/2022, 5:54 PM"username": {
// ...
"<http://ory.sh/kratos|ory.sh/kratos>": {
"credentials": {
"password": {
"identifier": true
}
}
}
red-machine-69654
08/16/2022, 5:54 PMred-machine-69654
08/16/2022, 5:54 PMred-machine-69654
08/16/2022, 5:54 PMred-machine-69654
08/16/2022, 5:55 PMred-machine-69654
08/16/2022, 5:55 PM❯ runway login
runway login
> tillschema
> ***********
login user tillschema: done
Logged in as tillschema
Configuration file written to "/Users/till/.cache/runway/runway.json"
red-machine-69654
08/16/2022, 5:55 PMred-machine-69654
08/16/2022, 5:58 PMred-machine-69654
08/16/2022, 6:12 PMmagnificent-energy-493
red-machine-69654
08/23/2022, 1:52 PMred-machine-69654
08/23/2022, 2:06 PMID: d9d8d739300700f7947fcd81affd89cf661120d323af3f65de53654499cd61ddcb212f86e56da0332c14b8116b607c4bfcce041a597f6c54ace12c8c710f8057 (created: 0001-01-01 00:00:00 +0000 UTC)
This API is wild.
schema.Id
followed by schema.GetCreatedAt().String()
red-machine-69654
08/23/2022, 2:42 PMidentity, _, err := a.client.V0alpha2Api.AdminGetIdentity(a.ctx, identityId).Execute()
if err != nil {
return err
}
identity.SetSchemaId(schemaId)
updateRequest := a.client.V0alpha2Api.AdminUpdateIdentity(a.ctx, ???)
I was hoping I can do SetSchemaId
and then pipe it into AdminUpdateIdentity
but that doesn't seem to work and my google fails me.proud-plumber-24205
08/23/2022, 3:21 PMproud-plumber-24205
08/23/2022, 3:21 PMred-machine-69654
08/23/2022, 3:22 PMred-machine-69654
08/23/2022, 3:22 PMred-machine-69654
08/23/2022, 3:23 PMred-machine-69654
08/23/2022, 3:23 PMidentity.SetSchemaId(schemaId)
traits := identity.GetTraits().(map[string]interface{})
body := ory.NewAdminUpdateIdentityBody(schemaId, *identity.State, traits)
updateRequest := a.client.V0alpha2Api.AdminUpdateIdentity(a.ctx, identityId)
updateRequest.AdminUpdateIdentityBody(*body)
updatedId, http, err := updateRequest.Execute()
if err != nil {
logrus.Printf("%v", http)
logrus.Debug("error running update")
logrus.Debug(http.Body)
return err
}
if updatedId.SchemaId != schemaId {
return fmt.Errorf("schema wasn't updated")
}
red-machine-69654
08/23/2022, 3:24 PMred-machine-69654
08/23/2022, 3:24 PMINFO[0000] &{500 Internal Server Error 500 HTTP/2.0 2 0 map[Alt-Svc:[h3=":443"; ma=86400, h3-29=":443"; ma=86400] B3:[0000000000000000a35fd389bd85f4ec-0dec4873cdfa0878-1] Cache-Control:[private, no-cache, no-store, must-revalidate] Cf-Cache-Status:[DYNAMIC] Cf-Ray:[73f4cedb2e015902-TXL] Content-Length:[170] Content-Type:[application/json] Date:[Tue, 23 Aug 2022 15:16:12 GMT] Expect-Ct:[max-age=604800, report-uri="<https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct>"] Server:[cloudflare] Set-Cookie:[__cflb=0pg1SKa3T3hcJoeWsdKc4AgvxrNdv5LXwoYLHHnT; SameSite=None; Secure; path=/; expires=Tue, 23-Aug-22 16:16:12 GMT; HttpOnly] Uber-Trace-Id:[0000000000000000a35fd389bd85f4ec:0dec4873cdfa0878:0:1] Vary:[Origin] X-B3-Sampled:[1] X-B3-Spanid:[0dec4873cdfa0878] X-B3-Traceid:[0000000000000000a35fd389bd85f4ec]] {{"error":{"code":500,"status":"Internal Server Error","request":"1ed86b49-3647-94bf-b308-2b19c609ca52","message":"invalid character '}' looking for beginning of value"}}
} 170 [] false false map[] 0xc0000c4800 0xc0000f80b0}
DEBU[0000] error running update
DEBU[0000] {{"error":{"code":500,"status":"Internal Server Error","request":"1ed86b49-3647-94bf-b308-2b19c609ca52","message":"invalid character '}' looking for beginning of value"}}
}
FATA[0000] 500 Internal Server Error
exit status 1
proud-plumber-24205
08/23/2022, 3:24 PMred-machine-69654
08/23/2022, 3:24 PMhigh-optician-2097
red-machine-69654
08/23/2022, 3:48 PMred-machine-69654
08/23/2022, 3:48 PMred-machine-69654
08/23/2022, 3:48 PM// check if schema exists
_, _, err := a.client.V0alpha2Api.GetIdentitySchema(a.ctx, schemaId).Execute()
if err != nil {
logrus.Debug("error getting schema")
return err
}
// check if identity exists
identity, _, err := a.client.V0alpha2Api.AdminGetIdentity(a.ctx, identityId).Execute()
if err != nil {
logrus.Debug("error getting identity")
return err
}
if identity.SchemaId == schemaId {
fmt.Println("already migrated, no update required")
return nil
}
identity.SetSchemaId(schemaId)
traits := identity.GetTraits().(map[string]interface{})
body := ory.NewAdminUpdateIdentityBody(schemaId, *identity.State, traits)
updateRequest := a.client.V0alpha2Api.AdminUpdateIdentity(a.ctx, identityId)
updateRequest.AdminUpdateIdentityBody(*body)
updatedId, http, err := updateRequest.Execute()
if err != nil {
logrus.Printf("%v", http)
logrus.Debug("error running update")
logrus.Debug(http.Body)
return err
}
if updatedId.SchemaId != schemaId {
return fmt.Errorf("schema wasn't updated")
}
return nil
red-machine-69654
08/23/2022, 3:48 PMhigh-optician-2097
red-machine-69654
08/23/2022, 3:53 PMred-machine-69654
08/23/2022, 3:53 PMhigh-optician-2097
red-machine-69654
08/23/2022, 3:56 PMred-machine-69654
08/23/2022, 3:57 PMred-machine-69654
08/23/2022, 3:57 PMhigh-optician-2097
red-machine-69654
08/24/2022, 7:13 AMhigh-optician-2097
high-optician-2097
red-machine-69654
08/24/2022, 8:22 AMhigh-optician-2097
red-machine-69654
08/24/2022, 8:32 AMhigh-optician-2097
high-optician-2097
red-machine-69654
08/24/2022, 9:11 AMhigh-optician-2097
high-optician-2097
high-optician-2097
package main
import (
"context"
"fmt"
"<http://github.com/sirupsen/logrus|github.com/sirupsen/logrus>"
)
import "<http://github.com/ory/client-go|github.com/ory/client-go>"
var YOUR_ORY_PAT="...."
var YOUR_ORY_SDK_URL="<https://YOUR_PROJECT.projects.oryapis.com>"
func main() {
if err := migrateSchema("<preset://email>", "f086edf7eb1d4e78731d4409303cdd173fab4b1e6ca79c09871290103d7c22072c252773792b29621f9ac272ced5ffa9ee63420a81f6dff6850c07a7cb26c2e4", "3974b38c-6622-417a-a1c2-532b06f67264"); err != nil {
panic(err)
}
}
func migrateSchema(fromSchema, toSchema, identityID string) error {
conf := client.NewConfiguration()
conf.Servers = client.ServerConfigurations{
{
URL: YOUR_ORY_SDK_URL,
},
}
oc := client.NewAPIClient(conf)
ctx := context.Background()
ctx = context.WithValue(ctx, client.ContextAccessToken, YOUR_PAT)
// check if schema exists
_, _, err := oc.V0alpha2Api.GetIdentitySchema(ctx, toSchema).Execute()
if err != nil {
logrus.Debug("error getting schema")
return err
}
// check if identity exists
identity, _, err := oc.V0alpha2Api.AdminGetIdentity(ctx, identityID).Execute()
if err != nil {
logrus.Debug("error getting identity")
return err
}
if identity.SchemaId == toSchema {
fmt.Println("already migrated, no update required")
return nil
}
traits := identity.GetTraits().(map[string]interface{})
body := client.NewAdminUpdateIdentityBody(toSchema, *identity.State, traits)
updatedId, _, err := oc.V0alpha2Api.AdminUpdateIdentity(ctx, identityID).AdminUpdateIdentityBody(*body).Execute()
if err != nil {
logrus.Debug("error running update")
return err
}
if updatedId.SchemaId != toSchema {
return fmt.Errorf("schema wasn't updated")
}
fmt.Println("done")
return nil
}
high-optician-2097
fromSchema
is actually not neededred-machine-69654
08/24/2022, 9:34 AMhigh-optician-2097
high-optician-2097
high-optician-2097
updateRequest := oc.V0alpha2Api.AdminUpdateIdentity(ctx, identityId)
updateRequest.AdminUpdateIdentityBody(*body)
updatedId, http, err := updateRequest.Execute()
high-optician-2097
high-optician-2097
high-optician-2097
updateRequest := oc.V0alpha2Api.AdminUpdateIdentity(ctx, identityId)
updateRequest = updateRequest.AdminUpdateIdentityBody(*body)
updatedId, http, err := updateRequest.Execute()
this probably works alsored-machine-69654
08/24/2022, 9:37 AMhigh-optician-2097
red-machine-69654
08/24/2022, 11:10 AMred-machine-69654
08/24/2022, 11:13 AMred-machine-69654
08/24/2022, 11:13 AMred-machine-69654
08/24/2022, 11:18 AMred-machine-69654
08/24/2022, 11:19 AMred-machine-69654
08/24/2022, 11:20 AMred-machine-69654
08/24/2022, 11:20 AMred-machine-69654
08/24/2022, 11:20 AMhigh-optician-2097