feat: add a way to check for specific OIDC claims
For now, only some hardcoded paths will check the claims (if there is a checker configured). If the check fails for those paths, the authentication will fail and it will return a 401 error
This commit is contained in:
1 parent
f01627339d
commit
ed95f21269
11 files changed
+268
-7
No files matched your search
@@ -0,0 +1,34 @@
|
||||
package checkers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
|
||||
)
|
||||
|
||||
// AcrChecker check if the acr in the claims has the exact same value
|
||||
// as the provided one
|
||||
type AcrChecker struct {
|
||||
value string
|
||||
}
|
||||
|
||||
// NewAcrChecker creates a new AcrChecker
|
||||
func NewAcrChecker(value string) *AcrChecker {
|
||||
return &AcrChecker{
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
// CheckClaims checks if the provided value matches the acr value in the
|
||||
// claims. It's an exact match.
|
||||
func (ac *AcrChecker) CheckClaims(claims map[string]interface{}) error {
|
||||
value, err := oidc.ReadStringClaim("acr", claims)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ac.value != value {
|
||||
return fmt.Errorf("wrong value for 'acr' - expected '%s' actual '%s'", ac.value, value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package checkers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
|
||||
)
|
||||
|
||||
// BooleanChecker checks whether the specified key has a matching boolean value
|
||||
type BooleanChecker struct {
|
||||
key string
|
||||
value bool
|
||||
}
|
||||
|
||||
// NewBooleanChecker creates a new BooleanChecker
|
||||
func NewBooleanChecker(key string, value bool) *BooleanChecker {
|
||||
return &BooleanChecker{
|
||||
key: key,
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
// CheckClaims checks the claims so the key held by the BooleanChecker matches
|
||||
// its boolean value.
|
||||
func (bcc *BooleanChecker) CheckClaims(claims map[string]interface{}) error {
|
||||
value, err := oidc.ReadBoolClaim(bcc.key, claims)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if value != bcc.value {
|
||||
return fmt.Errorf("wrong value for claim '%s' - expected '%t' actual '%t'", bcc.key, bcc.value, value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package checkers
|
||||
|
||||
// Checker will allow different checks based on the OIDC claims.
|
||||
// Each implementation will perform different checks.
|
||||
type Checker interface {
|
||||
// CheckClaims will check whether the claims match specific criteria.
|
||||
// If the check passes, nil will be returned, otherwise a proper error
|
||||
// will be returned instead.
|
||||
CheckClaims(map[string]interface{}) error
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package checkers
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Factory takes care of creating new claims checkers
|
||||
type Factory struct {
|
||||
}
|
||||
|
||||
// NewFactory creates a new factory
|
||||
func NewFactory() *Factory {
|
||||
return &Factory{}
|
||||
}
|
||||
|
||||
// GetChecker gets a new claims checker based on the provided name, and
|
||||
// configured with the provided parameter string.
|
||||
// If the name is unknown or there are problems with the parameters, a
|
||||
// NoopChecker will be returned instead.
|
||||
func (f *Factory) GetChecker(name, paramString string) Checker {
|
||||
params := f.parseParamString(paramString)
|
||||
switch name {
|
||||
case "Bool":
|
||||
key, keyok := params["key"]
|
||||
value, valueok := params["value"]
|
||||
if keyok && valueok {
|
||||
if boolValue, err := strconv.ParseBool(value); err == nil {
|
||||
return NewBooleanChecker(key, boolValue)
|
||||
}
|
||||
}
|
||||
case "Regexp":
|
||||
key, keyok := params["key"]
|
||||
pattern, patternok := params["value"]
|
||||
if keyok && patternok {
|
||||
return NewRegexpChecker(key, pattern)
|
||||
}
|
||||
case "Acr":
|
||||
value, valueok := params["value"]
|
||||
if valueok {
|
||||
return NewAcrChecker(value)
|
||||
}
|
||||
}
|
||||
return NewNoopChecker()
|
||||
}
|
||||
|
||||
func (f *Factory) parseParamString(paramString string) map[string]string {
|
||||
params := make(map[string]string)
|
||||
paramList := strings.Split(paramString, ";")
|
||||
for _, keyvalue := range paramList {
|
||||
p := strings.SplitN(keyvalue, "=", 2)
|
||||
if len(p) == 2 {
|
||||
params[p[0]] = p[1]
|
||||
}
|
||||
}
|
||||
return params
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package checkers
|
||||
|
||||
// NoopChecker doesn't check anything
|
||||
type NoopChecker struct {
|
||||
}
|
||||
|
||||
// NewNoopChecker creates a new NoopChecker
|
||||
func NewNoopChecker() *NoopChecker {
|
||||
return &NoopChecker{}
|
||||
}
|
||||
|
||||
// CheckClaims won't do anything and won't return an error
|
||||
func (nc *NoopChecker) CheckClaims(_ map[string]interface{}) error {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package checkers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
|
||||
)
|
||||
|
||||
// RegexpChecker checks whether the specific key matches the provided regular
|
||||
// expresion.
|
||||
type RegexpChecker struct {
|
||||
key string
|
||||
exp *regexp.Regexp
|
||||
}
|
||||
|
||||
// NewRegexpChecker creates a new RegexpChecker
|
||||
func NewRegexpChecker(key, pattern string) *RegexpChecker {
|
||||
return &RegexpChecker{
|
||||
key: key,
|
||||
exp: regexp.MustCompile(pattern),
|
||||
}
|
||||
}
|
||||
|
||||
// CheckClaims checks in the claims if the claims key's value matches the
|
||||
// provided regular expresion. If it doesn't match, an error is returned.
|
||||
func (rc *RegexpChecker) CheckClaims(claims map[string]interface{}) error {
|
||||
value, err := oidc.ReadStringClaim(rc.key, claims)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !rc.exp.MatchString(value) {
|
||||
return fmt.Errorf("wrong value for claim '%s' - value '%s' doesn't match regular expresion '%s'", rc.key, value, rc.exp.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -74,3 +74,21 @@ func ReadStringClaim(path string, claims map[string]interface{}) (string, error)
|
||||
|
||||
return value, fmt.Errorf("claim path '%s' not set or empty", path)
|
||||
}
|
||||
|
||||
func ReadBoolClaim(path string, claims map[string]interface{}) (bool, error) {
|
||||
// check the simple case first
|
||||
if value, ok := claims[path].(bool); ok {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
claim, err := WalkSegments(SplitWithEscaping(path, ".", "\\"), claims)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if value, ok := claim.(bool); ok {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
return false, fmt.Errorf("claim path '%s' not set or not have a boolean value", path)
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
||||
pkgmiddleware "github.com/owncloud/ocis/v2/ocis-pkg/middleware"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/oidc/checkers"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/runner"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/service/grpc"
|
||||
@@ -292,6 +293,8 @@ func loadMiddlewares(logger log.Logger, cfg *config.Config,
|
||||
RevaGatewaySelector: gatewaySelector,
|
||||
})
|
||||
}
|
||||
|
||||
checkerFactory := checkers.NewFactory()
|
||||
authenticators = append(authenticators, middleware.NewOIDCAuthenticator(
|
||||
middleware.Logger(logger),
|
||||
middleware.UserInfoCache(userInfoCache),
|
||||
@@ -306,6 +309,7 @@ func loadMiddlewares(logger log.Logger, cfg *config.Config,
|
||||
oidc.WithJWKSOptions(cfg.OIDC.JWKS),
|
||||
)),
|
||||
middleware.SkipUserInfo(cfg.OIDC.SkipUserInfo),
|
||||
middleware.ClaimsChecker(checkerFactory.GetChecker(cfg.OIDC.ClaimsChecker.Name, cfg.OIDC.ClaimsChecker.Params)),
|
||||
))
|
||||
authenticators = append(authenticators, middleware.PublicShareAuthenticator{
|
||||
Logger: logger,
|
||||
|
||||
@@ -110,13 +110,14 @@ const (
|
||||
// OIDC is the config for the OpenID-Connect middleware. If set the proxy will try to authenticate every request
|
||||
// with the configured oidc-provider
|
||||
type OIDC struct {
|
||||
Issuer string `yaml:"issuer" env:"OCIS_URL;OCIS_OIDC_ISSUER;PROXY_OIDC_ISSUER" desc:"URL of the OIDC issuer. It defaults to URL of the builtin IDP." introductionVersion:"pre5.0"`
|
||||
Insecure bool `yaml:"insecure" env:"OCIS_INSECURE;PROXY_OIDC_INSECURE" desc:"Disable TLS certificate validation for connections to the IDP. Note that this is not recommended for production environments." introductionVersion:"pre5.0"`
|
||||
AccessTokenVerifyMethod string `yaml:"access_token_verify_method" env:"PROXY_OIDC_ACCESS_TOKEN_VERIFY_METHOD" desc:"Sets how OIDC access tokens should be verified. Possible values are 'none' and 'jwt'. When using 'none', no special validation apart from using it for accessing the IPD's userinfo endpoint will be done. When using 'jwt', it tries to parse the access token as a jwt token and verifies the signature using the keys published on the IDP's 'jwks_uri'." introductionVersion:"pre5.0"`
|
||||
SkipUserInfo bool `yaml:"skip_user_info" env:"PROXY_OIDC_SKIP_USER_INFO" desc:"Do not look up user claims at the userinfo endpoint and directly read them from the access token. Incompatible with 'PROXY_OIDC_ACCESS_TOKEN_VERIFY_METHOD=none'." introductionVersion:"pre5.0"`
|
||||
UserinfoCache *Cache `yaml:"user_info_cache"`
|
||||
JWKS JWKS `yaml:"jwks"`
|
||||
RewriteWellKnown bool `yaml:"rewrite_well_known" env:"PROXY_OIDC_REWRITE_WELLKNOWN" desc:"Enables rewriting the /.well-known/openid-configuration to the configured OIDC issuer. Needed by the Desktop Client, Android Client and iOS Client to discover the OIDC provider." introductionVersion:"pre5.0"`
|
||||
Issuer string `yaml:"issuer" env:"OCIS_URL;OCIS_OIDC_ISSUER;PROXY_OIDC_ISSUER" desc:"URL of the OIDC issuer. It defaults to URL of the builtin IDP." introductionVersion:"pre5.0"`
|
||||
Insecure bool `yaml:"insecure" env:"OCIS_INSECURE;PROXY_OIDC_INSECURE" desc:"Disable TLS certificate validation for connections to the IDP. Note that this is not recommended for production environments." introductionVersion:"pre5.0"`
|
||||
AccessTokenVerifyMethod string `yaml:"access_token_verify_method" env:"PROXY_OIDC_ACCESS_TOKEN_VERIFY_METHOD" desc:"Sets how OIDC access tokens should be verified. Possible values are 'none' and 'jwt'. When using 'none', no special validation apart from using it for accessing the IPD's userinfo endpoint will be done. When using 'jwt', it tries to parse the access token as a jwt token and verifies the signature using the keys published on the IDP's 'jwks_uri'." introductionVersion:"pre5.0"`
|
||||
SkipUserInfo bool `yaml:"skip_user_info" env:"PROXY_OIDC_SKIP_USER_INFO" desc:"Do not look up user claims at the userinfo endpoint and directly read them from the access token. Incompatible with 'PROXY_OIDC_ACCESS_TOKEN_VERIFY_METHOD=none'." introductionVersion:"pre5.0"`
|
||||
UserinfoCache *Cache `yaml:"user_info_cache"`
|
||||
JWKS JWKS `yaml:"jwks"`
|
||||
RewriteWellKnown bool `yaml:"rewrite_well_known" env:"PROXY_OIDC_REWRITE_WELLKNOWN" desc:"Enables rewriting the /.well-known/openid-configuration to the configured OIDC issuer. Needed by the Desktop Client, Android Client and iOS Client to discover the OIDC provider." introductionVersion:"pre5.0"`
|
||||
ClaimsChecker ClaimsChecker `yaml:"claims_checker"`
|
||||
}
|
||||
|
||||
type JWKS struct {
|
||||
@@ -126,6 +127,11 @@ type JWKS struct {
|
||||
RefreshUnknownKID bool `yaml:"refresh_unknown_kid" env:"PROXY_OIDC_JWKS_REFRESH_UNKNOWN_KID" desc:"If set to 'true', the JWKS refresh request will occur every time an unknown KEY ID (KID) is seen. Always set a 'refresh_limit' when enabling this." introductionVersion:"pre5.0"`
|
||||
}
|
||||
|
||||
type ClaimsChecker struct {
|
||||
Name string `yaml:"name" env:"PROXY_OIDC_CLAIMSCHECKER_NAME" desc:"Name of the claims checker to be used. It can be 'Bool'" introductionVersion:"%%NEXT%%"`
|
||||
Params string `yaml:"params" env:"PROXY_OIDC_CLAIMSCHECKER_PARAMS" desc:"Parameters to be used by the chosen claims checker. Formatting depends on the specific checker, but it usually like 'param1=value1;param2=value2'" introductionVersion:"%%NEXT%%"`
|
||||
}
|
||||
|
||||
// Cache is a TTL cache configuration.
|
||||
type Cache struct {
|
||||
Store string `yaml:"store" env:"OCIS_CACHE_STORE;PROXY_OIDC_USERINFO_CACHE_STORE" desc:"The type of the cache store. Supported values are: 'memory', 'redis-sentinel', 'nats-js-kv', 'noop'. See the text description for details." introductionVersion:"pre5.0"`
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/oidc/checkers"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/shamaton/msgpack/v2"
|
||||
store "go-micro.dev/v4/store"
|
||||
@@ -34,6 +35,7 @@ func NewOIDCAuthenticator(opts ...Option) *OIDCAuthenticator {
|
||||
HTTPClient: options.HTTPClient,
|
||||
OIDCIss: options.OIDCIss,
|
||||
oidcClient: options.OIDCClient,
|
||||
claimsChecker: options.ClaimsChecker,
|
||||
AccessTokenVerifyMethod: options.AccessTokenVerifyMethod,
|
||||
skipUserInfo: options.SkipUserInfo,
|
||||
TimeFunc: time.Now,
|
||||
@@ -48,6 +50,7 @@ type OIDCAuthenticator struct {
|
||||
userInfoCache store.Store
|
||||
DefaultTokenCacheTTL time.Duration
|
||||
oidcClient oidc.OIDCClient
|
||||
claimsChecker checkers.Checker
|
||||
AccessTokenVerifyMethod string
|
||||
skipUserInfo bool
|
||||
TimeFunc func() time.Time
|
||||
@@ -170,6 +173,28 @@ func (m OIDCAuthenticator) shouldServe(req *http.Request) bool {
|
||||
return strings.HasPrefix(header, _bearerPrefix)
|
||||
}
|
||||
|
||||
// shouldCheckClaims returns true if we should check the claims for the
|
||||
// provided request.
|
||||
func (m *OIDCAuthenticator) shouldCheckClaims(r *http.Request) bool {
|
||||
// the list is currently hardcoded
|
||||
protectedPaths := []string{
|
||||
"/graph/v1.0/users",
|
||||
"/graph/v1.0/groups",
|
||||
"/graph/v1beta1/drives",
|
||||
}
|
||||
|
||||
for _, path := range protectedPaths {
|
||||
if r.URL.Path == path {
|
||||
q := r.URL.Query()
|
||||
// we need to check claims if the $search query is NOT present (or empty)
|
||||
if q.Get("$search") == "" { // if $query isn't present, it will return the empty string
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Authenticate implements the authenticator interface to authenticate requests via oidc auth.
|
||||
func (m *OIDCAuthenticator) Authenticate(r *http.Request) (*http.Request, bool) {
|
||||
// there is no bearer token on the request,
|
||||
@@ -185,6 +210,16 @@ func (m *OIDCAuthenticator) Authenticate(r *http.Request) (*http.Request, bool)
|
||||
}
|
||||
|
||||
claims, newSession, err := m.getClaims(token, r)
|
||||
if m.shouldCheckClaims(r) {
|
||||
if err := m.claimsChecker.CheckClaims(claims); err != nil {
|
||||
m.Logger.Error().
|
||||
Err(err).
|
||||
Str("path", r.URL.Path).
|
||||
Msg("can't access protected path without valid claims")
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
host, port, _ := net.SplitHostPort(r.RemoteAddr)
|
||||
m.Logger.Error().
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/oidc/checkers"
|
||||
policiessvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/policies/v0"
|
||||
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
|
||||
"github.com/owncloud/ocis/v2/services/proxy/pkg/config"
|
||||
@@ -41,6 +42,8 @@ type Options struct {
|
||||
OIDCClient oidc.OIDCClient
|
||||
// OIDCIss is the oidcAuth-issuer
|
||||
OIDCIss string
|
||||
// ClaimsChecker makes checks over the OIDC claims
|
||||
ClaimsChecker checkers.Checker
|
||||
// RevaGatewaySelector to send requests to the reva gateway
|
||||
RevaGatewaySelector pool.Selectable[gateway.GatewayAPIClient]
|
||||
// PreSignedURLConfig to configure the middleware
|
||||
@@ -139,6 +142,13 @@ func OIDCIss(iss string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// OIDCIss sets the oidcAuth issuer url
|
||||
func ClaimsChecker(cc checkers.Checker) Option {
|
||||
return func(o *Options) {
|
||||
o.ClaimsChecker = cc
|
||||
}
|
||||
}
|
||||
|
||||
// CredentialsByUserAgent sets UserAgentChallenges.
|
||||
func CredentialsByUserAgent(v map[string]string) Option {
|
||||
return func(o *Options) {
|
||||
|
||||
Reference in new issue
Block a user