Documentation
¶
Overview ¶
Package jwt provides methods to create and check JSON Web Tokens (JWT). It only implements HMAC 256 encryption and has a very small footprint, ideal for simple usage when authorizing clients
package main import ( auth "github.com/nilslice/jwt" "fmt" "net/http" "strings" "time" ) 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) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetClaims ¶
GetClaims() returns a token's claims, allowing you to check the values to make sure they match
func New ¶
New returns a token (string) and error. The token is a fully qualified JWT to be sent to a client via HTTP Header or other method. Error returned will be from the newEncoded unexported function.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.