RestAuth with curl

From RestAuth
Jump to: navigation, search

RestAuth is such a simple protocol, you can even use simple curl shell commands to access a RestAuth service. This is useful if you want to get some information but don't have a client at hand or want to develop a library and know what the server really returns.

All examples below use environment variables to make them copy/pasteable. At a bare minimum, set these environment variables with appropriate variables:

export RESTAUTH_HOST=http://[::1]:8000

If you start a testserver with python testserver you can literally copy/paste the data from above. If you want to use a different content type (by default, JSON is returned), you can also add some HTTP-Headers to the command line:

 curl -g  ... --header "Accept: application/pickle" --header "Content-Type: application/pickle"

The examples below will print the response body (if any) and the HTTP status code in a separate line. In general, response codes 200, 201 and 204 mean success, 4xx means a failure. See the Specification for details on all requests.

User management

Get all users
curl -g -X GET -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" $RESTAUTH_HOST/users/
Create a user
curl -g -X POST --header "Content-Type: application/json" -d '{"password": "nopass", "user": "username"}' -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/users/"

or, to create some properties in one request:

curl -g -X POST --header "Content-Type: application/json" -d '{"password": "nopass", "user": "username", "properties": {"propkey": "propval"}}' -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/users/"

... you can also omit the "password" parameter to create a user that has no password (and is not able to log in).

Verify a password
curl -g -X POST --header "Content-Type: application/json" -d '{"password": "nopass"}' -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/users/username/"
Change a password
curl -g -X PUT --header "Content-Type: application/json" -d '{"password": "nopass"}' -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/users/username/"
Delete a user
curl -g -X DELETE -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/users/username/"

Property management

Get all properties
curl -g -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/users/username/props/"
Create a property
curl -g -X POST -d '{"value": "value", "prop": "key"}' --header "Content-Type: application/json" -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/users/username/props/"
Set a property
curl -g -X PUT -d '{"value": "value"}' --header "Content-Type: application/json" -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/users/username/props/key/"
Set multiple properties
curl -g -X PUT -d '{"key": "value-new", "key2": "value"}' --header "Content-Type: application/json" -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/users/username/props/"
Delete property
curl -g -X DELETE -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/users/username/props/key/"

Group management

Get a list of groups
curl -g -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/groups/"
Create a group
curl -g -X POST -d '{"group": "groupname"}' --header "Content-Type: application/json" -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/groups/"
Verify that a group exists =
curl -g -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/groups/groupname/"
Get group members
curl -g -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/groups/groupname/users/"
Add user to a group
curl -g -X POST -d '{"user": "username"}' --header "Content-Type: application/json" -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/groups/groupname/users/
Get all groups of a user
curl -g -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/groups/?user=username"
Verify that a user is in a group =
curl -g -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/groups/groupname/users/username/"
Remove a user from a group
curl -g -X DELETE -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/groups/groupname/users/username/"
Get a list of subgroups =
curl -g -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/groups/groupname/groups/"
Add a group to a group
curl -g -X POST -d '{"group": "subgroup"}' --header "Content-Type: application/json" -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/groups/groupname/groups/"
Remove a group from a group =
curl -g -X DELETE -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/groups/groupname/groups/subgroup/"
Delete a group
curl -g -X DELETE -u $RESTAUTH_CREDENTIALS -w "\n%{http_code}\n" "$RESTAUTH_HOST/groups/groupname/"