forked from fatedier/frp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support multiple subjects in oidc ping
Validate the subject in an oidc ping against a list of logged in subjects. This resolves the issue that multiple connected FRP clients with different OIDC clients result in a failing ping. The ping would fail because the subject in memory would be the value of the last logged in FRPC. This change also changes the constructor of OidcAuthVerifier to take a TokenVerifier interface. This will not change production behavior, but makes testing easier because we can inject a mock verifier during testing. Resolves: fatedier#4466
- Loading branch information
Rob Kenis
committed
Oct 11, 2024
1 parent
fe4ca1b
commit d082976
Showing
3 changed files
with
85 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package auth_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/coreos/go-oidc/v3/oidc" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/fatedier/frp/pkg/auth" | ||
v1 "github.com/fatedier/frp/pkg/config/v1" | ||
"github.com/fatedier/frp/pkg/msg" | ||
) | ||
|
||
type mockTokenVerifier struct{} | ||
|
||
func (m *mockTokenVerifier) Verify(ctx context.Context, subject string) (*oidc.IDToken, error) { | ||
return &oidc.IDToken{ | ||
Subject: subject, | ||
}, nil | ||
} | ||
|
||
func TestPingWithEmptySubjectFromLoginFails(t *testing.T) { | ||
r := require.New(t) | ||
consumer := auth.NewOidcAuthVerifier([]v1.AuthScope{v1.AuthScopeHeartBeats}, &mockTokenVerifier{}) | ||
err := consumer.VerifyPing(&msg.Ping{ | ||
PrivilegeKey: "ping-without-login", | ||
Timestamp: time.Now().UnixMilli(), | ||
}) | ||
r.Error(err) | ||
r.Contains(err.Error(), "received different OIDC subject in login and ping") | ||
} | ||
|
||
func TestPingAfterLoginWithNewSubjectSucceeds(t *testing.T) { | ||
r := require.New(t) | ||
consumer := auth.NewOidcAuthVerifier([]v1.AuthScope{v1.AuthScopeHeartBeats}, &mockTokenVerifier{}) | ||
err := consumer.VerifyLogin(&msg.Login{ | ||
PrivilegeKey: "ping-after-login", | ||
}) | ||
r.NoError(err) | ||
|
||
err = consumer.VerifyPing(&msg.Ping{ | ||
PrivilegeKey: "ping-after-login", | ||
Timestamp: time.Now().UnixMilli(), | ||
}) | ||
r.NoError(err) | ||
} | ||
|
||
func TestPingAfterLoginWithDifferentSubjectFails(t *testing.T) { | ||
r := require.New(t) | ||
consumer := auth.NewOidcAuthVerifier([]v1.AuthScope{v1.AuthScopeHeartBeats}, &mockTokenVerifier{}) | ||
err := consumer.VerifyLogin(&msg.Login{ | ||
PrivilegeKey: "login-with-first-subject", | ||
}) | ||
r.NoError(err) | ||
|
||
err = consumer.VerifyPing(&msg.Ping{ | ||
PrivilegeKey: "ping-with-different-subject", | ||
Timestamp: time.Now().UnixMilli(), | ||
}) | ||
r.Error(err) | ||
r.Contains(err.Error(), "received different OIDC subject in login and ping") | ||
} |