Package net.ex337.scriptus.server.frontend.auth.transports

Source Code of net.ex337.scriptus.server.frontend.auth.transports.TwitterConnectionServlet

package net.ex337.scriptus.server.frontend.auth.transports;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ex337.scriptus.config.ScriptusConfig.TransportType;
import net.ex337.scriptus.exceptions.ScriptusRuntimeException;
import net.ex337.scriptus.model.TransportAccessToken;
import net.ex337.scriptus.server.frontend.auth.BaseServlet;

import org.brickred.socialauth.AuthProvider;
import org.brickred.socialauth.SocialAuthConfig;
import org.brickred.socialauth.SocialAuthManager;
import org.brickred.socialauth.util.SocialAuthUtil;

/**
*
* Servlet responsible for doing Twitter oAuth token requesting and saving.
*
* Uses openID for identifying users.
*
* @author ian
*
*/
public class TwitterConnectionServlet extends BaseServlet {

    private static final long serialVersionUID = -2332232512812118451L;

    private static final String SOCIAL_AUTH_MANAGER = "socialAuthManager";
   
    private SocialAuthConfig socialAuthConfig;
   
    private boolean notConfigured;
   
    @Override
    public void init() {
        super.init();
       
        Properties socialAuthProps = new Properties();
       
        if(f.getTwitterConsumerKey() == null) {
            notConfigured = true;
            return;
        }
       
        socialAuthProps.put("twitter.com.consumer_key", f.getTwitterConsumerKey());
        socialAuthProps.put("twitter.com.consumer_secret", f.getTwitterConsumerSecret());
       
        socialAuthConfig = SocialAuthConfig.getDefault();
        try{
            socialAuthConfig.load(socialAuthProps);
        } catch(Exception e)  {
            throw new ScriptusRuntimeException(e);
        }
       

    }
 
  @Override
  protected void doAuthGet(HttpServletRequest req, HttpServletResponse resp, String openid) throws ServletException, IOException {
   
      SocialAuthManager manager = (SocialAuthManager) req.getSession().getAttribute(SOCIAL_AUTH_MANAGER);
     
      if(manager == null) {
          /*
           * then either the session expired or the user is getting the
           * link they were posted to.
           */
          resp.sendRedirect(req.getContextPath()+"/connect");
          return;
      }
     
      Map<String, String> paramsMap = SocialAuthUtil.getRequestParametersMap(req);
     
      AuthProvider provider;
        try {
            provider = manager.connect(paramsMap);
        } catch (Exception e) {
            throw new ScriptusRuntimeException(e);
        }

        String tokenKey = provider.getAccessGrant().getKey();
        String tokenSecret = provider.getAccessGrant().getSecret();

        TransportAccessToken twitterT = new TransportAccessToken(openid, TransportType.Twitter, tokenKey, tokenSecret);
       
        d.saveTransportAccessToken(twitterT);
       
        req.getSession().removeAttribute(SOCIAL_AUTH_MANAGER);
       
        resp.sendRedirect(req.getContextPath()+"/transports");

  }

  @Override
  protected void doAuthPost(HttpServletRequest req, HttpServletResponse resp, String openid) throws ServletException, IOException {

        SocialAuthManager manager = new SocialAuthManager();
        try {
           
            manager.setSocialAuthConfig(socialAuthConfig);
           
            String successURL = req.getRequestURL().toString();
           
            String url = manager.getAuthenticationUrl(TransportType.Twitter.toString().toLowerCase(), successURL);
           
            req.getSession().setAttribute(SOCIAL_AUTH_MANAGER, manager);

            resp.sendRedirect(url);
           
        } catch (Exception e) {
            throw new ScriptusRuntimeException(e);
        }
       
  }

    @Override
    protected String getPageLabel() {
        return "transports";
    }

}
TOP

Related Classes of net.ex337.scriptus.server.frontend.auth.transports.TwitterConnectionServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.