-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restructures Admin Backend #40
Changes from 2 commits
008e54b
5162cbe
d637b3b
359c4e3
788a82b
db5776e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error handling please |
||
|
||
adminCreateIdentityBody := *client.NewAdminCreateIdentityBody( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move this to wrapper |
||
"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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'N' shouldn't be capital |
||
}) | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error handling |
||
|
||
var identity admin.Identity | ||
|
||
if err := json.Unmarshal(jsonString, &identity); err != nil { | ||
fmt.Println(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use logger |
||
} | ||
fmt.Fprintf(os.Stdout, "Identity details for id %v. Traits: %v\n", createdIdentity, identity) | ||
itsdarshankumar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package api | |
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
|
@@ -12,7 +13,8 @@ import ( | |
|
||
func HandleGetMFAFlow(c *gin.Context) { | ||
log.Logger.Debug("Get MFA") | ||
flow_cookie, flowID, csrf_token, err := login.InitializeLoginFlowWrapper("aal2") | ||
cookie, _ := c.Cookie("sdslabs_session") | ||
flow_cookie, flowID, csrf_token, err := login.InitializeLoginFlowWrapper("aal2", cookie) | ||
|
||
if err != nil { | ||
log.ErrorLogger("Initialize MFA Failed", err) | ||
|
@@ -51,8 +53,11 @@ func HandlePostMFAFlow(c *gin.Context) { | |
}) | ||
return | ||
} | ||
session_cookie, _ := c.Cookie("sdslabs_session") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error handling |
||
csrfToken := req_body.CsrfToken | ||
cookie := strings.Split(flow_cookie, ";")[0] + "; " + strings.Split(session_cookie, ";")[0] + "; x-csrf-token=" + csrfToken | ||
|
||
identity, session, err := login.SubmitLoginWithMFAWrapper(flow_cookie, req_body.FlowID, req_body.CsrfToken, req_body.TOTP) | ||
identity, session, err := login.SubmitLoginWithMFAWrapper(cookie, req_body.FlowID, req_body.CsrfToken, req_body.TOTP) | ||
|
||
if err != nil { | ||
log.ErrorLogger("Kratos post MFA flow failed", err) | ||
|
Aryan51203 marked this conversation as resolved.
Show resolved
Hide resolved
|
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" |
Aryan51203 marked this conversation as resolved.
Show resolved
Hide resolved
|
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 | ||
} |
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"` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error handling