oidc_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package auth_test
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/coreos/go-oidc/v3/oidc"
  7. "github.com/stretchr/testify/require"
  8. "github.com/fatedier/frp/pkg/auth"
  9. v1 "github.com/fatedier/frp/pkg/config/v1"
  10. "github.com/fatedier/frp/pkg/msg"
  11. )
  12. type mockTokenVerifier struct{}
  13. func (m *mockTokenVerifier) Verify(ctx context.Context, subject string) (*oidc.IDToken, error) {
  14. return &oidc.IDToken{
  15. Subject: subject,
  16. }, nil
  17. }
  18. func TestPingWithEmptySubjectFromLoginFails(t *testing.T) {
  19. r := require.New(t)
  20. consumer := auth.NewOidcAuthVerifier([]v1.AuthScope{v1.AuthScopeHeartBeats}, &mockTokenVerifier{})
  21. err := consumer.VerifyPing(&msg.Ping{
  22. PrivilegeKey: "ping-without-login",
  23. Timestamp: time.Now().UnixMilli(),
  24. })
  25. r.Error(err)
  26. r.Contains(err.Error(), "received different OIDC subject in login and ping")
  27. }
  28. func TestPingAfterLoginWithNewSubjectSucceeds(t *testing.T) {
  29. r := require.New(t)
  30. consumer := auth.NewOidcAuthVerifier([]v1.AuthScope{v1.AuthScopeHeartBeats}, &mockTokenVerifier{})
  31. err := consumer.VerifyLogin(&msg.Login{
  32. PrivilegeKey: "ping-after-login",
  33. })
  34. r.NoError(err)
  35. err = consumer.VerifyPing(&msg.Ping{
  36. PrivilegeKey: "ping-after-login",
  37. Timestamp: time.Now().UnixMilli(),
  38. })
  39. r.NoError(err)
  40. }
  41. func TestPingAfterLoginWithDifferentSubjectFails(t *testing.T) {
  42. r := require.New(t)
  43. consumer := auth.NewOidcAuthVerifier([]v1.AuthScope{v1.AuthScopeHeartBeats}, &mockTokenVerifier{})
  44. err := consumer.VerifyLogin(&msg.Login{
  45. PrivilegeKey: "login-with-first-subject",
  46. })
  47. r.NoError(err)
  48. err = consumer.VerifyPing(&msg.Ping{
  49. PrivilegeKey: "ping-with-different-subject",
  50. Timestamp: time.Now().UnixMilli(),
  51. })
  52. r.Error(err)
  53. r.Contains(err.Error(), "received different OIDC subject in login and ping")
  54. }