Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Update kratos #44

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Enter the Kratos directory and Change the Kratos Version to v0.10.0

```sh
cd kratos
git checkout v0.10.0
git checkout v1.3.1
```

Download the dependencies
Expand All @@ -110,5 +110,5 @@ Copy the Kratos config file and identity schema from nymeria
Run the following command to use Kratos in containerized form

```sh
docker-compose -f quickstart.yml -f quickstart-standalone.yml -f quickstart-postgres.yml up --build --force-recreate
docker compose -f quickstart.yml -f quickstart-standalone.yml -f quickstart-postgres.yml up --build --force-recreate
```
11 changes: 0 additions & 11 deletions api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ func HandleCreateIdentityFlow(c *gin.Context) {
return
}

if err != nil {
log.ErrorLogger("Unable to convert JSON to map", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to convert JSON to map",
})
return
}

createdIdentity, r, err := admin.CreateIdentityFlowWrapper(t)

if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ func Start() {

r.GET("/recovery", HandleGetRecoveryFlow)
r.POST("/recovery", HandlePostRecoveryFlow)
r.POST("/recovery-code", HandlePostRecoveryCodeFlow)

r.GET("/settings", HandleGetSettingsFlow)
r.POST("/updateprofile", HandleUpdateProfile)
r.POST("/changepassword", HandleChangePassword)
r.POST("/toggletotp", HandleToggleTOTP)

r.GET("/verification", HandleGetVerificationFlow)
r.POST("/verification-after-registration", HandleGetVerificationAfterRegistrationFlow)
r.POST("/verification", HandlePostVerificationFlow)
r.POST("/verification-code", HandlePostVerificationCodeFlow)

r.POST("/get_profile", HandlePostProfile)
r.POST("/get_verified_status", HandleGetVerifiedStatus)
Expand Down
52 changes: 49 additions & 3 deletions api/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func HandlePostRecoveryFlow(c *gin.Context) {
})
return
}

_, err = recovery.SubmitRecoveryFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Email)
var csrf_token string
csrf_token, err = recovery.SubmitRecoveryFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Email)

if err != nil {
log.ErrorLogger("POST Recovery flow failed", err)
Expand All @@ -74,6 +74,52 @@ func HandlePostRecoveryFlow(c *gin.Context) {
}

c.JSON(http.StatusOK, gin.H{
"message": "Mail sent with recovery link",
"message": "Mail sent with recovery code",
"csrf_token": csrf_token,
})
}

func HandlePostRecoveryCodeFlow(c *gin.Context) {
var t recovery.SubmitRecoveryAPIBody
err := c.BindJSON(&t)

if err != nil {
log.ErrorLogger("Unable to process json body", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to process json body",
})
return
}

cookie, err := c.Cookie("recovery_flow")

if err != nil {
log.ErrorLogger("Recovery Flow Cookie not found", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Recovery Flow Cookie not found",
})
return
}

session, err := recovery.SubmitRecoveryCodeFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.RecoveryCode)

if err != nil {
log.ErrorLogger("POST Recovery flow failed", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "POST Recovery Code flow failed",
})
return
}

c.SetCookie("sdslabs_session", session, 3600, "/", config.NymeriaConfig.URL.Domain, true, true)

c.JSON(http.StatusOK, gin.H{
"message": "Session cookie set for password change",
})
}
6 changes: 4 additions & 2 deletions api/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func HandlePostRegistrationFlow(c *gin.Context) {
return
}

session, err := registration.SubmitRegistrationFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Password, t.Traits)
flowID, sessionCookies, err := registration.SubmitRegistrationFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Password, t.Traits)

if err != nil {
log.ErrorLogger("Kratos post registration flow failed", err)
Expand All @@ -72,9 +72,11 @@ func HandlePostRegistrationFlow(c *gin.Context) {
return
}

c.SetCookie("sdslabs_session", session, 3600, "/", config.NymeriaConfig.URL.Domain, true, true)
c.SetCookie("verification_flow", sessionCookies[0], 3600, "/", config.NymeriaConfig.URL.Domain, true, true)
c.SetCookie("sdslabs_session", sessionCookies[1], 3600, "/", config.NymeriaConfig.URL.Domain, true, true)
c.JSON(http.StatusOK, gin.H{
"status": "created",
"flowID": flowID,
})

}
20 changes: 4 additions & 16 deletions api/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@ import (

func HandleGetSettingsFlow(c *gin.Context) {
log.Logger.Debug("Get Settings")
session_cookie, err1 := c.Cookie("sdslabs_session")
recovery_cookie, err2 := c.Cookie("ory_kratos_session")

if err1 != nil && err2 != nil {
var err error
if err1 != nil {
err = err1
} else {
err = err2
}
session_cookie, err := c.Cookie("sdslabs_session")

if err != nil {
log.ErrorLogger("Initialize Settings Failed", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
c.JSON(errCode, gin.H{
Expand All @@ -34,7 +27,7 @@ func HandleGetSettingsFlow(c *gin.Context) {
return
}

flow, flow_cookie, err := settings.InitializeSettingsFlowWrapper(session_cookie, recovery_cookie)
flow, flow_cookie, err := settings.InitializeSettingsFlowWrapper(session_cookie)

if err != nil {
log.ErrorLogger("Initialize Settings flow Failed", err)
Expand All @@ -46,11 +39,6 @@ func HandleGetSettingsFlow(c *gin.Context) {

c.SetCookie("settings_flow", flow_cookie, 3600, "/", config.NymeriaConfig.URL.Domain, true, true)

if recovery_cookie != "" {
recovery_cookie = "ory_kratos_session=" + recovery_cookie
c.SetCookie("sdslabs_session", recovery_cookie, 900, "/", config.NymeriaConfig.URL.Domain, false, true)
}

flowID := flow.GetId()

var csrf_token string
Expand Down
2 changes: 1 addition & 1 deletion api/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func HandleStatus(c *gin.Context) {
}

apiClient := client.NewAPIClient(config.KratosClientConfig)
resp, _, err := apiClient.V0alpha2Api.ToSession(context.Background()).Cookie(cookie).Execute()
resp, _, err := apiClient.FrontendAPI.ToSession(context.Background()).Cookie(cookie).Execute()
if err != nil {
fmt.Println(err)
}
Expand Down
93 changes: 91 additions & 2 deletions api/verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,49 @@ func HandleGetVerificationFlow(c *gin.Context) {
})
}

func HandleGetVerificationAfterRegistrationFlow(c *gin.Context) {
var t verification.SubmitVerificationBody
err := c.Bind(&t)

if err != nil {
log.ErrorLogger("Unable to process json body", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to process json body",
})
return
}

cookie, err := c.Cookie("verification_flow")

if err != nil {
log.ErrorLogger("Cookie not found", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Cookie not found",
})
return
}

csrf_token, err := verification.InitializeVerificationAfterRegistrationFlowWrapper(cookie, t.FlowID)

if err != nil {
log.ErrorLogger("Initialize Verification Failed", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Initialize Verification Failed",
})
return
}

c.JSON(http.StatusOK, gin.H{
"csrf_token": csrf_token,
})
}

func HandlePostVerificationFlow(c *gin.Context) {
var t verification.SubmitVerificationBody
err := c.Bind(&t)
Expand All @@ -61,7 +104,53 @@ func HandlePostVerificationFlow(c *gin.Context) {
return
}

_, err = verification.SubmitVerificationFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Email)
var csrf_token string

csrf_token, err = verification.SubmitVerificationFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Email)

if err != nil {
log.ErrorLogger("Post Verification flow failed", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Post Settings flow failed",
})
return
}

c.JSON(http.StatusOK, gin.H{
"message": "Account Verification Mail Sent",
"csrf_token": csrf_token,
})
}

func HandlePostVerificationCodeFlow(c *gin.Context) {
var t verification.SubmitVerificationBody
err := c.Bind(&t)

if err != nil {
log.ErrorLogger("Unable to process json body", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to process json body",
})
return
}

cookie, err := c.Cookie("verification_flow")

if err != nil {
log.ErrorLogger("Cookie not found", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Cookie not found",
})
return
}

_, err = verification.SubmitVerificationCodeFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.VerificationCode)

if err != nil {
log.ErrorLogger("Post Verification flow failed", err)
Expand All @@ -74,7 +163,7 @@ func HandlePostVerificationFlow(c *gin.Context) {
}

c.JSON(http.StatusOK, gin.H{
"message": "Account Verification Mail Sent",
"message": "Account Verification Done",
})

}
58 changes: 31 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
module github.com/sdslabs/nymeria

go 1.18
go 1.23

require (
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.8.1
github.com/lib/pq v1.10.7
github.com/ory/client-go v0.2.0-alpha.60
github.com/sirupsen/logrus v1.9.0
github.com/gin-contrib/cors v1.7.2
github.com/gin-gonic/gin v1.10.0
github.com/lib/pq v1.10.9
github.com/ory/client-go v1.14.3
github.com/sirupsen/logrus v1.9.3
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
)
require github.com/rogpeppe/go-internal v1.12.0 // indirect

require (
github.com/bytedance/sonic v1.12.1 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
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/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/net v0.3.0 // indirect
golang.org/x/oauth2 v0.3.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.9.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading
Loading