package oauth2 //nolint:testpackage import ( "net/http" "testing" "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config" "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/idtoken" "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/types" "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/state" "github.com/stretchr/testify/require" ) func BenchmarkGetAuthorizeParams(b *testing.B) { for _, tc := range []struct { name string input string }{ { name: "single", input: "prompt=login", }, { name: "multiple", input: "prompt=login&audience=vpn&kc_idp_hint=google", }, } { b.Run(tc.name, func(b *testing.B) { var params any b.ReportAllocs() b.ResetTimer() for b.Loop() { parsedParams, err := getAuthorizeParams(tc.input) if err != nil { b.Fatal(err) } params = parsedParams } _ = params }) } } func BenchmarkCheckClientIPAddr(b *testing.B) { session := state.State{IPAddr: "127.0.0.1"} b.Run("direct", func(b *testing.B) { conf := config.Defaults req := &http.Request{RemoteAddr: "127.0.0.1:12345", Header: make(http.Header)} b.ReportAllocs() b.ResetTimer() for b.Loop() { if err := checkClientIPAddr(req, &conf, session); err != nil { b.Fatal(err) } } }) b.Run("proxy-headers", func(b *testing.B) { conf := config.Defaults conf.HTTP.EnableProxyHeaders = true conf.HTTP.TrustedProxies = append(conf.HTTP.TrustedProxies, "10.0.0.1/32") req := &http.Request{RemoteAddr: "10.0.0.1:12345", Header: http.Header{"X-Forwarded-For": []string{"127.0.0.1, 10.0.0.1"}}} b.ReportAllocs() b.ResetTimer() for b.Loop() { if err := checkClientIPAddr(req, &conf, session); err != nil { b.Fatal(err) } } }) } func BenchmarkCheckIdentityCEL(b *testing.B) { for _, tc := range []struct { name string expression string state state.State token *idtoken.IDToken }{ { name: "equality", expression: "openvpn.commonName == user.username", state: state.State{ Client: state.ClientIdentifier{CommonName: "test-client"}, IPAddr: "127.0.0.1", }, token: &idtoken.IDToken{ IDTokenClaims: &idtoken.Claims{Claims: map[string]any{"preferred_username": "test-client"}}, }, }, { name: "lower-ascii", expression: "openvpn.commonName.lowerAscii() == user.username.lowerAscii()", state: state.State{ Client: state.ClientIdentifier{CommonName: "Test-Client"}, IPAddr: "127.0.0.1", }, token: &idtoken.IDToken{ IDTokenClaims: &idtoken.Claims{Claims: map[string]any{"preferred_username": "test-client"}}, }, }, } { b.Run(tc.name, func(b *testing.B) { conf := config.Defaults conf.OAuth2.Validate.Expression = tc.expression client := &Client{conf: &conf} require.NoError(b, client.initializeCELValidation()) b.ReportAllocs() b.ResetTimer() for b.Loop() { if err := client.CheckIdentityCEL( CELAuthModeInteractive, tc.state, tc.token, types.UserInfo{Username: "test-client"}, ); err != nil { b.Fatal(err) } } }) } }