jwt

package
v0.0.0-...-4cbec95 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2017 License: MIT Imports: 8 Imported by: 3

README

jwt for macross

The jwt middleware for Macross Web Framework

Requirements

macross

Getting Started

Create a server.go file with the following content:

package main

import (
	"macross"
	"macross/jwt"
	"net/http"
)

func main() {
	m := macross.New()


	m.Get("/", func(self *macross.Context) error {
		var data = map[string]interface{}{}
		data["version"] = "1.0.0"
		return self.JSON(http.StatusOK, data)
	})

	// 给用户返回token之前请先密码验证用户身份
	m.Post("/signin/", func(self *macross.Context) error {
		username := string(self.FormValue("username"))
		password := string(self.FormValue("password"))
		if (username == "insion") && (password == "PaSsworD") {
			claims := jwt.NewMapClaims()
			claims["address"] = "GD.GZ"
			tk, _ := jwt.NewToken("secret", "SigningMethodHS256", claims)

			return self.WriteData(tk)
		}
		return macross.ErrUnauthorized

	})

	g := m.Group("/jwt", jwt.JWT("secret"))
	g.Get("/say/", func(self *macross.Context) error {
		return self.WriteData("Hello, Macross")
	})

	m.Run(":9000")
}

Now run the following command to start the Web server:

go run server.go

You should be able to access URLs such as http://localhost:9000.

Documentation

Index

Constants

View Source
const (
	AlgorithmHS256 = "HS256"
)

Algorithims

Variables

View Source
var (
	Bearer = "Bearer" // 不能用const定义,需要允许被外部修改设定~
)
View Source
var (
	// DefaultJWTConfig is the default JWT auth middleware config.
	DefaultJWTConfig = JWTConfig{
		Skipper:       skipper.DefaultSkipper,
		Expires:       time.Hour,
		SigningMethod: AlgorithmHS256,
		ContextKey:    "jwt",
		TokenLookup:   "header:" + macross.HeaderAuthorization,
		Claims:        jwt.MapClaims{},
	}
)

Functions

func GetMapClaims

func GetMapClaims(self *macross.Context, contextKey ...string) jwt.MapClaims

func JWT

func JWT(key string) macross.Handler

JWT returns a JSON Web Token (JWT) auth middleware.

For valid token, it sets the user in context and calls next handler. For invalid token, it returns "401 - Unauthorized" error. For empty token, it returns "400 - Bad Request" error.

See: https://jwt.io/introduction See `JWTConfig.TokenLookup`

func JWTWithConfig

func JWTWithConfig(config JWTConfig) macross.Handler

JWTWithConfig returns a JWT auth middleware with config. See: `JWT()`.

func NewMapClaims

func NewMapClaims() jwt.MapClaims

func NewToken

func NewToken(alg string, claims jwt.MapClaims) *jwt.Token

func NewTokenString

func NewTokenString(secret string, alg string, claims jwt.MapClaims) (string, error)

Types

type JWTConfig

type JWTConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper skipper.Skipper
	Expires time.Duration
	// Signing key to validate token.
	// Required.
	SigningKey interface{} `json:"signing_key"`

	// Signing method, used to check token signing method.
	// Optional. Default value HS256.
	SigningMethod string `json:"signing_method"`

	// Context key to store user information from the token into context.
	// Optional. Default value "user".
	ContextKey string `json:"context_key"`

	// Claims are extendable claims data defining token content.
	// Optional. Default value jwt.MapClaims
	Claims jwt.Claims

	// TokenLookup is a string in the form of "<source>:<name>" that is used
	// to extract token from the request.
	// Optional. Default value "header:Authorization".
	// Possible values:
	// - "header:<name>"
	// - "query:<name>"
	// - "cookie:<name>"
	TokenLookup string `json:"token_lookup"`
	// contains filtered or unexported fields
}

JWTConfig defines the config for JWT middleware.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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