README

Authentication

Build Status Coverage Status Go.Dev reference Go Report Card Release Sourcegraph

This package provides multiple authenticators and middleware for authenticating a user.

Usage

See example.

Credit

Inspired by Yii Auth.

Documentation

Index

Constants

View Source
const IdentityKey contextKey = 1

    IdentityKey is the context key of identity.

    Variables

    View Source
    var (
    	ErrNoCredentials      = errors.New("no credentials provided")
    	ErrInvalidCredentials = errors.New("invalid credentials")
    )

      Errors

      Functions

      func NewMiddleware

      func NewMiddleware(authenticator Authenticator) func(http.Handler) http.Handler

        NewMiddleware returns a middleware with the given authenticator.

        Types

        type Authenticator

        type Authenticator interface {
        	// Authenticates the current user.
        	Authenticate(*http.Request, http.ResponseWriter) (Identity, error)
        
        	// Challenge generates challenges upon authentication failure.
        	Challenge(*http.Request, http.ResponseWriter)
        }

          Authenticator is an interface that authenticates an user.

          type Identity

          type Identity interface {
          	GetID() string
          }

            Identity is an interface that should be implemented by an user instance.

            func GetIdentity

            func GetIdentity(ctx context.Context) Identity

              GetIdentity retrieves the identity from context.

              type IdentityStore

              type IdentityStore interface {
              	// Gets identity by ID.
              	GetIdentity(ctx context.Context, id string) (Identity, error)
              
              	// Gets identity by the given token and token type.
              	GetIdentityByToken(ctx context.Context, token, tokenType string) (Identity, error)
              }

                IdentityStore is a store interface for retrieving identity by ID or token.

                Directories

                Path Synopsis