From a276b9323ec516cc7d1d1e2fa939daa4ddb8f973 Mon Sep 17 00:00:00 2001 From: Aryan Singh Date: Sat, 23 Dec 2023 04:42:47 +0530 Subject: [PATCH] Converts Recovery Flow from Code to Link --- api/recovery.go | 7 ++++--- pkg/wrapper/kratos/recovery/recovery.go | 21 +++++++++++++++------ pkg/wrapper/kratos/recovery/types.go | 4 ++-- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/api/recovery.go b/api/recovery.go index 85e833f..a2b1c26 100644 --- a/api/recovery.go +++ b/api/recovery.go @@ -61,7 +61,7 @@ func HandlePostRecoveryFlow(c *gin.Context) { return } - session, err := recovery.SubmitRecoveryFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Code, t.Method) + csrf_token, err := recovery.SubmitRecoveryFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Email, t.Token) if err != nil { log.ErrorLogger("POST Recovery flow failed", err) @@ -73,8 +73,9 @@ func HandlePostRecoveryFlow(c *gin.Context) { return } - c.SetCookie("sdslabs_session", session, 3600, "/", config.NymeriaConfig.URL.Domain, true, true) + // c.SetCookie("sdslabs_session", session, 3600, "/", config.NymeriaConfig.URL.Domain, true, true) c.JSON(http.StatusOK, gin.H{ - "message": "Mail sent with recovery code", + "message": "Mail sent with recovery code", + "csrf_token": csrf_token, }) } diff --git a/pkg/wrapper/kratos/recovery/recovery.go b/pkg/wrapper/kratos/recovery/recovery.go index db2fc85..ef715ca 100644 --- a/pkg/wrapper/kratos/recovery/recovery.go +++ b/pkg/wrapper/kratos/recovery/recovery.go @@ -36,16 +36,15 @@ func InitializeRecoveryFlowWrapper() (string, string, string, error) { return setCookie, resp.Id, csrf_token, nil } -func SubmitRecoveryFlowWrapper(cookie string, flowID string, csrfToken string, code string, method string) (string, error) { +func SubmitRecoveryFlowWrapper(cookie string, flowID string, csrfToken string, email string, token string) (string, error) { submitFlowBody := client.SubmitSelfServiceRecoveryFlowBody{ - SubmitSelfServiceRecoveryFlowWithCodeMethodBody: client.NewSubmitSelfServiceRecoveryFlowWithCodeMethodBody(method), + SubmitSelfServiceRecoveryFlowWithLinkMethodBody: client.NewSubmitSelfServiceRecoveryFlowWithLinkMethodBody(email, "link"), } - submitFlowBody.SubmitSelfServiceRecoveryFlowWithCodeMethodBody.SetCode(code) - submitFlowBody.SubmitSelfServiceRecoveryFlowWithCodeMethodBody.SetCsrfToken(csrfToken) + submitFlowBody.SubmitSelfServiceRecoveryFlowWithLinkMethodBody.SetCsrfToken(csrfToken) apiClient := client.NewAPIClient(config.KratosClientConfig) - _, r, err := apiClient.V0alpha2Api.SubmitSelfServiceRecoveryFlow(context.Background()).Flow(flowID).SubmitSelfServiceRecoveryFlowBody(submitFlowBody).Cookie(cookie).Execute() + resp, r, err := apiClient.V0alpha2Api.SubmitSelfServiceRecoveryFlow(context.Background()).Flow(flowID).Token(token).SubmitSelfServiceRecoveryFlowBody(submitFlowBody).Cookie(cookie).Execute() if err != nil { fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.SubmitSelfServiceRecoveryFlow``: %v\n", err) @@ -53,5 +52,15 @@ func SubmitRecoveryFlowWrapper(cookie string, flowID string, csrfToken string, c return "", err } - return "", nil + var csrf_token string + + for _, node := range resp.Ui.Nodes { + fmt.Println(node.Attributes.UiNodeInputAttributes) + if node.Attributes.UiNodeInputAttributes.Name == "csrf_token" { + csrf_token_interface := node.Attributes.UiNodeInputAttributes.Value + csrf_token, _ = csrf_token_interface.(string) + break + } + } + return csrf_token, nil } diff --git a/pkg/wrapper/kratos/recovery/types.go b/pkg/wrapper/kratos/recovery/types.go index 4777017..c9d06a1 100644 --- a/pkg/wrapper/kratos/recovery/types.go +++ b/pkg/wrapper/kratos/recovery/types.go @@ -3,6 +3,6 @@ package recovery type SubmitRecoveryAPIBody struct { CsrfToken string `json:"csrf_token"` FlowID string `json:"flowID"` - Code string `json:"code"` - Method string `json:"method"` + Email string `json:"email"` + Token string `json:"token"` }