Skip to content

Commit

Permalink
Merge branch 'dev' into oidc
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdarshankumar committed Dec 27, 2022
2 parents 11cd269 + a97bdb2 commit 5d54843
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 284 deletions.
2 changes: 2 additions & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func Start() {
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.GET("/update-identity/ban", c.UpdateBanIdentity)

r.GET("/register", HandleGetRegistrationFlow)
r.POST("/register", HandlePostRegistrationFlow)
Expand Down
2 changes: 1 addition & 1 deletion api/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"

"github.com/gin-gonic/gin"
client "github.com/ory/kratos-client-go"
client "github.com/ory/client-go"
"github.com/sdslabs/nymeria/config"
"github.com/sdslabs/nymeria/log"
)
Expand Down
2 changes: 1 addition & 1 deletion config/kratos.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
client "github.com/ory/kratos-client-go"
client "github.com/ory/client-go"
)

func getKratosClientConfig() *client.Configuration {
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/gin-gonic/gin v1.8.1
github.com/ory/client-go v0.2.0-alpha.48
github.com/ory/kratos-client-go v0.10.1
github.com/sirupsen/logrus v1.9.0
gopkg.in/yaml.v2 v2.4.0
Expand All @@ -24,10 +25,10 @@ require (
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20220930213112-107f3e3c3b0b // indirect
golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1 // indirect
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/net v0.3.0 // indirect
golang.org/x/oauth2 v0.3.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
282 changes: 19 additions & 263 deletions go.sum

Large diffs are not rendered by default.

117 changes: 107 additions & 10 deletions pkg/controller/admin/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"

"github.com/gin-gonic/gin"
client "github.com/ory/kratos-client-go"
m "github.com/sdslabs/nymeria/pkg/middleware"
client "github.com/ory/client-go"
"github.com/sdslabs/nymeria/log"
)

func CreateIdentity(c *gin.Context) {
apiClient := m.NewAdminMiddleware()
configuration := client.NewConfiguration()
configuration.Servers = []client.ServerConfiguration{
{
URL: "http://127.0.0.1:4434", // Kratos Admin API
},
}
apiClient := client.NewAPIClient(configuration)

id, _ := strconv.Atoi(c.PostForm("id"))
verified, _ := strconv.Atoi(c.PostForm("verified"))
active, _ := strconv.ParseBool(c.PostForm("active"))
Expand All @@ -38,22 +47,37 @@ func CreateIdentity(c *gin.Context) {

createdIdentity, r, err := apiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminCreateIdentity``: %v\n", err)
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",
})
}
fmt.Fprintf(os.Stdout, "Created identity with ID: %v\n", createdIdentity.Id)
c.JSON(http.StatusOK, gin.H{
"identity": createdIdentity.Id,
})

}

func GetIdentity(c *gin.Context) {
apiClient := m.NewAdminMiddleware()
configuration := client.NewConfiguration()
configuration.Servers = []client.ServerConfiguration{
{
URL: "http://127.0.0.1:4434", // Kratos Admin API
},
}
apiClient := client.NewAPIClient(configuration)

createdIdentity := c.Query("identity")
fmt.Println(createdIdentity)
getIdentity, r, err := apiClient.V0alpha2Api.AdminGetIdentity(context.Background(), createdIdentity).Execute()

if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminGetIdentity``: %v\n", err)
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)
Expand All @@ -63,17 +87,90 @@ func GetIdentity(c *gin.Context) {
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 DeleteIdentity(c *gin.Context) {
apiClient := m.NewAdminMiddleware()
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()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminDeleteIdentity``: %v\n", err)
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 ListIdentity(c *gin.Context) {
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()

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",
})
}
fmt.Println("Successfully Removed identity")
c.JSON(http.StatusOK, gin.H {
"identities": identities,
})
}

func UpdateBanIdentity(c *gin.Context) {
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()

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,
})
}
2 changes: 1 addition & 1 deletion pkg/middleware/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"

"github.com/gin-gonic/gin"
client "github.com/ory/kratos-client-go"
client "github.com/ory/client-go"
)
type kratosMiddleware struct {
client *client.APIClient
Expand Down
2 changes: 1 addition & 1 deletion pkg/wrapper/kratos/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package login
import (
"context"

client "github.com/ory/kratos-client-go"
client "github.com/ory/client-go"
"github.com/sdslabs/nymeria/config"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/wrapper/kratos/logout/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"net/http"

client "github.com/ory/kratos-client-go"
client "github.com/ory/client-go"
"github.com/sdslabs/nymeria/config"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/wrapper/kratos/recovery/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"os"

client "github.com/ory/kratos-client-go"
client "github.com/ory/client-go"
"github.com/sdslabs/nymeria/config"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/wrapper/kratos/registration/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"os"

client "github.com/ory/kratos-client-go"
client "github.com/ory/client-go"
"github.com/sdslabs/nymeria/config"
)

Expand Down

0 comments on commit 5d54843

Please sign in to comment.