Skip to content
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

feat: kratos nextjs with tailwindcss example #55

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added kratos-keto-nextjs-example/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions kratos-keto-nextjs-example/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ORY_SDK_URL=http://localhost:4433/
3 changes: 3 additions & 0 deletions kratos-keto-nextjs-example/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
2 changes: 2 additions & 0 deletions kratos-keto-nextjs-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.next
17 changes: 17 additions & 0 deletions kratos-keto-nextjs-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Basic Kratos and Keto Example:

# AFTER CLONING FOLLOW THE STEPS a AND b:

# a. cd ory && npm install

# b. cd ory && cd backend && go mod tidy

## 1. cd ory && docker-compose up -d && npm run dev

## 2. cd ory && cd backend && go run .

## Right now the Kratos part is commented out

## if you want to use the Kratos as well uncomment the index.tsx file inside ory folder then make changes in the init.go file inside backend folder

## if you just want to check only keto following the step 1 and two is enough.
27 changes: 27 additions & 0 deletions kratos-keto-nextjs-example/backend/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"server/routes"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

var checkconn *grpc.ClientConn
var writeconn *grpc.ClientConn
var err error

func GetConfig() *routes.Config {
checkconn, err = grpc.Dial("127.0.0.1:4466", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic(err.Error())
}
writeconn, err = grpc.Dial("127.0.0.1:4467", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic("Encountered error: " + err.Error())
}
return &routes.Config{
CheckConn: checkconn,
WriteConn: writeconn,
}
}
33 changes: 33 additions & 0 deletions kratos-keto-nextjs-example/backend/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module server

go 1.19

require (
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.8.1
github.com/ory/keto/proto v0.10.0-alpha.0
google.golang.org/grpc v1.50.0
)

require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/net v0.0.0-20220622184535-263ec571b305 // indirect
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20220622171453-ea41d75dfa0f // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
215 changes: 215 additions & 0 deletions kratos-keto-nextjs-example/backend/go.sum

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions kratos-keto-nextjs-example/backend/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"context"
"fmt"
"log"

rts "github.com/ory/keto/proto/ory/keto/relation_tuples/v1alpha2"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func initiate() {
conn, err := grpc.Dial("127.0.0.1:4467", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("Encountered error: " + err.Error())
}

client := rts.NewWriteServiceClient(conn)

_, err = client.TransactRelationTuples(context.Background(), &rts.TransactRelationTuplesRequest{
RelationTupleDeltas: []*rts.RelationTupleDelta{
{
Action: rts.RelationTupleDelta_ACTION_INSERT,
RelationTuple: &rts.RelationTuple{
Namespace: "messages",
Object: "admin",
Relation: "owner",
Subject: rts.NewSubjectID("vijeyash@gmail.com"),
},
},
{
Action: rts.RelationTupleDelta_ACTION_INSERT,

RelationTuple: &rts.RelationTuple{

Namespace: "messages",
Object: "admin",
Relation: "view",
Subject: rts.NewSubjectSet("messages", "admin", "owner"),
},
},
{
Action: rts.RelationTupleDelta_ACTION_INSERT,

RelationTuple: &rts.RelationTuple{

Namespace: "messages",
Object: "homepage",
Relation: "view",
Subject: rts.NewSubjectSet("messages", "admin", "owner"),
},
},
},
})
if err != nil {
log.Fatalf("Encountered error: " + err.Error())
}

fmt.Println("Successfully created tuple")

}
23 changes: 23 additions & 0 deletions kratos-keto-nextjs-example/backend/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)

func init() {
initiate()
}
func main() {
r := gin.Default()
config := cors.DefaultConfig()
config.AllowAllOrigins = true
r.Use(cors.New(config))

app := GetConfig()

r.POST("/writerelation", app.WriteHandler)
r.POST("/checkrelation", app.CheckHandler)
r.POST("deleterelation", app.DeleteHandler)
r.Run(":4000")
}
8 changes: 8 additions & 0 deletions kratos-keto-nextjs-example/backend/models/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package models


type Relation struct {
Object string `json:"object"`
Relation string `json:"relation"`
Subject string `json:"subject"`
}
110 changes: 110 additions & 0 deletions kratos-keto-nextjs-example/backend/routes/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package routes

import (
"context"
"server/models"

"github.com/gin-gonic/gin"
rts "github.com/ory/keto/proto/ory/keto/relation_tuples/v1alpha2"

"google.golang.org/grpc"
)

type Config struct {
CheckConn *grpc.ClientConn
WriteConn *grpc.ClientConn
}


func NewConfig(check, write *grpc.ClientConn) *Config {
return &Config{
CheckConn: check,
WriteConn: write,
}
}

func (config *Config) WriteHandler(c *gin.Context) {
client := rts.NewWriteServiceClient(config.WriteConn)
data := &models.Relation{}

c.BindJSON(data)
_, err := client.TransactRelationTuples(context.Background(), &rts.TransactRelationTuplesRequest{
RelationTupleDeltas: []*rts.RelationTupleDelta{
{
Action: rts.RelationTupleDelta_ACTION_INSERT,
RelationTuple: &rts.RelationTuple{
Namespace: "messages",
Object: data.Object,
Relation: data.Relation,
Subject: rts.NewSubjectID(data.Subject),
},
},
},
})
if err != nil {
c.JSON(500, gin.H{
"message": err.Error(),
})
return
}
c.JSON(200, gin.H{
"message": "Successfully created permission tuple",
})
}

func (config *Config) CheckHandler(c *gin.Context) {
client := rts.NewCheckServiceClient(config.CheckConn)
data := &models.Relation{}
c.BindJSON(data)
res, err := client.Check(context.Background(), &rts.CheckRequest{
Namespace: "messages",
Object: data.Object,
Relation: data.Relation,
Subject: rts.NewSubjectID(data.Subject),
})
if err != nil {
c.JSON(500, gin.H{
"error": err.Error(),
})
return
}
if res.Allowed {
c.JSON(200, gin.H{
"allowed": true,
})
return
}
c.JSON(200, gin.H{
"allowed": false,
})
}


func (config *Config) DeleteHandler(c *gin.Context) {
client := rts.NewWriteServiceClient(config.WriteConn)
data := &models.Relation{}

c.BindJSON(data)
_, err := client.TransactRelationTuples(context.Background(), &rts.TransactRelationTuplesRequest{
RelationTupleDeltas: []*rts.RelationTupleDelta{
{
RelationTuple: &rts.RelationTuple{
Namespace: "messages",
Object: data.Object,
Relation: data.Relation,
Subject: rts.NewSubjectID(data.Subject),
},
Action: rts.RelationTupleDelta_ACTION_DELETE,
},
},
})
if err != nil {
c.JSON(500, gin.H{
"message": err.Error(),
})
return
}
c.JSON(200, gin.H{
"message": "Successfully deleted permission tuple",
})
}
Loading