2
2
3
3
import com .avast .grpc .jwt .Constants ;
4
4
import io .grpc .*;
5
+ import org .slf4j .Logger ;
6
+ import org .slf4j .LoggerFactory ;
5
7
6
8
public class JwtServerInterceptor <T > implements ServerInterceptor {
9
+ private static Logger LOGGER = LoggerFactory .getLogger (JwtServerInterceptor .class );
10
+
7
11
public final io .grpc .Context .Key <T > AccessTokenContextKey = Context .key ("AccessToken" );
8
12
9
13
private static final String AUTH_HEADER_PREFIX = "Bearer " ;
@@ -19,33 +23,30 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
19
23
ServerCall <ReqT , RespT > call , Metadata headers , ServerCallHandler <ReqT , RespT > next ) {
20
24
String authHeader = headers .get (Constants .AuthorizationMetadataKey );
21
25
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 ());
26
29
return new ServerCall .Listener <ReqT >() {};
27
30
}
28
31
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 ());
35
38
return new ServerCall .Listener <ReqT >() {};
36
39
}
37
40
T token ;
38
41
try {
39
42
token = tokenParser .parseToValid (authHeader .substring (AUTH_HEADER_PREFIX .length ()));
40
43
} 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 ());
49
50
return new ServerCall .Listener <ReqT >() {};
50
51
}
51
52
return Contexts .interceptCall (
0 commit comments