pomerium/internal/sessions/header_store.go
Bobby DeSimone b9ab49c32c
internal/sessions: fix cookie clear session (#376)
CookieStore's ClearSession now properly clears the user session cookie by setting MaxAge to -1.

internal/sessions: move encoder interface to encoding package, and rename to MarshalUnmarshaler.
internal/encoding: move mock to own package
authenticate: use INFO log level for authZ error.

Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
2019-11-09 10:49:24 -08:00

61 lines
1.7 KiB
Go

package sessions // import "github.com/pomerium/pomerium/internal/sessions"
import (
"net/http"
"strings"
"github.com/pomerium/pomerium/internal/encoding"
)
const (
defaultAuthHeader = "Authorization"
defaultAuthType = "Bearer"
)
// HeaderStore implements the load session store interface using http
// authorization headers.
type HeaderStore struct {
authHeader string
authType string
encoder encoding.Unmarshaler
}
// NewHeaderStore returns a new header store for loading sessions from
// authorization header as defined in as defined in rfc2617
//
// NOTA BENE: While most servers do not log Authorization headers by default,
// you should ensure no other services are logging or leaking your auth headers.
func NewHeaderStore(enc encoding.Unmarshaler, headerType string) *HeaderStore {
if headerType == "" {
headerType = defaultAuthType
}
return &HeaderStore{
authHeader: defaultAuthHeader,
authType: headerType,
encoder: enc,
}
}
// LoadSession tries to retrieve the token string from the Authorization header.
func (as *HeaderStore) LoadSession(r *http.Request) (*State, error) {
cipherText := TokenFromHeader(r, as.authHeader, as.authType)
if cipherText == "" {
return nil, ErrNoSessionFound
}
var session State
if err := as.encoder.Unmarshal([]byte(cipherText), &session); err != nil {
return nil, ErrMalformed
}
return &session, nil
}
// TokenFromHeader retrieves the value of the authorization header from a given
// request, header key, and authentication type.
func TokenFromHeader(r *http.Request, authHeader, authType string) string {
bearer := r.Header.Get(authHeader)
atSize := len(authType)
if len(bearer) > atSize && strings.EqualFold(bearer[0:atSize], authType) {
return bearer[atSize+1:]
}
return ""
}