Skip to content

Commit

Permalink
Merge pull request #40 from sdslabs/VettelMajor
Browse files Browse the repository at this point in the history
Restructures Admin Backend
  • Loading branch information
itsdarshankumar authored Dec 14, 2023
2 parents 986292c + db5776e commit e02c324
Show file tree
Hide file tree
Showing 14 changed files with 357 additions and 46 deletions.
200 changes: 200 additions & 0 deletions api/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package api

import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"

"github.com/gin-gonic/gin"

"github.com/sdslabs/nymeria/log"
"github.com/sdslabs/nymeria/pkg/wrapper/kratos/admin"
)

func HandleCreateIdentityFlow(c *gin.Context) {

var t admin.Identity

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
}

var mappedJsonIdentity map[string]interface{}

data, err := json.Marshal(t)

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
}

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",
})
return
}
c.JSON(http.StatusOK, gin.H{
"identity": createdIdentity.Id,
})
}

func HandleGetIdentityFlow(c *gin.Context) {
createdIdentity := c.Query("identity")
getIdentity, r, err := admin.GetIdentityFlowWrapper(createdIdentity)

if err != nil {
log.ErrorLogger("Error while calling `AdminGetIdentity`", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Internal server error",
})
return
}

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

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,
"Traits": identity,
})
}

func HandleDeleteIdentityFlow(c *gin.Context) {

var t IdentityBody
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
}

r, err := admin.DeleteIdentityFlowWrapper(t.Identity)

if err != nil {
log.ErrorLogger("Error while calling `AdminDeleteIdentity`", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "INternal server error",
})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "removed identity",
})
}

func HandleListIdentity(c *gin.Context) {
identities, r, err := admin.ListIdentityFlowWrapper()
if err != nil {
log.ErrorLogger("Error while calling `AdminListIdentities`", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Internal server error",
})

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

func HandleBanIdentity(c *gin.Context) {
var t IdentityBody
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
}

id, r, err := admin.BanIdentityFlowWrapper(t.Identity)

if err != nil {
log.ErrorLogger("Error while calling `AdminPatchIdentities`", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"identities": id,
})
}
2 changes: 1 addition & 1 deletion api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func HandleGetLoginFlow(c *gin.Context) {
log.Logger.Debug("Get Login")
cookie, flowID, csrf_token, err := login.InitializeLoginFlowWrapper("aal1")
cookie, flowID, csrf_token, err := login.InitializeLoginFlowWrapper("aal1", "")

if err != nil {
log.ErrorLogger("Initialize Login Failed", err)
Expand Down
11 changes: 5 additions & 6 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"

c "github.com/sdslabs/nymeria/pkg/controller/admin"
"github.com/sdslabs/nymeria/pkg/middleware"
)

Expand Down Expand Up @@ -37,11 +36,11 @@ func Start() {
r.GET("/mfa", HandleGetMFAFlow)
r.POST("/mfa", HandlePostMFAFlow)

r.POST("/create-identity", c.CreateIdentity)
r.GET("/get-identity", c.GetIdentity)
r.POST("/delete-identity", c.DeleteIdentity)
r.GET("/list-identity", c.ListIdentity)
r.PUT("/update-identity/ban", c.UpdateBanIdentity)
r.POST("/create-identity", HandleCreateIdentityFlow)
r.GET("/get-identity", HandleGetIdentityFlow)
r.POST("/delete-identity", HandleDeleteIdentityFlow)
r.GET("/list-identity", HandleListIdentity)
r.PUT("/update-identity/ban", HandleBanIdentity)

r.GET("/register", HandleGetRegistrationFlow)
r.POST("/register", HandlePostRegistrationFlow)
Expand Down
35 changes: 33 additions & 2 deletions api/mfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package api

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

"github.com/gin-gonic/gin"

Expand All @@ -12,7 +14,20 @@ import (

func HandleGetMFAFlow(c *gin.Context) {
log.Logger.Debug("Get MFA")
flow_cookie, flowID, csrf_token, err := login.InitializeLoginFlowWrapper("aal2")
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 {
log.ErrorLogger("Initialize MFA Failed", err)
Expand Down Expand Up @@ -52,7 +67,23 @@ func HandlePostMFAFlow(c *gin.Context) {
return
}

identity, session, err := login.SubmitLoginWithMFAWrapper(flow_cookie, req_body.FlowID, req_body.CsrfToken, req_body.TOTP)
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

identity, session, err := login.SubmitLoginWithMFAWrapper(cookie, req_body.FlowID, req_body.CsrfToken, req_body.TOTP)

if err != nil {
log.ErrorLogger("Kratos post MFA flow failed", err)
Expand Down
4 changes: 4 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ type ApplicationPutBody struct {
type ApplicationBody struct {
ID int `json:"id"`
}

type IdentityBody struct {
Identity string `json:"identity"`
}
20 changes: 11 additions & 9 deletions config.sample.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
env: dev # dev|prod

url:
frontend_url: "http://localhost:4455"
kratos_url: "http://localhost:4433"
domain: "https://someaddress.com"
frontend_url: "http://localhost:4455"
kratos_url: "http://localhost:4433"
admin_kratos_url: "http://localhost:4434"

domain: "https://someaddress.com"

db:
dsn: ""
host: "localhost"
port: 5432
user: "postgres"
password: "pass"
db_name: "kratos_db"
dsn: ""
host: "localhost"
port: 5432
user: "postgres"
password: "pass"
db_name: "kratos_db"
14 changes: 13 additions & 1 deletion config/kratos.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ func getKratosClientConfig() *client.Configuration {
return configuration
}

func getKratosClientConfigAdmin() *client.Configuration {
configuration := client.NewConfiguration()
configuration.Servers = []client.ServerConfiguration{
{
URL: NymeriaConfig.URL.AdminKratosURL,
},
}

return configuration
}

var (
KratosClientConfig = getKratosClientConfig()
KratosClientConfig = getKratosClientConfig()
KratosClientConfigAdmin = getKratosClientConfigAdmin()
)
7 changes: 4 additions & 3 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ type NymeriaCfg struct {
DB DB `yaml:"db"`
}
type URL struct {
FrontendURL string `yaml:"frontend_url"`
KratosURL string `yaml:"kratos_url"`
Domain string `yaml:"domain"`
FrontendURL string `yaml:"frontend_url"`
KratosURL string `yaml:"kratos_url"`
AdminKratosURL string `yaml:"admin_kratos_url"`
Domain string `yaml:"domain"`
}

type DB struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/sdslabs/nymeria

go 1.17
go 1.18

require (
github.com/gin-contrib/cors v1.4.0
Expand Down
Loading

0 comments on commit e02c324

Please sign in to comment.