Skip to content

Commit

Permalink
PR review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryan51203 committed Dec 12, 2023
1 parent 5162cbe commit d637b3b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 21 deletions.
74 changes: 56 additions & 18 deletions api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/gin-gonic/gin"
client "github.com/ory/client-go"

"github.com/sdslabs/nymeria/log"
"github.com/sdslabs/nymeria/pkg/wrapper/kratos/admin"
Expand All @@ -22,33 +21,51 @@ func HandleCreateIdentityFlow(c *gin.Context) {
err := c.BindJSON(&t)

if err != nil {
log.ErrorLogger("Unable to process json body", err)
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",
"message": "Unable to process JSON body",
})
return
}

var mappedJsonIdentity map[string]interface{}

data, _ := json.Marshal(t)
json.Unmarshal(data, &mappedJsonIdentity)
data, err := json.Marshal(t)

adminCreateIdentityBody := *client.NewAdminCreateIdentityBody(
"default",
mappedJsonIdentity,
) // AdminCreateIdentityBody | (optional)
if err != nil {
log.ErrorLogger("Unable to convert map to json", err)

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

createdIdentity, r, err := admin.CreateIdentityFlowWrapper(adminCreateIdentityBody)
err = json.Unmarshal(data, &mappedJsonIdentity)

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(mappedJsonIdentity)

if err != nil {
log.ErrorLogger("Error while calling `AdminCreateIdentity`", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "INternal server error",
"error": "Internal server error",
})
return
}
Expand All @@ -70,13 +87,34 @@ func HandleGetIdentityFlow(c *gin.Context) {
return
}

jsonString, _ := json.Marshal(getIdentity.Traits)
jsonString, err := json.Marshal(getIdentity.Traits)

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

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

var identity admin.Identity

if err := json.Unmarshal(jsonString, &identity); err != nil {
fmt.Println(err)
err = json.Unmarshal(jsonString, &identity)

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
}

fmt.Fprintf(os.Stdout, "Identity details for id %v. Traits: %v\n", createdIdentity, identity)
c.JSON(http.StatusOK, gin.H{
"Identity": createdIdentity,
Expand All @@ -90,12 +128,12 @@ func HandleDeleteIdentityFlow(c *gin.Context) {
err := c.BindJSON(&t)

if err != nil {
log.ErrorLogger("Unable to process json body", err)
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",
"message": "Unable to process JSON body",
})
return
}
Expand Down Expand Up @@ -136,12 +174,12 @@ func HandleBanIdentity(c *gin.Context) {
err := c.BindJSON(&t)

if err != nil {
log.ErrorLogger("Unable to process json body", err)
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",
"message": "Unable to process JSON body",
})
return
}
Expand Down
30 changes: 28 additions & 2 deletions api/mfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"net/http"
"strconv"
"strings"

"github.com/gin-gonic/gin"
Expand All @@ -13,7 +14,19 @@ import (

func HandleGetMFAFlow(c *gin.Context) {
log.Logger.Debug("Get MFA")
cookie, _ := c.Cookie("sdslabs_session")
cookie, err := c.Cookie("sdslabs_session")

if err != nil {
log.ErrorLogger("Session 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
}

flow_cookie, flowID, csrf_token, err := login.InitializeLoginFlowWrapper("aal2", cookie)

if err != nil {
Expand Down Expand Up @@ -53,7 +66,20 @@ func HandlePostMFAFlow(c *gin.Context) {
})
return
}
session_cookie, _ := c.Cookie("sdslabs_session")

session_cookie, err := c.Cookie("sdslabs_session")

if err != nil {
log.ErrorLogger("Session 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
}

csrfToken := req_body.CsrfToken
cookie := strings.Split(flow_cookie, ";")[0] + "; " + strings.Split(session_cookie, ";")[0] + "; x-csrf-token=" + csrfToken

Expand Down
8 changes: 7 additions & 1 deletion pkg/wrapper/kratos/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import (
"github.com/sdslabs/nymeria/config"
)

func CreateIdentityFlowWrapper(adminCreateIdentityBody client.AdminCreateIdentityBody) (*client.Identity, *http.Response, error) {
func CreateIdentityFlowWrapper(identityMap map[string]interface{}) (*client.Identity, *http.Response, error) {
apiClient := client.NewAPIClient(config.KratosClientConfigAdmin)

adminCreateIdentityBody := *client.NewAdminCreateIdentityBody(
"default",
identityMap,
) // AdminCreateIdentityBody | (optional)

createdIdentity, r, err := apiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute()

return createdIdentity, r, err
Expand Down

0 comments on commit d637b3b

Please sign in to comment.