From 7abe102ccebfa952c246745c314e591a68bd9bb7 Mon Sep 17 00:00:00 2001 From: Aryan Singh Date: Sun, 26 Nov 2023 20:36:01 +0530 Subject: [PATCH] Restructures Admin Backend --- api/admin.go | 122 ++++++++++++++++++++++++++++++ api/main.go | 11 ++- api/types.go | 15 ++++ pkg/wrapper/kratos/admin/admin.go | 91 ++++++++++++++++++++++ 4 files changed, 233 insertions(+), 6 deletions(-) create mode 100644 api/admin.go create mode 100644 pkg/wrapper/kratos/admin/admin.go diff --git a/api/admin.go b/api/admin.go new file mode 100644 index 0000000..08b27d0 --- /dev/null +++ b/api/admin.go @@ -0,0 +1,122 @@ +package api + +import ( + "encoding/json" + "fmt" + "net/http" + "os" + "strconv" + + "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) { + + id, _ := strconv.Atoi(c.PostForm("id")) + verified, _ := strconv.Atoi(c.PostForm("verified")) + active, _ := strconv.ParseBool(c.PostForm("active")) + totp_enabled, _ := strconv.ParseBool(c.PostForm("totp_enabled")) + adminCreateIdentityBody := *client.NewAdminCreateIdentityBody( + "default", + map[string]interface{}{ + "id": id, + "name": c.PostForm("name"), + "email": c.PostForm("email"), + "phone_number": c.PostForm("phone_number"), + "password": c.PostForm("password"), + "img_url": c.PostForm("img_url"), + "active": active, + "verified": verified, + "role": c.PostForm("role"), + "created_at": c.PostForm("created_at"), + "totp_enabled": totp_enabled, + }, + ) // AdminCreateIdentityBody | (optional) + + createdIdentity,r,err := admin.InitializeCreateIdentityFlowWrapper(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", + }) + } + c.JSON(http.StatusOK, gin.H{ + "identity": createdIdentity.Id, + }) +} + +func HandleGetIdentityFlow(c *gin.Context) { + createdIdentity := c.Query("identity") + getIdentity, r, err :=admin.InitializeGetIdentityFlowWrapper(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", + }) + } + + jsonString, _ := json.Marshal(getIdentity.Traits) + + var identity 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) { + + r, err :=admin.InitializeDeleteIdentityFlowWrapper(c) + + 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", + }) + } + c.JSON(http.StatusOK, gin.H{ + "message": "removed identity", + }) +} + +func HandleListIdentity(c* gin.Context){ + identities, r, err := admin.IntializeListIdentityFlowWrapper() + 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", + }) + } + c.JSON(http.StatusOK, gin.H{ + "identities": identities, + }) +} + +func HandleBanIdentity(c* gin.Context){ + + id, r, err := admin.InitializeBanIdentityFlowWrapper(c) + + 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(), + }) + } + c.JSON(http.StatusOK, gin.H{ + "identities": id, + }) +} \ No newline at end of file diff --git a/api/main.go b/api/main.go index 1ae8209..8ce13cc 100644 --- a/api/main.go +++ b/api/main.go @@ -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" ) @@ -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) diff --git a/api/types.go b/api/types.go index de9d03c..9aa045d 100644 --- a/api/types.go +++ b/api/types.go @@ -18,3 +18,18 @@ type ApplicationPutBody struct { type ApplicationBody struct { ID int `json:"id"` } + +type Identity struct { + Id int + Name string + Username string + Email string + Phone_number uint64 + Password string + Image_url string + Active bool + Verified int + Created_at string + Github_id string + Dribble_id string +} diff --git a/pkg/wrapper/kratos/admin/admin.go b/pkg/wrapper/kratos/admin/admin.go new file mode 100644 index 0000000..aa05641 --- /dev/null +++ b/pkg/wrapper/kratos/admin/admin.go @@ -0,0 +1,91 @@ +package admin + +import ( + "context" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/ory/client-go" +) + +func InitializeCreateIdentityFlowWrapper(adminCreateIdentityBody client.AdminCreateIdentityBody) (*client.Identity,*http.Response, error) { + configuration := client.NewConfiguration() + configuration.Servers = []client.ServerConfiguration{ + { + URL: "http://127.0.0.1:4434", // Kratos Admin API + }, + } + apiClient := client.NewAPIClient(configuration) + createdIdentity, r, err := apiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute() + + return createdIdentity, r, err +} + +func InitializeGetIdentityFlowWrapper(createdIdentity string) (*client.Identity, *http.Response, error) { + configuration := client.NewConfiguration() + configuration.Servers = []client.ServerConfiguration{ + { + URL: "http://127.0.0.1:4434", // Kratos Admin API + }, + } + apiClient := client.NewAPIClient(configuration) + + getIdentity, r, err := apiClient.V0alpha2Api.AdminGetIdentity(context.Background(), createdIdentity).Execute() + + return getIdentity, r, err +} + +func InitializeDeleteIdentityFlowWrapper(c *gin.Context)(*http.Response, error) { + configuration := client.NewConfiguration() + configuration.Servers = []client.ServerConfiguration{ + { + URL: "http://127.0.0.1:4434", // Kratos Admin API + }, + } + apiClient := client.NewAPIClient(configuration) + + identity := c.PostForm("identity") + + r, err := apiClient.V0alpha2Api.AdminDeleteIdentity(context.Background(), identity).Execute() + + return r,err +} + +func IntializeListIdentityFlowWrapper()([]client.Identity,*http.Response,error){ + configuration := client.NewConfiguration() + configuration.Servers = []client.ServerConfiguration{ + { + URL: "http://127.0.0.1:4434", // Kratos Admin API + }, + } + apiClient := client.NewAPIClient(configuration) + + identities, r, err := apiClient.V0alpha2Api.AdminListIdentities(context.Background()).Execute() + + return identities,r,err + +} + +func InitializeBanIdentityFlowWrapper(c* gin.Context)(*client.Identity,*http.Response,error){ + configuration := client.NewConfiguration() + configuration.Servers = []client.ServerConfiguration{ + { + URL: "http://127.0.0.1:4434", // Kratos Admin API + }, + } + apiClient := client.NewAPIClient(configuration) + + identity := c.PostForm("identity") + + 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 +} \ No newline at end of file