<commit_before>/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.management.security.config.basic.filter;

import com.auth0.jwt.JWTVerifier;
import io.gravitee.common.http.HttpHeaders;
import io.gravitee.common.http.HttpStatusCode;
import io.gravitee.management.idp.api.authentication.UserDetails;
import io.gravitee.management.security.JWTCookieGenerator;
import io.gravitee.management.security.config.JWTClaims;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author <NAME> (azize at gravitee.io)
 * @author GraviteeSource Team
 */
public class JWTAuthenticationFilter extends GenericFilterBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(JWTAuthenticationFilter.class);

    private final JWTCookieGenerator jwtCookieGenerator;
    private final JWTVerifier jwtVerifier;

    public JWTAuthenticationFilter(final JWTCookieGenerator jwtCookieGenerator, final String jwtSecret) {
        this.jwtCookieGenerator = jwtCookieGenerator;
        this.jwtVerifier = new JWTVerifier(jwtSecret);
    }

    @Override
    @SuppressWarnings(value = "unchecked")
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        final Optional<Cookie> optionalStringToken;

        if (req.getCookies() == null) {
            optionalStringToken = Optional.empty();
        } else {
            optionalStringToken = Arrays.stream(req.getCookies())
                    .filter(cookie -> HttpHeaders.AUTHORIZATION.equals(cookie.getName()))
                    .findAny();
        }
        if (optionalStringToken.isPresent()) {
            String stringToken = optionalStringToken.get().getValue();

            final String authorizationSchema = "Bearer";
            if (stringToken.contains(authorizationSchema)) {
                stringToken = stringToken.substring(authorizationSchema.length()).trim();
                try {
                    final Map<String, Object> verify = jwtVerifier.verify(stringToken);

                    final List<SimpleGrantedAuthority> authorities = ((List<Map>) verify.get(JWTClaims.PERMISSIONS)).stream()
                            .map(map -> new SimpleGrantedAuthority(map.get("authority").toString()))
                            .collect(Collectors.toList());

                    final UserDetails userDetails = new UserDetails(getStringValue(verify.get(JWTClaims.SUBJECT)), "",
                            authorities, getStringValue(verify.get(JWTClaims.EMAIL)), getStringValue(verify.get(JWTClaims.FIRSTNAME)),
                            getStringValue(verify.get(JWTClaims.LASTNAME)));

                    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()));
                } catch (Exception e) {
                    LOGGER.error("Invalid token", e);

                    final Cookie bearerCookie = jwtCookieGenerator.generate(null);
                    res.addCookie(bearerCookie);

                    res.sendError(HttpStatusCode.UNAUTHORIZED_401);
                }
            } else {
                LOGGER.info("Authorization schema not found");
            }
        } else {
            LOGGER.info("Authorization cookie not found");
        }
        chain.doFilter(request, response);
    }

    private String getStringValue(final Object object) {
        if (object == null) {
            return "";
        }
        return object.toString();
    }

}<commit_msg>chore: Remove useless log<commit_after>/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.management.security.config.basic.filter;

import com.auth0.jwt.JWTVerifier;
import io.gravitee.common.http.HttpHeaders;
import io.gravitee.common.http.HttpStatusCode;
import io.gravitee.management.idp.api.authentication.UserDetails;
import io.gravitee.management.security.JWTCookieGenerator;
import io.gravitee.management.security.config.JWTClaims;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author <NAME> (azize at gravitee.io)
 * @author GraviteeSource Team
 */
public class JWTAuthenticationFilter extends GenericFilterBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(JWTAuthenticationFilter.class);

    private final JWTCookieGenerator jwtCookieGenerator;
    private final JWTVerifier jwtVerifier;

    public JWTAuthenticationFilter(final JWTCookieGenerator jwtCookieGenerator, final String jwtSecret) {
        this.jwtCookieGenerator = jwtCookieGenerator;
        this.jwtVerifier = new JWTVerifier(jwtSecret);
    }

    @Override
    @SuppressWarnings(value = "unchecked")
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        final Optional<Cookie> optionalStringToken;

        if (req.getCookies() == null) {
            optionalStringToken = Optional.empty();
        } else {
            optionalStringToken = Arrays.stream(req.getCookies())
                    .filter(cookie -> HttpHeaders.AUTHORIZATION.equals(cookie.getName()))
                    .findAny();
        }
        if (optionalStringToken.isPresent()) {
            String stringToken = optionalStringToken.get().getValue();

            final String authorizationSchema = "Bearer";
            if (stringToken.contains(authorizationSchema)) {
                stringToken = stringToken.substring(authorizationSchema.length()).trim();
                try {
                    final Map<String, Object> verify = jwtVerifier.verify(stringToken);

                    final List<SimpleGrantedAuthority> authorities = ((List<Map>) verify.get(JWTClaims.PERMISSIONS)).stream()
                            .map(map -> new SimpleGrantedAuthority(map.get("authority").toString()))
                            .collect(Collectors.toList());

                    final UserDetails userDetails = new UserDetails(getStringValue(verify.get(JWTClaims.SUBJECT)), "",
                            authorities, getStringValue(verify.get(JWTClaims.EMAIL)), getStringValue(verify.get(JWTClaims.FIRSTNAME)),
                            getStringValue(verify.get(JWTClaims.LASTNAME)));

                    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()));
                } catch (Exception e) {
                    LOGGER.error("Invalid token", e);

                    final Cookie bearerCookie = jwtCookieGenerator.generate(null);
                    res.addCookie(bearerCookie);

                    res.sendError(HttpStatusCode.UNAUTHORIZED_401);
                }
            } else {
                LOGGER.info("Authorization schema not found");
            }
        }

        chain.doFilter(request, response);
    }

    private String getStringValue(final Object object) {
        if (object == null) {
            return "";
        }
        return object.toString();
    }

}
{'fraction_non_alphanumeric': 0.09200603318250378, 'fraction_numerical': 0.002815485168426345, 'mean_word_length': 3.094689172498971, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2504190', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2513, 'n_words': 576}