Example Golang application with Role Based Access Control.
It is a HTTP Server, mimicking a string:string dictionary, with the following endpoints:
GET /v1/map/{key}- Returns a string
valuewith status 200 if found - Returns
NOT_FOUNDwith status 404 if not found
- Returns a string
POST /v1/map/{key}- The POST body is the
value - Returns the same string
valuewith status 200 if set - Returns
UNAUTHORISEDwith status 401 if not allowed
- The POST body is the
Start the server running on localhost:8080.
$ go run cmd/main.goPermit.io is a third party authorisation provider, that provides an easy to integrate with API for role based access control. For our purposes, we will use this as an alternative for our in memory solution.
To get started, you'll need to grab an API key from https://app.permit.io/settings/api-keys (you'll need to create a free account first!). Store this somewhere:
$ PERMIT_API_KEY=permit_key_skkdfbljsdfudfuybdfuygoydfubydkfubSet up the default data:
$ go run scripts/permit_setup.go -permit_api_key=$PERMIT_API_KEYThen, start the server running on localhost:8080.
$ go run cmd/main.go -permit_api_key=$PERMIT_API_KEYThere is a helper script to check all authorisation is set up correctly, this should be run in a separate terminal.
$ scripts/run_tests.shTry to get the value at key "notexist" with user "alice":
$ curl -H "User: alice" -X GET http://localhost:8080/v1/map/notexist
> NOT_FOUNDTry to set the value to "world" at key "hello" with user "alice":
$ curl -H "User: alice" -X POST http://localhost:8080/v1/map/hello -d "world"
> worldTry to get that value you just created at key "hello" with user "alice":
$ curl -H "User: alice" -X GET http://localhost:8080/v1/map/hello
> worldUser "bob" only has the role of "writer", so they are not able to use the GET endpoint:
$ curl -H "User: bob" -X GET http://localhost:8080/v1/map/hello
> UNAUTHORISEDUser "charli" only has the role of "reader", so they are not able to use the POST endpoint:
$ curl -H "User: charli" -X POST http://localhost:8080/v1/map/hello -d "world"
> UNAUTHORISED