Skip to content

zupzup/rust-casbin-example

main
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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