2016-10-20 20:30:53 +02:00
|
|
|
+++
|
2016-11-17 08:46:00 +02:00
|
|
|
title = "JWT Example"
|
|
|
|
description = "JWT example for Echo"
|
2016-11-21 00:16:22 +02:00
|
|
|
[menu.main]
|
2016-10-20 20:30:53 +02:00
|
|
|
name = "JWT"
|
2016-11-29 06:30:42 +02:00
|
|
|
identifier = "recipe-jwt"
|
2016-10-20 20:30:53 +02:00
|
|
|
parent = "recipes"
|
|
|
|
weight = 11
|
|
|
|
+++
|
|
|
|
|
|
|
|
- JWT authentication using HS256 algorithm.
|
|
|
|
- JWT is retrieved from `Authorization` request header.
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Server using Map claims
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
`server.go`
|
|
|
|
|
|
|
|
{{< embed "jwt/map-claims/server.go" >}}
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Server using custom claims
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
`server.go`
|
|
|
|
|
|
|
|
{{< embed "jwt/custom-claims/server.go" >}}
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Client
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
`curl`
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
### Login
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
Login using username and password to retrieve a token.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
curl -X POST -d 'username=jon' -d 'password=shhh!' localhost:1323/login
|
|
|
|
```
|
|
|
|
|
|
|
|
*Response*
|
|
|
|
|
|
|
|
```js
|
|
|
|
{
|
|
|
|
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0NjE5NTcxMzZ9.RB3arc4-OyzASAaUhC2W3ReWaXAt_z2Fd3BN4aWTgEY"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
### Request
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
Request a restricted resource using the token in `Authorization` request header.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
curl localhost:1323/restricted -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0NjE5NTcxMzZ9.RB3arc4-OyzASAaUhC2W3ReWaXAt_z2Fd3BN4aWTgEY"
|
|
|
|
```
|
|
|
|
|
|
|
|
*Response*
|
|
|
|
|
|
|
|
```
|
|
|
|
Welcome Jon Snow!
|
|
|
|
```
|
|
|
|
|
2016-11-17 09:05:37 +02:00
|
|
|
## Source Code
|
|
|
|
|
|
|
|
- [With default Map claims]({{< source "jwt/map-claims" >}})
|
|
|
|
- [With custom claims]({{< source "jwt/custom-claims" >}})
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Maintainers
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
- [vishr](https://github.com/vishr)
|
|
|
|
- [axdg](https://github.com/axdg)
|
|
|
|
- [matcornic](https://github.com/matcornic)
|