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 26, 2023
1 parent 986292c commit 7abe102
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 6 deletions.
122 changes: 122 additions & 0 deletions api/admin.go
Original file line number Diff line number Diff line change
@@ -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,
})
}
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
15 changes: 15 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
91 changes: 91 additions & 0 deletions pkg/wrapper/kratos/admin/admin.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 7abe102

Please sign in to comment.