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

Using Ory Keto with RBAC design for permission service #1

Draft
wants to merge 4 commits into
base: main
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
145 changes: 142 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Created by https://www.toptal.com/developers/gitignore/api/go,vim,linux,macos,dotenv,eclipse,windows,sublimetext,jetbrains+all,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=go,vim,linux,macos,dotenv,eclipse,windows,sublimetext,jetbrains+all,visualstudiocode
# Created by https://www.toptal.com/developers/gitignore/api/go,node,vim,linux,macos,dotenv,eclipse,windows,sublimetext,jetbrains+all,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=go,node,vim,linux,macos,dotenv,eclipse,windows,sublimetext,jetbrains+all,visualstudiocode

### dotenv ###
.env
Expand Down Expand Up @@ -229,6 +229,145 @@ Temporary Items
# iCloud generated files
*.icloud

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

### Node Patch ###
# Serverless Webpack directories
.webpack/

# Optional stylelint cache

# SvelteKit build / generate output
.svelte-kit

### SublimeText ###
# Cache files for Sublime Text
*.tmlanguage.cache
Expand Down Expand Up @@ -327,4 +466,4 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/go,vim,linux,macos,dotenv,eclipse,windows,sublimetext,jetbrains+all,visualstudiocode
# End of https://www.toptal.com/developers/gitignore/api/go,node,vim,linux,macos,dotenv,eclipse,windows,sublimetext,jetbrains+all,visualstudiocode
8 changes: 0 additions & 8 deletions config/keto/keto-dev.yml

This file was deleted.

11 changes: 11 additions & 0 deletions config/keto/keto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: v0.12.0-alpha.0

dsn: memory

namespaces:
location: file://./namespaces.keto.ts

log:
level: debug
format: text
leak_sensitive_values: true
70 changes: 70 additions & 0 deletions config/keto/namespaces.keto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Context, Namespace, SubjectSet } from "@ory/keto-namespace-types"

class User implements Namespace {}

class Role implements Namespace {
related: {
members: (User | Role)[]
}
}

class Global implements Namespace {
related: {
editors: Role[]
}

permits = {
tournament_edit: (ctx: Context): boolean => this.related.editors.includes(ctx.subject),
}
}

class OsuTournament implements Namespace {
related: {
viewers: (User | SubjectSet<Role, "members">)[]
editors: (User | SubjectSet<Role, "members">)[]

mappool_viewers: (User | SubjectSet<Role, "members">)[]
mappool_editors: (User | SubjectSet<Role, "members">)[]

match_viewers: (User | SubjectSet<Role, "members">)[]
match_editors: (User | SubjectSet<Role, "members">)[]

global: Global[]
}

permits = {
edit: (ctx: Context): boolean =>
this.related.editors.includes(ctx.subject) ||
this.related.global.traverse((p) => p.permits.tournament_edit(ctx)),

mappool_edit: (ctx: Context): boolean =>
this.permits.edit(ctx) ||
this.related.mappool_editors.includes(ctx.subject),

match_edit: (ctx: Context): boolean =>
this.permits.edit(ctx) ||
this.related.match_editors.includes(ctx.subject)
}
}

class OsuMappool implements Namespace {
related: {
tournament: OsuTournament[]
}

permits = {
edit: (ctx: Context): boolean =>
this.related.tournament.traverse((p) => p.permits.mappool_edit(ctx))
}
}

class OsuMatch implements Namespace {
related: {
tournament: OsuTournament[]
}

permits = {
edit: (ctx: Context): boolean =>
this.related.tournament.traverse((p) => p.permits.match_edit(ctx))
}
}
46 changes: 46 additions & 0 deletions config/keto/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions config/keto/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"private": true,
"devDependencies": {
"@ory/keto-namespace-types": "^0.13.0-alpha.0",
"typescript": "^5.5.3"
}
}
22 changes: 22 additions & 0 deletions config/keto/relation-tuples/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"namespace": "Global",
"object": "*",
"relation": "editors",
"subject_set": {
"namespace": "Role",
"object": "admin",
"relation": "members"
}
},
{
"namespace": "Global",
"object": "*",
"relation": "editors",
"subject_set": {
"namespace": "Role",
"object": "developer",
"relation": "members"
}
}
]
32 changes: 32 additions & 0 deletions config/keto/relation-tuples/osu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"namespace": "OsuTournament",
"object": "*",
"relation": "editors",
"subject_set": {
"namespace": "Role",
"object": "organizer",
"relation": "members"
}
},
{
"namespace": "OsuTournament",
"object": "*",
"relation": "mappool_editors",
"subject_set": {
"namespace": "Role",
"object": "mappooler",
"relation": "members"
}
},
{
"namespace": "OsuTournament",
"object": "*",
"relation": "match_editors",
"subject_set": {
"namespace": "Role",
"object": "refree",
"relation": "members"
}
}
]
3 changes: 3 additions & 0 deletions config/keto/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@ory/keto-namespace-types/tsconfig.json"
}
10 changes: 5 additions & 5 deletions config/kratos/kratos-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ version: v0.13.0
dsn: memory
dev: true

log:
level: debug
format: text
leak_sensitive_values: true

serve:
public:
base_url: http://127.0.0.1:4433/
Expand Down Expand Up @@ -122,11 +127,6 @@ selfservice:
# value: OVERWRITE_ME
# in: header

log:
level: debug
format: text
leak_sensitive_values: true

secrets:
cookie:
- PLEASE-CHANGE-ME-I-AM-VERY-INSECURE
Expand Down
1 change: 0 additions & 1 deletion config/kratos/kratos.yml

This file was deleted.

Loading
Loading