client

package
v0.0.0-...-395d67b Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2017 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AlgorithmHS256 = "HS256"
)

Algorithims

Variables

View Source
var (
	// DefaultJWTConfig is the default JWT auth middleware config.
	DefaultJWTConfig = JWTConfig{
		Skipper: func(c echo.Context) bool {
			return false
		},
		SigningMethod: AlgorithmHS256,
		ContextKey:    "user",
		TokenLookup:   "header:" + echo.HeaderAuthorization,
	}
)

Functions

func CreateToken

func CreateToken(hID interface{}, encrypt bool) (string, error)

CreateToken creates a jwt token with a ID

func JWT

func JWT(key []byte) echo.MiddlewareFunc

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 sends "401 - Unauthorized" response. For empty or invalid `Authorization` header, it sends "400 - Bad Request".

See: https://jwt.io/introduction

func JWTParse

func JWTParse(auth string, config JWTConfig) (*jwt.Token, error)

func JWTWithConfig

func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc

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

func WillTokenExpire

func WillTokenExpire(expAt int64) bool

WillTokenExpire checks if a token will expire the current range is 5-30 minutes

Types

type API

type API struct {
	DB *gorm.DB
}

API holds all related api handlers

func NewAPI

func NewAPI(d *gorm.DB) *API

NewAPI creates a new api and insert the database provided in it

func (*API) Middleware

func (a *API) Middleware(isTest bool) func(echo.HandlerFunc) echo.HandlerFunc

Middleware handles JWT tokens

func (*API) PostLogin

func (a *API) PostLogin(c echo.Context) error

PostLogin handles post requests to login a existing user

func (*API) PostRegister

func (a *API) PostRegister(c echo.Context) error

PostRegister handles post requests to register a new user account

type Client

type Client struct {
	gorm.Model
	FirstName string `gorm:"first_name" json:"first_name,omitempty"`
	LastName  string `gorm:"last_name" json:"last_name,omitempty"`
	Email     string `gorm:"email" json:"email,omitempty"`
	Secret    string `gorm:"secret" json:"secret,omitempty"`
}

Client is the struct that holds client information

func NewClient

func NewClient() *Client

NewClient creates a new model

type JWTConfig

type JWTConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper

	// Signing key to validate token.
	// Required.
	SigningKey []byte `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"`

	// 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>"
	TokenLookup string `json:"token_lookup"`
}

JWTConfig defines the config for JWT middleware.

type Register

type Register struct {
	FirstName string `json:"first_name" valid:"alphanum,length(2|100),required"`
	LastName  string `json:"last_name"  valid:"alphanum,length(2|100),required"`
	Email     string `json:"email"      valid:"email,length(6|255),required"`
	Secret    string `json:"password"   valid:"length(6|255),required"`

	Admin bool `json:"admin,omitempty"`
}

Register handles post requests to register a new user account

type UserToken

type UserToken struct {
	UID string `json:"id"`
	jwt.StandardClaims
}

Jump to

Keyboard shortcuts

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