Skip to content

Commit bb8d294

Browse files
authored
Merge pull request #45 from sdslabs/Update-Kratos
Fix error messages.
2 parents d0aa79c + 108488c commit bb8d294

File tree

13 files changed

+380
-134
lines changed

13 files changed

+380
-134
lines changed

api/application.go

+23-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package api
22

33
import (
44
"net/http"
5-
"strconv"
65
"strings"
76

87
"github.com/gin-gonic/gin"
@@ -17,11 +16,11 @@ func HandleGetApplication(c *gin.Context) {
1716

1817
if err != nil {
1918
log.ErrorLogger("Unable to get application data", err)
20-
2119
errCode := helper.ExtractErrorCode(err)
20+
errStr := helper.ExtractErrorString(err)
2221
c.JSON(errCode, gin.H{
23-
"error": strings.Split(err.Error(), " ")[1],
24-
"message": "Unable to get application data",
22+
"error": http.StatusText(errCode),
23+
"message": "Unable to get application data: " + errStr,
2524
})
2625
return
2726
}
@@ -37,11 +36,11 @@ func HandlePostApplication(c *gin.Context) {
3736
err := c.BindJSON(&body)
3837

3938
if err != nil {
40-
log.ErrorLogger("Unable to process json body", err)
41-
4239
errCode := helper.ExtractErrorCode(err)
40+
41+
log.ErrorLogger("Couldn't create application.", err)
4342
c.JSON(errCode, gin.H{
44-
"error": strings.Split(err.Error(), " ")[1],
43+
"error": http.StatusText(errCode),
4544
"message": "Unable to process json body",
4645
})
4746
return
@@ -52,10 +51,11 @@ func HandlePostApplication(c *gin.Context) {
5251
if err != nil {
5352
log.ErrorLogger("Create application failed", err)
5453

55-
errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
54+
errCode := helper.ExtractErrorCode(err)
55+
errStr := helper.ExtractErrorString(err)
5656
c.JSON(errCode, gin.H{
57-
"error": strings.Split(err.Error(), " ")[1],
58-
"message": "Create application failed",
57+
"error": http.StatusText(errCode),
58+
"message": "Create application failed: " + errStr,
5959
})
6060
return
6161
}
@@ -75,7 +75,7 @@ func HandlePutApplication(c *gin.Context) {
7575

7676
errCode := helper.ExtractErrorCode(err)
7777
c.JSON(errCode, gin.H{
78-
"error": strings.Split(err.Error(), " ")[1],
78+
"error": http.StatusText(errCode),
7979
"message": "Unable to process json body",
8080
})
8181
return
@@ -86,10 +86,11 @@ func HandlePutApplication(c *gin.Context) {
8686
if err != nil {
8787
log.ErrorLogger("Update application failed", err)
8888

89-
errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
89+
errCode := helper.ExtractErrorCode(err)
90+
errStr := helper.ExtractErrorString(err)
9091
c.JSON(errCode, gin.H{
91-
"error": strings.Split(err.Error(), " ")[1],
92-
"message": "Update application failed",
92+
"error": http.StatusText(errCode),
93+
"message": "Update application failed: " + errStr,
9394
})
9495
return
9596
}
@@ -109,7 +110,7 @@ func HandleDeleteApplication(c *gin.Context) {
109110

110111
errCode := helper.ExtractErrorCode(err)
111112
c.JSON(errCode, gin.H{
112-
"error": strings.Split(err.Error(), " ")[1],
113+
"error": http.StatusText(errCode),
113114
"message": "Unable to process json body",
114115
})
115116
return
@@ -120,16 +121,17 @@ func HandleDeleteApplication(c *gin.Context) {
120121
if err != nil {
121122
log.ErrorLogger("Delete application failed", err)
122123

123-
errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
124+
errCode := helper.ExtractErrorCode(err)
125+
errStr := helper.ExtractErrorString(err)
124126
c.JSON(errCode, gin.H{
125-
"error": strings.Split(err.Error(), " ")[1],
126-
"message": "Delete application failed",
127+
"error": http.StatusText(errCode),
128+
"message": "Delete application failed: " + errStr,
127129
})
128130
return
129131
}
130132

131133
c.JSON(http.StatusOK, gin.H{
132-
"status": "application deleted",
134+
"message": "application deleted",
133135
})
134136

135137
}
@@ -154,7 +156,7 @@ func HandleUpdateClientSecret(c *gin.Context) {
154156
if err != nil {
155157
log.ErrorLogger("Client Secret update failed", err)
156158

157-
errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
159+
errCode := helper.ExtractErrorCode(err)
158160
c.JSON(errCode, gin.H{
159161
"error": strings.Split(err.Error(), " ")[1],
160162
"message": "Client Secret update failed",
@@ -163,7 +165,7 @@ func HandleUpdateClientSecret(c *gin.Context) {
163165
}
164166

165167
c.JSON(http.StatusOK, gin.H{
166-
"status": "Client Secret updated successfully",
168+
"message": "Client Secret updated successfully",
167169
})
168170

169171
}

api/login.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package api
22

33
import (
44
"net/http"
5-
"strconv"
6-
"strings"
75

86
"github.com/gin-gonic/gin"
97

@@ -70,7 +68,7 @@ func HandlePostLoginFlow(c *gin.Context) {
7068
if session == "" {
7169
log.ErrorLogger("Post login flow failed", err)
7270

73-
errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
71+
errCode := helper.ExtractErrorCode(err)
7472
c.JSON(errCode, gin.H{
7573
"error": err.Error(),
7674
"message": errMsg,

api/mfa.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ func HandlePostMFAFlow(c *gin.Context) {
8383
csrfToken := req_body.CsrfToken
8484
cookie := strings.Split(flow_cookie, ";")[0] + "; " + strings.Split(session_cookie, ";")[0] + "; x-csrf-token=" + csrfToken
8585

86-
identity, session, err := login.SubmitLoginWithMFAWrapper(cookie, req_body.FlowID, req_body.CsrfToken, req_body.TOTP)
86+
identity, session, errMsg, err := login.SubmitLoginWithMFAWrapper(cookie, req_body.FlowID, req_body.CsrfToken, req_body.TOTP)
8787

8888
if err != nil {
8989
log.ErrorLogger("Kratos post MFA flow failed", err)
9090
c.JSON(http.StatusInternalServerError, gin.H{
91-
"error": "internal server error",
91+
"error": "MFA Failed",
92+
"message": errMsg,
9293
})
9394
return
9495
}

api/recovery.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func HandlePostRecoveryCodeFlow(c *gin.Context) {
104104
return
105105
}
106106

107-
session, err := recovery.SubmitRecoveryCodeFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.RecoveryCode)
107+
session, errMsg, err := recovery.SubmitRecoveryCodeFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.RecoveryCode)
108108

109109
if err != nil {
110110
log.ErrorLogger("POST Recovery flow failed", err)
@@ -116,6 +116,14 @@ func HandlePostRecoveryCodeFlow(c *gin.Context) {
116116
return
117117
}
118118

119+
if session == "" {
120+
c.JSON(http.StatusBadRequest, gin.H{
121+
"error": errMsg,
122+
"message": "POST Recovery Code flow failed",
123+
})
124+
return
125+
}
126+
119127
c.SetCookie("sdslabs_session", session, 3600, "/", config.NymeriaConfig.URL.Domain, true, true)
120128

121129
c.JSON(http.StatusOK, gin.H{

api/settings.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package api
22

33
import (
44
"net/http"
5-
"strconv"
6-
"strings"
75

86
"github.com/gin-gonic/gin"
97

@@ -148,7 +146,7 @@ func HandleUpdateProfile(c *gin.Context) {
148146
if err != nil {
149147
log.ErrorLogger("Kratos post settings update profile flow failed", err)
150148

151-
errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
149+
errCode := helper.ExtractErrorCode(err)
152150
c.JSON(errCode, gin.H{
153151
"error": err.Error(),
154152
"message": "Kratos post settings update profile flow failed",
@@ -205,10 +203,10 @@ func HandleChangePassword(c *gin.Context) {
205203
if err != nil {
206204
log.ErrorLogger("Kratos post settings change password flow failed", err)
207205

208-
errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
206+
errCode := helper.ExtractErrorCode(err)
209207
c.JSON(errCode, gin.H{
210208
"error": err.Error(),
211-
"message": "Kratos post settings change password flow failed",
209+
"message": msg,
212210
})
213211
return
214212
}
@@ -235,7 +233,7 @@ func HandleChangePassword(c *gin.Context) {
235233
if err != nil {
236234
log.ErrorLogger("Kratos post settings update profile flow failed", err)
237235

238-
errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
236+
errCode := helper.ExtractErrorCode(err)
239237
c.JSON(errCode, gin.H{
240238
"error": err.Error(),
241239
"message": "Kratos post settings update profile flow failed",
@@ -291,10 +289,10 @@ func HandleToggleTOTP(c *gin.Context) {
291289
if err != nil {
292290
log.ErrorLogger("Kratos post settings toggle totp flow failed", err)
293291

294-
errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
292+
errCode := helper.ExtractErrorCode(err)
295293
c.JSON(errCode, gin.H{
296294
"error": err.Error(),
297-
"message": "Kratos post settings toggle totp flow failed",
295+
"message": msg,
298296
})
299297
return
300298
}
@@ -323,7 +321,7 @@ func HandleToggleTOTP(c *gin.Context) {
323321
if err != nil {
324322
log.ErrorLogger("Kratos post settings update profile flow failed", err)
325323

326-
errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
324+
errCode := helper.ExtractErrorCode(err)
327325
c.JSON(errCode, gin.H{
328326
"error": err.Error(),
329327
"message": "Kratos post settings update profile flow failed",

go.mod

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ module github.com/sdslabs/nymeria
33
go 1.23
44

55
require (
6-
github.com/gin-contrib/cors v1.7.2
6+
github.com/gin-contrib/cors v1.7.3
77
github.com/gin-gonic/gin v1.10.0
88
github.com/lib/pq v1.10.9
9-
github.com/ory/client-go v1.14.3
9+
github.com/ory/client-go v1.15.17
1010
github.com/sirupsen/logrus v1.9.3
1111
gopkg.in/yaml.v2 v2.4.0
1212
)
1313

1414
require github.com/rogpeppe/go-internal v1.12.0 // indirect
1515

1616
require (
17-
github.com/bytedance/sonic v1.12.1 // indirect
18-
github.com/bytedance/sonic/loader v0.2.0 // indirect
17+
github.com/bytedance/sonic v1.12.6 // indirect
18+
github.com/bytedance/sonic/loader v0.2.1 // indirect
1919
github.com/cloudwego/base64x v0.1.4 // indirect
2020
github.com/cloudwego/iasm v0.2.0 // indirect
21-
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
22-
github.com/gin-contrib/sse v0.1.0 // indirect
21+
github.com/gabriel-vasile/mimetype v1.4.7 // indirect
22+
github.com/gin-contrib/sse v1.0.0 // indirect
2323
github.com/go-playground/locales v0.14.1 // indirect
2424
github.com/go-playground/universal-translator v0.18.1 // indirect
25-
github.com/go-playground/validator/v10 v10.22.0 // indirect
26-
github.com/goccy/go-json v0.10.3 // indirect
25+
github.com/go-playground/validator/v10 v10.23.0 // indirect
26+
github.com/goccy/go-json v0.10.4 // indirect
2727
github.com/json-iterator/go v1.1.12 // indirect
28-
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
28+
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
2929
github.com/kr/text v0.2.0 // indirect
3030
github.com/leodido/go-urn v1.4.0 // indirect
3131
github.com/mattn/go-isatty v0.0.20 // indirect
@@ -34,12 +34,12 @@ require (
3434
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
3535
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
3636
github.com/ugorji/go/codec v1.2.12 // indirect
37-
golang.org/x/arch v0.9.0 // indirect
38-
golang.org/x/crypto v0.26.0 // indirect
39-
golang.org/x/net v0.28.0 // indirect
40-
golang.org/x/oauth2 v0.22.0 // indirect
41-
golang.org/x/sys v0.24.0 // indirect
42-
golang.org/x/text v0.17.0 // indirect
43-
google.golang.org/protobuf v1.34.2 // indirect
37+
golang.org/x/arch v0.12.0 // indirect
38+
golang.org/x/crypto v0.31.0 // indirect
39+
golang.org/x/net v0.33.0 // indirect
40+
golang.org/x/oauth2 v0.24.0 // indirect
41+
golang.org/x/sys v0.28.0 // indirect
42+
golang.org/x/text v0.21.0 // indirect
43+
google.golang.org/protobuf v1.36.1 // indirect
4444
gopkg.in/yaml.v3 v3.0.1 // indirect
4545
)

0 commit comments

Comments
 (0)