Categorygithub.com/abbot/go-http-auth
modulepackage
0.4.0
Repository: https://github.com/abbot/go-http-auth.git
Documentation: pkg.go.dev

# README

HTTP Authentication implementation in Go

This is an implementation of HTTP Basic and HTTP Digest authentication in Go language. It is designed as a simple wrapper for http.RequestHandler functions.

Features

  • Supports HTTP Basic and HTTP Digest authentication.
  • Supports htpasswd and htdigest formatted files.
  • Automatic reloading of password files.
  • Pluggable interface for user/password storage.
  • Supports MD5, SHA1 and BCrypt for Basic authentication password storage.
  • Configurable Digest nonce cache size with expiration.
  • Wrapper for legacy http handlers (http.HandlerFunc interface)

Example usage

This is a complete working example for Basic auth:

package main

import (
        "fmt"
        "net/http"

        auth "github.com/abbot/go-http-auth"
)

func Secret(user, realm string) string {
        if user == "john" {
                // password is "hello"
                return "$1$dlPL2MqE$oQmn16q49SqdmhenQuNgs1"
        }
        return ""
}

func handle(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
        fmt.Fprintf(w, "<html><body><h1>Hello, %s!</h1></body></html>", r.Username)
}

func main() {
        authenticator := auth.NewBasicAuthenticator("example.com", Secret)
        http.HandleFunc("/", authenticator.Wrap(handle))
        http.ListenAndServe(":8080", nil)
}

See more examples in the "examples" directory.

Legal

This module is developed under Apache 2.0 license, and can be used for open and proprietary projects.

Copyright 2012-2013 Lev Shamardin

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file or any other part of this project except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

# Functions

Parse Authorization header from the http.Request.
FromContext returns authentication information from the context or nil if no such information present.
H function for MD5 algorithm (returns a lower-case hex MD5 digest).
SecretProvider implementation based on htdigest-formated files.
SecretProvider implementation based on htpasswd-formated files.
No description provided by the author
MD5 password crypt implementation */.
No description provided by the author
No description provided by the author
No description provided by the author
ParseList parses a comma-separated list of values as described by RFC 2068 and returns list elements.
ParsePairs extracts key/value pairs from a comma-separated list of values as described by RFC 2068 and returns a map[key]value.
RandomKey returns a random 16-byte base64 alphabet string.

# Constants

AuthUsernameHeader is the header set by JustCheck functions.
Default values for ClientCacheSize and ClientCacheTolerance for DigestAuth */.
No description provided by the author

# Variables

NormalHeaders are the regular Headers used by an HTTP Server for request authentication.
ProxyHeaders are Headers used by an HTTP Proxy server for proxy access authentication.

# Structs

Request handlers must take AuthenticatedRequest instead of http.Request */.
No description provided by the author
No description provided by the author
Common functions for file auto-reloading */.
Headers contains header and error codes used by authenticator.
Structure used for htdigest file authentication.
Structure used for htdigest file authentication.
Info contains authentication information for the request.
No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

AuthenticatedHandlerFunc is like http.HandlerFunc, but takes AuthenticatedRequest instead of http.Request */.
Authenticator wraps an AuthenticatedHandlerFunc with authentication-checking code.
SecretProvider is used by authenticators.