mirror of
https://github.com/pomerium/pomerium.git
synced 2025-06-03 03:12:50 +02:00
authenticate/proxy: add backend refresh (#438)
This commit is contained in:
parent
9a330613aa
commit
ec029c679b
35 changed files with 1226 additions and 445 deletions
64
internal/sessions/header/header_store.go
Normal file
64
internal/sessions/header/header_store.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
package header // import "github.com/pomerium/pomerium/internal/sessions/header"
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/encoding"
|
||||
"github.com/pomerium/pomerium/internal/sessions"
|
||||
)
|
||||
|
||||
var _ sessions.SessionLoader = &Store{}
|
||||
|
||||
const (
|
||||
defaultAuthHeader = "Authorization"
|
||||
defaultAuthType = "Bearer"
|
||||
)
|
||||
|
||||
// Store implements the load session store interface using http
|
||||
// authorization headers.
|
||||
type Store struct {
|
||||
authHeader string
|
||||
authType string
|
||||
encoder encoding.Unmarshaler
|
||||
}
|
||||
|
||||
// NewStore 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 NewStore(enc encoding.Unmarshaler, headerType string) *Store {
|
||||
if headerType == "" {
|
||||
headerType = defaultAuthType
|
||||
}
|
||||
return &Store{
|
||||
authHeader: defaultAuthHeader,
|
||||
authType: headerType,
|
||||
encoder: enc,
|
||||
}
|
||||
}
|
||||
|
||||
// LoadSession tries to retrieve the token string from the Authorization header.
|
||||
func (as *Store) LoadSession(r *http.Request) (*sessions.State, error) {
|
||||
cipherText := TokenFromHeader(r, as.authHeader, as.authType)
|
||||
if cipherText == "" {
|
||||
return nil, sessions.ErrNoSessionFound
|
||||
}
|
||||
var session sessions.State
|
||||
if err := as.encoder.Unmarshal([]byte(cipherText), &session); err != nil {
|
||||
return nil, sessions.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 ""
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue