JWT

JWT middleware configuration can be found here.

This is cookbook for:

  • JWT authentication using HS256 algorithm.
  • JWT is retrieved from Authorization request header.

Server

Using custom claims

cookbook/jwt/custom-claims/server.go

  1. loading...

Using a user-defined KeyFunc

cookbook/jwt/user-defined-keyfunc/server.go

  1. loading...

Client

Login

Login using username and password to retrieve a token.

  1. curl -X POST -d 'username=jon' -d 'password=shhh!' localhost:1323/login

Response

  1. {
  2. "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0NjE5NTcxMzZ9.RB3arc4-OyzASAaUhC2W3ReWaXAt_z2Fd3BN4aWTgEY"
  3. }

Request

Request a restricted resource using the token in Authorization request header.

  1. curl localhost:1323/restricted -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0NjE5NTcxMzZ9.RB3arc4-OyzASAaUhC2W3ReWaXAt_z2Fd3BN4aWTgEY"

Response

  1. Welcome Jon Snow!