| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package validation |
| 16 |
| 17 | import ( |
| 18 | "net/http" |
| 19 | "net/http/httptest" |
| 20 | "testing" |
| 21 |
| 22 | "github.com/stretchr/testify/require" |
| 23 |
| 24 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 25 | ) |
| 26 |
| 27 | func TestValidateOIDCClientCredentialsConfig(t *testing.T) { |
| 28 | tokenServer := httptest.NewServer(http.NotFoundHandler()) |
| 29 | defer tokenServer.Close() |
| 30 |
| 31 | t.Run("valid", func(t *testing.T) { |
| 32 | require.NoError(t, ValidateOIDCClientCredentialsConfig(&v1.AuthOIDCClientConfig{ |
| 33 | ClientID: "test-client", |
| 34 | TokenEndpointURL: tokenServer.URL, |
| 35 | AdditionalEndpointParams: map[string]string{ |
| 36 | "resource": "api", |
| 37 | }, |
| 38 | })) |
| 39 | }) |
| 40 |
| 41 | t.Run("invalid token endpoint url", func(t *testing.T) { |
| 42 | err := ValidateOIDCClientCredentialsConfig(&v1.AuthOIDCClientConfig{ |
| 43 | ClientID: "test-client", |
| 44 | TokenEndpointURL: "://bad", |
| 45 | }) |
| 46 | require.ErrorContains(t, err, "auth.oidc.tokenEndpointURL") |
| 47 | }) |
| 48 |
| 49 | t.Run("missing client id", func(t *testing.T) { |
| 50 | err := ValidateOIDCClientCredentialsConfig(&v1.AuthOIDCClientConfig{ |
| 51 | TokenEndpointURL: tokenServer.URL, |
| 52 | }) |
| 53 | require.ErrorContains(t, err, "auth.oidc.clientID is required") |
| 54 | }) |
| 55 |
| 56 | t.Run("scope endpoint param is not allowed", func(t *testing.T) { |
| 57 | err := ValidateOIDCClientCredentialsConfig(&v1.AuthOIDCClientConfig{ |
| 58 | ClientID: "test-client", |
| 59 | TokenEndpointURL: tokenServer.URL, |
| 60 | AdditionalEndpointParams: map[string]string{ |
| 61 | "scope": "email", |
| 62 | }, |
| 63 | }) |
| 64 | require.ErrorContains(t, err, "auth.oidc.additionalEndpointParams.scope is not allowed; use auth.oidc.scope instead") |
| 65 | }) |
| 66 |
| 67 | t.Run("audience conflict", func(t *testing.T) { |
| 68 | err := ValidateOIDCClientCredentialsConfig(&v1.AuthOIDCClientConfig{ |
| 69 | ClientID: "test-client", |
| 70 | TokenEndpointURL: tokenServer.URL, |
| 71 | Audience: "api", |
| 72 | AdditionalEndpointParams: map[string]string{ |
| 73 | "audience": "override", |
| 74 | }, |
| 75 | }) |
| 76 | require.ErrorContains(t, err, "cannot specify both auth.oidc.audience and auth.oidc.additionalEndpointParams.audience") |
| 77 | }) |
| 78 | } |