001/** 002 * Copyright (C) 2006-2020 Talend Inc. - www.talend.com 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.talend.sdk.component.server.front.security; 017 018import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE; 019import static org.talend.sdk.component.server.front.model.ErrorDictionary.UNAUTHORIZED; 020 021import java.io.IOException; 022 023import javax.enterprise.context.Dependent; 024import javax.enterprise.event.Event; 025import javax.inject.Inject; 026import javax.servlet.http.HttpServletRequest; 027import javax.ws.rs.container.ContainerRequestContext; 028import javax.ws.rs.container.ContainerRequestFilter; 029import javax.ws.rs.container.PreMatching; 030import javax.ws.rs.core.Context; 031import javax.ws.rs.core.Response; 032import javax.ws.rs.ext.Provider; 033 034import org.talend.sdk.component.server.front.model.error.ErrorPayload; 035import org.talend.sdk.component.server.service.security.event.OnConnection; 036 037@Provider 038@Dependent 039@PreMatching 040public class ConnectionSecurityProvider implements ContainerRequestFilter { 041 042 public static final String SKIP = ConnectionSecurityProvider.class.getName() + ".skip"; 043 044 @Context 045 private HttpServletRequest request; 046 047 @Inject 048 private Event<OnConnection> onConnectionEvent; 049 050 @Override 051 public void filter(final ContainerRequestContext requestContext) throws IOException { 052 if (Boolean.TRUE.equals(request.getAttribute(SKIP))) { 053 return; 054 } 055 056 final OnConnection onConnection = new OnConnection(); 057 onConnectionEvent.fire(onConnection); 058 if (!onConnection.isValid()) { 059 requestContext 060 .abortWith(Response 061 .status(Response.Status.UNAUTHORIZED) 062 .entity(new ErrorPayload(UNAUTHORIZED, "Invalid connection credentials")) 063 .type(APPLICATION_JSON_TYPE) 064 .build()); 065 } 066 } 067}