22
33import com .avast .grpc .jwt .Constants ;
44import io .grpc .*;
5+ import org .slf4j .Logger ;
6+ import org .slf4j .LoggerFactory ;
57
68public class JwtServerInterceptor <T > implements ServerInterceptor {
9+ private static Logger LOGGER = LoggerFactory .getLogger (JwtServerInterceptor .class );
10+
711 public final io .grpc .Context .Key <T > AccessTokenContextKey = Context .key ("AccessToken" );
812
913 private static final String AUTH_HEADER_PREFIX = "Bearer " ;
@@ -19,33 +23,30 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
1923 ServerCall <ReqT , RespT > call , Metadata headers , ServerCallHandler <ReqT , RespT > next ) {
2024 String authHeader = headers .get (Constants .AuthorizationMetadataKey );
2125 if (authHeader == null ) {
22- call .close (
23- Status .UNAUTHENTICATED .withDescription (
24- Constants .AuthorizationMetadataKey .name () + " header not found" ),
25- new Metadata ());
26+ String msg = Constants .AuthorizationMetadataKey .name () + " header not found" ;
27+ LOGGER .warn (msg );
28+ call .close (Status .UNAUTHENTICATED .withDescription (msg ), new Metadata ());
2629 return new ServerCall .Listener <ReqT >() {};
2730 }
2831 if (!authHeader .startsWith (AUTH_HEADER_PREFIX )) {
29- call . close (
30- Status . UNAUTHENTICATED . withDescription (
31- Constants . AuthorizationMetadataKey . name ()
32- + " header does not start with "
33- + AUTH_HEADER_PREFIX ),
34- new Metadata ());
32+ String msg =
33+ Constants . AuthorizationMetadataKey . name ()
34+ + " header does not start with "
35+ + AUTH_HEADER_PREFIX ;
36+ LOGGER . warn ( msg );
37+ call . close ( Status . UNAUTHENTICATED . withDescription ( msg ), new Metadata ());
3538 return new ServerCall .Listener <ReqT >() {};
3639 }
3740 T token ;
3841 try {
3942 token = tokenParser .parseToValid (authHeader .substring (AUTH_HEADER_PREFIX .length ()));
4043 } catch (Exception e ) {
41- call .close (
42- Status .UNAUTHENTICATED
43- .withDescription (
44- Constants .AuthorizationMetadataKey .name ()
45- + " header validation failed: "
46- + e .getMessage ())
47- .withCause (e ),
48- new Metadata ());
44+ String msg =
45+ Constants .AuthorizationMetadataKey .name ()
46+ + " header validation failed: "
47+ + e .getMessage ();
48+ LOGGER .warn (msg , e );
49+ call .close (Status .UNAUTHENTICATED .withDescription (msg ).withCause (e ), new Metadata ());
4950 return new ServerCall .Listener <ReqT >() {};
5051 }
5152 return Contexts .interceptCall (
0 commit comments