Commit ed043b2a authored by Michel Dirix's avatar Michel Dirix

- Connection with OAuth (Google, Facebook, Twitter)

- javascript alert replaced by notifications
parent 0fd3b602
......@@ -69,7 +69,9 @@
<!-- <hibernate.connection.url>jdbc:mysql://mysql-studio-frascati.jelastic.com/easysoa?autoReconnect=true</hibernate.connection.url> -->
<!-- <hibernate.connection.username>root</hibernate.connection.username> -->
<!-- <hibernate.connection.password></hibernate.connection.password> -->
<!-- <hibernate.connection.url>jdbc:mysql://mysql-stutio-frascati-dev.jelastic.dogado.eu/studio?autoReconnect=true</hibernate.connection.url> -->
<!-- <hibernate.connection.username>root</hibernate.connection.username> -->
<!-- <hibernate.connection.password></hibernate.connection.password> -->
</properties>
......
/**
* EasySOA
*
* Copyright (C) 2011-2012 Inria, University of Lille 1
*
* 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 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
*
* Contact: frascati@ow2.org
*
* Author: Michel Dirix
*
* Contributor(s):
*
*/
package org.easysoa.api;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.easysoa.model.User;
public interface OAuth {
String getId();
String getAuthentificationUrl(HttpServletRequest req, HttpServletResponse rep);
User getUserInformations(HttpServletRequest req, HttpServletResponse rep);
}
/**
* EasySOA
*
* Copyright (C) 2011-2012 Inria, University of Lille 1
*
* 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 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
*
* Contact: frascati@ow2.org
*
* Author: Michel Dirix
*
* Contributor(s):
*
*/
package org.easysoa.api;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.easysoa.model.User;
import org.osoa.sca.annotations.Service;
@Service
public interface OAuthProcessor {
/**
* Give the url where the user have to authorize the application to use its social account
* @param id the id of the social network (Twitter, Google, Facebook)
* @param req session request
* @param rep session response
* @return the url
*/
String getAuthentificationUrl(String id, HttpServletRequest req, HttpServletResponse rep);
/**
* Give the user from the social account
* @param id the id of the social network (Twitter, Google, Facebook)
* @param req session request
* @param rep session response
* @return the informations
*/
User getUserInformations(String id, HttpServletRequest req, HttpServletResponse rep);
}
......@@ -28,13 +28,11 @@
package org.easysoa.api;
import org.easysoa.model.SocialNetwork;
import org.easysoa.model.User;
import org.osoa.sca.annotations.Service;
/**
*
* @author dirix
*/
@Service
public interface Users {
......@@ -69,7 +67,7 @@ public interface Users {
* Search User with the corresponding <b>id</b>
*
* @param id
* @return
* @return the user found
*/
User searchUser(Long id);
......@@ -77,7 +75,34 @@ public interface Users {
* Search User with the corresponding <b>idString</b>
*
* @param idString
* @return
* @return the user found
*/
User searchUser(String idString);
/**
* Test if an user exists with the given social id
* @param socialId the social id (id of the social account)
* @param network the id's network (Twitter, Facebook, Google)
* @return true if the user exists, false otherwises
*/
boolean isSocialUserExist(String socialId, String network);
/**
* Connect an user by socials informations
* @param socialId the id of the social account
* @param network the id's network (Twitter, Facebook, Google)
* @return
*/
User connectBySocialId(String socialId, String network);
/**
* Create new account from social network informations and account creation form
* @param user
* @param civility
* @param town
* @param country
* @return the user
*/
User createSocialAccount(User user, String civility, String town,
String country);
}
package org.easysoa.impl;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.easysoa.api.OAuth;
import org.easysoa.model.Civility;
import org.easysoa.model.SocialNetwork;
import org.easysoa.model.User;
import org.json.JSONObject;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.FacebookApi;
import org.scribe.builder.api.TwitterApi;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
public class FacebookOAuthImpl extends HttpServlet implements OAuth{
private static final String API_KEY = "381556121883076";
private static final String API_SECRET = "13ef43c0b18a72154f9593c798b26d6c";
private static final String CALLBACK_URL = "http://stutio-frascati-dev.jelastic.dogado.eu/easySoa/index.html?cmd=Facebook";
private final static String SCOPE = "publish_actions,publish_stream,status_update";
private static final String REQUEST = "https://graph.facebook.com/me";
@Override
public String getId() {
return "Facebook";
}
@Override
public String getAuthentificationUrl(HttpServletRequest req,
HttpServletResponse rep) {
System.out.println("getAuthentificationUrl Facebook");
OAuthService service = new ServiceBuilder()
.provider(FacebookApi.class)
.apiKey(API_KEY)
.apiSecret(API_SECRET)
.callback(CALLBACK_URL)
.scope(SCOPE)
.build();
req.getSession().setAttribute("service", service);
String authUrl = service.getAuthorizationUrl(null);
return authUrl;
}
@Override
public User getUserInformations(HttpServletRequest req,
HttpServletResponse rep) {
System.out.println("getUserInformations Facebook");
OAuthService service = (OAuthService)req.getSession().getAttribute("service");
Verifier verifier = new Verifier(req.getParameter("code"));
Token accessToken = service.getAccessToken(null, verifier);
OAuthRequest request = new OAuthRequest(Verb.GET,REQUEST);
service.signRequest(accessToken, request);
Response response = request.send();
System.out.println(response.getBody());
return this.parse(response.getBody());
}
private User parse(String body) {
try{
JSONObject jsonObjet = new JSONObject(body);
String id = null;
Civility civility = null;
String name = null;
String surname = null;
if(jsonObjet.getString("gender")!=null || !jsonObjet.getString("gender").equals("")){
if(jsonObjet.getString("gender").equals("female")){
civility = Civility.MRS;
}
else{
civility = Civility.MR;
}
}
if(jsonObjet.getString("last_name")!=null){
surname = jsonObjet.getString("last_name");
}
if(jsonObjet.getString("first_name")!=null){
name = jsonObjet.getString("first_name");
}
if(jsonObjet.getString("id")!=null){
id = jsonObjet.getString("id");
}
User user = new User("",name,surname,"","",civility,null,"",id,SocialNetwork.FACEBOOK);
return user;
}
catch(Exception e){
return null;
}
}
}
package org.easysoa.impl;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.easysoa.api.OAuth;
import org.easysoa.model.Civility;
import org.easysoa.model.SocialNetwork;
import org.easysoa.model.User;
import org.json.JSONObject;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.GoogleApi;
import org.scribe.builder.api.TwitterApi;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
public class GoogleOAuthImpl extends HttpServlet implements OAuth{
private static final String API_KEY = "sdleasysoa2.jelastic.com";
private static final String API_SECRET = "zaoL5ddxKeo7THF_QZP4DjUC";
private static final String CALLBACK_URL = "http://stutio-frascati-dev.jelastic.dogado.eu/easySoa/index.html?cmd=Google";
private final static String SCOPE = "https://www.googleapis.com/auth/userinfo.profile";
private static final String REQUEST = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
@Override
public String getId() {
return "Google";
}
@Override
public String getAuthentificationUrl(HttpServletRequest req,
HttpServletResponse rep) {
OAuthService service = new ServiceBuilder()
.provider(GoogleApi.class)
.apiKey(API_KEY)
.apiSecret(API_SECRET)
.callback(CALLBACK_URL)
.scope(SCOPE)
.build();
Token requestToken = service.getRequestToken();
req.getSession().setAttribute("token", requestToken);
req.getSession().setAttribute("service", service);
String authUrl = service.getAuthorizationUrl(requestToken);
return authUrl;
}
@Override
public User getUserInformations(HttpServletRequest req,
HttpServletResponse rep) {
OAuthService service = (OAuthService)req.getSession().getAttribute("service");
Token requestToken = (Token) req.getSession().getAttribute("token");
Verifier verifier = new Verifier(req.getParameter("oauth_verifier"));
Token accessToken = service.getAccessToken(requestToken, verifier);
OAuthRequest request = new OAuthRequest(Verb.GET,REQUEST);
service.signRequest(accessToken, request);
Response response = request.send();
System.out.println(response.getBody());
return this.parse(response.getBody());
}
private User parse(String body) {
try{
JSONObject jsonObjet = new JSONObject(body);
String id = null;
Civility civility = null;
String name = null;
String surname = null;
if(jsonObjet.getString("family_name")!=null){
surname = jsonObjet.getString("family_name");
}
if(jsonObjet.getString("given_name")!=null){
name = jsonObjet.getString("given_name");
}
if(jsonObjet.getString("id")!=null){
id = jsonObjet.getString("id");
}
User user = new User("",name,surname,"","",civility,null,"",id,SocialNetwork.GOOGLE);
return user;
}
catch(Exception e){
e.printStackTrace();
return null;
}
}
}
/**
* EasySOA
*
* Copyright (C) 2011-2012 Inria, University of Lille 1
*
* 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 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
*
* Contact: frascati@ow2.org
*
* Author: Michel Dirix
*
* Contributor(s):
*
*/
package org.easysoa.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.easysoa.api.OAuth;
import org.easysoa.api.OAuthProcessor;
import org.easysoa.model.User;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Reference;
import org.osoa.sca.annotations.Scope;
@Scope("COMPOSITE")
public class OAuthProcessorImpl implements OAuthProcessor{
@Reference
protected List<OAuth> processors;
protected Map<String, OAuth> processorMap;
@Init
public final void initializeProcessorsByID() {
this.processorMap = new HashMap<String, OAuth>();
for (OAuth social : this.processors) {
this.processorMap.put(social.getId(), social);
System.out.println(social.getId());
}
}
@Override
public String getAuthentificationUrl(String id, HttpServletRequest req,
HttpServletResponse rep) {
return this.processorMap.get(id).getAuthentificationUrl(req, rep);
}
@Override
public User getUserInformations(String id, HttpServletRequest req,
HttpServletResponse rep) {
System.out.println("getUserInformations : " + id);
return this.processorMap.get(id).getUserInformations(req,rep);
}
}
......@@ -221,7 +221,7 @@ public class ServiceManagerImpl implements ServiceManager {
}
@Override
public Composite searchComposite(String name, User user) {
System.out.println("search composite : "+name);
try {
......
/**
* EasySOA
*
* Copyright (C) 2011-2012 Inria, University of Lille 1
*
* 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 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
*
* Contact: frascati@ow2.org
*
* Author: Michel Dirix
*
* Contributor(s):
*
*/
package org.easysoa.impl;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.easysoa.api.OAuth;
import org.easysoa.model.SocialNetwork;
import org.easysoa.model.User;
import org.json.JSONObject;
import org.osoa.sca.annotations.Scope;
import org.osoa.sca.annotations.Service;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.TwitterApi;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
@Service(Servlet.class)
@Scope("COMPOSITE")
public class TwitterOAuthImpl extends HttpServlet implements OAuth {
private static final String API_KEY = "Oi4FAURNtHdCTlbWB6JTA";
private static final String API_SECRET = "0MgvjRmvzYisISLe69AfDZMvpk7kmcwbGy2bwAj6ec";
private static final String CALLBACK_URL = "http://stutio-frascati-dev.jelastic.dogado.eu/easySoa/index.html?cmd=Twitter";
private static final String REQUEST = "http://api.twitter.com/1/account/verify_credentials.json";
@Override
public String getId() {
return "Twitter";
}
@Override
public String getAuthentificationUrl(HttpServletRequest req,
HttpServletResponse rep) {
OAuthService service = new ServiceBuilder()
.provider(TwitterApi.class)
.apiKey(API_KEY)
.apiSecret(API_SECRET)
.callback(CALLBACK_URL)
.build();
req.getSession().setAttribute("service", service);
Token requestToken = service.getRequestToken();
req.getSession().setAttribute("token", requestToken);
String authUrl = service.getAuthorizationUrl(requestToken);
return authUrl;
}
@Override
public User getUserInformations(HttpServletRequest req,
HttpServletResponse rep) {
try {
OAuthService service = new ServiceBuilder()
.provider(TwitterApi.class)
.apiKey(API_KEY)
.apiSecret(API_SECRET)
.callback(CALLBACK_URL)
.build();
Token requestToken = (Token) req.getSession().getAttribute("token");
Verifier verifier = new Verifier(req.getParameter("oauth_verifier"));
Token accessToken = service.getAccessToken(requestToken, verifier);
OAuthRequest request = new OAuthRequest(Verb.GET,REQUEST);
service.signRequest(accessToken, request);
Response response = request.send();
System.out.println(response.getBody());
return this.parse(response.getBody());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private User parse(String body) {
try{
JSONObject jsonObjet = new JSONObject(body);
String name = null;
String id = null;
if(jsonObjet.getString("name")!=null){
name = jsonObjet.getString("name");
}
if(jsonObjet.getString("id")!=null){
id = jsonObjet.getString("id");
}
System.out.println("Parse id : "+id);
User user = new User("","",name,"","",null,null,"", id, SocialNetwork.TWITTER);
return user;
}
catch(Exception e){
return null;
}
}
}
......@@ -31,6 +31,7 @@ import java.io.File;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import org.easysoa.api.Friends;
......@@ -39,6 +40,7 @@ import org.easysoa.api.PreferencesManagerItf;
import org.easysoa.api.Provider;
import org.easysoa.api.Users;
import org.easysoa.model.Civility;
import org.easysoa.model.SocialNetwork;
import org.easysoa.model.Town;
import org.easysoa.model.User;
import org.easysoa.utils.PasswordManager;
......@@ -59,7 +61,7 @@ public class UsersImpl implements Users {
@Override
public User connect(String login, String password) {
public User connect(String login, String password) {
try {
String encryptedPass = PasswordManager.cryptPassword(password);
EntityManager em = db.get();
......@@ -91,7 +93,7 @@ public class UsersImpl implements Users {
return null;
}
try{
user = new User(login, name, surname, encryptedPass, mail, civilityValue, t, birthday);
user = new User(login, name, surname, encryptedPass, mail, civilityValue, t, birthday, null, null);
em.getTransaction().begin();
this.createWorkspace(user);
......@@ -107,8 +109,41 @@ public class UsersImpl implements Users {
}
//TODO - Send mail to user
User userx = em.find(User.class, user.getId());
return userx;
return user;
}
@Override
public User createSocialAccount(User user,String civility, String town, String country){
EntityManager em = db.get();
Civility civilityValue = Civility.fromValue(civility);
Town t = null;
try {
t = Town.searchTown(em, town, country);
}
catch(Exception e){
return null;
}
try{
user.setCivility(civilityValue);
user.setTown(t);
em.getTransaction().begin();
this.createWorkspace(user);
em.persist(user);
em.getTransaction().commit();
} catch (Exception e) {
em.getTransaction().rollback();
Logger.getLogger("EasySOALogger").severe("Error trying to create accounting: " + e.getMessage());
e.printStackTrace();
return null;
}
//TODO - Send mail to user
return user;
}
private void createWorkspace(User user) {
......@@ -139,4 +174,41 @@ public class UsersImpl implements Users {
Long id = Long.parseLong(idString);
return this.searchUser(id);
}
@Override
public boolean isSocialUserExist(String socialId, String socialNetwork) {
System.out.println("isSocialUserExist : "+socialId + "/" +socialNetwork);
EntityManager em = db.get();
Query query = em.createQuery("SELECT u FROM AppUser u WHERE u.socialNetworkId = :id and u.socialNetwork = :network");
query.setParameter("id", socialId);
query.setParameter("network", SocialNetwork.fromValue(socialNetwork));
try{
User user = (User)query.getSingleResult();
if(user != null){
return true;
}
}
catch(NoResultException nre){
return false;
}
return false;
}
@Override
public User connectBySocialId(String socialId, String socialNetwork) {
try {
System.out.println("connectBySocialId : "+socialId + "/" +socialNetwork);
EntityManager em = db.get();
Query query = em.createQuery("SELECT u FROM AppUser u WHERE u.socialNetworkId = :id and u.socialNetwork = :network");
query.setParameter("id", socialId);