<commit_before>package org.xcolab.view.pages.loginregister.singlesignon;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.util.UriComponentsBuilder;

import org.xcolab.client.admin.enums.ConfigurationAttributeKey;
import org.xcolab.util.http.RequestHelper;
import org.xcolab.util.http.UriBuilder;
import org.xcolab.util.http.exceptions.translation.TranslationErrorHandler;
import org.xcolab.util.http.exceptions.translation.facebook.FacebookExceptionTranslator;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;

public final class FacebookUtil {

    private static final Logger _log = LoggerFactory.getLogger(FacebookUtil.class);

    public static final String FB_PROFILE_PIC_URL_FORMAT_STRING =
            "https://graph.facebook.com/%d/?fields=picture.type(large)";
    public static final String AUTH_URL = "https://graph.facebook.com/oauth/authorize";

    private static final String GRAPH_URL = "https://graph.facebook.com";
    private static final String TOKEN_URL = "https://graph.facebook.com/oauth/access_token";

    private static final RequestHelper requestHelper = new RequestHelper(
            new TranslationErrorHandler(new FacebookExceptionTranslator()));

    private FacebookUtil() {
    }

    /**
     * Get the real image URL
     */
    public static String getFacebookPictureURLString(String fbProfilePictureURL) {
        try {
            //TODO: this doesn't work -> requires token!
            // Get real facebook image URL
            InputStream is = new URL(fbProfilePictureURL).openStream();
            StringWriter writer = new StringWriter();
            IOUtils.copy(is, writer, "UTF-8");
            String json = writer.toString();

            JSONObject fbJson = new JSONObject(json);
            return fbJson.getJSONObject("picture").getJSONObject("data").getString("url");
        } catch (JSONException | IOException e) {
            _log.error("Error while getting facebook picture url", e);
        }

        return null;
    }

    public static String getAccessToken(HttpServletRequest request, String code) {
        UriBuilder uriBuilder = new UriBuilder(UriComponentsBuilder.fromHttpUrl(TOKEN_URL))
                .queryParam("client_id", ConfigurationAttributeKey.FACEBOOK_APPLICATION_ID.get())
                .queryParam("redirect_uri", getAuthRedirectURL(request))
                .queryParam("client_secret", ConfigurationAttributeKey.FACEBOOK_APPLICATION_SECRET.get())
                .queryParam("code", code);
        final String requestResult = requestHelper.post(uriBuilder, null, String.class);
        //TODO: copied from liferay - this could be nicer
        if (StringUtils.isNotEmpty(requestResult)) {
            int x = requestResult.indexOf("access_token=");
            if (x >= 0) {
                int y = requestResult.indexOf(38, x);
                if (y < x) {
                    y = requestResult.length();
                }

                return requestResult.substring(x + 13, y);
            }
        }

        return null;
    }

    public static JSONObject getGraphResources(String path, String accessToken, String fields) {
        UriBuilder uriBuilder = new UriBuilder(UriComponentsBuilder.fromHttpUrl(GRAPH_URL + path))
                .queryParam("access_token", accessToken)
                .queryParam("fields", fields);
        final String json = requestHelper.getUnchecked(uriBuilder, String.class);
        try {
            return new JSONObject(json);
        } catch (JSONException e) {
            return null;
        }
    }

    public static String getAuthRedirectURL(HttpServletRequest request) {
        return ConfigurationAttributeKey.COLAB_URL.get() + SsoEndpoint.FACEBOOK_CALLBACK.getUrl();
    }
}<commit_msg>[COLAB-1868] Fixed parsing of Facebook access token<commit_after>package org.xcolab.view.pages.loginregister.singlesignon;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.util.UriComponentsBuilder;

import org.xcolab.client.admin.enums.ConfigurationAttributeKey;
import org.xcolab.util.http.RequestHelper;
import org.xcolab.util.http.UriBuilder;
import org.xcolab.util.http.exceptions.translation.TranslationErrorHandler;
import org.xcolab.util.http.exceptions.translation.facebook.FacebookExceptionTranslator;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;

public final class FacebookUtil {

    private static final Logger _log = LoggerFactory.getLogger(FacebookUtil.class);

    public static final String FB_PROFILE_PIC_URL_FORMAT_STRING =
            "https://graph.facebook.com/%d/?fields=picture.type(large)";
    public static final String AUTH_URL = "https://graph.facebook.com/oauth/authorize";

    private static final String GRAPH_URL = "https://graph.facebook.com";
    private static final String TOKEN_URL = "https://graph.facebook.com/oauth/access_token";

    private static final RequestHelper requestHelper = new RequestHelper(
            new TranslationErrorHandler(new FacebookExceptionTranslator()));

    private FacebookUtil() {
    }

    /**
     * Get the real image URL
     */
    public static String getFacebookPictureURLString(String fbProfilePictureURL) {
        try {
            //TODO: this doesn't work -> requires token!
            // Get real facebook image URL
            InputStream is = new URL(fbProfilePictureURL).openStream();
            StringWriter writer = new StringWriter();
            IOUtils.copy(is, writer, "UTF-8");
            String json = writer.toString();

            JSONObject fbJson = new JSONObject(json);
            return fbJson.getJSONObject("picture").getJSONObject("data").getString("url");
        } catch (JSONException | IOException e) {
            _log.error("Error while getting facebook picture url", e);
        }

        return null;
    }

    public static String getAccessToken(HttpServletRequest request, String code) {
        UriBuilder uriBuilder = new UriBuilder(UriComponentsBuilder.fromHttpUrl(TOKEN_URL))
                .queryParam("client_id", ConfigurationAttributeKey.FACEBOOK_APPLICATION_ID.get())
                .queryParam("redirect_uri", getAuthRedirectURL(request))
                .queryParam("client_secret", ConfigurationAttributeKey.FACEBOOK_APPLICATION_SECRET.get())
                .queryParam("code", code);
        final String requestResult = requestHelper.post(uriBuilder, null, String.class);

        if (StringUtils.isEmpty(requestResult)) {
            throw new FacebookResponseException("Token request returned empty string");
        }
        JSONObject json = new JSONObject(requestResult);
        if (!json.has("access_token")) {
            throw new FacebookResponseException("No access token returned.");
        }
        return json.getString("access_token");
    }

    public static JSONObject getGraphResources(String path, String accessToken, String fields) {
        UriBuilder uriBuilder = new UriBuilder(UriComponentsBuilder.fromHttpUrl(GRAPH_URL + path))
                .queryParam("access_token", accessToken)
                .queryParam("fields", fields);
        final String json = requestHelper.getUnchecked(uriBuilder, String.class);
        try {
            return new JSONObject(json);
        } catch (JSONException e) {
            return null;
        }
    }

    public static String getAuthRedirectURL(HttpServletRequest request) {
        return ConfigurationAttributeKey.COLAB_URL.get() + SsoEndpoint.FACEBOOK_CALLBACK.getUrl();
    }

    private static class FacebookResponseException extends RuntimeException {

        public FacebookResponseException(String message) {
            super(message);
        }
    }
}
{'fraction_non_alphanumeric': 0.09522646807471616, 'fraction_numerical': 0.0020754486631668905, 'mean_word_length': 3.1269521410579344, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '21453', 'n_tokens_mistral': 2211, 'n_tokens_neox': 2119, 'n_words': 474}