001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.googleapps;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.Http;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.PropertiesUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.Time;
025    import com.liferay.portal.model.Company;
026    import com.liferay.portal.service.CompanyLocalServiceUtil;
027    import com.liferay.portal.util.PrefsPropsUtil;
028    
029    import java.util.Properties;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class GAuthenticator {
035    
036            public GAuthenticator(long companyId) {
037                    _companyId = companyId;
038    
039                    init(true);
040            }
041    
042            public String getAuthenticationToken() {
043                    init(false);
044    
045                    return _authenticationToken;
046            }
047    
048            public long getCompanyId() {
049                    return _companyId;
050            }
051    
052            public String getDomain() {
053                    return _domain;
054            }
055    
056            public void init(boolean manual) {
057                    if (manual || isStale()) {
058                            try {
059                                    doInit();
060                            }
061                            catch (Exception e) {
062                                    _log.error(e, e);
063                            }
064                    }
065            }
066    
067            protected void doInit() throws Exception {
068                    Company company = CompanyLocalServiceUtil.getCompany(_companyId);
069    
070                    _domain = company.getMx();
071                    _userName = PrefsPropsUtil.getString(
072                            _companyId, PropsKeys.GOOGLE_APPS_USERNAME);
073                    _password = PrefsPropsUtil.getString(
074                            _companyId, PropsKeys.GOOGLE_APPS_PASSWORD);
075    
076                    if (!_userName.contains(StringPool.AT)) {
077                            _userName += StringPool.AT + _domain;
078                    }
079    
080                    Http.Options options = new Http.Options();
081    
082                    options.addPart("Email", _userName);
083                    options.addPart("Passwd", _password);
084                    options.addPart("accountType", "HOSTED");
085                    options.addPart("service", "apps");
086                    options.setLocation("https://www.google.com/accounts/ClientLogin");
087                    options.setPost(true);
088    
089                    String content = HttpUtil.URLtoString(options);
090    
091                    Properties properties = PropertiesUtil.load(content);
092    
093                    _authenticationToken = properties.getProperty("Auth");
094    
095                    _initTime = System.currentTimeMillis();
096            }
097    
098            private boolean isStale() {
099                    if ((_initTime + (Time.HOUR * 23)) > System.currentTimeMillis()) {
100                            return false;
101                    }
102                    else {
103                            return true;
104                    }
105            }
106    
107            private static Log _log = LogFactoryUtil.getLog(GAuthenticator.class);
108    
109            private String _authenticationToken;
110            private long _companyId;
111            private String _domain;
112            private long _initTime;
113            private String _password;
114            private String _userName;
115    
116    }