Skip to content

Commit

Permalink
Restructures Admin Backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryan51203 committed Nov 29, 2023
1 parent 986292c commit 008e54b
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 19 deletions.
162 changes: 162 additions & 0 deletions api/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package api

import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"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"
)

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, _ := json.Marshal(t)
json.Unmarshal(data, &mappedJsonIdentity)

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

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

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, _ := json.Marshal(getIdentity.Traits)

var identity admin.Identity

if err := json.Unmarshal(jsonString, &identity); err != nil {
fmt.Println(err)
}
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,
})
}
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
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
58 changes: 58 additions & 0 deletions pkg/wrapper/kratos/admin/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package admin

import (
"context"
"net/http"

"github.com/ory/client-go"

"github.com/sdslabs/nymeria/config"
)

func CreateIdentityFlowWrapper(adminCreateIdentityBody client.AdminCreateIdentityBody) (*client.Identity, *http.Response, error) {
apiClient := client.NewAPIClient(config.KratosClientConfigAdmin)
createdIdentity, r, err := apiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute()

return createdIdentity, r, err
}

func GetIdentityFlowWrapper(createdIdentity string) (*client.Identity, *http.Response, error) {
apiClient := client.NewAPIClient(config.KratosClientConfigAdmin)

getIdentity, r, err := apiClient.V0alpha2Api.AdminGetIdentity(context.Background(), createdIdentity).Execute()

return getIdentity, r, err
}

func DeleteIdentityFlowWrapper(identity string) (*http.Response, error) {
apiClient := client.NewAPIClient(config.KratosClientConfigAdmin)

r, err := apiClient.V0alpha2Api.AdminDeleteIdentity(context.Background(), identity).Execute()

return r, err
}

func ListIdentityFlowWrapper() ([]client.Identity, *http.Response, error) {
apiClient := client.NewAPIClient(config.KratosClientConfigAdmin)

identities, r, err := apiClient.V0alpha2Api.AdminListIdentities(context.Background()).Execute()

return identities, r, err

}

func BanIdentityFlowWrapper(identity string) (*client.Identity, *http.Response, error) {
apiClient := client.NewAPIClient(config.KratosClientConfigAdmin)

jsonPatch := []client.JsonPatch{
{
From: nil,
Op: "replace",
Path: "/active",
Value: false,
},
}
id, r, err := apiClient.V0alpha2Api.AdminPatchIdentity(context.Background(), identity).JsonPatch(jsonPatch).Execute()

return id, r, err
}
14 changes: 14 additions & 0 deletions pkg/wrapper/kratos/admin/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package admin

type Identity struct {
Name string `json:"name"`
Email string `json:"email"`
Phone_number string `json:"phone_number"`
Password string `json:"password"`
Image_url string `json:"img_url"`
Active bool `json:"active"`
Verified bool `json:"verified"`
Role string `json:"role"`
Created_at string `json:"created_at"`
Totp_enabled bool `json:"totp_enabled"`
}

0 comments on commit 008e54b

Please sign in to comment.