From e7316de6529b4dd069bfaf914963bce14a22f173 Mon Sep 17 00:00:00 2001 From: Aryan Date: Tue, 24 Dec 2024 02:50:56 +0530 Subject: [PATCH] Adds error message extractor --- api/admin.go | 23 +++++---- api/application.go | 10 ++-- api/login.go | 12 +++-- api/logout.go | 13 +++-- api/mfa.go | 6 +-- api/profile.go | 7 ++- api/recovery.go | 17 +++---- api/registration.go | 22 +++++---- api/settings.go | 29 +++++------ api/verification.go | 17 +++---- helper/extracter.go | 49 +++++++++++++++++++ helper/types.go | 9 ++++ pkg/db/application.go | 4 +- pkg/middleware/app_authorization.go | 12 ++--- pkg/wrapper/kratos/login/login.go | 10 ++-- .../kratos/registration/registration.go | 13 +++-- 16 files changed, 156 insertions(+), 97 deletions(-) create mode 100644 helper/extracter.go create mode 100644 helper/types.go diff --git a/api/admin.go b/api/admin.go index 9807879..20956f4 100644 --- a/api/admin.go +++ b/api/admin.go @@ -5,11 +5,10 @@ import ( "fmt" "net/http" "os" - "strconv" - "strings" "github.com/gin-gonic/gin" + "github.com/sdslabs/nymeria/helper" "github.com/sdslabs/nymeria/log" "github.com/sdslabs/nymeria/pkg/middleware" "github.com/sdslabs/nymeria/pkg/wrapper/kratos/admin" @@ -24,7 +23,7 @@ func HandleCreateIdentityFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process JSON body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process JSON body", @@ -65,7 +64,7 @@ func HandleGetIdentityFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to convert map to json", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to convert map to json", @@ -80,7 +79,7 @@ func HandleGetIdentityFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to convert JSON to map", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to convert JSON to map", @@ -103,7 +102,7 @@ func HandleDeleteIdentityFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process JSON body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process JSON body", @@ -114,7 +113,7 @@ func HandleDeleteIdentityFlow(c *gin.Context) { session, err := middleware.GetSession(c) if err != nil { log.ErrorLogger("Unable to get session", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to get session", @@ -170,7 +169,7 @@ func HandleBanIdentity(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process JSON body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process JSON body", @@ -181,7 +180,7 @@ func HandleBanIdentity(c *gin.Context) { session, err := middleware.GetSession(c) if err != nil { log.ErrorLogger("Unable to get session", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to get session", @@ -232,7 +231,7 @@ func HandleRemoveBanIdentity(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process JSON body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process JSON body", @@ -273,7 +272,7 @@ func HandleRoleSwitch(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process JSON body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process JSON body", @@ -284,7 +283,7 @@ func HandleRoleSwitch(c *gin.Context) { session, err := middleware.GetSession(c) if err != nil { log.ErrorLogger("Unable to get session", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to get session", diff --git a/api/application.go b/api/application.go index b3ad702..8de3c53 100644 --- a/api/application.go +++ b/api/application.go @@ -18,7 +18,7 @@ func HandleGetApplication(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to get application data", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": strings.Split(err.Error(), " ")[1], "message": "Unable to get application data", @@ -39,7 +39,7 @@ func HandlePostApplication(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": strings.Split(err.Error(), " ")[1], "message": "Unable to process json body", @@ -73,7 +73,7 @@ func HandlePutApplication(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": strings.Split(err.Error(), " ")[1], "message": "Unable to process json body", @@ -107,7 +107,7 @@ func HandleDeleteApplication(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": strings.Split(err.Error(), " ")[1], "message": "Unable to process json body", @@ -141,7 +141,7 @@ func HandleUpdateClientSecret(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": strings.Split(err.Error(), " ")[1], "message": "Unable to process json body", diff --git a/api/login.go b/api/login.go index 457f0f7..c076044 100644 --- a/api/login.go +++ b/api/login.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/sdslabs/nymeria/config" + "github.com/sdslabs/nymeria/helper" "github.com/sdslabs/nymeria/log" "github.com/sdslabs/nymeria/pkg/wrapper/kratos/login" ) @@ -19,7 +20,8 @@ func HandleGetLoginFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Initialize Login Failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) + c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Initialize Login Failed", @@ -42,7 +44,7 @@ func HandlePostLoginFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process json body", @@ -55,7 +57,7 @@ func HandlePostLoginFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Cookie not found", @@ -63,7 +65,7 @@ func HandlePostLoginFlow(c *gin.Context) { return } - identity, session, err := login.SubmitLoginFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Password, t.Identifier) // _ is USERID + identity, session, errMsg, err := login.SubmitLoginFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Password, t.Identifier) // _ is USERID if session == "" { log.ErrorLogger("Post login flow failed", err) @@ -71,7 +73,7 @@ func HandlePostLoginFlow(c *gin.Context) { errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0]) c.JSON(errCode, gin.H{ "error": err.Error(), - "message": "Kratos post login flow failed", + "message": errMsg, }) return } diff --git a/api/logout.go b/api/logout.go index be24203..d0ea51f 100644 --- a/api/logout.go +++ b/api/logout.go @@ -2,12 +2,11 @@ package api import ( "net/http" - "strconv" - "strings" "github.com/gin-gonic/gin" "github.com/sdslabs/nymeria/config" + "github.com/sdslabs/nymeria/helper" "github.com/sdslabs/nymeria/log" "github.com/sdslabs/nymeria/pkg/wrapper/kratos/logout" ) @@ -17,7 +16,7 @@ func HandleGetLogoutFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Session cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Session cookie not found", @@ -29,7 +28,7 @@ func HandleGetLogoutFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Kratos get logout flow failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Kratos get logout flow failed", @@ -49,7 +48,7 @@ func HandlePostLogoutFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process json body", @@ -61,7 +60,7 @@ func HandlePostLogoutFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Session cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Session cookie not found", @@ -72,7 +71,7 @@ func HandlePostLogoutFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Kratos get logout flow failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Kratos get logout flow failed", diff --git a/api/mfa.go b/api/mfa.go index e70dd61..cdf7322 100644 --- a/api/mfa.go +++ b/api/mfa.go @@ -2,12 +2,12 @@ package api import ( "net/http" - "strconv" "strings" "github.com/gin-gonic/gin" "github.com/sdslabs/nymeria/config" + "github.com/sdslabs/nymeria/helper" "github.com/sdslabs/nymeria/log" "github.com/sdslabs/nymeria/pkg/wrapper/kratos/login" ) @@ -19,7 +19,7 @@ func HandleGetMFAFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Session Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Cookie not found", @@ -72,7 +72,7 @@ func HandlePostMFAFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Session Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Cookie not found", diff --git a/api/profile.go b/api/profile.go index bfbde85..ebf2572 100644 --- a/api/profile.go +++ b/api/profile.go @@ -2,11 +2,10 @@ package api import ( "net/http" - "strconv" - "strings" "github.com/gin-gonic/gin" + "github.com/sdslabs/nymeria/helper" "github.com/sdslabs/nymeria/log" "github.com/sdslabs/nymeria/pkg/middleware" ) @@ -15,7 +14,7 @@ func HandlePostProfile(c *gin.Context) { session, err := middleware.GetSession(c) if err != nil { log.ErrorLogger("Unable to get session", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to get session", @@ -33,7 +32,7 @@ func HandleGetVerifiedStatus(c *gin.Context) { session, err := middleware.GetSession(c) if err != nil { log.ErrorLogger("Unable to get session", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to get session", diff --git a/api/recovery.go b/api/recovery.go index 3bd6b03..0474b46 100644 --- a/api/recovery.go +++ b/api/recovery.go @@ -2,12 +2,11 @@ package api import ( "net/http" - "strconv" - "strings" "github.com/gin-gonic/gin" "github.com/sdslabs/nymeria/config" + "github.com/sdslabs/nymeria/helper" "github.com/sdslabs/nymeria/log" "github.com/sdslabs/nymeria/pkg/wrapper/kratos/recovery" ) @@ -19,7 +18,7 @@ func HandleGetRecoveryFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Initialize Recovery Failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Initialize Recovery Failed", @@ -41,7 +40,7 @@ func HandlePostRecoveryFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process json body", @@ -53,7 +52,7 @@ func HandlePostRecoveryFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Cookie not found", @@ -65,7 +64,7 @@ func HandlePostRecoveryFlow(c *gin.Context) { if err != nil { log.ErrorLogger("POST Recovery flow failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "POST Recovery flow failed", @@ -85,7 +84,7 @@ func HandlePostRecoveryCodeFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process json body", @@ -97,7 +96,7 @@ func HandlePostRecoveryCodeFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Recovery Flow Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Recovery Flow Cookie not found", @@ -109,7 +108,7 @@ func HandlePostRecoveryCodeFlow(c *gin.Context) { if err != nil { log.ErrorLogger("POST Recovery flow failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "POST Recovery Code flow failed", diff --git a/api/registration.go b/api/registration.go index 039c50a..6907ad0 100644 --- a/api/registration.go +++ b/api/registration.go @@ -2,12 +2,11 @@ package api import ( "net/http" - "strconv" - "strings" "github.com/gin-gonic/gin" "github.com/sdslabs/nymeria/config" + "github.com/sdslabs/nymeria/helper" "github.com/sdslabs/nymeria/log" "github.com/sdslabs/nymeria/pkg/wrapper/kratos/registration" "github.com/sdslabs/nymeria/pkg/wrapper/kratos/verification" @@ -18,7 +17,12 @@ func HandleGetRegistrationFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Kratos get registration flow failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) + + if errCode == 0 { + errCode = http.StatusInternalServerError + } + c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Kratos get registration flow failed", @@ -41,7 +45,7 @@ func HandlePostRegistrationFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process json body", @@ -53,7 +57,7 @@ func HandlePostRegistrationFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "cookie not found", @@ -61,14 +65,14 @@ func HandlePostRegistrationFlow(c *gin.Context) { return } - flowID, sessionCookies, err := registration.SubmitRegistrationFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Password, t.Traits) + flowID, sessionCookies, errMsg, err := registration.SubmitRegistrationFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Password, t.Traits) if err != nil { log.ErrorLogger("Kratos post registration flow failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), - "message": "Kratos post registration flow failed", + "message": errMsg, }) return } @@ -77,7 +81,7 @@ func HandlePostRegistrationFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Initialize Verification Failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Initialize Verification Failed", diff --git a/api/settings.go b/api/settings.go index df317f5..d2bd20d 100644 --- a/api/settings.go +++ b/api/settings.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/sdslabs/nymeria/config" + "github.com/sdslabs/nymeria/helper" "github.com/sdslabs/nymeria/log" "github.com/sdslabs/nymeria/pkg/middleware" "github.com/sdslabs/nymeria/pkg/wrapper/kratos/settings" @@ -19,7 +20,7 @@ func HandleGetSettingsFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Initialize Settings Failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Initialize Settings Failed", @@ -31,7 +32,7 @@ func HandleGetSettingsFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Initialize Settings flow Failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), }) @@ -85,7 +86,7 @@ func HandleUpdateProfile(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process json body", @@ -96,7 +97,7 @@ func HandleUpdateProfile(c *gin.Context) { session, err := middleware.GetSession(c) if err != nil { log.ErrorLogger("Unable to get session", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to get session", @@ -122,7 +123,7 @@ func HandleUpdateProfile(c *gin.Context) { if err != nil { log.ErrorLogger("Flow Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Cookie not found", @@ -134,7 +135,7 @@ func HandleUpdateProfile(c *gin.Context) { if err != nil { log.ErrorLogger("Session Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Cookie not found", @@ -167,7 +168,7 @@ func HandleChangePassword(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process json body", @@ -179,7 +180,7 @@ func HandleChangePassword(c *gin.Context) { if err != nil { log.ErrorLogger("Flow Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Cookie not found", @@ -191,7 +192,7 @@ func HandleChangePassword(c *gin.Context) { if err != nil { log.ErrorLogger("Session Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Cookie not found", @@ -215,7 +216,7 @@ func HandleChangePassword(c *gin.Context) { session, err := middleware.GetSession(c) if err != nil { log.ErrorLogger("Unable to get session", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to get session", @@ -253,7 +254,7 @@ func HandleToggleTOTP(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process json body", @@ -265,7 +266,7 @@ func HandleToggleTOTP(c *gin.Context) { if err != nil { log.ErrorLogger("Flow Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Cookie not found", @@ -277,7 +278,7 @@ func HandleToggleTOTP(c *gin.Context) { if err != nil { log.ErrorLogger("Session Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Cookie not found", @@ -302,7 +303,7 @@ func HandleToggleTOTP(c *gin.Context) { session, err := middleware.GetSession(c) if err != nil { log.ErrorLogger("Unable to get session", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to get session", diff --git a/api/verification.go b/api/verification.go index c873b09..37845d4 100644 --- a/api/verification.go +++ b/api/verification.go @@ -2,12 +2,11 @@ package api import ( "net/http" - "strconv" - "strings" "github.com/gin-gonic/gin" "github.com/sdslabs/nymeria/config" + "github.com/sdslabs/nymeria/helper" "github.com/sdslabs/nymeria/log" "github.com/sdslabs/nymeria/pkg/wrapper/kratos/verification" ) @@ -19,7 +18,7 @@ func HandleGetVerificationFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Initialize Verification Failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Initialize Verification Failed", @@ -41,7 +40,7 @@ func HandlePostVerificationFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process json body", @@ -53,7 +52,7 @@ func HandlePostVerificationFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Cookie not found", @@ -67,7 +66,7 @@ func HandlePostVerificationFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Post Verification flow failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Post Settings flow failed", @@ -87,7 +86,7 @@ func HandlePostVerificationCodeFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Unable to process json body", @@ -99,7 +98,7 @@ func HandlePostVerificationCodeFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Cookie not found", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": err.Error(), "message": "Cookie not found", @@ -111,7 +110,7 @@ func HandlePostVerificationCodeFlow(c *gin.Context) { if err != nil { log.ErrorLogger("Post Verification flow failed", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) if errCode == 0 { errCode = http.StatusBadRequest diff --git a/helper/extracter.go b/helper/extracter.go new file mode 100644 index 0000000..a228b6a --- /dev/null +++ b/helper/extracter.go @@ -0,0 +1,49 @@ +package helper + +import ( + "encoding/json" + "io" + "net/http" + "strconv" + "strings" + + "github.com/sdslabs/nymeria/log" +) + +func ExtractErrorCode(Error error) int { + errCode, err := strconv.Atoi(strings.Split(Error.Error(), " ")[0]) + + if err != nil { + log.ErrorLogger("Error code extractor failed: ", err) + return http.StatusInternalServerError + } + + if errCode == 0 { + return http.StatusInternalServerError + } + return errCode +} + +func ExtractErrorMessage(r *http.Response) string { + + body, err := io.ReadAll(r.Body) + + if err != nil { + log.ErrorLogger("Error message extractor failed: ", err) + return "Error" + } + + var resp HttpResponseBody + err = json.Unmarshal(body, &resp) + + if err != nil { + log.ErrorLogger("Error message extractor failed: ", err) + return "Error" + } + + if len(resp.UI.Messages) == 0 { + return "Error" + } + + return resp.UI.Messages[0].Text +} diff --git a/helper/types.go b/helper/types.go new file mode 100644 index 0000000..185e2e8 --- /dev/null +++ b/helper/types.go @@ -0,0 +1,9 @@ +package helper + +type HttpResponseBody struct { + UI struct { + Messages []struct { + Text string `json:"text"` + } `json:"messages"` + } `json:"ui"` +} diff --git a/pkg/db/application.go b/pkg/db/application.go index 5726c54..7d96c9a 100644 --- a/pkg/db/application.go +++ b/pkg/db/application.go @@ -1,8 +1,6 @@ package db -import ( - "github.com/sdslabs/nymeria/helper" -) +import "github.com/sdslabs/nymeria/helper" func CreateApplication(name string, redirectURL string, allowedDomains string, organization string, clientKey string, clientSecret string) error { sqlStatement := `INSERT INTO application (name, redirect_url, allowed_domains, organization, created_at, client_key, client_secret) VALUES ($1, $2, $3, $4, now(), $5,$6);` diff --git a/pkg/middleware/app_authorization.go b/pkg/middleware/app_authorization.go index b94c58e..f73bc98 100644 --- a/pkg/middleware/app_authorization.go +++ b/pkg/middleware/app_authorization.go @@ -1,11 +1,11 @@ package middleware import ( - "strconv" "strings" "github.com/gin-gonic/gin" + "github.com/sdslabs/nymeria/helper" "github.com/sdslabs/nymeria/log" "github.com/sdslabs/nymeria/pkg/db" ) @@ -15,7 +15,7 @@ func HandleAppAuthorization(c *gin.Context) { err := c.BindJSON(&body) if err != nil { log.ErrorLogger("Unable to process json body", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": strings.Split(err.Error(), " ")[1], "message": "Unable to process json body", @@ -25,7 +25,7 @@ func HandleAppAuthorization(c *gin.Context) { app, err := db.GetApplication(body.ClientKey, body.ClientSecret) if err != nil { log.ErrorLogger("Unable to get application", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": strings.Split(err.Error(), " ")[1], "message": "Internal Server Error", @@ -34,7 +34,7 @@ func HandleAppAuthorization(c *gin.Context) { } if app.RedirectURL != body.RedirectURL { log.ErrorLogger("Redirect URL does not match", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": strings.Split(err.Error(), " ")[1], "message": "Redirect URL does not match", @@ -43,7 +43,7 @@ func HandleAppAuthorization(c *gin.Context) { } if app.ClientKey != body.ClientKey { log.ErrorLogger("Client Key does not match", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": strings.Split(err.Error(), " ")[1], "message": "Unauthorized", @@ -52,7 +52,7 @@ func HandleAppAuthorization(c *gin.Context) { } if app.ClientSecret != body.ClientSecret { log.ErrorLogger("Client Secret does not match", err) - errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + errCode := helper.ExtractErrorCode(err) c.JSON(errCode, gin.H{ "error": strings.Split(err.Error(), " ")[1], "message": "Unauthorized", diff --git a/pkg/wrapper/kratos/login/login.go b/pkg/wrapper/kratos/login/login.go index ddbd3d6..6d443b6 100644 --- a/pkg/wrapper/kratos/login/login.go +++ b/pkg/wrapper/kratos/login/login.go @@ -6,6 +6,7 @@ import ( client "github.com/ory/client-go" "github.com/sdslabs/nymeria/config" + "github.com/sdslabs/nymeria/helper" ) func InitializeLoginFlowWrapper(aal string, cookie string) (string, string, string, error) { @@ -34,7 +35,7 @@ func InitializeLoginFlowWrapper(aal string, cookie string) (string, string, stri return setCookie, resp.Id, csrf_token, nil } -func SubmitLoginFlowWrapper(cookie string, flowID string, csrfToken string, pass string, identifier string) (client.Session, string, error) { +func SubmitLoginFlowWrapper(cookie string, flowID string, csrfToken string, pass string, identifier string) (client.Session, string, string, error) { submitDataBody := client.UpdateLoginFlowBody{UpdateLoginFlowWithPasswordMethod: client.NewUpdateLoginFlowWithPasswordMethod(identifier, "password", pass)} // SubmitSelfServiceLoginFlowBody | submitDataBody.UpdateLoginFlowWithPasswordMethod.SetCsrfToken(csrfToken) @@ -47,12 +48,13 @@ func SubmitLoginFlowWrapper(cookie string, flowID string, csrfToken string, pass if err != nil { if responseCookies == nil { - return *client.NewSessionWithDefaults(), "", err + msg := helper.ExtractErrorMessage(r) + return *client.NewSessionWithDefaults(), "", msg, err } - return *client.NewSessionWithDefaults(), responseCookies[1], err + return *client.NewSessionWithDefaults(), responseCookies[1], "", err } - return resp.Session, responseCookies[1], nil + return resp.Session, responseCookies[1], "", nil } func SubmitLoginWithMFAWrapper(cookie string, flowID string, csrfToken string, totp string) (client.Session, string, error) { diff --git a/pkg/wrapper/kratos/registration/registration.go b/pkg/wrapper/kratos/registration/registration.go index ed82ab2..5c476e1 100644 --- a/pkg/wrapper/kratos/registration/registration.go +++ b/pkg/wrapper/kratos/registration/registration.go @@ -2,12 +2,11 @@ package registration import ( "context" - "fmt" - "os" client "github.com/ory/client-go" "github.com/sdslabs/nymeria/config" + "github.com/sdslabs/nymeria/helper" "github.com/sdslabs/nymeria/pkg/middleware" ) @@ -36,7 +35,7 @@ func InitializeRegistrationFlowWrapper() (string, string, string, error) { return setCookie, resp.Id, csrf_token, nil } -func SubmitRegistrationFlowWrapper(cookie string, flowID string, csrfToken string, password string, data Traits) (string, []string, error) { +func SubmitRegistrationFlowWrapper(cookie string, flowID string, csrfToken string, password string, data Traits) (string, []string, string, error) { timeStamp := middleware.CurrentTimeStamp() trait := map[string]interface{}{ "email": data.Email, @@ -58,12 +57,12 @@ func SubmitRegistrationFlowWrapper(cookie string, flowID string, csrfToken strin resp, r, err := apiClient.FrontendAPI.UpdateRegistrationFlow(context.Background()).Flow(flowID).UpdateRegistrationFlowBody(submitDataBody).Cookie(cookie).Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.SubmitSelfServiceRegistrationFlow``: %v\n", err) - fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r) - return "", nil, err + msg := helper.ExtractErrorMessage(r) + + return "", nil, msg, err } responseCookies := r.Header["Set-Cookie"] - return resp.GetContinueWith()[1].ContinueWithVerificationUi.GetFlow().Id, responseCookies, nil + return resp.GetContinueWith()[1].ContinueWithVerificationUi.GetFlow().Id, responseCookies, "", nil }