Skip to content

zupzup/rust-casbin-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-casbin-example

Simple example of using casbin-rs in a warp web service for role-based authorization.

There are three hard-coded users:

  • sibylle (Admin)
  • herbert (Member)
  • gordon (Anonymous)

Endpoints:

  • POST /login with the username only ({ "name": "sibylle" }), returns a session token
  • /logout invalidates the session token
  • /member only members can access this (sibylle & herbert)
  • /admin only admins can access this (herbert)

You can run this using make dev, which starts a server on http://localhost:8080

curl -X POST http://localhost:8080/login -d '{ "name": "herbert" }' -H "content-type: application/json"
=> $TOKEN
curl http://localhost:8080/member -H "authorization: Bearer $TOKEN" -H "content-type: application/json"
=> 200
curl http://localhost:8080/admin -H "authorization: Bearer $TOKEN" -H "content-type: application/json"
=> 200

curl -X POST http://localhost:8080/login -d '{ "name": "sibylle" }' -H "content-type: application/json"
=> $TOKEN
curl http://localhost:8080/member -H "authorization: Bearer $TOKEN" -H "content-type: application/json"
=> 200
curl http://localhost:8080/admin -H "authorization: Bearer $TOKEN" -H "content-type: application/json"
=> 401

curl -X POST http://localhost:8080/login -d '{ "name": "gordon" }' -H "content-type: application/json"
=> $TOKEN
curl http://localhost:8080/member -H "authorization: Bearer $TOKEN" -H "content-type: application/json"
=> 401
curl http://localhost:8080/admin -H "authorization: Bearer $TOKEN" -H "content-type: application/json"
=> 401

About

An example of using casbin within a rust web application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published