Package rfc7617

import "funnyhaha.technology/rfc/rfc7617"
View the source code

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

Example shows a 401 that challenges the client for Basic credentials encoded as UTF-8.

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"))
}

Output:

401 Basic realm="WallyWorld", charset="UTF-8"

Index

Variables

var ErrInvalidRealm = errors.New("invalid realm")
source

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) Challenge

MustChallenge 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.Challenge

Challenge returns c as an rfc9110.Challenge:

rfc9110.Unauthorized(basic.Challenge())

(Challenge) WithCharsetUTF8 source

func (c Challenge) WithCharsetUTF8() Challenge

WithCharsetUTF8 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.