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

Authentication and Authorization #61

Closed
amit-c1x opened this issue Mar 20, 2018 · 8 comments
Closed

Authentication and Authorization #61

amit-c1x opened this issue Mar 20, 2018 · 8 comments

Comments

@amit-c1x
Copy link

Hello,

We are using this library for last few months and are very happy with it. We would like to know if it is possible to handle auth/authorization using jax-rs annotations?
Additionally, i have looked at adding an authentication handler but couldn't quite get it working. Would be great if someone could give pointers to get it working.

@Service
@Slf4j
public class AuthenticationHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

    @Autowired
    private AuthenticationService authenticationService;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {

        log.info("In Auth Handler");
        if (!authenticationService.isAuthenticated(request)) {
            HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
            ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
            return;
        }
        ctx.fireChannelRead(request);
    }
}

Below is how i add this handler in channelpipeline

                .setChannelPipelineModifier(new ChannelPipelineModifier() {
                    @Override
                    public void modify(ChannelPipeline channelPipeline) {
                        CorsConfig corsConfig = CorsConfigBuilder.
                                forAnyOrigin().
                                allowNullOrigin()
                                .allowCredentials()
                                .allowedRequestMethods(HttpMethod.CONNECT, HttpMethod.GET, HttpMethod.PUT, HttpMethod.OPTIONS, HttpMethod.POST, HttpMethod.DELETE)
                                .allowedRequestHeaders("Content-Type", "Access-Control-Allow-Headers", "Authorization", "X-Requested-With", "authorization")
                                .exposeHeaders("Content-Type", "Access-Control-Allow-Headers", "Authorization", "X-Requested-With", "authorization")
                                .build();
                        channelPipeline.addBefore("router", "cors", new CorsHandler(corsConfig));
                        channelPipeline.addBefore("router", "auth", authenticationHandler);
@chtyim
Copy link
Contributor

chtyim commented Mar 20, 2018

Do you see any error / exception or not observing the AuthenticationHandler being triggered? Would you mind give some more details?

@amit-c1x
Copy link
Author

I dont see AuthenticationHandler being triggered. No error as such.

@chtyim
Copy link
Contributor

chtyim commented Mar 20, 2018

From the client perspective, does it get any response or does it hang? Does it act like the auth handler is skipped?

@amit-c1x
Copy link
Author

yes, it seems auth handler is skipped.

@chtyim
Copy link
Contributor

chtyim commented Mar 20, 2018

Can you change the type provided to the SimpleChannelInboundHandler to HttpRequest instead of FullHttpRequest? The HttpObjectAggregator is added by the "router", hence any handler that goes before the "router" will only get HttpRequest

@amit-c1x
Copy link
Author

Yup it works now. Thanks!

Additionally, is there any planned effort for adding authentication annotations?
How would you suggest adding auth in my application? As usual, we have mix of authentication and open APIs

@chtyim
Copy link
Contributor

chtyim commented Mar 20, 2018

Supporting the jax-rs authentication annotation is a good suggestion but we currently don't have any plan on that yet. For now, you have to perform authentication in your auth handler based on the request. If you are interested in helping to add the auth annotation support, that would be great!

@amit-c1x
Copy link
Author

Will try to put in a PR soon. Closing this for now. Thanks a lot for the help!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants