<commit_before>// Package hawk implements the Hawk HTTP authentication scheme.
package hawk

import (
	"bytes"
	"crypto/hmac"
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"hash"
	"io"
	"net"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
	"strings"
	"time"
)

// Now is a func() time.Time that is used by the package to get the current time.
var Now = time.Now

// MaxTimestampSkew is the maximum ±skew that a request timestamp can have without returning ErrTimestampSkew.
var MaxTimestampSkew = time.Minute

var (
	ErrNoAuth             = AuthError("no Authorization header or bewit parameter found")
	ErrReplay             = AuthError("request nonce is being replayed")
	ErrInvalidMAC         = AuthError("invalid MAC")
	ErrBewitExpired       = AuthError("bewit expired")
	ErrTimestampSkew      = AuthError("timestamp skew too high")
	ErrMissingServerAuth  = AuthError("missing Server-Authentication header")
	ErrInvalidBewitMethod = AuthError("bewit only allows HEAD and GET requests")
)

type AuthError string

func (e AuthError) Error() string { return "hawk: " + string(e) }

type CredentialErrorType int

const (
	UnknownID CredentialErrorType = iota
	UnknownApp
	IDAppMismatch
)

func (t CredentialErrorType) String() string {
	switch t {
	case UnknownApp:
		return "unknown app"
	case IDAppMismatch:
		return "id/app mismatch"
	}
	return "unknown id"
}

// CredentialError is returned by a CredentialsLookupFunc when the provided credentials
// ID is invalid.
type CredentialError struct {
	Type        CredentialErrorType
	Credentials *Credentials
}

func (e *CredentialError) Error() string {
	return fmt.Sprintf("hawk: credential error with id %s and app %s: %s", e.Credentials.ID, e.Credentials.App, e.Type)
}

type Credentials struct {
	ID   string
	Key  string
	Hash func() hash.Hash

	// Data may be set in a CredentialsLookupFunc to correlate the credentials
	// with an internal data record.
	Data interface{}

	App      string
	Delegate string
}

func (creds *Credentials) MAC() hash.Hash { return hmac.New(creds.Hash, []byte(creds.Key)) }

type AuthType int

const (
	AuthHeader AuthType = iota
	AuthResponse
	AuthBewit
)

func (a AuthType) String() string {
	switch a {
	case AuthResponse:
		return "response"
	case AuthBewit:<commit_msg>Set hawk.Credentials.MAC() to use sha256 as default hash func<commit_after>// Package hawk implements the Hawk HTTP authentication scheme.
package hawk

import (
	"bytes"
	"crypto/hmac"
	"crypto/rand"
	"crypto/sha256"
	"encoding/base64"
	"fmt"
	"hash"
	"io"
	"net"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
	"strings"
	"time"
)

// Now is a func() time.Time that is used by the package to get the current time.
var Now = time.Now

// MaxTimestampSkew is the maximum ±skew that a request timestamp can have without returning ErrTimestampSkew.
var MaxTimestampSkew = time.Minute

var (
	ErrNoAuth             = AuthError("no Authorization header or bewit parameter found")
	ErrReplay             = AuthError("request nonce is being replayed")
	ErrInvalidMAC         = AuthError("invalid MAC")
	ErrBewitExpired       = AuthError("bewit expired")
	ErrTimestampSkew      = AuthError("timestamp skew too high")
	ErrMissingServerAuth  = AuthError("missing Server-Authentication header")
	ErrInvalidBewitMethod = AuthError("bewit only allows HEAD and GET requests")
)

type AuthError string

func (e AuthError) Error() string { return "hawk: " + string(e) }

type CredentialErrorType int

const (
	UnknownID CredentialErrorType = iota
	UnknownApp
	IDAppMismatch
)

func (t CredentialErrorType) String() string {
	switch t {
	case UnknownApp:
		return "unknown app"
	case IDAppMismatch:
		return "id/app mismatch"
	}
	return "unknown id"
}

// CredentialError is returned by a CredentialsLookupFunc when the provided credentials
// ID is invalid.
type CredentialError struct {
	Type        CredentialErrorType
	Credentials *Credentials
}

func (e *CredentialError) Error() string {
	return fmt.Sprintf("hawk: credential error with id %s and app %s: %s", e.Credentials.ID, e.Credentials.App, e.Type)
}

type Credentials struct {
	ID   string
	Key  string
	Hash func() hash.Hash

	// Data may be set in a CredentialsLookupFunc to correlate the credentials
	// with an internal data record.
	Data interface{}

	App      string
	Delegate string
}

func (creds *Credentials) MAC() hash.Hash {
	if creds.Hash != nil {
		return hmac.New(creds.Hash, []byte(creds.Key))
	} else {
		// use a default hash
		return hmac.New(sha256.New, []byte(creds.Key))
	}
}

type AuthType int

const (
	AuthHeader AuthType = iota
	AuthResponse
	AuthBewit
)

func (a AuthType) String() string {
	switch a {
	case AuthResponse:
		return "response"
	case AuthBewit:
{'fraction_non_alphanumeric': 0.09139440529575059, 'fraction_numerical': 0.002775998291693359, 'mean_word_length': 3.9305263157894736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3640815', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1513, 'n_words': 501}