英文:
Problem with understanding the ORY Kratos golang SDK
问题
所以我正在编写一个用于注册/登录/注销用户的API。
我使用以下包https://github.com/ory/client-go来实现这个功能。
下面的代码应该用于注册用户:
var newIdentity models.NewIdentity
err := fctx.BodyParser(&newIdentity) // 将请求体解析为结构体
if err != nil {
c.Log.Error("在解组时发生错误。错误信息:%s", err.Error())
return err
}
adminCreateIdentityBody := *ory.NewAdminCreateIdentityBody(
"default",
map[string]interface{}{
"email": newIdentity.Email,
// 我认为在这里应该插入一个密码,但是它不起作用
"name": map[string]string{
"first": newIdentity.Name.First,
"last": newIdentity.Name.Last,
},
})
createdIdentity, resp, err := c.OryAdminApiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute()
问题是,我不太明白如何为正在创建的用户获取会话。
我也找不到如何在adminCreateIdentityBody中设置正在创建的用户的密码。
英文:
So I am writing an API for registering/logging in/logging out users
I use following package https://github.com/ory/client-go for this
The code below should register the user
var newIdentity models.NewIdentity
err := fctx.BodyParser(&newIdentity) // request body to struct
if err != nil {
c.Log.Error("Error occurred during unmarshalling. Error: %s", err.Error())
return err
}
adminCreateIdentityBody := *ory.NewAdminCreateIdentityBody(
"default",
map[string]interface{}{
"email": newIdentity.Email,
// I think that somewhere here I should insert a password, but it doesn't work
"name": map[string]string{
"first": newIdentity.Name.First,
"last": newIdentity.Name.Last,
},
})
createdIdentity, resp, err := c.OryAdminApiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute()
The problem is that I don't quite understand how I can get the session to the client for the user I'm creating
I also could not find how to write down the password for the user being created in
adminCreateIdentityBody
答案1
得分: 1
这段代码是用来在后端创建用户的,使用了Gin进行Web路由。代码中设置了从简单的HTML表单中发送的密码。
import (
/* your other imports here */
kratosclient "github.com/ory/kratos-client-go"
)
func Route(dsn string) {
router := gin.Default()
router.POST("/signup", func(c *gin.Context) {
log.Println("Sign up!")
var signupRequest SignupRequest
error := c.Bind(&signupRequest)
if error != nil {
// Required parameter wasn't there; client error.
// status = http.StatusBadRequest
// response = ErrorResponse()
log.Println("Parameter error - ", error)
return
}
configuration := kratosclient.NewConfiguration()
configuration.Servers = []kratosclient.ServerConfiguration{
{
URL: "http://host.docker.internal:4434", // Kratos Admin API - when running Kratos container on "same machine/host/localhost", as this app
// URL: "http://127.0.0.1:4434", // Kratos Admin API
},
}
apiClient := kratosclient.NewAPIClient(configuration)
// Set password
myPw := *kratosclient.NewAdminCreateIdentityImportCredentialsPassword()
myConfig := kratosclient.AdminCreateIdentityImportCredentialsPasswordConfig{}
myPw.SetConfig(myConfig)
myPw.Config.SetPassword(signupRequest.Password)
adminCreateIdentityBody := *kratosclient.NewAdminCreateIdentityBody(
"default",
map[string]interface{}{
"email": signupRequest.Email,
"name": map[string]string{
"first": "foo",
"last": "bar",
},
},
)
myCredentials := kratosclient.AdminIdentityImportCredentials{}
myCredentials.SetPassword(myPw)
adminCreateIdentityBody.SetCredentials(myCredentials)
createdIdentity, r, err := apiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminCreateIdentity`: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
// response from `AdminCreateIdentity`: Identity
fmt.Fprintf(os.Stdout, "Created identity with ID: %v\n", createdIdentity.Id)
getIdentity, r, err := apiClient.V0alpha2Api.AdminGetIdentity(context.Background(), createdIdentity.Id).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminGetIdentity`: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
fmt.Fprintf(os.Stdout, "Email for identity with id %v. Traits %v\n", createdIdentity.Id, getIdentity.Traits)
c.HTML(http.StatusOK, "signupdone.tmpl", gin.H{
"email": signupRequest.Email,
})
// We could delete the identity like below:
// r, err = apiClient.V0alpha2Api.AdminDeleteIdentity(context.Background(), getIdentity.Id).Execute()
// if err != nil {
// fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminDeleteIdentity`: %v\n", err)
// fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
// }
// fmt.Println("Successfully Removed identity")
})
}
type SignupRequest struct {
Email string `form:"email" binding:"required"`
Password string `form:"pw" binding:"required"`
}
我对Go的了解还很浅,可能有更好的表达方式,但这段代码在我的容器上是可以工作的。
英文:
This won't answer all of your questions but I figured out how to create users on the backend. I'm using Gin for web routing so if you're not that are some parts that might be different. Also this code is a bit dirty but it does set the password sent from a simple HTML post form:
import (
/* your other imports here */
kratosclient "github.com/ory/kratos-client-go"
)
func Route(dsn string) {
router := gin.Default()
router.POST("/signup", func(c *gin.Context) {
log.Println("Sign up!")
var signupRequest SignupRequest
error := c.Bind(&signupRequest)
if error != nil {
// Required parameter wasn't there; client error.
// status = http.StatusBadRequest
// response = ErrorResponse()
log.Println("Parameter error - ", error)
return
}
configuration := kratosclient.NewConfiguration()
configuration.Servers = []kratosclient.ServerConfiguration{
{
URL: "http://host.docker.internal:4434", // Kratos Admin API - when running Kratos container on "same machine/host/localhost", as this app
// URL: "http://127.0.0.1:4434", // Kratos Admin API
},
}
apiClient := kratosclient.NewAPIClient(configuration)
// Set password
myPw := *kratosclient.NewAdminCreateIdentityImportCredentialsPassword()
myConfig := kratosclient.AdminCreateIdentityImportCredentialsPasswordConfig{}
myPw.SetConfig(myConfig)
myPw.Config.SetPassword(signupRequest.Password)
adminCreateIdentityBody := *kratosclient.NewAdminCreateIdentityBody(
"default",
map[string]interface{}{
"email": signupRequest.Email,
"name": map[string]string{
"first": "foo",
"last": "bar",
},
},
)
myCredentials := kratosclient.AdminIdentityImportCredentials{}
myCredentials.SetPassword(myPw)
adminCreateIdentityBody.SetCredentials(myCredentials)
createdIdentity, r, err := apiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminCreateIdentity``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
// response from `AdminCreateIdentity`: Identity
fmt.Fprintf(os.Stdout, "Created identity with ID: %v\n", createdIdentity.Id)
getIdentity, r, err := apiClient.V0alpha2Api.AdminGetIdentity(context.Background(), createdIdentity.Id).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminGetIdentity``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
fmt.Fprintf(os.Stdout, "Email for identity with id %v. Traits %v\n", createdIdentity.Id, getIdentity.Traits)
c.HTML(http.StatusOK, "signupdone.tmpl", gin.H{
"email": signupRequest.Email,
})
// We could delete the identity like below:
// r, err = apiClient.V0alpha2Api.AdminDeleteIdentity(context.Background(), getIdentity.Id).Execute()
// if err != nil {
// fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminDeleteIdentity``: %v\n", err)
// fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
// }
// fmt.Println("Successfully Removed identity")
})
}
type SignupRequest struct {
Email string `form:"email" binding:"required"`
Password string `form:"pw" binding:"required"`
}
I've barely just touched Go yet so there might be some better ways of formulating things, but this does work, on my container 👨🏻🎓.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论