2015-05-21 20:14:46 +02:00
|
|
|
package auth
|
2015-04-17 22:32:51 +02:00
|
|
|
|
|
|
|
import (
|
2015-06-30 19:56:29 +02:00
|
|
|
"fmt"
|
2015-04-17 22:32:51 +02:00
|
|
|
"net/http"
|
2015-06-30 19:56:29 +02:00
|
|
|
"net/url"
|
2015-04-17 22:32:51 +02:00
|
|
|
"strings"
|
2016-08-23 16:09:18 +02:00
|
|
|
"sync"
|
2015-04-17 22:32:51 +02:00
|
|
|
)
|
|
|
|
|
2015-05-21 20:14:46 +02:00
|
|
|
// Challenge carries information from a WWW-Authenticate response header.
|
|
|
|
// See RFC 2617.
|
|
|
|
type Challenge struct {
|
|
|
|
// Scheme is the auth-scheme according to RFC 2617
|
|
|
|
Scheme string
|
|
|
|
|
|
|
|
// Parameters are the auth-params according to RFC 2617
|
2015-04-17 22:32:51 +02:00
|
|
|
Parameters map[string]string
|
|
|
|
}
|
|
|
|
|
2015-06-30 19:56:29 +02:00
|
|
|
// ChallengeManager manages the challenges for endpoints.
|
|
|
|
// The challenges are pulled out of HTTP responses. Only
|
|
|
|
// responses which expect challenges should be added to
|
|
|
|
// the manager, since a non-unauthorized request will be
|
|
|
|
// viewed as not requiring challenges.
|
|
|
|
type ChallengeManager interface {
|
|
|
|
// GetChallenges returns the challenges for the given
|
|
|
|
// endpoint URL.
|
2016-02-26 23:18:09 +01:00
|
|
|
GetChallenges(endpoint url.URL) ([]Challenge, error)
|
2015-06-30 19:56:29 +02:00
|
|
|
|
|
|
|
// AddResponse adds the response to the challenge
|
|
|
|
// manager. The challenges will be parsed out of
|
|
|
|
// the WWW-Authenicate headers and added to the
|
|
|
|
// URL which was produced the response. If the
|
|
|
|
// response was authorized, any challenges for the
|
|
|
|
// endpoint will be cleared.
|
|
|
|
AddResponse(resp *http.Response) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSimpleChallengeManager returns an instance of
|
|
|
|
// ChallengeManger which only maps endpoints to challenges
|
|
|
|
// based on the responses which have been added the
|
|
|
|
// manager. The simple manager will make no attempt to
|
|
|
|
// perform requests on the endpoints or cache the responses
|
|
|
|
// to a backend.
|
|
|
|
func NewSimpleChallengeManager() ChallengeManager {
|
2016-08-23 16:09:18 +02:00
|
|
|
return &simpleChallengeManager{
|
|
|
|
Challanges: make(map[string][]Challenge),
|
|
|
|
}
|
2015-06-30 19:56:29 +02:00
|
|
|
}
|
|
|
|
|
2016-08-25 01:34:36 +02:00
|
|
|
type simpleChallengeManager struct {
|
2016-08-23 16:09:18 +02:00
|
|
|
sync.RWMutex
|
2016-08-25 01:34:36 +02:00
|
|
|
Challanges map[string][]Challenge
|
2016-08-23 16:09:18 +02:00
|
|
|
}
|
2015-06-30 19:56:29 +02:00
|
|
|
|
2016-09-01 23:46:31 +02:00
|
|
|
func normalizeURL(endpoint *url.URL) {
|
2016-02-26 23:18:09 +01:00
|
|
|
endpoint.Host = strings.ToLower(endpoint.Host)
|
2016-09-19 23:14:07 +02:00
|
|
|
endpoint.Host = canonicalAddr(endpoint)
|
2016-09-01 23:46:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *simpleChallengeManager) GetChallenges(endpoint url.URL) ([]Challenge, error) {
|
|
|
|
normalizeURL(&endpoint)
|
2016-02-26 23:18:09 +01:00
|
|
|
|
2016-08-23 16:09:18 +02:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
challenges := m.Challanges[endpoint.String()]
|
2015-06-30 19:56:29 +02:00
|
|
|
return challenges, nil
|
|
|
|
}
|
|
|
|
|
2016-08-23 16:09:18 +02:00
|
|
|
func (m *simpleChallengeManager) AddResponse(resp *http.Response) error {
|
2015-06-30 19:56:29 +02:00
|
|
|
challenges := ResponseChallenges(resp)
|
|
|
|
if resp.Request == nil {
|
|
|
|
return fmt.Errorf("missing request reference")
|
|
|
|
}
|
|
|
|
urlCopy := url.URL{
|
|
|
|
Path: resp.Request.URL.Path,
|
2016-09-01 23:46:31 +02:00
|
|
|
Host: resp.Request.URL.Host,
|
2015-06-30 19:56:29 +02:00
|
|
|
Scheme: resp.Request.URL.Scheme,
|
|
|
|
}
|
2016-09-19 23:14:07 +02:00
|
|
|
normalizeURL(&urlCopy)
|
|
|
|
|
2016-08-23 16:09:18 +02:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
m.Challanges[urlCopy.String()] = challenges
|
2015-06-30 19:56:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-16 01:10:48 +02:00
|
|
|
// Octet types from RFC 2616.
|
|
|
|
type octetType byte
|
|
|
|
|
2015-04-17 22:32:51 +02:00
|
|
|
var octetTypes [256]octetType
|
|
|
|
|
|
|
|
const (
|
|
|
|
isToken octetType = 1 << iota
|
|
|
|
isSpace
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// OCTET = <any 8-bit sequence of data>
|
|
|
|
// CHAR = <any US-ASCII character (octets 0 - 127)>
|
|
|
|
// CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
|
|
|
|
// CR = <US-ASCII CR, carriage return (13)>
|
|
|
|
// LF = <US-ASCII LF, linefeed (10)>
|
|
|
|
// SP = <US-ASCII SP, space (32)>
|
|
|
|
// HT = <US-ASCII HT, horizontal-tab (9)>
|
|
|
|
// <"> = <US-ASCII double-quote mark (34)>
|
|
|
|
// CRLF = CR LF
|
|
|
|
// LWS = [CRLF] 1*( SP | HT )
|
|
|
|
// TEXT = <any OCTET except CTLs, but including LWS>
|
|
|
|
// separators = "(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\" | <">
|
|
|
|
// | "/" | "[" | "]" | "?" | "=" | "{" | "}" | SP | HT
|
|
|
|
// token = 1*<any CHAR except CTLs or separators>
|
|
|
|
// qdtext = <any TEXT except <">>
|
|
|
|
|
|
|
|
for c := 0; c < 256; c++ {
|
|
|
|
var t octetType
|
|
|
|
isCtl := c <= 31 || c == 127
|
|
|
|
isChar := 0 <= c && c <= 127
|
|
|
|
isSeparator := strings.IndexRune(" \t\"(),/:;<=>?@[]\\{}", rune(c)) >= 0
|
|
|
|
if strings.IndexRune(" \t\r\n", rune(c)) >= 0 {
|
|
|
|
t |= isSpace
|
|
|
|
}
|
|
|
|
if isChar && !isCtl && !isSeparator {
|
|
|
|
t |= isToken
|
|
|
|
}
|
|
|
|
octetTypes[c] = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 01:10:48 +02:00
|
|
|
// ResponseChallenges returns a list of authorization challenges
|
|
|
|
// for the given http Response. Challenges are only checked if
|
|
|
|
// the response status code was a 401.
|
|
|
|
func ResponseChallenges(resp *http.Response) []Challenge {
|
2015-05-21 20:14:46 +02:00
|
|
|
if resp.StatusCode == http.StatusUnauthorized {
|
|
|
|
// Parse the WWW-Authenticate Header and store the challenges
|
|
|
|
// on this endpoint object.
|
2015-06-16 01:10:48 +02:00
|
|
|
return parseAuthHeader(resp.Header)
|
2015-05-21 20:14:46 +02:00
|
|
|
}
|
|
|
|
|
2015-06-16 01:10:48 +02:00
|
|
|
return nil
|
2015-05-21 20:14:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseAuthHeader(header http.Header) []Challenge {
|
|
|
|
challenges := []Challenge{}
|
2015-04-17 22:32:51 +02:00
|
|
|
for _, h := range header[http.CanonicalHeaderKey("WWW-Authenticate")] {
|
|
|
|
v, p := parseValueAndParams(h)
|
|
|
|
if v != "" {
|
2015-05-21 20:14:46 +02:00
|
|
|
challenges = append(challenges, Challenge{Scheme: v, Parameters: p})
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return challenges
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseValueAndParams(header string) (value string, params map[string]string) {
|
|
|
|
params = make(map[string]string)
|
|
|
|
value, s := expectToken(header)
|
|
|
|
if value == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
value = strings.ToLower(value)
|
|
|
|
s = "," + skipSpace(s)
|
|
|
|
for strings.HasPrefix(s, ",") {
|
|
|
|
var pkey string
|
|
|
|
pkey, s = expectToken(skipSpace(s[1:]))
|
|
|
|
if pkey == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(s, "=") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var pvalue string
|
|
|
|
pvalue, s = expectTokenOrQuoted(s[1:])
|
|
|
|
if pvalue == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pkey = strings.ToLower(pkey)
|
|
|
|
params[pkey] = pvalue
|
|
|
|
s = skipSpace(s)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func skipSpace(s string) (rest string) {
|
|
|
|
i := 0
|
|
|
|
for ; i < len(s); i++ {
|
|
|
|
if octetTypes[s[i]]&isSpace == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s[i:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectToken(s string) (token, rest string) {
|
|
|
|
i := 0
|
|
|
|
for ; i < len(s); i++ {
|
|
|
|
if octetTypes[s[i]]&isToken == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s[:i], s[i:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectTokenOrQuoted(s string) (value string, rest string) {
|
|
|
|
if !strings.HasPrefix(s, "\"") {
|
|
|
|
return expectToken(s)
|
|
|
|
}
|
|
|
|
s = s[1:]
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
switch s[i] {
|
|
|
|
case '"':
|
|
|
|
return s[:i], s[i+1:]
|
|
|
|
case '\\':
|
|
|
|
p := make([]byte, len(s)-1)
|
|
|
|
j := copy(p, s[:i])
|
|
|
|
escape := true
|
2015-05-07 22:16:52 +02:00
|
|
|
for i = i + 1; i < len(s); i++ {
|
2015-04-17 22:32:51 +02:00
|
|
|
b := s[i]
|
|
|
|
switch {
|
|
|
|
case escape:
|
|
|
|
escape = false
|
|
|
|
p[j] = b
|
|
|
|
j++
|
|
|
|
case b == '\\':
|
|
|
|
escape = true
|
|
|
|
case b == '"':
|
|
|
|
return string(p[:j]), s[i+1:]
|
|
|
|
default:
|
|
|
|
p[j] = b
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", ""
|
|
|
|
}
|