TripleParity / docks-api

RESTful API Server for Docks
https://tripleparity.github.io/docks-api/stack-api-spec.html
GNU General Public License v3.0
0 stars 0 forks source link

Basic admin JWT authentication implementation #38

Closed devosray closed 6 years ago

devosray commented 6 years ago

This pull request adds the dependencies for and implements a basic version of JWT authentication to the Docks API. The only username and password combination accepted right now is admin/admin.

All Docks-API endpoints have authorization enabled except for the new token endpoint.

The token endpoint can be reached at /api/auth/token. To obtain a token, make a POST request to the token endpoint with the following JSON body:

{username: "admin", password: "admin"}

The API will then return the JWT inside of a JSON object as follows:

{jwt: long.jwt.here}

To access any of the authorized endpoints, you need to send the JWT along inside of the Authorization HTTP header in the following format:

Authorization: Bearer long.jwt.here

Example of the above commands using curl:

-> {"jwt":"eyJhbGciOiJIUzI1NiIsInR5cC..."}

- Access protected resource with JWT:

curl http://localhost:8080/docker/containers/json -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cC..."

-> [{"Id":"f65163fe4a1e5bbb2a4c32f99273e8d6c3381e5a9f2f4807dd3f6c4b23b60141","Names":[...

- Access protected endpoint without the token:

curl -I http://localhost:8080/docker/container/json -> HTTP/1.1 401 Unauthorized



In the future, usernames and passwords will be checked against a database. The format of the JWT and the endpoint should stay the same.