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

178 lines
5.3 KiB
Go
Raw Normal View History

package middleware
import (
"context"
"net/http"
"strings"
"time"
2021-08-12 13:57:58 +02:00
"github.com/golang-jwt/jwt/v4"
gOidc "github.com/coreos/go-oidc/v3/oidc"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/oidc"
2021-01-16 16:38:01 +01:00
"github.com/owncloud/ocis/ocis-pkg/sync"
2021-08-12 13:57:58 +02:00
"github.com/owncloud/ocis/proxy/pkg/config"
"golang.org/x/oauth2"
)
2020-11-16 14:01:32 +01:00
// OIDCProvider used to mock the oidc provider during tests
type OIDCProvider interface {
UserInfo(ctx context.Context, ts oauth2.TokenSource) (*gOidc.UserInfo, error)
}
2020-11-16 14:01:32 +01:00
// OIDCAuth provides a middleware to check access secured by a static token.
func OIDCAuth(optionSetters ...Option) func(next http.Handler) http.Handler {
options := newOptions(optionSetters...)
2021-01-16 16:38:01 +01:00
tokenCache := sync.NewCache(options.UserinfoCacheSize)
h := oidcAuth{
2021-08-12 13:57:58 +02:00
logger: options.Logger,
providerFunc: options.OIDCProviderFunc,
httpClient: options.HTTPClient,
oidcIss: options.OIDCIss,
TokenManagerConfig: options.TokenManagerConfig,
tokenCache: &tokenCache,
tokenCacheTTL: options.UserinfoCacheTTL,
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
2020-12-01 16:57:36 +01:00
// there is no bearer token on the request,
if !h.shouldServe(req) {
2020-12-01 16:57:36 +01:00
// oidc supported but token not present, add header and handover to the next middleware.
2020-12-02 15:51:39 +01:00
userAgentAuthenticateLockIn(w, req, options.CredentialsByUserAgent, "bearer")
2020-12-01 16:57:36 +01:00
next.ServeHTTP(w, req)
return
}
if h.getProvider() == nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
token := strings.TrimPrefix(req.Header.Get("Authorization"), "Bearer ")
claims, status := h.getClaims(token, req)
if status != 0 {
w.WriteHeader(status)
return
}
// inject claims to the request context for the account_uuid middleware.
2021-07-21 15:37:12 +00:00
req = req.WithContext(oidc.NewContext(req.Context(), claims))
// store claims in context
// uses the original context, not the one with probably reduced security
2021-07-21 15:37:12 +00:00
next.ServeHTTP(w, req.WithContext(oidc.NewContext(req.Context(), claims)))
})
}
}
type oidcAuth struct {
2021-08-12 13:57:58 +02:00
logger log.Logger
provider OIDCProvider
providerFunc func() (OIDCProvider, error)
httpClient *http.Client
oidcIss string
tokenCache *sync.Cache
tokenCacheTTL time.Duration
TokenManagerConfig config.TokenManager
}
2021-07-21 15:37:12 +00:00
func (m oidcAuth) getClaims(token string, req *http.Request) (claims map[string]interface{}, status int) {
2021-01-18 14:12:10 +01:00
hit := m.tokenCache.Load(token)
if hit == nil {
// TODO cache userinfo for access token if we can determine the expiry (which works in case it is a jwt based access token)
oauth2Token := &oauth2.Token{
AccessToken: token,
}
userInfo, err := m.getProvider().UserInfo(
context.WithValue(req.Context(), oauth2.HTTPClient, m.httpClient),
oauth2.StaticTokenSource(oauth2Token),
)
if err != nil {
2021-03-03 15:30:11 +01:00
m.logger.Error().Err(err).Msg("Failed to get userinfo")
status = http.StatusUnauthorized
return
}
if err := userInfo.Claims(&claims); err != nil {
m.logger.Error().Err(err).Interface("userinfo", userInfo).Msg("failed to unmarshal userinfo claims")
status = http.StatusInternalServerError
return
}
expiration := m.extractExpiration(token)
2021-01-18 14:12:10 +01:00
m.tokenCache.Store(token, claims, expiration)
m.logger.Debug().Interface("claims", claims).Interface("userInfo", userInfo).Time("expiration", expiration.UTC()).Msg("unmarshalled and cached userinfo")
return
}
2021-02-26 14:24:25 +01:00
var ok bool
2021-07-21 15:37:12 +00:00
if claims, ok = hit.V.(map[string]interface{}); !ok {
status = http.StatusInternalServerError
return
}
m.logger.Debug().Interface("claims", claims).Msg("cache hit for userinfo")
return
}
// extractExpiration tries to parse and extract the expiration from the provided token. It might not even be a jwt.
// defaults to the configured fallback TTL.
// TODO: use introspection endpoint if available in the oidc configuration. Still needs a fallback to configured TTL.
func (m oidcAuth) extractExpiration(token string) time.Time {
defaultExpiration := time.Now().Add(m.tokenCacheTTL)
2021-08-12 13:57:58 +02:00
t, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
return []byte(m.TokenManagerConfig.JWTSecret), nil
})
if err != nil {
return defaultExpiration
}
2021-08-12 13:57:58 +02:00
at, ok := t.Claims.(jwt.StandardClaims)
if !ok || at.ExpiresAt == 0 {
return defaultExpiration
}
return time.Unix(at.ExpiresAt, 0)
}
func (m oidcAuth) shouldServe(req *http.Request) bool {
header := req.Header.Get("Authorization")
if m.oidcIss == "" {
return false
}
// todo: looks dirty, check later
2020-11-18 11:14:29 +01:00
// TODO: make a PR to coreos/go-oidc for exposing userinfo endpoint on provider, see https://github.com/coreos/go-oidc/issues/248
2021-06-16 16:59:11 +02:00
for _, ignoringPath := range []string{"/konnect/v1/userinfo", "/status.php"} {
if req.URL.Path == ignoringPath {
return false
}
}
2020-11-16 14:01:32 +01:00
return strings.HasPrefix(header, "Bearer ")
}
2020-11-18 11:14:29 +01:00
func (m *oidcAuth) getProvider() OIDCProvider {
2020-11-18 11:14:29 +01:00
if m.provider == nil {
// Lazily initialize a provider
// provider needs to be cached as when it is created
// it will fetch the keys from the issuer using the .well-known
// endpoint
provider, err := m.providerFunc()
if err != nil {
m.logger.Error().Err(err).Msg("could not initialize oidcAuth provider")
return nil
}
m.provider = provider
}
return m.provider
}