keyauth

package module
v0.0.0-...-d02ab20 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

keyauth(This is a community driven project)

keyauth for hertz

Install

go get github.com/hertz-contrib/keyauth
import
import "github.com/hertz-contrib/keyauth"

Example

package main

import (
	"context"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/common/utils"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
	"github.com/hertz-contrib/keyauth"
)

func main() {
	h := server.Default()
	h.Use(keyauth.New(
		keyauth.WithContextKey("token"),
		keyauth.WithKeyLookUp("query:token", ""),
	))
	h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
		value, _ := ctx.Get("token")
		ctx.JSON(consts.StatusOK, utils.H{"ping": value})
	})
	h.Spin()
}

Test

curl 'http://127.0.0.1:8888/ping?token=token'

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMissingOrMalformedAPIKey = errors.New("missing or malformed API Key")

ErrMissingOrMalformedAPIKey When there is no request of the key thrown ErrMissingOrMalformedAPIKey

Functions

func KeyFromCookie

func KeyFromCookie(name string) func(*app.RequestContext) (string, error)

KeyFromCookie returns a function that extracts api key from the named cookie.

func KeyFromForm

func KeyFromForm(param string) func(*app.RequestContext) (string, error)

KeyFromForm returns a function that extracts api key from the form.

func KeyFromHeader

func KeyFromHeader(header, authScheme string) func(*app.RequestContext) (string, error)

KeyFromHeader returns a function that extracts api key from the request header.

func KeyFromParam

func KeyFromParam(param string) func(*app.RequestContext) (string, error)

KeyFromParam returns a function that extracts api key from the url param string.

func KeyFromQuery

func KeyFromQuery(param string) func(*app.RequestContext) (string, error)

KeyFromQuery returns a function that extracts api key from the query string.

func New

func New(opts ...Option) app.HandlerFunc

Types

type KeyAuthErrorHandler

type KeyAuthErrorHandler func(context.Context, *app.RequestContext, error)

type KeyAuthFilterHandler

type KeyAuthFilterHandler func(c context.Context, ctx *app.RequestContext) bool

type KeyAuthValidatorHandler

type KeyAuthValidatorHandler func(context.Context, *app.RequestContext, string) (bool, error)

type Option

type Option struct {
	F func(o *Options)
}

Option is the only struct that can be used to set Options.

func WithContextKey

func WithContextKey(key string) Option

func WithErrorHandler

func WithErrorHandler(f KeyAuthErrorHandler) Option

func WithFilter

func WithFilter(f KeyAuthFilterHandler) Option

func WithKeyLookUp

func WithKeyLookUp(lookup, authScheme string) Option

func WithSuccessHandler

func WithSuccessHandler(f app.HandlerFunc) Option

func WithValidator

func WithValidator(f KeyAuthValidatorHandler) Option

type Options

type Options struct {
	// contains filtered or unexported fields
}

func NewOptions

func NewOptions(opts ...Option) *Options

func (*Options) Apply

func (o *Options) Apply(opts []Option)

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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