mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 14:39:23 -05:00
Multiple access tokens can be valid for one session, for example after a refresh. A logout only removed the cached claims of the token in the session lookup, so older tokens stayed accepted. With PROXY_OIDC_SKIP_USER_INFO=true, even the removed token was accepted again, because a still valid JWT was verified anew. Write a marker on every verified logout and check it on every authenticated request, for cached and uncached tokens. This also covers tokens the proxy has not seen before the logout. If the marker store cannot be read, the cached claims are not used and the userinfo endpoint decides about the token. With PROXY_OIDC_SKIP_USER_INFO=true there is no such check, so the request is rejected. A corrupt marker always rejects. The cached record now keeps the verified subject, session and issue time, because a userinfo response can overwrite them in the claims. The cache database is renamed to start with a fresh cache. Markers get their own database because nats-js-kv only supports one TTL per bucket. They are kept for the new PROXY_OIDC_MAX_ACCESS_TOKEN_LIFETIME plus a buffer. A JWT access token with a longer lifetime or without an expiration can outlive its marker, so the proxy logs a warning for it. Logout tokens without iat are now rejected, as the spec requires the claim. Publishing the logout event and removing cached records are now best effort, so a failed publish no longer prevents the logout.
224 lines
5.3 KiB
Go
224 lines
5.3 KiB
Go
package oidc_test
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"testing"
|
|
|
|
"github.com/MicahParks/keyfunc/v2"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/opencloud-eu/opencloud/pkg/oidc"
|
|
)
|
|
|
|
type signingKey struct {
|
|
priv any
|
|
jwks *keyfunc.JWKS
|
|
}
|
|
|
|
func TestLogoutVerify(t *testing.T) {
|
|
tests := []logoutVerificationTest{
|
|
{
|
|
name: "good token",
|
|
logoutToken: jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
|
|
"iss": "https://foo",
|
|
"sub": "248289761001",
|
|
"aud": "s6BhdRkqt3",
|
|
"iat": 1471566154,
|
|
"jti": "bWJq",
|
|
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
|
|
"events": map[string]any{
|
|
"http://schemas.openid.net/event/backchannel-logout": struct{}{},
|
|
},
|
|
}),
|
|
signKey: newRSAKey(t),
|
|
},
|
|
{
|
|
name: "missing iat",
|
|
logoutToken: jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
|
|
"iss": "https://foo",
|
|
"sub": "248289761001",
|
|
"aud": "s6BhdRkqt3",
|
|
"jti": "bWJq",
|
|
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
|
|
"events": map[string]any{
|
|
"http://schemas.openid.net/event/backchannel-logout": struct{}{},
|
|
},
|
|
}),
|
|
signKey: newRSAKey(t),
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "invalid issuer",
|
|
issuer: "https://bar",
|
|
logoutToken: jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
|
|
"iss": "https://foo1",
|
|
"sub": "248289761001",
|
|
"events": map[string]any{
|
|
"http://schemas.openid.net/event/backchannel-logout": struct{}{},
|
|
},
|
|
}),
|
|
signKey: newRSAKey(t),
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "invalid sig",
|
|
logoutToken: jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
|
|
"iss": "https://foo",
|
|
"sub": "248289761001",
|
|
"aud": "s6BhdRkqt3",
|
|
"iat": 1471566154,
|
|
"jti": "bWJq",
|
|
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
|
|
"events": map[string]any{
|
|
"http://schemas.openid.net/event/backchannel-logout": struct{}{},
|
|
},
|
|
}),
|
|
signKey: newRSAKey(t),
|
|
verificationKey: newRSAKey(t),
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "no sid and no sub",
|
|
logoutToken: jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
|
|
"iss": "https://foo",
|
|
"aud": "s6BhdRkqt3",
|
|
"iat": 1471566154,
|
|
"jti": "bWJq",
|
|
"events": map[string]any{
|
|
"http://schemas.openid.net/event/backchannel-logout": struct{}{},
|
|
},
|
|
}),
|
|
signKey: newRSAKey(t),
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Prohibited nonce present",
|
|
logoutToken: jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
|
|
"iss": "https://foo",
|
|
"sub": "248289761001",
|
|
"aud": "s6BhdRkqt3",
|
|
"iat": 1471566154,
|
|
"jti": "bWJq",
|
|
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
|
|
"nonce": "123",
|
|
"events": map[string]any{
|
|
"http://schemas.openid.net/event/backchannel-logout": struct{}{},
|
|
},
|
|
}),
|
|
signKey: newRSAKey(t),
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Wrong Event string",
|
|
logoutToken: jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
|
|
"iss": "https://foo",
|
|
"sub": "248289761001",
|
|
"aud": "s6BhdRkqt3",
|
|
"iat": 1471566154,
|
|
"jti": "bWJq",
|
|
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
|
|
"events": map[string]any{
|
|
"http://blah.blah.blash/event/backchannel-logout": struct{}{},
|
|
},
|
|
}),
|
|
signKey: newRSAKey(t),
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "No Event string",
|
|
logoutToken: jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
|
|
"iss": "https://foo",
|
|
"sub": "248289761001",
|
|
"aud": "s6BhdRkqt3",
|
|
"iat": 1471566154,
|
|
"jti": "bWJq",
|
|
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
|
|
}),
|
|
signKey: newRSAKey(t),
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, test.run)
|
|
}
|
|
}
|
|
|
|
type logoutVerificationTest struct {
|
|
// Name of the subtest.
|
|
name string
|
|
|
|
// If not provided defaults to "https://foo"
|
|
issuer string
|
|
|
|
// JWT payload (just the claims).
|
|
logoutToken *jwt.Token
|
|
|
|
// Key to sign the ID Token with.
|
|
signKey *signingKey
|
|
// If not provided defaults to signKey. Only useful when
|
|
// testing invalid signatures.
|
|
verificationKey *signingKey
|
|
|
|
wantErr bool
|
|
}
|
|
|
|
func (v logoutVerificationTest) runGetToken(t *testing.T) (*oidc.LogoutToken, error) {
|
|
// token := v.signKey.sign(t, []byte(v.logoutToken))
|
|
v.logoutToken.Header["kid"] = "1"
|
|
token, err := v.logoutToken.SignedString(v.signKey.priv)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
issuer := "https://foo"
|
|
var jwks *keyfunc.JWKS
|
|
if v.verificationKey == nil {
|
|
jwks = v.signKey.jwks
|
|
} else {
|
|
jwks = v.verificationKey.jwks
|
|
}
|
|
|
|
pm := oidc.ProviderMetadata{}
|
|
verifier, err := oidc.NewOIDCClient(
|
|
oidc.WithOidcIssuer(issuer),
|
|
oidc.WithJWKS(jwks),
|
|
oidc.WithProviderMetadata(&pm),
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
return verifier.VerifyLogoutToken(ctx, token)
|
|
}
|
|
|
|
func (l logoutVerificationTest) run(t *testing.T) {
|
|
_, err := l.runGetToken(t)
|
|
if err != nil && !l.wantErr {
|
|
t.Errorf("%v", err)
|
|
}
|
|
if err == nil && l.wantErr {
|
|
t.Errorf("expected error")
|
|
}
|
|
}
|
|
|
|
func newRSAKey(t testing.TB) *signingKey {
|
|
priv, err := rsa.GenerateKey(rand.Reader, 1028)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
givenKey := keyfunc.NewGivenRSA(
|
|
&priv.PublicKey,
|
|
keyfunc.GivenKeyOptions{Algorithm: jwt.SigningMethodRS256.Alg()},
|
|
)
|
|
jwks := keyfunc.NewGiven(
|
|
map[string]keyfunc.GivenKey{
|
|
"1": givenKey,
|
|
},
|
|
)
|
|
|
|
return &signingKey{priv, jwks}
|
|
}
|