summaryrefslogtreecommitdiff
path: root/cmd/ponzu/vendor/github.com/nilslice/jwt/README.md
blob: a384dd1dcbd7d38c0487184950abfe4c07baf48f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# JWT

### Usage
	$ go get github.com/nilslice/jwt

package jwt provides methods to create and check JSON Web Tokens. It only implements HMAC 256 encryption and has a very small footprint, ideal for simple usage when authorizing clients

```go
	package main

	import (
		auth "github.com/nilslice/jwt"
		"fmt"
		"net/http"
		"strings"
	)

	func main() {
	http.HandleFunc("/auth/new", func(res http.ResponseWriter, req *http.Request) {
			claims := map[string]interface{}{"exp": time.Now().Add(time.Hour * 24).Unix()}
			token, err := auth.New(claims)
			if err != nil {
				http.Error(res, "Error", 500)
				return
			}
			res.Header().Add("Authorization", "Bearer "+token)

			res.WriteHeader(http.StatusOK)
		})

		http.HandleFunc("/auth", func(res http.ResponseWriter, req *http.Request) {
			userToken := strings.Split(req.Header.Get("Authorization"), " ")[1]

			if auth.Passes(userToken) {
				fmt.Println("ok")
			} else {
				fmt.Println("no")
			}
		})

		http.ListenAndServe(":8080", nil)
	}
```