The goal of this assignment is to build a simple HTTP service (with the appropriate unit tests) with two endpoints:
POST /auth Accepts JSON input in the format:
{"username": "", "password": ""}
and returns JWT OAUTH 2/OIDC token with the username as a subject. The username and the password don't have to be verified, but should not accept empty strings. The JWT token should expire in one hour.
It should return appropriate error status code if the JSON payload is not valid, or the username and password are not valid (are empty)
POST /sum Protected with a valid JWT token, generated by the /auth endpoint, provided as a Bearer Authorization header.
Accepts arbitrary JSON document as payload, which can contain a variety of things: arrays [1,2,3,4], arbitrary objects {"a":1, "b":2, "c":3}, numbers, and strings. The endpoint should find all of the numbers throughout the document and add them together.
For example:
[1,2,3,4] and {"a":6,"b":4} both have a sum of 10. [[[2]]] and {"a":{"b":4},"c":-2} both have a sum of 2. {"a":[-1,1,"dark"]} and [-1,{"a":1, "b":"light"}] both have a sum of 0. [] and {} both have a sum of 0. This is not an exhaustive list of examples. Think about what other edge cases there might be.
The response should be the hex digest of the SHA256 hash of the sum of all numbers in the document. It should return the appropriate error status code if the JWT token or the JSON payload are not valid.
We're using assymetric encryption(ES256) for JWT. Therefore we need to generate keys:
make jwtkeys
Make sure you checked config.yaml
before launch.
Start
make serve
or
go run cmd/main.go serve --config config.yaml
You also can build docke container. Dont forget to replace PATH_TO_YOUR_GIT_TOKEN
docker build -t doer-api --build-arg GITHUB_TOKEN="$(cat ~/PATH_TO_YOUR_GIT_TOKEN)" .
make test
make opencoverage
POST /auth Get JWT Token by username/password. Credentials must be not empty. Free tokens for everyone!
curl --location --request POST 'localhost:8080/auth' \
--header 'Content-Type: application/json' \
--data-raw '{
"password":"Alice",
"username":"1337"
}'
400 Bad Request
{"error":"Unexpected EOF"}
422 Unprocessable Entity
{"error":"Key: 'AuthRequest.Username' Error:Field validation for 'Username' failed on the 'required' tag"}
500 Internal Server Error
{"error":"internal error"}
200 OK
{
"payload": "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2Nzc2OTI1MzUsImlhdCI6MTY3NzY4ODkzNSwiaXNzIjoiZG9lci5hdXRoIiwic3ViIjoiMiJ9.IHx0zP00Ef6uAeOAVWSpyDFehYM2OkBazke5m_pTSGoYduipMjp-lvHaUV0REvC19KJjoTE-w_rYE6mFWkECew"
}
Sum all digits from request. Authorization header must be provided
curl --location --request POST 'localhost:8080/sum' \
--header 'Authorization: eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2Nzc3Njg0MzAsImlhdCI6MTY3Nzc2NDgzMCwiaXNzIjoiZG9lci5hdXRoIiwic3ViIjoiMiJ9.TdAmnKS9ZH-MhIbkWRt5FVn-Xgng_unScbltzznt7LJ_r2VUvzG7BDKuub4V33Z8micD6Hb_N0AiFWrVtTYQvQ' \
--header 'Content-Type: application/json' \
--data-raw '[[[2]]]'
400 Bad Request
{"error":"Unexpected EOF"}
200 OK
{
"sum": 2,
"hash": "dbc1b4c900ffe48d575b5da5c638040125f65db0fe3e24494b76ea986457d986"
}
We need to know what version of app is currently serving our requests.
GET /build
curl localhost:8080/build
{
"version": "feature/assignment",
"commit_hash": "5356d19",
"build_date": "2023-03-02T20:29:26+0600",
"go_version": "go1.20.1",
"os": "linux",
"arch": "amd64",
"compiler": "gc"
}