jwt

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2018 License: MIT Imports: 7 Imported by: 114

README

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

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)
}

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

func GetClaims(token string) map[string]interface{}

GetClaims() returns a token's claims, allowing you to check the values to make sure they match

func New

func New(claims map[string]interface{}) (string, error)

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.

func Passes

func Passes(token string) bool

Passes returns a bool indicating whether a token (string) provided has been signed by our server. If true, the client is authenticated and may proceed.

func Secret

func Secret(key []byte)

Secret is a helper function to set the unexported privateKey variable used when signing and verifying tokens. Its argument is type []byte since we expect users to read this value from a file which can be excluded from source code.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL