basicauth

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2019 License: MIT Imports: 4 Imported by: 0

README

cod-basic-auth

Build Status

Basic auth middleware for cod.

package main

import (
	"bytes"
	"fmt"

	"github.com/vicanso/cod"
	basicauth "github.com/vicanso/cod-basic-auth"
	"github.com/vicanso/hes"
)

func main() {
	d := cod.New()

	d.Use(basicauth.New(basicauth.Config{
		Validate: func(account, pwd string, c *cod.Context) (bool, error) {
			if account == "tree.xie" && pwd == "password" {
				return true, nil
			}
			if account == "n" {
				return false, hes.New("account is invalid")
			}
			return false, nil
		},
	}))

	d.GET("/", func(c *cod.Context) (err error) {
		c.BodyBuffer = bytes.NewBufferString("hello world")
		return
	})

	d.ListenAndServe(":7001")
}

Documentation

Index

Constants

View Source
const (

	// ErrCategory basic auth error category
	ErrCategory = "cod-basic-auth"
)

Variables

This section is empty.

Functions

func New

func New(config Config) cod.Handler

New new basic auth

Types

type Config

type Config struct {
	Realm    string
	Validate Validate
	Skipper  cod.Skipper
}

Config basic auth config

type Validate

type Validate func(username string, password string, c *cod.Context) (bool, error)

Validate validate function

Jump to

Keyboard shortcuts

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