chore: Add audience restriction for OIDC access tokens
This commit is contained in:
6 files changed
+204
-24
No files matched your search
@@ -0,0 +1,13 @@
|
||||
Enhancement: Add audience restriction for OIDC access tokens
|
||||
|
||||
We added a new proxy configuration option `PROXY_OIDC_ACCESS_TOKEN_VERIFY_AUD`
|
||||
which lets operators restrict the JWT access tokens that are accepted to a list
|
||||
of allowed audiences. When set, a token is only accepted if one of the
|
||||
configured values is present in its `aud` (audience) claim or matches its `azp`
|
||||
(authorized party) claim. This is useful when the OIDC provider (IDP) is shared
|
||||
between multiple applications, so that tokens issued for another application are
|
||||
not accepted by ownCloud Infinite Scale.
|
||||
|
||||
The check is disabled by default (empty list) to preserve the existing behavior.
|
||||
|
||||
https://github.com/owncloud/ocis/pull/12581
|
||||
+53
-17
@@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -53,13 +54,14 @@ type oidcClient struct {
|
||||
// Logger to use for logging, must be set
|
||||
Logger log.Logger
|
||||
|
||||
issuer string
|
||||
provider *ProviderMetadata
|
||||
providerLock *sync.Mutex
|
||||
skipIssuerValidation bool
|
||||
accessTokenVerifyMethod string
|
||||
remoteKeySet KeySet
|
||||
algorithms []string
|
||||
issuer string
|
||||
provider *ProviderMetadata
|
||||
providerLock *sync.Mutex
|
||||
skipIssuerValidation bool
|
||||
accessTokenVerifyMethod string
|
||||
accessTokenVerifyAudience []string
|
||||
remoteKeySet KeySet
|
||||
algorithms []string
|
||||
|
||||
JWKSOptions config.JWKS
|
||||
JWKS *keyfunc.JWKS
|
||||
@@ -88,16 +90,17 @@ func NewOIDCClient(opts ...Option) OIDCClient {
|
||||
options := newOptions(opts...)
|
||||
|
||||
return &oidcClient{
|
||||
Logger: options.Logger,
|
||||
issuer: options.OIDCIssuer,
|
||||
httpClient: options.HTTPClient,
|
||||
accessTokenVerifyMethod: options.AccessTokenVerifyMethod,
|
||||
JWKSOptions: options.JWKSOptions, // TODO I don't like that we pass down config options ...
|
||||
JWKS: options.JWKS,
|
||||
providerLock: &sync.Mutex{},
|
||||
jwksLock: &sync.Mutex{},
|
||||
remoteKeySet: options.KeySet,
|
||||
provider: options.ProviderMetadata,
|
||||
Logger: options.Logger,
|
||||
issuer: options.OIDCIssuer,
|
||||
httpClient: options.HTTPClient,
|
||||
accessTokenVerifyMethod: options.AccessTokenVerifyMethod,
|
||||
accessTokenVerifyAudience: options.AccessTokenVerifyAudience,
|
||||
JWKSOptions: options.JWKSOptions, // TODO I don't like that we pass down config options ...
|
||||
JWKS: options.JWKS,
|
||||
providerLock: &sync.Mutex{},
|
||||
jwksLock: &sync.Mutex{},
|
||||
remoteKeySet: options.KeySet,
|
||||
provider: options.ProviderMetadata,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,9 +317,42 @@ func (c *oidcClient) verifyAccessTokenJWT(token string) (RegClaimsWithSID, jwt.M
|
||||
return claims, mapClaims, err
|
||||
}
|
||||
|
||||
// Verify the token was issued for this instance. Keycloak puts a generic
|
||||
// value (e.g. "account") in "aud" and the real client id in "azp", so we
|
||||
// accept the token when the allowlist matches either claim.
|
||||
if err := c.verifyAudience(claims.Audience, azpFromClaims(mapClaims)); err != nil {
|
||||
c.Logger.Info().Err(err).Msg("Access token rejected: audience not allowed.")
|
||||
return claims, mapClaims, err
|
||||
}
|
||||
|
||||
return claims, mapClaims, nil
|
||||
}
|
||||
|
||||
// verifyAudience checks that the token's "aud" or "azp" claim matches the
|
||||
// configured allowlist. An empty allowlist disables the check.
|
||||
func (c *oidcClient) verifyAudience(audiences jwt.ClaimStrings, azp string) error {
|
||||
if len(c.accessTokenVerifyAudience) == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, allowed := range c.accessTokenVerifyAudience {
|
||||
allowed = strings.TrimSpace(allowed)
|
||||
if allowed == "" {
|
||||
continue
|
||||
}
|
||||
if allowed == azp || slices.Contains(audiences, allowed) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.New("oidc: access token audience is not allowed")
|
||||
}
|
||||
|
||||
func azpFromClaims(claims jwt.MapClaims) string {
|
||||
if azp, ok := claims["azp"].(string); ok {
|
||||
return azp
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *oidcClient) VerifyLogoutToken(ctx context.Context, rawToken string) (*LogoutToken, error) {
|
||||
var claims LogoutToken
|
||||
if err := c.lookupWellKnownOpenidConfiguration(ctx); err != nil {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/MicahParks/keyfunc/v2"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
|
||||
"github.com/owncloud/ocis/v2/services/proxy/pkg/config"
|
||||
)
|
||||
|
||||
type signingKey struct {
|
||||
@@ -186,6 +187,120 @@ func (l logoutVerificationTest) run(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyAccessTokenAudience(t *testing.T) {
|
||||
const issuer = "https://foo"
|
||||
key := newRSAKey(t)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
// allowlist configured on the client (empty disables the check)
|
||||
verifyAud []string
|
||||
// claims of the presented access token
|
||||
aud interface{}
|
||||
azp string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "check disabled accepts foreign audience",
|
||||
verifyAud: nil,
|
||||
aud: "account",
|
||||
azp: "evil-app",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "rejects foreign azp with generic aud (keycloak style)",
|
||||
verifyAud: []string{"web"},
|
||||
aud: "account",
|
||||
azp: "evil-app",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "accepts matching azp with generic aud (keycloak style)",
|
||||
verifyAud: []string{"web"},
|
||||
aud: "account",
|
||||
azp: "web",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "accepts matching aud (builtin idp style)",
|
||||
verifyAud: []string{"web"},
|
||||
aud: "web",
|
||||
azp: "",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "accepts when one of multiple audiences matches",
|
||||
verifyAud: []string{"desktop", "web"},
|
||||
aud: []string{"account", "web"},
|
||||
azp: "evil-app",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "rejects when neither aud nor azp match",
|
||||
verifyAud: []string{"web", "desktop"},
|
||||
aud: []string{"account", "other"},
|
||||
azp: "evil-app",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "rejects when azp is empty and aud does not match",
|
||||
verifyAud: []string{"web", "desktop"},
|
||||
aud: []string{"account", "other"},
|
||||
azp: "",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "tolerates whitespace around configured audience",
|
||||
verifyAud: []string{" web ", "desktop"},
|
||||
aud: "account",
|
||||
azp: "web",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "blank configured audience does not match missing azp",
|
||||
verifyAud: []string{" "},
|
||||
aud: "account",
|
||||
azp: "",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
claims := jwt.MapClaims{
|
||||
"iss": issuer,
|
||||
"sub": "einstein",
|
||||
"aud": tc.aud,
|
||||
}
|
||||
if tc.azp != "" {
|
||||
claims["azp"] = tc.azp
|
||||
}
|
||||
tok := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
|
||||
tok.Header["kid"] = "1"
|
||||
signed, err := tok.SignedString(key.priv)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
client := oidc.NewOIDCClient(
|
||||
oidc.WithOidcIssuer(issuer),
|
||||
oidc.WithJWKS(key.jwks),
|
||||
oidc.WithProviderMetadata(&oidc.ProviderMetadata{}),
|
||||
oidc.WithAccessTokenVerifyMethod(config.AccessTokenVerificationJWT),
|
||||
oidc.WithAccessTokenVerifyAudience(tc.verifyAud),
|
||||
)
|
||||
|
||||
_, _, err = client.VerifyAccessToken(context.Background(), signed)
|
||||
if tc.wantErr && err == nil {
|
||||
t.Errorf("expected error, got nil")
|
||||
}
|
||||
if !tc.wantErr && err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newRSAKey(t testing.TB) *signingKey {
|
||||
priv, err := rsa.GenerateKey(rand.Reader, 1028)
|
||||
if err != nil {
|
||||
|
||||
@@ -35,6 +35,11 @@ type Options struct {
|
||||
// AccessTokenVerifyMethod to use when verifying access tokens
|
||||
// TODO pass a function or interface to verify? an AccessTokenVerifier?
|
||||
AccessTokenVerifyMethod string
|
||||
// AccessTokenVerifyAudience is the list of accepted audiences for JWT
|
||||
// access tokens. When non-empty, a token is only accepted if one of these
|
||||
// values is present in its "aud" claim or matches its "azp" claim. An empty
|
||||
// list disables the check.
|
||||
AccessTokenVerifyAudience []string
|
||||
// Config to use
|
||||
Config *goidc.Config
|
||||
|
||||
@@ -74,6 +79,14 @@ func WithAccessTokenVerifyMethod(val string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// WithAccessTokenVerifyAudience provides a function to set the
|
||||
// accessTokenVerifyAudience option.
|
||||
func WithAccessTokenVerifyAudience(val []string) Option {
|
||||
return func(o *Options) {
|
||||
o.AccessTokenVerifyAudience = val
|
||||
}
|
||||
}
|
||||
|
||||
// WithHTTPClient provides a function to set the httpClient option.
|
||||
func WithHTTPClient(val *http.Client) Option {
|
||||
return func(o *Options) {
|
||||
|
||||
@@ -109,6 +109,7 @@ func Server(cfg *config.Config) *cli.Command {
|
||||
|
||||
oidcClient := oidc.NewOIDCClient(
|
||||
oidc.WithAccessTokenVerifyMethod(cfg.OIDC.AccessTokenVerifyMethod),
|
||||
oidc.WithAccessTokenVerifyAudience(cfg.OIDC.AccessTokenVerifyAud),
|
||||
oidc.WithLogger(logger),
|
||||
oidc.WithHTTPClient(oidcHTTPClient),
|
||||
oidc.WithOidcIssuer(cfg.OIDC.Issuer),
|
||||
@@ -301,6 +302,7 @@ func loadMiddlewares(logger log.Logger, cfg *config.Config,
|
||||
middleware.OIDCIss(cfg.OIDC.Issuer),
|
||||
middleware.OIDCClient(oidc.NewOIDCClient(
|
||||
oidc.WithAccessTokenVerifyMethod(cfg.OIDC.AccessTokenVerifyMethod),
|
||||
oidc.WithAccessTokenVerifyAudience(cfg.OIDC.AccessTokenVerifyAud),
|
||||
oidc.WithLogger(logger),
|
||||
oidc.WithHTTPClient(oidcHTTPClient),
|
||||
oidc.WithOidcIssuer(cfg.OIDC.Issuer),
|
||||
|
||||
@@ -113,13 +113,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 IDP'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"`
|
||||
AccessTokenVerifyAud []string `yaml:"access_token_verify_aud" env:"PROXY_OIDC_ACCESS_TOKEN_VERIFY_AUD" desc:"A list of accepted audiences for OIDC access tokens, usually the OIDC client IDs used to access ownCloud (for example the web and desktop client IDs). When set, a JWT access token is only accepted if one of these values is present in its 'aud' claim or matches its 'azp' claim. This prevents tokens issued for other applications of a shared IDP from being accepted. When set via the environment variable, use a comma-separated list. Has no effect when 'access_token_verify_method' is 'none'. Leave empty to disable the check (default)." introductionVersion:"8.2.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"`
|
||||
}
|
||||
|
||||
type JWKS struct {
|
||||
|
||||
Reference in new issue
Block a user