Skip to content

Commit

Permalink
Kratosifies Settings Flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryan51203 committed Dec 23, 2023
1 parent e8f2261 commit 645e0e0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 88 deletions.
6 changes: 3 additions & 3 deletions api/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func HandleUpdateProfile(c *gin.Context) {
return
}

msg, err := settings.SubmitSettingsFlowProfileMethod(flow_cookie, session_cookie, req_body.FlowID, req_body.CsrfToken, req_body.Method, traitsinterface)
msg, err := settings.SubmitSettingsFlowProfileMethod(flow_cookie, session_cookie, req_body.FlowID, req_body.CsrfToken, traitsinterface)

if err != nil {
log.ErrorLogger("Kratos post settings flow failed", err)
Expand Down Expand Up @@ -194,7 +194,7 @@ func HandleChangePassword(c *gin.Context) {
return
}

msg, err := settings.SubmitSettingsFlowPasswordMethod(flow_cookie, session_cookie, req_body.FlowID, req_body.CsrfToken, req_body.Method, req_body.Password)
msg, err := settings.SubmitSettingsFlowPasswordMethod(flow_cookie, session_cookie, req_body.FlowID, req_body.CsrfToken, req_body.Password)

if err != nil {
log.ErrorLogger("Kratos post settings flow failed", err)
Expand Down Expand Up @@ -251,7 +251,7 @@ func HandleToggleTOTP(c *gin.Context) {
return
}

msg, err := settings.SubmitSettingsFlowTOTPMethod(flow_cookie, session_cookie, req_body.FlowID, req_body.CsrfToken, req_body.Method, req_body.TOTPCode, req_body.TOTPUnlink)
msg, err := settings.SubmitSettingsFlowTOTPMethod(flow_cookie, session_cookie, req_body.FlowID, req_body.CsrfToken, req_body.TOTPCode, req_body.TOTPUnlink)

if err != nil {
log.ErrorLogger("Kratos post settings flow failed", err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.18
require (
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.8.1
github.com/goccy/go-json v0.9.7
github.com/lib/pq v1.10.7
github.com/ory/client-go v0.2.0-alpha.60
github.com/sirupsen/logrus v1.9.0
Expand All @@ -23,6 +22,7 @@ require (
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
Expand Down
114 changes: 34 additions & 80 deletions pkg/wrapper/kratos/settings/settings.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package settings

import (
"bytes"
"context"
"errors"
"net/http"
"fmt"
"os"
"strings"

"github.com/goccy/go-json"
client "github.com/ory/client-go"

"github.com/sdslabs/nymeria/config"
Expand Down Expand Up @@ -36,109 +34,65 @@ func InitializeSettingsFlowWrapper(session_cookie string, recovery_cookie string
return *resp, cookie, nil
}

func SubmitSettingsFlowPasswordMethod(flow_cookie string, session_cookie string, flowID string, csrfToken string, method string, password string) (string, error) {
client := &http.Client{}

var req_body SubmitSettingsWithPasswordBody
req_body.Method = method
req_body.Password = password
req_body.CsrfToken = csrfToken

body, err := json.Marshal(req_body)
if err != nil {
return "", err
}
req, err := http.NewRequest(http.MethodPost, "http://127.0.0.1:4433/self-service/settings", bytes.NewBuffer(body))

q := req.URL.Query()
q.Add("flow", flowID)

if err != nil {
return "", err
func SubmitSettingsFlowPasswordMethod(flow_cookie string, session_cookie string, flowID string, csrfToken string, password string) (string, error) {
submitFlowBody := client.SubmitSelfServiceSettingsFlowBody{
SubmitSelfServiceSettingsFlowWithPasswordMethodBody: client.NewSubmitSelfServiceSettingsFlowWithPasswordMethodBody("password", password),
}

submitFlowBody.SubmitSelfServiceSettingsFlowWithPasswordMethodBody.SetCsrfToken(csrfToken)
cookie := strings.Split(flow_cookie, ";")[0] + "; " + strings.Split(session_cookie, ";")[0] + "; x-csrf-token=" + csrfToken
req.URL.RawQuery = q.Encode()
req.Header.Set("Cookie", cookie)
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
apiClient := client.NewAPIClient(config.KratosClientConfig)
_, r, err := apiClient.V0alpha2Api.SubmitSelfServiceSettingsFlow(context.Background()).Flow(flowID).Cookie(cookie).SubmitSelfServiceSettingsFlowBody(submitFlowBody).Execute()

if err != nil || resp.StatusCode != 200 {
error := errors.New(resp.Status)
return "", error
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.SubmitSelfServiceVerificationFlow``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
return "", err
}

return "Password Changed", nil
}

func SubmitSettingsFlowProfileMethod(flow_cookie string, session_cookie string, flowID string, csrfToken string, method string, traits map[string]interface{}) (string, error) {
client := &http.Client{}

var req_body SubmitSettingsProfileRequest
req_body.Method = method
req_body.CsrfToken = csrfToken
req_body.Traits = traits

body, err := json.Marshal(req_body)
if err != nil {
return "", err
func SubmitSettingsFlowProfileMethod(flow_cookie string, session_cookie string, flowID string, csrfToken string, traits map[string]interface{}) (string, error) {
submitFlowBody := client.SubmitSelfServiceSettingsFlowBody{
SubmitSelfServiceSettingsFlowWithProfileMethodBody: client.NewSubmitSelfServiceSettingsFlowWithProfileMethodBody("profile", traits),
}

req, err := http.NewRequest(http.MethodPost, "http://127.0.0.1:4433/self-service/settings", bytes.NewBuffer(body))
q := req.URL.Query()
q.Add("flow", flowID)
submitFlowBody.SubmitSelfServiceSettingsFlowWithProfileMethodBody.SetCsrfToken(csrfToken)

if err != nil {
return "", err
}
cookie := strings.Split(flow_cookie, ";")[0] + "; " + strings.Split(session_cookie, ";")[0] + "; x-csrf-token=" + csrfToken
req.URL.RawQuery = q.Encode()
req.Header.Set("Cookie", cookie)
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
apiClient := client.NewAPIClient(config.KratosClientConfig)
_, r, err := apiClient.V0alpha2Api.SubmitSelfServiceSettingsFlow(context.Background()).Flow(flowID).Cookie(cookie).SubmitSelfServiceSettingsFlowBody(submitFlowBody).Execute()

if err != nil || resp.StatusCode != 200 {
error := errors.New(resp.Status)
return "", error
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.SubmitSelfServiceVerificationFlow``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
return "", err
}

return "Profile Updated", nil
}

func SubmitSettingsFlowTOTPMethod(flow_cookie string, session_cookie string, flowID string, csrfToken string, method string, TOTPcode string, TOTPUnlink bool) (string, error) {
client := &http.Client{}

var req_body SubmitSettingsWithTOTPBody
req_body.Method = method
req_body.TotpCode = TOTPcode
req_body.TotpUnlink = TOTPUnlink
req_body.CsrfToken = csrfToken

body, err := json.Marshal(req_body)
if err != nil {
return "", err
func SubmitSettingsFlowTOTPMethod(flow_cookie string, session_cookie string, flowID string, csrfToken string, TOTPcode string, TOTPUnlink bool) (string, error) {
submitFlowBody := client.SubmitSelfServiceSettingsFlowBody{
SubmitSelfServiceSettingsFlowWithTotpMethodBody: client.NewSubmitSelfServiceSettingsFlowWithTotpMethodBody("totp"),
}
req, err := http.NewRequest(http.MethodPost, "http://127.0.0.1:4433/self-service/settings", bytes.NewBuffer(body))

q := req.URL.Query()
q.Add("flow", flowID)

if err != nil {
return "", err
}
submitFlowBody.SubmitSelfServiceSettingsFlowWithTotpMethodBody.SetCsrfToken(csrfToken)
submitFlowBody.SubmitSelfServiceSettingsFlowWithTotpMethodBody.SetTotpCode(TOTPcode)
submitFlowBody.SubmitSelfServiceSettingsFlowWithTotpMethodBody.SetTotpUnlink(TOTPUnlink)

cookie := strings.Split(flow_cookie, ";")[0] + "; " + strings.Split(session_cookie, ";")[0] + "; x-csrf-token=" + csrfToken
req.URL.RawQuery = q.Encode()
req.Header.Set("Cookie", cookie)
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
apiClient := client.NewAPIClient(config.KratosClientConfig)
_, r, err := apiClient.V0alpha2Api.SubmitSelfServiceSettingsFlow(context.Background()).Flow(flowID).Cookie(cookie).SubmitSelfServiceSettingsFlowBody(submitFlowBody).Execute()

if err != nil || resp.StatusCode != 200 {
error := errors.New(resp.Status)
return "", error
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.SubmitSelfServiceVerificationFlow``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
return "", err
}

return "Totp Toggled", nil
Expand Down
5 changes: 1 addition & 4 deletions pkg/wrapper/kratos/settings/types.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package settings

type UpdateProfileAPIBody struct {
Method string `json:"method"`
FlowID string `json:"flowID"`
CsrfToken string `json:"csrf_token"`
Traits Traits `json:"Traits"`
Traits Traits `json:"traits"`
}

type ChangePasswordAPIBody struct {
Method string `json:"method"`
FlowID string `json:"flowID"`
CsrfToken string `json:"csrf_token"`
Password string `json:"password"`
}

type ToggleTOTPAPIBody struct {
Method string `json:"method"`
FlowID string `json:"flowID"`
CsrfToken string `json:"csrf_token"`
TOTPCode string `json:"totp_code"`
Expand Down

0 comments on commit 645e0e0

Please sign in to comment.