digest

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 7 Imported by: 0

README

digest - package provides header generator for digest authentication.

Quick start

go get -u github.com/gregoryv/digest

Example

The Auth object can be reused for subsequent requests thought it is not thread safe.

req, _ := http.NewRequest("GET", "/", nil)
resp, _ := http.DefaultClient.Do(req)
if resp.StatusCode == http.StatusUnauthorized {
    auth := NewAuth("john.doe", "secret")
    err := auth.Parse(resp.Header.Get("www-authenticate"))
    if err != nil {
        // cannot authenticate using this package
    }
    auth.Authorize(req)
}
resp, _ := http.DefaultClient.Do(req)

// and for the next request just authorize it before sending
auth.Authorize(req2)

Documentation

Overview

Package provides header generator for digest authentication.

The Auth object can be reused for subsequent requests.

Example:

req, _ := http.NewRequest("GET", "/", nil)
resp, _ := http.DefaultClient.Do(req)
if resp.StatusCode == http.StatusUnauthorized {
    auth := NewAuth("john.doe", "secret")
    err := auth.Parse(resp.Header.Get("www-authenticate"))
    if err != nil {
        // cannot authenticate using this package
    }
    auth.Authorize(req)
}
resp, _ := http.DefaultClient.Do(req)

// and for the next request just authorize it before sending
auth.Authorize(req2)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

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

func NewAuth

func NewAuth(username, pwd string) *Auth

func (*Auth) Authorize

func (auth *Auth) Authorize(req *http.Request)

Authorize sets the Authorization header on the given request. Also each call updates nc by one.

Example
auth := NewAuth("john.doe", "secret")
req, _ := http.NewRequest("GET", "/", nil)
auth.Authorize(req)
Output:

func (*Auth) Header

func (auth *Auth) Header(method, uri string) string
Example

Example is the same as described on wikipedia at https://en.wikipedia.org/wiki/Digest_access_authentication

wwwAuth := fmt.Sprintf(
	"Digest %s=%q, %s=%q, %s=%q, %s=%s, %s=%q",
	"realm", "testrealm@host.com",
	"nonce", "dcd98b7102dd2f0e8b11d0f600bfb0c093",
	"algorithm", "MD5",
	"qop", "auth",
	"opaque", "5ccc069c403ebaf9f0171e9517f40e41")
auth := NewAuth("Mufasa", "Circle Of Life")
err := auth.Parse(wwwAuth)
if err != nil {
	// cannot use digest
}

auth.cnonce = "0a4f113b"
hdr := auth.Header("GET", "/dir/index.html")
fmt.Println(strings.Replace(hdr, ", ", ",\n", -1))
Output:

Digest username="Mufasa",
realm="testrealm@host.com",
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
uri="/dir/index.html",
qop=auth,
nc=00000001,
cnonce="0a4f113b",
response="6629fae49393a05397450978507c4ef1",
opaque="5ccc069c403ebaf9f0171e9517f40e41"

func (*Auth) Parse

func (auth *Auth) Parse(authHeader string) error

Parse parses the WWW-Authenticate header value. Once correctly parsed use the Authorize func to set the response header on a subsequent request.

func (*Auth) SetHash added in v0.2.0

func (a *Auth) SetHash(v crypto.Hash)

SetHash to use during authorization. Auth.Parse tries to guess it from the algorithm but when that fails you override it.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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