-
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 008e54b
Showing
8 changed files
with
271 additions
and
19 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,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, | ||
}) | ||
} |
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 |
---|---|---|
@@ -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" |
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,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 | ||
} |
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,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"` | ||
} |