Skip to content

Commit 03619b7

Browse files
committed
feat: slf4j based logging
1 parent 373eb85 commit 03619b7

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

core/build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
archivesBaseName = 'grpc-java-jwt'
22

33
dependencies {
4-
compile "io.grpc:grpc-core:$grpcVersion"
4+
compile "io.grpc:grpc-core:$grpcVersion"
5+
compile "org.slf4j:slf4j-api:1.7.26"
56
}

core/src/main/java/com/avast/grpc/jwt/client/JwtCallCredentials.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import io.grpc.Metadata;
66
import io.grpc.Status;
77
import java.util.concurrent.Executor;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
810

911
public abstract class JwtCallCredentials extends CallCredentials {
12+
private static Logger LOGGER = LoggerFactory.getLogger(JwtCallCredentials.class);
1013

1114
public static JwtCallCredentials synchronous(SynchronousJwtTokenProvider tokenProvider) {
1215
return new Synchronous(tokenProvider);
@@ -30,10 +33,9 @@ protected void applyToken(MetadataApplier applier, String jwtToken) {
3033
}
3134

3235
protected void applyFailure(MetadataApplier applier, Throwable e) {
33-
applier.fail(
34-
Status.UNAUTHENTICATED
35-
.withDescription("An exception when obtaining JWT token")
36-
.withCause(e));
36+
String msg = "An exception when obtaining JWT token";
37+
LOGGER.error(msg, e);
38+
applier.fail(Status.UNAUTHENTICATED.withDescription(msg).withCause(e));
3739
}
3840

3941
public static class Synchronous extends JwtCallCredentials {

core/src/main/java/com/avast/grpc/jwt/server/JwtServerInterceptor.java

+19-18
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import com.avast.grpc.jwt.Constants;
44
import io.grpc.*;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
57

68
public 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

Comments
 (0)