auth

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

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

Go to latest
Published: Apr 10, 2016 License: MIT Imports: 5 Imported by: 0

README

auth Build Status GoDoc Coverage Status Go Report Card

Simple HTTP authentication middleware, supporting Basic, Bearer, token and other authentication schemes.

Installation

go get -u gopkg.in/vinxi/auth.v0

API

See godoc reference.

Examples

Unique basic user auth
package main

import (
  "fmt"
  "gopkg.in/vinxi/auth.v0"
  "gopkg.in/vinxi/vinxi.v0"
)

const port = 3100

func main() {
  // Create a new vinxi proxy
  vs := vinxi.NewServer(vinxi.ServerOptions{Port: port})
  
  // Attach the auth middleware 
  vs.Use(auth.User("foo", "pas$w0rd"))
  
  // Target server to forward
  vs.Forward("http://httpbin.org")

  fmt.Printf("Server listening on port: %d\n", port)
  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}
Custom config allowing multiple credentials types
package main

import (
  "fmt"
  "gopkg.in/vinxi/auth.v0"
  "gopkg.in/vinxi/vinxi.v0"
)

const port = 3100

func main() {
  // Create a new vinxi proxy
  vs := vinxi.NewServer(vinxi.ServerOptions{Port: port})

  // Bind the auth middleware with custom config
  // Any of the following credentials will be authorized
  tokens := []auth.Token{
    {Type: "Basic", Value: "foo:s3cr3t"},
    {Type: "Bearer", Value: "s3cr3t"},
    {Value: "s3cr3t token"},
  }
  vs.Use(auth.New(&auth.Config{Tokens: tokens}))

  // Target server to forward
  vs.Forward("http://httpbin.org")

  fmt.Printf("Server listening on port: %d\n", port)
  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}

License

MIT

Documentation

Index

Constants

View Source
const Version = "0.1.0"

Version defines the current package semantic version.

Variables

This section is empty.

Functions

func DecodeBasicAuthHeader

func DecodeBasicAuthHeader(value string) (string, error)

DecodeBasicAuthHeader decodes a given string as HTTP basic auth scheme.

func MatchAuthHeader

func MatchAuthHeader(tokens []Token, token Token) bool

MatchAuthHeader matches an authorization header againts the allowed tokens.

Types

type BasicAuth

type BasicAuth struct {
	User, Password string
}

BasicAuth represents the user-password pair used as helper struct.

type Config

type Config struct {
	// RealM token used for unauthorized
	RealM string

	// Tokens stores a list of allowed authorization tokens.
	// Tokens could be of any type.
	Tokens []Token

	// Matchers stores a list of matchers used to compare an incoming
	// authorization header againts the registered allowed tokens.
	Matchers []Matcher
}

Config represents the authorization middleware configuration.

type Handler

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

Handler authentication middleware handler.

func New

func New(cfg *Config) *Handler

New creates a new authorization middleware handler with the given config.

func Tokens

func Tokens(tokens ...string) *Handler

Tokens creates a new auth handler allowing the given token strings. Tokens must be transported via Authorization header.

func User

func User(user, password string) *Handler

User function is optional but handy, used to check input parameters when creating new middlewares

func Users

func Users(users ...BasicAuth) *Handler

Users creates a new auth middleware allowing access to the list of users.

func (*Handler) HandleHTTP

func (a *Handler) HandleHTTP(w http.ResponseWriter, r *http.Request, h http.Handler)

HandleHTTP will be called each time the request hits the location with this middleware activated

type Matcher

type Matcher func([]Token, Token) bool

Matcher represents the required auth header matcher function signature implemented by matchers.

type Token

type Token struct {
	// Type stores the authorization type. Usually Basic or Bearer.
	Type string

	// Value stores the authorization token.
	// If the authorization token has no type, this field will be filled instead.
	Value string
}

Token represents the parsed schema of an HTTP authorization header.

func ParseAuthHeader

func ParseAuthHeader(header string) (Token, error)

ParseAuthHeader parses the given authentication header

Source Files

  • auth.go
  • basic.go
  • version.go

Directories

Path Synopsis
_examples
config command
mux command
user command

Jump to

Keyboard shortcuts

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