Package org.atomojo.www.apps.login

Source Code of org.atomojo.www.apps.login.LoginAction$LoginActor

/*
* LoginForm.java
*
* Created on September 7, 2007, 10:21 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package org.atomojo.www.apps.login;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.UUID;
import java.util.logging.Level;
import org.atomojo.app.client.XMLRepresentationParser;
import org.atomojo.www.util.Identity;
import org.infoset.xml.Document;
import org.infoset.xml.Element;
import org.infoset.xml.Name;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.ChallengeResponse;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.CookieSetting;
import org.restlet.data.Form;
import org.restlet.data.Method;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.representation.Representation;

/**
*
* @author alex
*/
public class LoginAction extends ActionResource
{
   static Name NAME = Name.create("{http://www.atomojo.org/Vocabulary/Auth/2007/1/0}name");
   static Name EMAIL = Name.create("{http://www.atomojo.org/Vocabulary/Auth/2007/1/0}email");
   public interface LoginActor {
      public void authenticated(Form authForm,Identity identity);
      public void unauthorized();
   }

   static String toString(InputStream is)
      throws IOException
   {
      if (is==null) {
         return null;
      }
      StringBuilder builder = new StringBuilder();
      Reader r = new InputStreamReader(is,"UTF-8");
      char [] buffer = new char[1024];
      int len;
      while ((len=r.read(buffer))>0) {
         builder.append(buffer,0,len);
      }
      return builder.toString();
   }
  
   protected LoginActor actor;

   /** Creates a new instance of LoginForm */
   public LoginAction()
   {
      actor = new LoginActor() {
         public void authenticated(Form authForm,Identity identity)
         {
            String name = getCookieName();
            if (name!=null) {
               CookieSetting cookie = new CookieSetting("I",identity.getSession());
               cookie.setPath(getCookiePath());
               getResponse().getCookieSettings().add(cookie);
            }
            if (name!=null && idManager!=null) {
               idManager.add(identity.getSession(), identity);
            }
            String redirect = authForm.getFirstValue("redirect");
            if (redirect!=null && redirect.length()!=0) {
               getResponse().redirectSeeOther(redirect);
            } else {
               getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
            }
         }
         public void unauthorized() {
            getResponse().setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
         }
      };
   }

   public Representation post(Representation rep) {
      Reference service = getReferenceAttribute(getRequest(),"auth-service",confService);
      if (service==null) {
         getResponse().setStatus(Status.SERVER_ERROR_SERVICE_UNAVAILABLE);
         return null;
      }
      getLogger().info("Using service "+service);
      final Form form = new Form(rep);
      String username = form.getFirstValue("username");
      String domain = form.getFirstValue("domain");
      String email = username;
      if (domain!=null && domain.length()>0 && email.indexOf('@')<0) {
         email += "@"+domain;
      }
      String password = form.getFirstValue("password");
      login(getContext().createChildContext(),service,loginApp,loginType,username,password,email,form,actor);
      return null;
   }
  
   public static void login(Context context,Reference service,String application,String type,String username, String password, String email,Form form,LoginActor actor)
   {
      boolean isGoogle = "google.ClientLogin".equals(type);
      Client client = new Client(context.createChildContext(),service.getSchemeProtocol());
      client.getContext().getAttributes().put("hostnameVerifier", org.apache.commons.ssl.HostnameVerifier.DEFAULT);
      if (isGoogle) {
         Request request = new Request(Method.POST,service);
         Form authForm = new Form();
         context.getLogger().info("Performing google auth for "+username);
         authForm.add("accountType", "HOSTED_OR_GOOGLE");
         authForm.add("service", "apps");
         authForm.add("source", application);
         authForm.add("Email", username);
         authForm.add("Passwd", password);
         request.setEntity(authForm.getWebRepresentation());
         Response response = client.handle(request);
         if (response.getStatus().isSuccess()) {
            context.getLogger().info("Authenticated "+username);
            actor.authenticated(form,new Identity(UUID.randomUUID().toString(),username,username,username,email));
         } else {
            context.getLogger().info("Authorization request for "+username+" returned: "+response.getStatus().getCode());
            actor.unauthorized();
         }
      } else {
         Request request = new Request(Method.GET,service);
         request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,username,password));
         Response response = client.handle(request);
         if (response.getStatus().isSuccess()) {
            XMLRepresentationParser parser = new XMLRepresentationParser();
            try {
               Document doc = parser.load(response.getEntity());
               String session = doc.getDocumentElement().getAttributeValue("id");
               String id = doc.getDocumentElement().getAttributeValue("user-id");
               String alias = doc.getDocumentElement().getAttributeValue("user-alias");
               Element nameE = doc.getDocumentElement().getFirstElementNamed(NAME);
               Element emailE = doc.getDocumentElement().getFirstElementNamed(EMAIL);
               Identity identity = new Identity(session,id,alias,nameE==null ? null : nameE.getText(),emailE==null ? null : emailE.getText());
               context.getLogger().info("Authenticated "+username);
               actor.authenticated(form,identity);
            } catch (Exception ex) {
               context.getLogger().log(Level.SEVERE,"Cannot parse auth result.",ex);
               actor.unauthorized();
            }
         } else {
            context.getLogger().info("Authorization request for "+username+" returned: "+response.getStatus().getCode());
            actor.unauthorized();
         }
      }
   }
  
}
TOP

Related Classes of org.atomojo.www.apps.login.LoginAction$LoginActor

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.