Documentation ¶
Index ¶
Examples ¶
Constants ¶
const Prefix = "Digest "
Prefix for digest authentication headers
Variables ¶
var ErrNoChallenge = errors.New("digest: no challenge found")
ErrNoChallenge indicates that no WWW-Authenticate headers were found.
Functions ¶
Types ¶
type Challenge ¶
type Challenge struct { Realm string Domain []string Nonce string Opaque string Stale bool Algorithm string QOP []string Charset string Userhash bool }
Challenge is a challenge sent in the WWW-Authenticate header
func FindChallenge ¶
FindChallenge returns the first supported challenge in the headers
func ParseChallenge ¶
ParseChallenge parses the WWW-Authenticate header challenge
func (*Challenge) SupportsQOP ¶
SupportsQOP returns true if the challenge advertises support for the provided qop value
type Credentials ¶
type Credentials struct { Username string Realm string Nonce string URI string Response string Algorithm string Cnonce string Opaque string QOP string Nc int Userhash bool }
Credentials is a parsed version of the Authorization header
func Digest ¶
func Digest(chal *Challenge, o Options) (*Credentials, error)
Digest creates credentials from a challenge and request options. Note: if you want to re-use a challenge, you must increment the Count.
Example ¶
package main import ( "net/http" "github.com/icholy/digest" ) func main() { // The first request will return a 401 Unauthorized response req, _ := http.NewRequest(http.MethodGet, "http://httpbin.org/digest-auth/auth/foo/bar/SHA-512", nil) res, _ := http.DefaultClient.Do(req) // Create digest credentials from the request challenge chal, _ := digest.FindChallenge(res.Header) cred, _ := digest.Digest(chal, digest.Options{ Method: req.Method, URI: req.URL.RequestURI(), Username: "foo", Password: "bar", }) // Try the request again with the credentials req.Header.Set("Authorization", cred.String()) res, _ = http.DefaultClient.Do(req) println(res.Status) }
Output:
func ParseCredentials ¶
func ParseCredentials(s string) (*Credentials, error)
ParseCredentials parses the Authorization header value into credentials
func (*Credentials) String ¶
func (c *Credentials) String() string
String formats the credentials into the header format
type Options ¶
type Options struct { Method string URI string GetBody func() (io.ReadCloser, error) Count int Username string Password string // used for testing Cnonce string }
Options for creating a credentials
type Transport ¶
type Transport struct { Username string Password string // Digest computes the digest credentials. // If nil, the Digest function is used. Digest func(*http.Request, *Challenge, Options) (*Credentials, error) // FindChallenge extracts the challenge from the request headers. // If nil, the FindChallenge function is used. FindChallenge func(http.Header) (*Challenge, error) // Transport specifies the mechanism by which individual // HTTP requests are made. // If nil, DefaultTransport is used. Transport http.RoundTripper // Jar specifies the cookie jar. // // The Jar is used to insert relevant cookies into every // outbound Request and is updated with the cookie values // of every inbound Response. The Jar is consulted for every // redirect that the Client follows. // // If Jar is nil, cookies are only sent if they are explicitly // set on the Request. Jar http.CookieJar // NoReuse prevents the transport from reusing challenges. NoReuse bool // contains filtered or unexported fields }
Transport implements http.RoundTripper
Example ¶
package main import ( "net/http" "github.com/icholy/digest" ) func main() { client := http.Client{ Transport: &digest.Transport{ Username: "foo", Password: "bar", }, } res, _ := client.Get("http://httpbin.org/digest-auth/auth/foo/bar/SHA-512") println(res.Status) }
Output: