| 9 | "testing" |
| 10 | "time" |
| 11 |
| 12 | "github.com/coreos/go-oidc/v3/oidc" |
| 13 | "github.com/stretchr/testify/require" |
| 14 |
| 15 | "github.com/fatedier/frp/pkg/auth" |
| 16 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 17 | "github.com/fatedier/frp/pkg/msg" |
| 18 | ) |
| 19 |
| 20 | type mockTokenVerifier struct{} |
| 21 |
| 22 | func (m *mockTokenVerifier) Verify(ctx context.Context, subject string) (*oidc.IDToken, error) { |
| 23 | return &oidc.IDToken{ |
| 24 | Subject: subject, |
| 25 | }, nil |
| 26 | } |
| 27 |
| 28 | func TestPingWithEmptySubjectFromLoginFails(t *testing.T) { |
| 29 | r := require.New(t) |
| 30 | consumer := auth.NewOidcAuthVerifier([]v1.AuthScope{v1.AuthScopeHeartBeats}, &mockTokenVerifier{}) |
| 31 | err := consumer.VerifyPing(&msg.Ping{ |
| 32 | PrivilegeKey: "ping-without-login", |
| 33 | Timestamp: time.Now().UnixMilli(), |
| 34 | }) |
| 35 | r.Error(err) |
| 36 | r.Contains(err.Error(), "received different OIDC subject in login and ping") |
| 37 | } |
| 38 |
| 39 | func TestPingAfterLoginWithNewSubjectSucceeds(t *testing.T) { |
| 40 | r := require.New(t) |
| 41 | consumer := auth.NewOidcAuthVerifier([]v1.AuthScope{v1.AuthScopeHeartBeats}, &mockTokenVerifier{}) |
| 42 | err := consumer.VerifyLogin(&msg.Login{ |
| 43 | PrivilegeKey: "ping-after-login", |
| 44 | }) |
| 45 | r.NoError(err) |
| 46 |
| 47 | err = consumer.VerifyPing(&msg.Ping{ |
| 48 | PrivilegeKey: "ping-after-login", |
| 49 | Timestamp: time.Now().UnixMilli(), |
| 50 | }) |
| 51 | r.NoError(err) |
| 52 | } |
| 53 |
| 54 | func TestPingAfterLoginWithDifferentSubjectFails(t *testing.T) { |
| 55 | r := require.New(t) |
| 56 | consumer := auth.NewOidcAuthVerifier([]v1.AuthScope{v1.AuthScopeHeartBeats}, &mockTokenVerifier{}) |
| 57 | err := consumer.VerifyLogin(&msg.Login{ |
| 58 | PrivilegeKey: "login-with-first-subject", |
| 59 | }) |
| 60 | r.NoError(err) |
| 61 |
| 62 | err = consumer.VerifyPing(&msg.Ping{ |
| 63 | PrivilegeKey: "ping-with-different-subject", |
| 64 | Timestamp: time.Now().UnixMilli(), |
| 65 | }) |
| 66 | r.Error(err) |
| 67 | r.Contains(err.Error(), "received different OIDC subject in login and ping") |
| 68 | } |
| 69 |
| 70 | func TestOidcAuthProviderFallsBackWhenNoExpiry(t *testing.T) { |
| 71 | r := require.New(t) |
| 72 |
| 73 | var requestCount atomic.Int32 |
| 74 | tokenServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 75 | requestCount.Add(1) |
| 76 | w.Header().Set("Content-Type", "application/json") |
| 77 | _ = json.NewEncoder(w).Encode(map[string]any{ //nolint:gosec // test-only dummy token response |
| 78 | "access_token": "fresh-test-token", |
| 79 | "token_type": "Bearer", |
| 80 | }) |
| 81 | })) |
| 82 | defer tokenServer.Close() |
| 83 |
| 84 | provider, err := auth.NewOidcAuthSetter( |
| 85 | []v1.AuthScope{v1.AuthScopeHeartBeats}, |
| 86 | v1.AuthOIDCClientConfig{ |
| 87 | ClientID: "test-client", |
| 88 | ClientSecret: "test-secret", |
| 89 | TokenEndpointURL: tokenServer.URL, |
| 90 | }, |
| 91 | ) |
| 92 | r.NoError(err) |
| 93 |
| 94 | // Constructor no longer fetches a token eagerly. |
| 95 | // The first SetLogin triggers the adaptive probe. |
| 96 | r.Equal(int32(0), requestCount.Load()) |
| 97 |
| 98 | loginMsg := &msg.Login{} |
| 99 | err = provider.SetLogin(loginMsg) |
| 100 | r.NoError(err) |
| 101 | r.Equal("fresh-test-token", loginMsg.PrivilegeKey) |
| 102 |
| 103 | for range 3 { |
| 104 | pingMsg := &msg.Ping{} |
| 105 | err = provider.SetPing(pingMsg) |
| 106 | r.NoError(err) |
| 107 | r.Equal("fresh-test-token", pingMsg.PrivilegeKey) |
| 108 | } |
| 109 |
| 110 | // 1 probe (login) + 3 pings = 4 requests (probe doubles as the login token fetch) |
| 111 | r.Equal(int32(4), requestCount.Load(), "each call should fetch a fresh token when expires_in is missing") |
| 112 | } |
| 113 |
| 114 | func TestOidcAuthProviderCachesToken(t *testing.T) { |
| 115 | r := require.New(t) |
| 116 |
| 117 | var requestCount atomic.Int32 |
| 118 | tokenServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 119 | requestCount.Add(1) |
| 120 | w.Header().Set("Content-Type", "application/json") |
| 121 | _ = json.NewEncoder(w).Encode(map[string]any{ //nolint:gosec // test-only dummy token response |
| 122 | "access_token": "cached-test-token", |
| 123 | "token_type": "Bearer", |
| 124 | "expires_in": 3600, |
| 125 | }) |
| 126 | })) |
| 127 | defer tokenServer.Close() |
| 128 |
| 129 | provider, err := auth.NewOidcAuthSetter( |
| 130 | []v1.AuthScope{v1.AuthScopeHeartBeats}, |
| 131 | v1.AuthOIDCClientConfig{ |
| 132 | ClientID: "test-client", |
| 133 | ClientSecret: "test-secret", |
| 134 | TokenEndpointURL: tokenServer.URL, |
| 135 | }, |
| 136 | ) |
| 137 | r.NoError(err) |
| 138 |
| 139 | // Constructor no longer fetches eagerly; first SetLogin triggers the probe. |
| 140 | r.Equal(int32(0), requestCount.Load()) |
| 141 |
| 142 | // SetLogin triggers the adaptive probe and caches the token. |
| 143 | loginMsg := &msg.Login{} |
| 144 | err = provider.SetLogin(loginMsg) |
| 145 | r.NoError(err) |
| 146 | r.Equal("cached-test-token", loginMsg.PrivilegeKey) |
| 147 | r.Equal(int32(1), requestCount.Load()) |
| 148 |
| 149 | // Subsequent calls should also reuse the cached token |
| 150 | for range 5 { |
| 151 | pingMsg := &msg.Ping{} |
| 152 | err = provider.SetPing(pingMsg) |
| 153 | r.NoError(err) |
| 154 | r.Equal("cached-test-token", pingMsg.PrivilegeKey) |
| 155 | } |
| 156 | r.Equal(int32(1), requestCount.Load(), "token endpoint should only be called once; cached token should be reused") |
| 157 | } |
| 158 |
| 159 | func TestOidcAuthProviderRetriesOnInitialFailure(t *testing.T) { |
| 160 | r := require.New(t) |
| 161 |
| 162 | var requestCount atomic.Int32 |
| 163 | tokenServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 164 | n := requestCount.Add(1) |
| 165 | // The oauth2 library retries once internally, so we need two |
| 166 | // consecutive failures to surface an error to the caller. |
| 167 | if n <= 2 { |
| 168 | w.Header().Set("Content-Type", "application/json") |
| 169 | w.WriteHeader(http.StatusBadRequest) |
| 170 | _ = json.NewEncoder(w).Encode(map[string]any{ |
| 171 | "error": "temporarily_unavailable", |
| 172 | "error_description": "service is starting up", |
| 173 | }) |
| 174 | return |
| 175 | } |
| 176 | w.Header().Set("Content-Type", "application/json") |
| 177 | _ = json.NewEncoder(w).Encode(map[string]any{ //nolint:gosec // test-only dummy token response |
| 178 | "access_token": "retry-test-token", |
| 179 | "token_type": "Bearer", |
| 180 | "expires_in": 3600, |
| 181 | }) |
| 182 | })) |
| 183 | defer tokenServer.Close() |
| 184 |
| 185 | // Constructor succeeds even though the IdP is "down". |
| 186 | provider, err := auth.NewOidcAuthSetter( |
| 187 | []v1.AuthScope{v1.AuthScopeHeartBeats}, |
| 188 | v1.AuthOIDCClientConfig{ |
| 189 | ClientID: "test-client", |
| 190 | ClientSecret: "test-secret", |
| 191 | TokenEndpointURL: tokenServer.URL, |
| 192 | }, |
| 193 | ) |
| 194 | r.NoError(err) |
| 195 | r.Equal(int32(0), requestCount.Load()) |
| 196 |
| 197 | // First SetLogin hits the IdP, which returns an error (after internal retry). |
| 198 | loginMsg := &msg.Login{} |
| 199 | err = provider.SetLogin(loginMsg) |
| 200 | r.Error(err) |
| 201 | r.Equal(int32(2), requestCount.Load()) |
| 202 |
| 203 | // Second SetLogin retries and succeeds. |
| 204 | err = provider.SetLogin(loginMsg) |
| 205 | r.NoError(err) |
| 206 | r.Equal("retry-test-token", loginMsg.PrivilegeKey) |
| 207 | r.Equal(int32(3), requestCount.Load()) |
| 208 |
| 209 | // Subsequent calls use cached token. |
| 210 | pingMsg := &msg.Ping{} |
| 211 | err = provider.SetPing(pingMsg) |
| 212 | r.NoError(err) |
| 213 | r.Equal("retry-test-token", pingMsg.PrivilegeKey) |
| 214 | r.Equal(int32(3), requestCount.Load()) |
| 215 | } |
| 216 |
| 217 | func TestNewOidcAuthSetterRejectsInvalidStaticConfig(t *testing.T) { |
| 218 | r := require.New(t) |
| 219 | tokenServer := httptest.NewServer(http.NotFoundHandler()) |
| 220 | defer tokenServer.Close() |
| 221 |
| 222 | _, err := auth.NewOidcAuthSetter(nil, v1.AuthOIDCClientConfig{ |
| 223 | ClientID: "test-client", |
| 224 | TokenEndpointURL: "://bad", |
| 225 | }) |
| 226 | r.Error(err) |
| 227 | r.Contains(err.Error(), "auth.oidc.tokenEndpointURL") |
| 228 |
| 229 | _, err = auth.NewOidcAuthSetter(nil, v1.AuthOIDCClientConfig{ |
| 230 | TokenEndpointURL: tokenServer.URL, |
| 231 | }) |
| 232 | r.Error(err) |
| 233 | r.Contains(err.Error(), "auth.oidc.clientID is required") |
| 234 |
| 235 | _, err = auth.NewOidcAuthSetter(nil, v1.AuthOIDCClientConfig{ |
| 236 | ClientID: "test-client", |
| 237 | TokenEndpointURL: tokenServer.URL, |
| 238 | AdditionalEndpointParams: map[string]string{ |
| 239 | "scope": "profile", |
| 240 | }, |
| 241 | }) |
| 242 | r.Error(err) |
| 243 | r.Contains(err.Error(), "auth.oidc.additionalEndpointParams.scope is not allowed; use auth.oidc.scope instead") |
| 244 |
| 245 | _, err = auth.NewOidcAuthSetter(nil, v1.AuthOIDCClientConfig{ |
| 246 | ClientID: "test-client", |
| 247 | TokenEndpointURL: tokenServer.URL, |
| 248 | Audience: "api", |
| 249 | AdditionalEndpointParams: map[string]string{"audience": "override"}, |
| 250 | }) |
| 251 | r.Error(err) |
| 252 | r.Contains(err.Error(), "cannot specify both auth.oidc.audience and auth.oidc.additionalEndpointParams.audience") |
| 253 | } |