@@ -4016,6 +4016,123 @@ func TestIntrospectionAuthentication(t *testing.T) {
40164016 })
40174017}
40184018
4019+ func TestUseCustomization (t * testing.T ) {
4020+ t .Parallel ()
4021+
4022+ authHeader := func (token string ) http.Header {
4023+ return http.Header {
4024+ "Authorization" : []string {"Bearer " + token },
4025+ }
4026+ }
4027+
4028+ testRequest := func (t * testing.T , xEnv * testenv.Environment , header http.Header , expectSuccess bool ) string {
4029+ t .Helper ()
4030+
4031+ res , err := xEnv .MakeRequest (http .MethodPost , "/graphql" , header , strings .NewReader (employeesQuery ))
4032+ require .NoError (t , err )
4033+ defer res .Body .Close ()
4034+
4035+ if expectSuccess {
4036+ require .Equal (t , http .StatusOK , res .StatusCode )
4037+ require .Equal (t , JwksName , res .Header .Get (xAuthenticatedByHeader ))
4038+ } else {
4039+ require .Equal (t , http .StatusUnauthorized , res .StatusCode )
4040+ }
4041+
4042+ data , err := io .ReadAll (res .Body )
4043+ require .NoError (t , err )
4044+ return string (data )
4045+ }
4046+
4047+ testSetup := func (t * testing.T , crypto jwks.Crypto , allowedUse ... string ) (string , []authentication.Authenticator ) {
4048+ t .Helper ()
4049+
4050+ authServer , err := jwks .NewServerWithOptions (t , jwks .WithProviders (crypto ), jwks .WithUse ("" ))
4051+ require .NoError (t , err )
4052+ t .Cleanup (authServer .Close )
4053+
4054+ cfg := toJWKSConfig (authServer .JWKSURL (), time .Second * 5 )
4055+ cfg .AllowedUse = allowedUse
4056+
4057+ tokenDecoder , err := authentication .NewJwksTokenDecoder (
4058+ NewContextWithCancel (t ),
4059+ zap .NewNop (),
4060+ []authentication.JWKSConfig {cfg },
4061+ )
4062+ require .NoError (t , err )
4063+
4064+ authOptions := authentication.HttpHeaderAuthenticatorOptions {
4065+ Name : JwksName ,
4066+ TokenDecoder : tokenDecoder ,
4067+ }
4068+ authenticator , err := authentication .NewHttpHeaderAuthenticator (authOptions )
4069+ require .NoError (t , err )
4070+
4071+ authenticators := []authentication.Authenticator {authenticator }
4072+
4073+ token , err := authServer .TokenForKID (crypto .KID (), nil , false )
4074+ require .NoError (t , err )
4075+
4076+ return token , authenticators
4077+ }
4078+
4079+ t .Run ("Use option" , func (t * testing.T ) {
4080+ t .Parallel ()
4081+
4082+ t .Run ("Test authentication with empty use should fail by default" , func (t * testing.T ) {
4083+ t .Parallel ()
4084+
4085+ rsaCrypto , err := jwks .NewRSACrypto ("test" , jwkset .AlgRS256 , 2048 )
4086+ require .NoError (t , err )
4087+
4088+ token , authenticators := testSetup (t , rsaCrypto )
4089+
4090+ accessController , err := core .NewAccessController (core.AccessControllerOptions {
4091+ Authenticators : authenticators ,
4092+ AuthenticationRequired : true ,
4093+ SkipIntrospectionQueries : false ,
4094+ IntrospectionSkipSecret : "" ,
4095+ })
4096+ require .NoError (t , err )
4097+
4098+ testenv .Run (t , & testenv.Config {
4099+ RouterOptions : []core.Option {
4100+ core .WithAccessController (accessController ),
4101+ },
4102+ }, func (t * testing.T , xEnv * testenv.Environment ) {
4103+ body := testRequest (t , xEnv , authHeader (token ), false )
4104+ require .Equal (t , unauthorizedExpectedData , string (body ))
4105+ })
4106+ })
4107+
4108+ t .Run ("Test authentication with empty use should succeed if allowed" , func (t * testing.T ) {
4109+ t .Parallel ()
4110+
4111+ rsaCrypto , err := jwks .NewRSACrypto ("test" , jwkset .AlgRS256 , 2048 )
4112+ require .NoError (t , err )
4113+
4114+ token , authenticators := testSetup (t , rsaCrypto , "" )
4115+
4116+ accessController , err := core .NewAccessController (core.AccessControllerOptions {
4117+ Authenticators : authenticators ,
4118+ AuthenticationRequired : true ,
4119+ SkipIntrospectionQueries : false ,
4120+ IntrospectionSkipSecret : "" ,
4121+ })
4122+ require .NoError (t , err )
4123+
4124+ testenv .Run (t , & testenv.Config {
4125+ RouterOptions : []core.Option {
4126+ core .WithAccessController (accessController ),
4127+ },
4128+ }, func (t * testing.T , xEnv * testenv.Environment ) {
4129+ body := testRequest (t , xEnv , authHeader (token ), true )
4130+ require .Equal (t , employeesExpectedData , string (body ))
4131+ })
4132+ })
4133+ })
4134+ }
4135+
40194136func toJWKSConfig (url string , refresh time.Duration , allowedAlgorithms ... string ) authentication.JWKSConfig {
40204137 return authentication.JWKSConfig {
40214138 URL : url ,
0 commit comments