Simplistic Example of role-based HTTP Authorization with casbin using jwt tokens.
Inspired by zupzup/casbin-http-role-example
Run with
go run main.go
Which starts a server at http://localhost:8080
with the following routes:
POST /login
- accessible if not logged in- takes
name
as a form-data parameter - there is no password - Valid Users:
Admin
ID:1
, Role:admin
Sabine
ID:2
, Role:member
Sepp
ID:3
, Role:member
- takes
POST /logout
- accessible if logged inGET /member/current
- accessible if logged in as a memberGET /member/role
- accessible if logged in as a memberGET /admin/stuff
- accessible if logged in as an admin
curl -X "POST" "http://localhost:8080/login" \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode "name=Sabine"
curl "http://localhost:8080/logout" \
-H 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTUwODA1MjYsImlhdCI6MTYxNTA3Njg5NiwibmJmIjoxNjE1MDc2ODk2LCJyb2xlIjoiYWRtaW4iLCJ1c2VySUQiOjF9.6qU62-RzCpSqjsUjbFeq1oIlQGDJQpBBm2iCcqtIMwo'
curl "http://localhost:8080/member/current" \
-H 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTUwODA1MjYsImlhdCI6MTYxNTA3Njg5NiwibmJmIjoxNjE1MDc2ODk2LCJyb2xlIjoiYWRtaW4iLCJ1c2VySUQiOjF9.6qU62-RzCpSqjsUjbFeq1oIlQGDJQpBBm2iCcqtIMwo'
curl "http://localhost:8080/member/role" \
-H 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTUwODA1NzYsImlhdCI6MTYxNTA3Njk0NiwibmJmIjoxNjE1MDc2OTQ2LCJyb2xlIjoibWVtYmVyIiwidXNlcklEIjoyfQ.WPKsSvuBRbI7Pdv0GubJRrElcHe244bCtxDUq6nuT2w'
curl "http://localhost:8080/admin/stuff" \
-H 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTUwODA1NzYsImlhdCI6MTYxNTA3Njk0NiwibmJmIjoxNjE1MDc2OTQ2LCJyb2xlIjoibWVtYmVyIiwidXNlcklEIjoyfQ.WPKsSvuBRbI7Pdv0GubJRrElcHe244bCtxDUq6nuT2w'