security

package
v0.0.0-...-9931aa1 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2018 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnauthorized = errors.New("unauthorized token")
)

Functions

func DecodeJwt

func DecodeJwt(secret Secret, tokenString EncodedJwt) (token *jwt.Token, err error)

func GetActualRemoteHost

func GetActualRemoteHost(r *http.Request) (host string, err error)

Types

type EncodedJwt

type EncodedJwt string

func EncodeJwt

func EncodeJwt(secret Secret, claims *jwt.StandardClaims) (EncodedJwt, error)

func GenJwt

func GenJwt(secret Secret, fileId string) EncodedJwt

func GetJwt

func GetJwt(r *http.Request) EncodedJwt

type Guard

type Guard struct {
	SecretKey Secret
	// contains filtered or unexported fields
}

Guard is to ensure data access security. There are 2 ways to check access:

  1. white list. It's checking request ip address.
  2. JSON Web Token(JWT) generated from secretKey. The jwt can come from:
  3. url parameter jwt=...
  4. request header "Authorization"
  5. cookie with the name "jwt"

The white list is checked first because it is easy. Then the JWT is checked.

The Guard will also check these claims if provided: 1. "exp" Expiration Time 2. "nbf" Not Before

Generating JWT:

  1. use HS256 to sign
  2. optionally set "exp", "nbf" fields, in Unix time, the number of seconds elapsed since January 1, 1970 UTC.

Referenced: https://github.com/pkieltyka/jwtauth/blob/master/jwtauth.go

func NewGuard

func NewGuard(whiteList []string, secretKey string) *Guard

func (*Guard) Secure

func (g *Guard) Secure(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request)

func (*Guard) WhiteList

func (g *Guard) WhiteList(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request)

type Secret

type Secret string

Jump to

Keyboard shortcuts

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