AnsweredAssumed Answered

custom Authentication in Alfresco using google apps account

Question asked by nancyaggarwal on Jun 28, 2015
Hi,

I want to login to alfresco share using google apps account and for that i have made my custom authentication folder in C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\alfresco\subsystems\Authentication\mycustomauthentication with the following files:

1. mycustom-authentication.properties
2. mycustom-authentication-context.xml
3. mycustom-filter.properties
4. mycontext-filter-context.xml


and i have added the mycustomauthenticationImpl.java jar file in alfresco/tomcat/webapps/alfresco/WEB-INF/lib



package org.alfresco.repo.security.authentication.MyCustomAuthentication;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import net.sf.acegisecurity.Authentication;

import org.alfresco.repo.security.authentication.AbstractAuthenticationComponent;
import org.alfresco.repo.security.authentication.AuthenticationException;
import org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParseException;
import org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParser;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.http.ParseException;
import org.json.JSONException;
import org.json.JSONObject;
public class MyCustomAuthenticationImpl extends AbstractAuthenticationComponent{
   
     String authorizationURL = "https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.email&state=%2Fprofile&response_type=code&client_id=xxxxxxx&redirect_uri=xxxxx";

   
   

   public void authenticateImpl(String userName, char[] password, HttpServletRequest request) throws AuthenticationException

   {

   System.out.println("userName = "+userName + " ::::::::::::::: password = "+String.valueOf(password));
   
   // Here you can write your own code for authentication any API call or any other authentication code
   //protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
   String code =  request.getParameter("code");
    String foros = "code="+code +
                "&client_id=xxxxx" +
                "&client_secret=xxxx" +
                "&redirect_uri=xxx+
                "&grant_type=authorization_code";
   
    HttpClient client = new HttpClient();
    String url = "https://accounts.google.com/o/oauth2/token";
    PostMethod post = new PostMethod(url);
    post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    try {
        post.setRequestEntity(new StringRequestEntity(foros, null, null));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    String accessToken = null;
    try {
        client.executeMethod(post);
        String resp = post.getResponseBodyAsString();
        JSONParser jsonParser = new JSONParser();
        Object obj = jsonParser.parse(resp);
        JSONObject parsed = (JSONObject)obj;           
        accessToken = (String) parsed.get("access_token");
    } catch (HttpException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    } catch (JSONParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
   
    GetMethod getUserInfo = new GetMethod("https://www.googleapis.com/oauth2/v1/userinfo?access_token='+accessToken);

    String googleId = null;
    String email = null;
    String name = null;
    String firstName = null;
    String lastName = null;
    try {
        client.executeMethod(getUserInfo);
        String resp = getUserInfo.getResponseBodyAsString();
        JSONParser jsonParser = new JSONParser();
        Object obj = jsonParser.parse(resp);
        JSONObject parsed = (JSONObject)obj;
        googleId = (String) parsed.get("id");
        email = (String) parsed.get("email");
        name = (String) parsed.get("name");
        firstName = (String) parsed.get("given_name");
        lastName = (String) parsed.get("family_name");
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    } catch (JSONParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
   
   
   /////////////////////////////////////
   if(!String.valueOf(password).equals(userName + "123"))

   // when authentication fails
   {
   throw new AuthenticationException("Test auth failed…");
   }
   else
   {

      setCurrentUser(userName);
   }
   }

   /**

   * The default is not to support Authentication token base authentication

   */

   public Authentication authenticate(Authentication accessToken ) throws AuthenticationException

   {

   System.out.println("authenticating vi vi token");

   //throw new AlfrescoRuntimeException("Authentication via token not supported");

   return accessToken;

   }

   protected boolean implementationAllowsGuestLogin() {

   // TODO Auto-generated method stub

   return false;

   }

   
}




But i am not able to logs in using my google id.


Below is my alfresco.log


2015-06-28 12:31:31,196 INFO  [org.alfresco.repo.admin] [localhost-startStop-1] Using database URL 'jdbc:postgresql://localhost:5432/alfresco' with user 'alfresco'.
2015-06-28 12:31:32,415 INFO  [org.alfresco.repo.admin] [localhost-startStop-1] Connected to database PostgreSQL version 9.3.6
2015-06-28 12:31:38,665 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'sysAdmin' subsystem, ID: [sysAdmin, default]
2015-06-28 12:31:38,700 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'sysAdmin' subsystem, ID: [sysAdmin, default] complete
2015-06-28 12:31:38,711 INFO  [org.alfresco.repo.domain.schema.SchemaBootstrap] [localhost-startStop-1] Ignoring script patch (post-Hibernate): patch.db-V4.2-metadata-query-indexes
2015-06-28 12:31:48,313 INFO  [org.springframework.extensions.webscripts.TemplateProcessorRegistry] [localhost-startStop-1] Registered template processor Repository Template Processor for extension ftl
2015-06-28 12:31:48,316 INFO  [org.springframework.extensions.webscripts.ScriptProcessorRegistry] [localhost-startStop-1] Registered script processor Repository Script Processor for extension js
2015-06-28 12:31:51,309 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'ContentStore' subsystem, ID: [ContentStore, managed, unencrypted]
2015-06-28 12:31:51,345 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'ContentStore' subsystem, ID: [ContentStore, managed, unencrypted] complete
2015-06-28 12:31:51,420 INFO  [org.alfresco.repo.domain.schema.SchemaBootstrap] [localhost-startStop-1] Connecting to database: jdbc:postgresql://localhost:5432/alfresco, UserName=alfresco, PostgreSQL Native Driver
2015-06-28 12:31:51,420 INFO  [org.alfresco.repo.domain.schema.SchemaBootstrap] [localhost-startStop-1] Schema managed by database dialect org.hibernate.dialect.PostgreSQLDialect.
2015-06-28 12:31:52,566 INFO  [org.alfresco.repo.domain.schema.SchemaBootstrap] [localhost-startStop-1] No changes were made to the schema.
2015-06-28 12:31:53,561 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'Authentication' subsystem, ID: [Authentication, managed, mycustomauthentication]
2015-06-28 12:31:53,610 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'Authentication' subsystem, ID: [Authentication, managed, mycustomauthentication] complete
2015-06-28 12:31:56,085 INFO  [org.alfresco.enterprise.repo.sync.SyncAdminServiceImpl] [localhost-startStop-1] There is no key for cloud sync, cloud sync turned off
2015-06-28 12:31:56,103 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'thirdparty' subsystem, ID: [thirdparty, default]
2015-06-28 12:31:58,592 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'thirdparty' subsystem, ID: [thirdparty, default] complete
2015-06-28 12:31:58,592 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'OOoDirect' subsystem, ID: [OOoDirect, default]
2015-06-28 12:31:58,696 WARN  [org.alfresco.util.AbstractTriggerBean] [localhost-startStop-1] Job openOfficeConnectionTesterTrigger is not enabled
2015-06-28 12:31:58,951 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'OOoDirect' subsystem, ID: [OOoDirect, default] complete
2015-06-28 12:31:58,952 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'OOoJodconverter' subsystem, ID: [OOoJodconverter, default]
2015-06-28 12:31:58,999 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'OOoJodconverter' subsystem, ID: [OOoJodconverter, default] complete
2015-06-28 12:31:59,002 WARN  [org.alfresco.enterprise.repo.cluster.core.ClusteringBootstrap] [localhost-startStop-1] License does not permit clustering: clustering is disabled.
2015-06-28 12:31:59,016 INFO  [org.alfresco.repo.admin.ConfigurationChecker] [localhost-startStop-1] The Alfresco root data directory ('dir.root') is: C:\Alfresco\alf_data
2015-06-28 12:31:59,017 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'Search' subsystem, ID: [Search, managed, solr4]
2015-06-28 12:31:59,411 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'Search' subsystem, ID: [Search, managed, solr4] complete
2015-06-28 12:31:59,439 INFO  [org.alfresco.repo.admin.patch.PatchExecuter] [localhost-startStop-1] Checking for patches to apply …
2015-06-28 12:31:59,906 INFO  [org.alfresco.repo.admin.patch.PatchExecuter] [localhost-startStop-1] No patches were required.
2015-06-28 12:31:59,939 INFO  [org.alfresco.repo.module.ModuleServiceImpl] [localhost-startStop-1] Found 1 module(s).
2015-06-28 12:32:00,016 INFO  [org.alfresco.repo.module.ModuleServiceImpl] [localhost-startStop-1] Starting module 'org.alfresco.integrations.google.docs' version 3.0.0.
2015-06-28 12:32:00,020 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'fileServers' subsystem, ID: [fileServers, default]
2015-06-28 12:32:00,602 ERROR [org.alfresco.fileserver] [localhost-startStop-1] No enabled CIFS authenticator found in authentication chain. CIFS Server disabled
2015-06-28 12:32:00,603 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'fileServers' subsystem, ID: [fileServers, default] complete
2015-06-28 12:32:00,603 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'imap' subsystem, ID: [imap, default]
2015-06-28 12:32:00,710 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'imap' subsystem, ID: [imap, default] complete
2015-06-28 12:32:00,710 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'email' subsystem, ID: [email, outbound]
2015-06-28 12:32:00,840 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'email' subsystem, ID: [email, outbound] complete
2015-06-28 12:32:00,840 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'email' subsystem, ID: [email, inbound]
2015-06-28 12:32:00,951 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'email' subsystem, ID: [email, inbound] complete
2015-06-28 12:32:00,951 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'Subscriptions' subsystem, ID: [Subscriptions, default]
2015-06-28 12:32:00,974 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'Subscriptions' subsystem, ID: [Subscriptions, default] complete
2015-06-28 12:32:01,027 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'Synchronization' subsystem, ID: [Synchronization, default]
2015-06-28 12:32:01,158 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'Synchronization' subsystem, ID: [Synchronization, default] complete
2015-06-28 12:32:01,284 INFO  [org.alfresco.service.descriptor.DescriptorService] [localhost-startStop-1] Alfresco JVM - v1.8.0_31-b13; maximum heap size 2974.750MB
2015-06-28 12:32:01,315 INFO  [org.alfresco.service.descriptor.DescriptorService] [localhost-startStop-1] Alfresco license: Mode ENTERPRISE, NO CLUSTER granted to Trial User limited to 30 days expiring Thu Jul 23 00:00:00 IST 2015 (25 days remaining).
2015-06-28 12:32:01,315 INFO  [org.alfresco.service.descriptor.DescriptorService] [localhost-startStop-1] Server Mode :UNKNOWN
2015-06-28 12:32:01,316 INFO  [org.alfresco.service.descriptor.DescriptorService] [localhost-startStop-1] Alfresco started (Enterprise). Current version: 5.0.1 (r100823-b68) schema 8,022. Originally installed version: 5.0.1 (r100823-b68) schema 8,022.
2015-06-28 12:32:01,327 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'ActivitiesFeed' subsystem, ID: [ActivitiesFeed, default]
2015-06-28 12:32:01,620 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'ActivitiesFeed' subsystem, ID: [ActivitiesFeed, default] complete
2015-06-28 12:32:01,620 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'Replication' subsystem, ID: [Replication, default]
2015-06-28 12:32:01,637 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'Replication' subsystem, ID: [Replication, default] complete
2015-06-28 12:32:02,812 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Starting 'googledocs' subsystem, ID: [googledocs, drive]
2015-06-28 12:32:02,974 INFO  [org.alfresco.repo.management.subsystems.ChildApplicationContextFactory] [localhost-startStop-1] Startup of 'googledocs' subsystem, ID: [googledocs, drive] complete
2015-06-28 12:32:07,493 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [asynchronouslyRefreshedCacheThreadPool1] Registered 497 Web Scripts (+0 failed), 649 URLs
2015-06-28 12:32:07,495 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [asynchronouslyRefreshedCacheThreadPool1] Registered 1 Package Description Documents (+0 failed)
2015-06-28 12:32:07,497 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [asynchronouslyRefreshedCacheThreadPool1] Registered 0 Schema Description Documents (+0 failed)
2015-06-28 12:32:09,712 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [localhost-startStop-1] Registered 497 Web Scripts (+0 failed), 649 URLs
2015-06-28 12:32:09,712 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [localhost-startStop-1] Registered 1 Package Description Documents (+0 failed)
2015-06-28 12:32:09,712 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [localhost-startStop-1] Registered 0 Schema Description Documents (+0 failed)
2015-06-28 12:32:09,716 INFO  [org.springframework.extensions.webscripts.AbstractRuntimeContainer] [localhost-startStop-1] Initialised Repository Web Script Container (in 6682.205ms)
2015-06-28 12:32:09,735 INFO  [org.springframework.extensions.webscripts.TemplateProcessorRegistry] [localhost-startStop-1] Registered template processor freemarker for extension ftl
2015-06-28 12:32:09,738 INFO  [org.springframework.extensions.webscripts.ScriptProcessorRegistry] [localhost-startStop-1] Registered script processor javascript for extension js
2015-06-28 12:32:11,855 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [asynchronouslyRefreshedCacheThreadPool1] Registered 497 Web Scripts (+0 failed), 649 URLs
2015-06-28 12:32:11,855 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [asynchronouslyRefreshedCacheThreadPool1] Registered 1 Package Description Documents (+0 failed)
2015-06-28 12:32:11,855 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [asynchronouslyRefreshedCacheThreadPool1] Registered 0 Schema Description Documents (+0 failed)
2015-06-28 12:32:11,916 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [asynchronouslyRefreshedCacheThreadPool1] Registered 11 Web Scripts (+0 failed), 82 URLs
2015-06-28 12:32:11,916 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [asynchronouslyRefreshedCacheThreadPool1] Registered 0 Package Description Documents (+0 failed)
2015-06-28 12:32:11,917 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [asynchronouslyRefreshedCacheThreadPool1] Registered 0 Schema Description Documents (+0 failed)
2015-06-28 12:32:12,074 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [localhost-startStop-1] Registered 11 Web Scripts (+0 failed), 82 URLs
2015-06-28 12:32:12,074 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [localhost-startStop-1] Registered 0 Package Description Documents (+0 failed)
2015-06-28 12:32:12,077 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [localhost-startStop-1] Registered 0 Schema Description Documents (+0 failed)
2015-06-28 12:32:12,082 INFO  [org.springframework.extensions.webscripts.AbstractRuntimeContainer] [localhost-startStop-1] Initialised Public Api Web Script Container (in 2279.512ms)
2015-06-28 12:32:12,211 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [asynchronouslyRefreshedCacheThreadPool1] Registered 11 Web Scripts (+0 failed), 82 URLs
2015-06-28 12:32:12,212 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [asynchronouslyRefreshedCacheThreadPool1] Registered 0 Package Description Documents (+0 failed)
2015-06-28 12:32:12,212 INFO  [org.springframework.extensions.webscripts.DeclarativeRegistry] [asynchronouslyRefreshedCacheThreadPool1] Registered 0 Schema Description Documents (+0 failed)



Please anyone help me with this. I am struggling with this from last 2 weeks.


Thanks & Regards,
Nancy

Outcomes