<commit_before>//
//CardBox library - framework for matchmaking networked games
//Copyright (C) 2005-2011 Three Rings Design, Inc., All Rights Reserved
//http://github.com/threerings/game-gardens
//
//This library is free software; you can redistribute it and/or modify it
//under the terms of the GNU Lesser General Public License as published
//by the Free Software Foundation; either version 2.1 of the License, or
//(at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.hextilla.cardbook.logic;

import java.util.Calendar;
import java.util.logging.Level;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.restfb.FacebookClient;
import com.restfb.DefaultFacebookClient;
import com.restfb.types.User;

import com.samskivert.servlet.user.InvalidUsernameException;
import com.samskivert.servlet.user.Username;
import com.samskivert.servlet.util.FriendlyException;
import com.samskivert.servlet.util.ParameterUtil;

import com.samskivert.util.StringUtil;
import com.samskivert.util.Tuple;
import com.samskivert.velocity.Application;
import com.samskivert.velocity.InvocationContext;
import com.samskivert.velocity.Logic;

import com.hextilla.cardbox.data.CardBoxCodes;
import com.hextilla.cardbox.server.CardBoxConfig;
import com.hextilla.cardbox.server.persist.GameRecord;

import com.hextilla.cardbook.CardbookApp;
import com.hextilla.cardbook.auth.FBUserManager;

import com.hextilla.cardbox.server.persist.FBUserRecord;
import com.hextilla.cardbox.server.persist.FBUserRepository;

import com.hextilla.cardbox.facebook.CardBoxFacebookConfig;

import static com.hextilla.cardbook.Log.log;

/**
*  OAuth 2.0 authentication through Facebook, and
*  handling user authentication accordingly
*/
public class fbauth implements Logic
{
	 // documentation inherited
	 public void invoke (Application app, InvocationContext ctx)
	     throws Exception
	 {
	     HttpServletRequest req = ctx.getRequest();
	     HttpServletResponse rsp = ctx.getResponse();
	     
	     String fbcode = req.getParameter("code");
		 if (StringUtil.isBlank(fbcode))
		 {
			 throw new FriendlyException("error.fbauth_failure");
		 }
		 
		 URL authUrl = new URL(CardBoxFacebookConfig.getAuthURL(fbcode));
		 Tuple<String,Integer> accessCreds = getAuthenticated(authUrl);
		 if (accessCreds == null) {
			 throw new FriendlyException("error.fbauth_failure");
		 }
		 
		 String accessToken = accessCreds.left;
		 int expires = accessCreds.right;
		 
		 FacebookClient fbClient = new DefaultFacebookClient(accessToken);
		 User fbUser = fbClient.fetchObject("me", User.class);
		 
		 boolean playerExists = false, authSuccess = false;
		 FBUserRecord authUser = null;
		 FBUserManager userman = ((CardbookApp)app).getUserManager();
		 
		 // Attempt to log the user in, assuming they've played the game before
		 // If they're new, we need to create an account for them.
		 try {
			 authUser = userman.login(fbUser.getId(), accessToken, expires, req, rsp);
			 playerExists = true;
		 } catch (Exception e) {
			 log.info("New user: [user=" + fbUser.getId() + ",token=" + accessToken + "] Successful FB Authentication");
		 }
		 if (!playerExists) {
			 Calendar now = Calendar.getInstance();
			 FBUserRepository repo = userman.getRepository();
			 try {
				 authUser = new FBUserRecord();
				 authUser.init(fbUser, now.getTimeInMillis());
				 repo.insertUser(authUser);
				 authUser = userman.login(fbUser.getId(), accessToken, expires, req, rsp);
			 } catch (Exception e) {
				 log.warning("Creating new user: [user=" + fbUser.getId() + ",token=" + accessToken + "] failed due to exception", e);
				 throw new FriendlyException("error.database_error");
			 }
		 }
		 
		 ctx.put("fbauthed", authSuccess);
		 if (authUser != null) {<commit_msg>Fixed redirection on successful authentication<commit_after>//
//CardBox library - framework for matchmaking networked games
//Copyright (C) 2005-2011 Three Rings Design, Inc., All Rights Reserved
//http://github.com/threerings/game-gardens
//
//This library is free software; you can redistribute it and/or modify it
//under the terms of the GNU Lesser General Public License as published
//by the Free Software Foundation; either version 2.1 of the License, or
//(at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.hextilla.cardbook.logic;

import java.util.Calendar;
import java.util.logging.Level;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.restfb.FacebookClient;
import com.restfb.DefaultFacebookClient;
import com.restfb.types.User;

import com.samskivert.servlet.user.InvalidUsernameException;
import com.samskivert.servlet.user.Username;
import com.samskivert.servlet.util.FriendlyException;
import com.samskivert.servlet.util.ParameterUtil;

import com.samskivert.util.StringUtil;
import com.samskivert.util.Tuple;
import com.samskivert.velocity.Application;
import com.samskivert.velocity.InvocationContext;
import com.samskivert.velocity.Logic;

import com.hextilla.cardbox.data.CardBoxCodes;
import com.hextilla.cardbox.server.CardBoxConfig;
import com.hextilla.cardbox.server.persist.GameRecord;

import com.hextilla.cardbook.CardbookApp;
import com.hextilla.cardbook.auth.FBUserManager;

import com.hextilla.cardbox.server.persist.FBUserRecord;
import com.hextilla.cardbox.server.persist.FBUserRepository;

import com.hextilla.cardbox.facebook.CardBoxFacebookConfig;

import static com.hextilla.cardbook.Log.log;

/**
*  OAuth 2.0 authentication through Facebook, and
*  handling user authentication accordingly
*/
public class fbauth implements Logic
{
	 // documentation inherited
	 public void invoke (Application app, InvocationContext ctx)
	     throws Exception
	 {
	     HttpServletRequest req = ctx.getRequest();
	     HttpServletResponse rsp = ctx.getResponse();
	     
	     String fbcode = req.getParameter("code");
		 if (StringUtil.isBlank(fbcode))
		 {
			 throw new FriendlyException("error.fbauth_failure");
		 }
		 
		 URL authUrl = new URL(CardBoxFacebookConfig.getAuthURL(fbcode));
		 Tuple<String,Integer> accessCreds = getAuthenticated(authUrl);
		 if (accessCreds == null) {
			 throw new FriendlyException("error.fbauth_failure");
		 }
		 
		 String accessToken = accessCreds.left;
		 int expires = accessCreds.right;
		 
		 FacebookClient fbClient = new DefaultFacebookClient(accessToken);
		 User fbUser = fbClient.fetchObject("me", User.class);
		 
		 boolean playerExists = false, authSuccess = false;
		 FBUserRecord authUser = null;
		 FBUserManager userman = ((CardbookApp)app).getUserManager();
		 
		 // Attempt to log the user in, assuming they've played the game before
		 // If they're new, we need to create an account for them.
		 try {
			 authUser = userman.login(fbUser.getId(), accessToken, expires, req, rsp);
			 playerExists = true;
		 } catch (Exception e) {
			 log.info("New user: [user=" + fbUser.getId() + ",token=" + accessToken + "] Successful FB Authentication");
		 }
		 if (!playerExists) {
			 Calendar now = Calendar.getInstance();
			 FBUserRepository repo = userman.getRepository();
			 try {
				 authUser = new FBUserRecord();
				 authUser.init(fbUser, now.getTimeInMillis());
				 repo.insertUser(authUser);
				 authUser = userman.login(fbUser.getId(), accessToken, expires, req, rsp);
				 authSuccess = true;
			 } catch (Exception e) {
				 log.warning("Creating new user: [user=" + fbUser.getId() + ",token=" + accessToken + "] failed due to exception", e);
				 throw new FriendlyException("error.database_error");
			 }
		 }
		 
		 ctx.put("fbauthed", authSuccess);
		 if (authUser != null) {
{'fraction_non_alphanumeric': 0.1030316845224527, 'fraction_numerical': 0.0059266013220879874, 'mean_word_length': 4.295715147857574, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1890190', 'n_tokens_mistral': 2814, 'n_tokens_neox': 2467, 'n_words': 730}