/**
 * Copyright 2016 Pinterest, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *    
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pinterest.teletraan.security;

import com.pinterest.deployservice.common.DeployInternalException;
import com.pinterest.teletraan.TeletraanServiceContext;
import com.pinterest.teletraan.exception.TeletaanInternalException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

@Provider
@Priority(Priorities.AUTHENTICATION)
public class TokenAuthFilter implements ContainerRequestFilter {

    private static final String AUTHENTICATION_HEADER = "Authorization";
    private static final String TELETRAAN_TOKEN_SCHEME = "token";
    private static final Logger LOG = LoggerFactory.getLogger(TokenAuthFilter.class);

    private UserDataHelper userDataHelper;

    public TokenAuthFilter(String userDataUrl, String groupDataUrl, String tokenCacheSpec, TeletraanServiceContext context) throws Exception {
        userDataHelper = new UserDataHelper(userDataUrl, groupDataUrl, tokenCacheSpec, context);
    }

    @Override
    public void filter(ContainerRequestContext context) throws IOException {
        SecurityContext securityContext;
        try {
            securityContext = authenticate(context);
        } catch (Exception e) {
            LOG.info("Authentication failed. Reason: " + e.getMessage());
            throw new TeletaanInternalException(Response.Status.UNAUTHORIZED,
                "Failed to authenticate user. " + e.getMessage());
        }
        context.setSecurityContext(securityContext);
    }

    private SecurityContext authenticate(ContainerRequestContext context) throws Exception {
        String authCredentials = context.getHeaderString(AUTHENTICATION_HEADER);
        if (StringUtils.isEmpty(authCredentials)) {
            throw new DeployInternalException("Can not find HTTP header: Authorization!");
        }

        String[] schemeAndToken = authCredentials.trim().split(" ");
        String scheme = schemeAndToken[0].trim();
        String token = schemeAndToken[1].trim();

        if (scheme.equalsIgnoreCase(TELETRAAN_TOKEN_SCHEME)) {
            return userDataHelper.getUserSecurityContext(token);
        }

        throw new DeployInternalException("Authorization scheme " + scheme + " is not supported!");
    }
}

@@ -1958,32 +1958,89 @@
 s IOException %7B%0A
+        if(!context.getMethod().equals(%22OPTIONS%22)) %7B%0A    
         Security
@@ -2064,16 +2064,21 @@
 ontext;%0A
+%0A    
         
@@ -2095,16 +2095,20 @@
         
+   
+ 
 security
@@ -2148,16 +2148,20 @@
         
+    
 %7D catch 
@@ -2188,16 +2188,20 @@
         
+  
+  
 LOG.info
@@ -2246,32 +2246,36 @@
 .getMessage());%0A
+    
             thro
@@ -2352,16 +2352,24 @@
         
+        
 %22Failed 
@@ -2419,16 +2419,20 @@
         
+    
 %7D%0A      
@@ -2433,16 +2433,20 @@
         
+ 
+   
 context.
@@ -2474,32 +2474,42 @@
 curityContext);%0A
+        %7D%0A
     %7D%0A%0A    priva

Test auth in swagger ui