Package rfc7617
Overview
Package rfc7617 builds challenges for the Basic authentication scheme, defined by RFC 7617 (The 'Basic' HTTP Authentication Scheme):
basic := rfc7617.MustChallenge("WallyWorld").WithCharsetUTF8()
rfc9110.Unauthorized(basic.Challenge())The package doesn't parse credentials, because net/http.Request.BasicAuth already does.
The RFC text is in rfc7617.txt in this directory, and the doc comments cite
its sections. Example shows a 401 that challenges the client for Basic credentials encoded
as UTF-8. Output:Example
package main
import (
"fmt"
"funnyhaha.technology/rfc/rfc7617"
"funnyhaha.technology/rfc/rfc9110"
)
func main() {
basic := rfc7617.MustChallenge("WallyWorld").WithCharsetUTF8()
s := rfc9110.Unauthorized(basic.Challenge())
fmt.Println(s.Code(), s.Header().Get("WWW-Authenticate"))
}
401 Basic realm="WallyWorld", charset="UTF-8"
Index
Variables
var ErrInvalidRealm = errors.New("invalid realm")ErrInvalidRealm is returned for a realm that's empty or contains a control character.
Types
Challenge source
type Challenge struct {
// contains filtered or unexported fields
}Challenge is a Basic challenge for a realm. Build it once, with NewChallenge or MustChallenge.
See RFC 7617, Section 2.
MustChallenge source
func MustChallenge(realm string) ChallengeMustChallenge is like NewChallenge but panics if realm isn't valid. Use it while wiring a server, so a bad realm stops it starting.
NewChallenge source
func NewChallenge(realm string) (Challenge, error)NewChallenge returns a challenge for the realm, which Section 2 says is REQUIRED. It returns ErrInvalidRealm if realm is empty or contains a control character.
(Challenge) Challenge source
func (c Challenge) Challenge() rfc9110.ChallengeChallenge returns c as an rfc9110.Challenge:
rfc9110.Unauthorized(basic.Challenge())(Challenge) WithCharsetUTF8 source
func (c Challenge) WithCharsetUTF8() ChallengeWithCharsetUTF8 returns a copy of c with charset="UTF-8", telling the client to encode the user-id and password as UTF-8. "UTF-8" is the only value Section 2.1 allows.