diff --git a/api/admin.go b/api/admin.go new file mode 100644 index 0000000..0616fd1 --- /dev/null +++ b/api/admin.go @@ -0,0 +1,200 @@ +package api + +import ( + "encoding/json" + "fmt" + "net/http" + "os" + "strconv" + "strings" + + "github.com/gin-gonic/gin" + + "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, err := json.Marshal(t) + + if err != nil { + log.ErrorLogger("Unable to convert map to json", err) + + errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + c.JSON(errCode, gin.H{ + "error": err.Error(), + "message": "Unable to convert map to json", + }) + return + } + + err = json.Unmarshal(data, &mappedJsonIdentity) + + if err != nil { + log.ErrorLogger("Unable to convert JSON to map", err) + + errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + c.JSON(errCode, gin.H{ + "error": err.Error(), + "message": "Unable to convert JSON to map", + }) + return + } + + createdIdentity, r, err := admin.CreateIdentityFlowWrapper(mappedJsonIdentity) + + 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, err := json.Marshal(getIdentity.Traits) + + if err != nil { + log.ErrorLogger("Unable to convert map to json", err) + + errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + c.JSON(errCode, gin.H{ + "error": err.Error(), + "message": "Unable to convert map to json", + }) + return + } + + var identity admin.Identity + + err = json.Unmarshal(jsonString, &identity) + + if err != nil { + log.ErrorLogger("Unable to convert JSON to map", err) + + errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + c.JSON(errCode, gin.H{ + "error": err.Error(), + "message": "Unable to convert JSON to map", + }) + return + } + + 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, + }) +} diff --git a/api/login.go b/api/login.go index 2d5e5fc..a6d2ebc 100644 --- a/api/login.go +++ b/api/login.go @@ -14,7 +14,7 @@ import ( func HandleGetLoginFlow(c *gin.Context) { log.Logger.Debug("Get Login") - cookie, flowID, csrf_token, err := login.InitializeLoginFlowWrapper("aal1") + cookie, flowID, csrf_token, err := login.InitializeLoginFlowWrapper("aal1", "") if err != nil { log.ErrorLogger("Initialize Login Failed", err) 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/mfa.go b/api/mfa.go index c4fb725..e70dd61 100644 --- a/api/mfa.go +++ b/api/mfa.go @@ -2,6 +2,8 @@ package api import ( "net/http" + "strconv" + "strings" "github.com/gin-gonic/gin" @@ -12,7 +14,20 @@ import ( func HandleGetMFAFlow(c *gin.Context) { log.Logger.Debug("Get MFA") - flow_cookie, flowID, csrf_token, err := login.InitializeLoginFlowWrapper("aal2") + cookie, err := c.Cookie("sdslabs_session") + + if err != nil { + log.ErrorLogger("Session Cookie not found", err) + + errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + c.JSON(errCode, gin.H{ + "error": err.Error(), + "message": "Cookie not found", + }) + return + } + + flow_cookie, flowID, csrf_token, err := login.InitializeLoginFlowWrapper("aal2", cookie) if err != nil { log.ErrorLogger("Initialize MFA Failed", err) @@ -52,7 +67,23 @@ func HandlePostMFAFlow(c *gin.Context) { return } - identity, session, err := login.SubmitLoginWithMFAWrapper(flow_cookie, req_body.FlowID, req_body.CsrfToken, req_body.TOTP) + session_cookie, err := c.Cookie("sdslabs_session") + + if err != nil { + log.ErrorLogger("Session Cookie not found", err) + + errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + c.JSON(errCode, gin.H{ + "error": err.Error(), + "message": "Cookie not found", + }) + return + } + + csrfToken := req_body.CsrfToken + cookie := strings.Split(flow_cookie, ";")[0] + "; " + strings.Split(session_cookie, ";")[0] + "; x-csrf-token=" + csrfToken + + 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) diff --git a/api/types.go b/api/types.go index de9d03c..2c16544 100644 --- a/api/types.go +++ b/api/types.go @@ -18,3 +18,7 @@ type ApplicationPutBody struct { type ApplicationBody struct { ID int `json:"id"` } + +type IdentityBody struct { + Identity string `json:"identity"` +} diff --git a/config.sample.yaml b/config.sample.yaml index c92e008..b7ce8f3 100644 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -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" diff --git a/config/kratos.go b/config/kratos.go index 3b3489e..6716485 100644 --- a/config/kratos.go +++ b/config/kratos.go @@ -15,6 +15,18 @@ func getKratosClientConfig() *client.Configuration { return configuration } +func getKratosClientConfigAdmin() *client.Configuration { + configuration := client.NewConfiguration() + configuration.Servers = []client.ServerConfiguration{ + { + URL: NymeriaConfig.URL.AdminKratosURL, + }, + } + + return configuration +} + var ( - KratosClientConfig = getKratosClientConfig() + KratosClientConfig = getKratosClientConfig() + KratosClientConfigAdmin = getKratosClientConfigAdmin() ) diff --git a/config/types.go b/config/types.go index b5a1033..fbd71d9 100644 --- a/config/types.go +++ b/config/types.go @@ -6,9 +6,10 @@ type NymeriaCfg struct { DB DB `yaml:"db"` } type URL struct { - FrontendURL string `yaml:"frontend_url"` - KratosURL string `yaml:"kratos_url"` - Domain string `yaml:"domain"` + FrontendURL string `yaml:"frontend_url"` + KratosURL string `yaml:"kratos_url"` + AdminKratosURL string `yaml:"admin_kratos_url"` + Domain string `yaml:"domain"` } type DB struct { diff --git a/go.mod b/go.mod index cd6b9d0..d2219d9 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/sdslabs/nymeria -go 1.17 +go 1.18 require ( github.com/gin-contrib/cors v1.4.0 diff --git a/go.sum b/go.sum index 65b9caf..e75d0f4 100644 --- a/go.sum +++ b/go.sum @@ -19,7 +19,6 @@ cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvf cloud.google.com/go/bigquery v1.5.0/go.mod h1:snEHRnqQbz117VIFhE8bmtwIDY80NLUZUMb4Nv6dBIg= cloud.google.com/go/bigquery v1.7.0/go.mod h1://okPTzCYNXSlb24MZs83e2Do+h+VXtc4gLoIoXIAPc= cloud.google.com/go/bigquery v1.8.0/go.mod h1:J5hqkt3O0uAFnINi6JXValWIb1v0goeZM77hZzJN/fQ= -cloud.google.com/go/compute/metadata v0.2.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= cloud.google.com/go/datastore v1.1.0/go.mod h1:umbIZjpQpHh4hmRpGhH4tLFup+FVzqBi1b3c64qFpCk= cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= @@ -105,7 +104,6 @@ github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= @@ -176,14 +174,12 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo= github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= @@ -195,7 +191,6 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU= golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= @@ -229,7 +224,6 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -258,8 +252,6 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= golang.org/x/net v0.3.0 h1:VWL6FNY2bEEmsGVKabSlHu5Irp34xmMRoqb/9lF9lxk= golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -278,7 +270,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -309,17 +300,11 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -327,7 +312,6 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -373,7 +357,6 @@ golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roY golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/wrapper/kratos/admin/admin.go b/pkg/wrapper/kratos/admin/admin.go new file mode 100644 index 0000000..27bb09f --- /dev/null +++ b/pkg/wrapper/kratos/admin/admin.go @@ -0,0 +1,64 @@ +package admin + +import ( + "context" + "net/http" + + client "github.com/ory/client-go" + + "github.com/sdslabs/nymeria/config" +) + +func CreateIdentityFlowWrapper(identityMap map[string]interface{}) (*client.Identity, *http.Response, error) { + apiClient := client.NewAPIClient(config.KratosClientConfigAdmin) + + adminCreateIdentityBody := *client.NewAdminCreateIdentityBody( + "default", + identityMap, + ) // AdminCreateIdentityBody | (optional) + + 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 +} diff --git a/pkg/wrapper/kratos/admin/types.go b/pkg/wrapper/kratos/admin/types.go new file mode 100644 index 0000000..0be4721 --- /dev/null +++ b/pkg/wrapper/kratos/admin/types.go @@ -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"` +} diff --git a/pkg/wrapper/kratos/login/login.go b/pkg/wrapper/kratos/login/login.go index c380b2d..0cf9baf 100644 --- a/pkg/wrapper/kratos/login/login.go +++ b/pkg/wrapper/kratos/login/login.go @@ -8,12 +8,13 @@ import ( "github.com/sdslabs/nymeria/config" ) -func InitializeLoginFlowWrapper(aal string) (string, string, string, error) { +func InitializeLoginFlowWrapper(aal string, cookie string) (string, string, string, error) { refresh := false // bool | Refresh a login session If set to true, this will refresh an existing login session by asking the user to sign in again. This will reset the authenticated_at time of the session. (optional) returnTo := "http://127.0.0.1:4455/ping" // string | The URL to return the browser to after the flow was completed. (optional) apiClient := client.NewAPIClient(config.KratosClientConfig) - resp, r, err := apiClient.V0alpha2Api.InitializeSelfServiceLoginFlowForBrowsers(context.Background()).Refresh(refresh).Aal(aal).ReturnTo(returnTo).Execute() + + resp, r, err := apiClient.V0alpha2Api.InitializeSelfServiceLoginFlowForBrowsers(context.Background()).Refresh(refresh).Aal(aal).ReturnTo(returnTo).Cookie(cookie).Execute() if err != nil { return "", "", "", err @@ -40,7 +41,6 @@ func SubmitLoginFlowWrapper(cookie string, flowID string, csrfToken string, pass apiClient := client.NewAPIClient(config.KratosClientConfig) resp, r, err := apiClient.V0alpha2Api.SubmitSelfServiceLoginFlow(context.Background()).Flow(flowID).SubmitSelfServiceLoginFlowBody(submitDataBody).XSessionToken("").Cookie(cookie).Execute() - if err != nil { return *client.NewSessionWithDefaults(), "", err } @@ -53,9 +53,10 @@ func SubmitLoginFlowWrapper(cookie string, flowID string, csrfToken string, pass func SubmitLoginWithMFAWrapper(cookie string, flowID string, csrfToken string, totp string) (client.Session, string, error) { submitDataBody := client.SubmitSelfServiceLoginFlowBody{SubmitSelfServiceLoginFlowWithTotpMethodBody: client.NewSubmitSelfServiceLoginFlowWithTotpMethodBody("totp", totp)} // SubmitSelfServiceLoginFlowBody | - submitDataBody.SubmitSelfServiceLoginFlowWithPasswordMethodBody.SetCsrfToken(csrfToken) + submitDataBody.SubmitSelfServiceLoginFlowWithTotpMethodBody.SetCsrfToken(csrfToken) apiClient := client.NewAPIClient(config.KratosClientConfig) + resp, r, err := apiClient.V0alpha2Api.SubmitSelfServiceLoginFlow(context.Background()).Flow(flowID).SubmitSelfServiceLoginFlowBody(submitDataBody).XSessionToken("").Cookie(cookie).Execute() if err != nil { @@ -64,5 +65,5 @@ func SubmitLoginWithMFAWrapper(cookie string, flowID string, csrfToken string, t responseCookies := r.Header["Set-Cookie"] - return resp.Session, responseCookies[1], nil + return resp.Session, responseCookies[0], nil } diff --git a/pkg/wrapper/kratos/settings/settings.go b/pkg/wrapper/kratos/settings/settings.go index 614e805..03c1412 100644 --- a/pkg/wrapper/kratos/settings/settings.go +++ b/pkg/wrapper/kratos/settings/settings.go @@ -124,7 +124,7 @@ func SubmitSettingsFlowTOTPMethod(flow_cookie string, session_cookie string, flo cookie := strings.Split(flow_cookie, ";")[0] + "; " + strings.Split(session_cookie, ";")[0] + "; x-csrf-token=" + csrfToken req.URL.RawQuery = q.Encode() req.Header.Set("Cookie", cookie) - req.Header.Set("Contentp-Type", "application/json") + req.Header.Set("Content-Type", "application/json") resp, err := client.Do(req)