Help us shape the future of Go! Take the 2026 Go Developer Survey

gdigest

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

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

Go to latest
Published: May 30, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

README

gdigest

license goreportcard

gdigest handles HTTP Digest (Access) Authentication. Basically a straight-forward implementation after existing wiki page. Nothing fancy...

gdigest supports:

  • Algorithm: Undefined, MD5 and MD5-session
  • Quality of protection: Undefined, auth and auth-int
  • Unique client nonce per request
  • Nonce counter

Usage

Authorization Header

Handle the authorization header by your own:

package main

import (
    "fmt"
    "github.com/mrccnt/gdigest"
)

func main()  {
    
	// Do it yourself
	
    // Create a new digest reference for given host, user and pass.
    // In this example we are dealing with a CalDAV server.
    digest := gdigest.NewDigest("<username>", "<password>", "https://cal.example.com")
    
    // Every time you need to send an authenticated request to the server
    // you need to fetch a seperate authorization header. In this case we want to
    // fetch calendar data from a server via REPORT method at given URI.
    authString, err := digest.Do("/calendars/<username>/default", "REPORT", "")
    if err != nil {
        panic(err)
    }
    
    fmt.Println(authString)
}

As a result of calling digest.Do() the ready to use http authorization header will be returned as a string. This header can be used for the next digest (access) authenticated http request.

Beautified authorization header example:

Digest  realm="ExampleCalendar",
        nonce="a39bbdf0983957e6",
        algorithm="MD5",
        qop="auth",
        nc="00000001",
        cnonce="55e0a867f0fc959b",
        response="5a7ab8e5b9ffb7005a7ab8e5b9ffb70a",
        opaque="50849db2c592506cf600cc2e68485efa",
        uri="/calendars/acme/default",
        username="acme"
digest.Request

Let the request object handle it for you:

package main

import (
	"fmt"
	"github.com/mrccnt/gdigest"
	"io/ioutil"
	"net/http"
	"strings"
)

func main()  {
    
	// Instead of http.NewRequest use gdigest.NewRequest
    req, err := gdigest.NewRequest("REPORT", "https://cal.example.com/calendars/<username>/default", strings.NewReader("<body>"))
    if err != nil {
        panic(err.Error())
    }
    
    // Function SetDigestAuth() is available in addition to SetBasicAuth
    err = req.SetDigestAuth("<username>", "<password>", "https://cal.example.com", "/calendars/<username>/default", "REPORT", "<body>")
    if err != nil {
        panic(err.Error())
    }

    client := http.Client{}

    // Use "req.Request" instead of "req"
    res, err := client.Do(req.Request)
    if err != nil {
        panic(err.Error())
    }
    defer res.Body.Close()
    
    content, err := ioutil.ReadAll(res.Body)
    if err != nil {
        panic(err.Error())
    }
    
    fmt.Println(string(content))
}

Clean Code

That is more or less a reminder to myself... ;)

    golangci-lint run --fix --enable=gofmt --out-format checkstyle > checkstyle.xml

Documentation

Index

Constants

View Source
const (
	AlgUnspec = ""
	AlgMD5    = "MD5"
	AlgSess   = "MD5-sess"
	QopUnspec = ""
	QopAuth   = "auth"
	QopInt    = "auth-int"
)

Algorithm & Quality of protection hashing options

Variables

View Source
var (
	ErrAuthStatus  = errors.New("status code 401 expected")
	ErrAuthHeader  = errors.New("no www-authenticate response header found")
	ErrAuthInvalid = errors.New("www-authenticate is invalid")
	ErrAlgInvalid  = errors.New("invalid algorithm")
	ErrQopInvalid  = errors.New("invalid qop")
)

Possible errors

Functions

This section is empty.

Types

type Digest

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

Digest stores authentication parameters

func NewDigest

func NewDigest(user string, pass string, host string) *Digest

NewDigest returns reference of Digest

func (*Digest) Do

func (d *Digest) Do(uri string, method string, body string) (string, error)

Do returns the generated auth string for given values for your next requests authorization header. Simply skip supplying a body parameter (leave empty) if you know your server does not make use of qop=auth-int

type Request

type Request struct {
	*http.Request
}

Request is a custom http.Request

func NewRequest

func NewRequest(method, url string, body io.Reader) (*Request, error)

NewRequest reflects http.NewRequest and returns a *DigestRequest

func (*Request) SetDigestAuth

func (r *Request) SetDigestAuth(user, pass, host, uri, method, body string) error

SetDigestAuth enables digest authentication

Jump to

Keyboard shortcuts

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