Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added authentication propagating listener #233

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
Context ctx = Context.current()
.withValue(GrpcSecurity.AUTHENTICATION_CONTEXT_KEY, SecurityContextHolder.getContext().getAuthentication());

return Contexts.interceptCall(ctx, call, headers, next);
return Contexts.interceptCall(ctx, call, headers, authenticationPropagatingHandler(next));
} catch (AccessDeniedException e) {
return fail(next, call, headers, Status.PERMISSION_DENIED, e);
} catch (Exception e) {
Expand All @@ -107,6 +107,48 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(

}

private <ReqT, RespT> ServerCallHandler<ReqT, RespT> authenticationPropagatingHandler(ServerCallHandler<ReqT, RespT> next) {

return (call, headers) -> new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(next.startCall(call, headers)) {

@Override
public void onMessage(ReqT message) {
propagateAuthentication(() -> super.onMessage(message));
}

@Override
public void onHalfClose() {
propagateAuthentication(super::onHalfClose);
}

@Override
public void onCancel() {
propagateAuthentication(super::onCancel);
}

@Override
public void onComplete() {
propagateAuthentication(super::onComplete);
}

@Override
public void onReady() {
propagateAuthentication(super::onReady);
}

private void propagateAuthentication(Runnable runnable) {
try {
SecurityContextHolder.getContext().setAuthentication(GrpcSecurity.AUTHENTICATION_CONTEXT_KEY.get());
runnable.run();
} finally {
SecurityContextHolder.clearContext();
}
}

};

}

private <RespT, ReqT> ServerCall.Listener<ReqT> fail(ServerCallHandler<ReqT, RespT> next, ServerCall<ReqT, RespT> call, Metadata headers,final Status status, Exception exception) {

if (authCfg.isFailFast()) {
Expand Down