-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
986292c
commit 7abe102
Showing
4 changed files
with
233 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |