Creating boilerplate for Go-chi with some good defaults.
Note
This repository is updated infrequently, as I backport changes from my other projects into this. However, at any given point, the repo should be considered a good starting point.
- Fully documented codebase with GoDoc.
- Logging with zerolog
- Routing with go-chi
- OpenAPI with go-swagger
- Input Validation with go-playground/validator
- Sane HTTP Security Headers with secure
- Custom Redis Cache Middleware with go-redis
- Optional: Memcached implementation
- OAuth 2.0 client.
- Password hashing with bcrypt
- Token Grant
- Token Validation + RBAC
- Token Refresh
- Token Revoke
- JWT authentication.
Run make
to see all available commands.
cd server
make packages_install
cd server
make run
-
JWTIDs were used, but for the
refresh token
only. This is because therefresh token
is persisted in theredis
cache, and therefore needs to be revoked.The access token
is not persisted, and therefore does not need to be revoked. This has the following benefits:-
The
access token
doesn't need to be validated against the DB or cache, on each request. And instead therefresh token
requires this only during a refresh. -
This avoids too many DB/cache lookups, and therefore improves performance.
-
You can however, choose to use JWTIDs for both the
access token
andrefresh token
, if you want to preventreplay attacks
.
-
-
The API returns both an
access token
and arefresh token
, it is recommended that theaccess token
is stored in memory, and therefresh token
is stored in a cookie with thesecure
&http-only
flags set. -
The
refresh token
is also persisted in theredis
cache for validation and revocation. -
Persisting the
access token
in memory, means that the token is not persisted across browser restarts, and is therefore more secure. -
Your client should refresh the
access token
when it expires, using therefresh token
stored in the cookie. -
In case of higher security requirements, you can follow one of the following patterns:
-
Use a
refresh token
with a short expiry time, and refresh therefresh token
on every request. -
Omit the
refresh token
entirely, and use a short livedaccess token
, and prompt the user to login again when theaccess token
expires.
-
-
You may also consider using JWTIDs, to prevent replay attacks.