Hi, I am currently building kratos, keto and oathk...
# talk-kratos
a
Hi, I am currently building kratos, keto and oathkeeper and trying to make it work. I have an after registration hook which works fine if I do the API request by myself but If I use the Ory SDK, it seems not to work. Thats as far as I can go looking at the source code. May someone look at my code and tell me, what I did wrong?
Copy code
This does not works:
func (kc *KratosClient) UpdateAdminMetaData(ctx context.Context, signUpRequest requests.SignUpRequest, role string) (interface{}, error) {
	// Construct the traits from the signUpRequest
	traits := map[string]interface{}{
		"email":           signUpRequest.Email,
		"firstname":       signUpRequest.Firstname,
		"lastname":        signUpRequest.Lastname
	}

	// Convert traits to json.RawMessage
	// traitsBytes, err := json.Marshal(traits)
	// if err != nil {
	// 	kc.logger.Error().Msgf("Error marshalling traits: %v", err)
	// 	return nil, err
	// }

	// Set up the metadata you want to add
	metadataAdmin := AdminMetadata{
		Role:      role,
	}

	kc.logger.Info().Msgf("Metadata admin: %v", metadataAdmin)
	// Create a new Identity object
	identity := &ory.UpdateIdentityBody{}
	kc.logger.Info().Msgf("Identity: %v", identity)
	// Set the state, schema ID, and traits
	identity.SetState("active")     
	identity.SetSchemaId("default") 
	identity.SetTraits(traits)
	// var traitsMap map[string]interface{}
	// err = json.Unmarshal(traitsBytes, &traitsMap)
	// if err != nil {
	// 	kc.logger.Error().Msgf("Error unmarshalling traits: %v", err)
	// 	return nil, err
	// }

	// identity.SetTraits(traitsMap)

	// Set the admin metadata
	identity.SetMetadataAdmin(metadataAdmin)

	kc.logger.Info().Msgf("Identity end: %v", identity)
	// Use the Ory SDK to update the identity
	updatedIdentity, res, err := kc.oryClient.IdentityAPI.UpdateIdentity(ctx, signUpRequest.IdentityId.String()).UpdateIdentityBody(*identity).Execute()
	if err != nil {
		kc.logger.Error().Msgf("Error updating identity metadata: %v", err)
		kc.logger.Error().Msgf("Response: %v", res)
		kc.logger.Error().Msgf("Updated identity: %v", updatedIdentity)
		return nil, err
	}

	kc.logger.Info().Msg("Admin metadata updated successfully")
	return identity, nil
}
and here the logging I got:
Copy code
{"level":"info","time":"2023-11-21T18:47:46+01:00","message":"Identity: &{<nil> <nil> <nil>   map[] map[]}"}
{"level":"info","time":"2023-11-21T18:47:46+01:00","message":"Identity end: &{<nil> {ADMIN} <nil> default active map[email:popoip@poas.de firstname:okdas lastname:dasopk position:dalksm,] map[]}"}
{"level":"error","time":"2023-11-21T18:47:46+01:00","message":"Error updating identity metadata: undefined response type"}
{"level":"error","time":"2023-11-21T18:47:46+01:00","message":"Response: &{404 Not Found 404 HTTP/1.1 1 1 map[Content-Length:[19] Content-Type:[text/plain; charset=utf-8] Date:[Tue, 21 Nov 2023 17:47:46 GMT] X-Content-Type-Options:[nosniff]] {404 page not found\n} 19 [] false false map[] 0x1400057e400 <nil>}"}
{"level":"error","time":"2023-11-21T18:47:46+01:00","message":"Updated identity: <nil>"}
{"level":"error","time":"2023-11-21T18:47:46+01:00","message":"Error updating admin metadata: undefined response type"}
t
My guess is proxy issues. What's the URL your config is set to? If you're using docker you can check logs in your proxy or kratos itself to see what path was called. You can spin up a Dozzle container to conveniently look at logs. Just adding, you shouldn't expose or connect to admin endpoints via proxy.
a
@tall-easter-2247 I have this same function with a direct admin api call and that works. so it does not have anything to do with proxy. this one does not. I am not sure why that is. the oathkeeper, kratos and keto instances run in docker but my services run locally so I basically use host.docker.internal as host. Maybe the UpdateIdentityBody way is the wrong way?
Nevermind. I found a solution. I had to change the
[]byte
representation of the traits back into a
map[string]interface{}
. I seem to have done it wrong before, it works now.