httpdigest

package module
v0.0.0-...-1057ac3 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2023 License: MIT Imports: 15 Imported by: 4

README

GoDoc

Go HTTP Digest Authentication

Overview

This is package based on the (currently unmantained) digest package. I coded this new approach to cover my use case, the monero-wallet-rpc digest authentication. For now, it only supports auth qop, MD5.

Usage

See the godocs for details.

License

MIT (see LICENSE.txt)

Documentation

Overview

The httpdigest package provides an implementation of http.RoundTripper that resolves a HTTP Digest Authentication (https://tools.ietf.org/html/rfc2617). At the moment, this only implements the MD5 and "auth" portions of the RFC. This package was created initially to cover a monero-wallet-rpc call using digest authentication.

Example (monero-wallet-rpc with digest):

package main

import (
	//"net/http"
	"fmt"

	"github.com/gabstv/go-monero/walletrpc"
	"github.com/gabstv/httpdigest"
)

func main() {
	t := httpdigest.New("john", "doe")

	// to do a normal http request:
	//
	// cl := &http.Client{
	// 	Transport: t,
	// }
	// req, _ := http.NewRequest(http.MethodGet, "url", nil)
	// resp, err := cl.Do(req)

	client := walletrpc.New(walletrpc.Config{
		Address:   "http://127.0.0.1:29567/json_rpc",
		Transport: t,
	})

	balance, unlocked, err := client.Getbalance()

	if err != nil {
		panic(err)
	}
	fmt.Println("balance", walletrpc.XMRToDecimal(balance))
	fmt.Println("unlocked balance", walletrpc.XMRToDecimal(unlocked))
}

Index

Constants

This section is empty.

Variables

View Source
var Debug bool

Functions

This section is empty.

Types

type DigestInput

type DigestInput struct {
	Username  string
	Password  string
	DigestURI string
	// nonce-count
	// The nc-value is the hexadecimal
	// count of the number of requests (including the current request)
	// that the client has sent with the nonce value in this request.  For
	// example, in the first request sent in response to a given nonce
	// value, the client sends "nc=00000001".  The purpose of this
	// directive is to allow the server to detect request replays by
	// maintaining its own copy of this count - if the same nc-value is
	// seen twice, then the request is a replay.
	NonceCount uint
	Cnonce     string
	Method     string
}

type Transport

type Transport struct {
	Username  string
	Password  string
	Transport http.RoundTripper
	// Generator function for cnonce. If not specified, the transport will
	// generate one automatically.
	CnonceGen func() string
}

Transport is an implementation of http.RoundTripper that can handle http digest authentication.

func New

func New(username, password string) *Transport

NewTransport creates a new digest transport using the http.DefaultTransport. You may change the underlying transport if needed (i.e: handling self-signed certificates).

func (*Transport) Client

func (t *Transport) Client() (*http.Client, error)

Client returns an HTTP client that uses the digest transport.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip makes a request expecting a 401 response that will require digest authentication. If a 401 is received, it creates the credentials it needs and makes a follow-up request.

type WWWAuth

type WWWAuth struct {
	Realm     string
	Domain    string
	Nonce     string
	Opaque    string
	Stale     string
	Algorithm string
	Qop       string
}

func ParseWWWAuthenticate

func ParseWWWAuthenticate(entry string) (wwwa *WWWAuth, err error)

func (*WWWAuth) Digest

func (a *WWWAuth) Digest(inp DigestInput) (auth string, err error)

Jump to

Keyboard shortcuts

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