Skip to content

Commit

Permalink
Converts Recovery Flow from Code to Link
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryan51203 committed Dec 22, 2023
1 parent e0d3251 commit a276b93
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
7 changes: 4 additions & 3 deletions api/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
})
}
21 changes: 15 additions & 6 deletions pkg/wrapper/kratos/recovery/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,31 @@ 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)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
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
}
4 changes: 2 additions & 2 deletions pkg/wrapper/kratos/recovery/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit a276b93

Please sign in to comment.