Files
ocis/proxy/pkg/middleware/authentication.go
T

133 lines
4.3 KiB
Go
Raw Normal View History

2020-11-30 17:19:03 +01:00
package middleware
import (
"fmt"
"net/http"
"regexp"
2020-12-01 16:57:36 +01:00
"strings"
2020-11-30 17:19:03 +01:00
)
2020-12-04 13:51:48 +01:00
var (
// SupportedAuthStrategies stores configured challenges.
SupportedAuthStrategies []string
2020-12-01 16:57:36 +01:00
2020-12-04 13:51:48 +01:00
// ProxyWwwAuthenticate is a list of endpoints that do not rely on reva underlying authentication, such as ocs.
// services that fallback to reva authentication are declared in the "frontend" command on oCIS. It is a list of strings
2020-12-04 13:51:48 +01:00
// to be regexp compiled.
ProxyWwwAuthenticate = []string{"/ocs/v[12].php/cloud/"}
// WWWAuthenticate captures the Www-Authenticate header string.
WWWAuthenticate = "Www-Authenticate"
)
// userAgentLocker aids in dependency injection for helper methods. The set of fields is arbitrary and the only relation
// they share is to fulfill their duty and lock a User-Agent to its correct challenge if configured.
type userAgentLocker struct {
w http.ResponseWriter
r *http.Request
locks map[string]string // locks represents a reva user-agent:challenge mapping.
fallback string
}
2020-12-01 16:57:36 +01:00
// Authentication is a higher order authentication middleware.
2020-11-30 17:19:03 +01:00
func Authentication(opts ...Option) func(next http.Handler) http.Handler {
options := newOptions(opts...)
2020-12-01 16:57:36 +01:00
2020-12-04 13:51:48 +01:00
configureSupportedChallenges(options)
oidc := newOIDCAuth(options)
basic := newBasicAuth(options)
2020-11-30 17:19:03 +01:00
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2020-12-01 16:57:36 +01:00
if options.OIDCIss != "" && options.EnableBasicAuth {
oidc(basic(next)).ServeHTTP(w, r)
}
if options.OIDCIss != "" && !options.EnableBasicAuth {
2020-11-30 17:19:03 +01:00
oidc(next).ServeHTTP(w, r)
2020-12-01 16:57:36 +01:00
}
if options.OIDCIss == "" && options.EnableBasicAuth {
2020-11-30 17:19:03 +01:00
basic(next).ServeHTTP(w, r)
}
})
}
}
2020-12-01 16:57:36 +01:00
2020-12-04 13:51:48 +01:00
// configureSupportedChallenges adds known authentication challenges to the current session.
func configureSupportedChallenges(options Options) {
if options.OIDCIss != "" {
SupportedAuthStrategies = append(SupportedAuthStrategies, "bearer")
}
if options.EnableBasicAuth {
SupportedAuthStrategies = append(SupportedAuthStrategies, "basic")
}
}
2020-12-01 16:57:36 +01:00
func writeSupportedAuthenticateHeader(w http.ResponseWriter, r *http.Request) {
for i := 0; i < len(SupportedAuthStrategies); i++ {
2020-12-04 13:51:48 +01:00
w.Header().Add(WWWAuthenticate, fmt.Sprintf("%v realm=\"%s\", charset=\"UTF-8\"", strings.Title(SupportedAuthStrategies[i]), r.Host))
2020-12-01 16:57:36 +01:00
}
}
2020-12-02 15:51:39 +01:00
func removeSuperfluousAuthenticate(w http.ResponseWriter) {
2020-12-04 13:51:48 +01:00
w.Header().Del(WWWAuthenticate)
2020-12-02 15:51:39 +01:00
}
// userAgentAuthenticateLockIn sets Www-Authenticate according to configured user agents. This is useful for the case of
// legacy clients that do not support protocols like OIDC or OAuth and want to lock a given user agent to a challenge
// such as basic. For more context check https://github.com/cs3org/reva/pull/1350
2020-12-04 13:51:48 +01:00
func userAgentAuthenticateLockIn(w http.ResponseWriter, r *http.Request, locks map[string]string, fallback string) {
u := userAgentLocker{
w: w,
r: r,
locks: locks,
fallback: fallback,
}
2020-12-02 15:51:39 +01:00
for i := 0; i < len(ProxyWwwAuthenticate); i++ {
2020-12-04 13:51:48 +01:00
evalRequestURI(&u, i)
}
}
func evalRequestURI(l *userAgentLocker, i int) {
r := regexp.MustCompile(ProxyWwwAuthenticate[i])
if r.Match([]byte(l.r.RequestURI)) {
for k, v := range l.locks {
if strings.Contains(k, l.r.UserAgent()) {
removeSuperfluousAuthenticate(l.w)
l.w.Header().Add(WWWAuthenticate, fmt.Sprintf("%v realm=\"%s\", charset=\"UTF-8\"", strings.Title(v), l.r.Host))
return
2020-12-02 15:51:39 +01:00
}
}
2020-12-04 13:51:48 +01:00
l.w.Header().Add(WWWAuthenticate, fmt.Sprintf("%v realm=\"%s\", charset=\"UTF-8\"", strings.Title(l.fallback), l.r.Host))
2020-12-02 15:51:39 +01:00
}
}
2020-12-04 13:51:48 +01:00
// newOIDCAuth returns a configured oidc middleware
func newOIDCAuth(options Options) func(http.Handler) http.Handler {
return OIDCAuth(
Logger(options.Logger),
OIDCProviderFunc(options.OIDCProviderFunc),
HTTPClient(options.HTTPClient),
OIDCIss(options.OIDCIss),
TokenCacheSize(options.UserinfoCacheSize),
2021-02-22 18:37:14 +01:00
TokenCacheTTL(options.UserinfoCacheTTL),
2020-12-04 13:51:48 +01:00
CredentialsByUserAgent(options.CredentialsByUserAgent),
)
}
2020-12-04 13:53:34 +01:00
// newBasicAuth returns a configured basic middleware
2020-12-04 13:51:48 +01:00
func newBasicAuth(options Options) func(http.Handler) http.Handler {
return BasicAuth(
UserProvider(options.UserProvider),
2020-12-04 13:51:48 +01:00
Logger(options.Logger),
EnableBasicAuth(options.EnableBasicAuth),
AccountsClient(options.AccountsClient),
OIDCIss(options.OIDCIss),
2021-11-11 11:43:10 +01:00
UserOIDCClaim(options.UserOIDCClaim),
UserCS3Claim(options.UserCS3Claim),
2020-12-04 13:51:48 +01:00
CredentialsByUserAgent(options.CredentialsByUserAgent),
)
}