001    /**
002     * Copyright (c) 2000-2013 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 GAuthenticator(String domain, String userName, String password) {
043                    _domain = domain;
044                    _userName = userName;
045                    _password = password;
046    
047                    init(true);
048            }
049    
050            public String getAuthenticationToken() {
051                    init(false);
052    
053                    return _authenticationToken;
054            }
055    
056            public long getCompanyId() {
057                    return _companyId;
058            }
059    
060            public String getDomain() {
061                    return _domain;
062            }
063    
064            public String getError() {
065                    return _error;
066            }
067    
068            public void init(boolean manual) {
069                    if (manual || isStale()) {
070                            try {
071                                    doInit();
072                            }
073                            catch (Exception e) {
074                                    _log.error(e, e);
075                            }
076                    }
077            }
078    
079            protected void doInit() throws Exception {
080                    if (_companyId > 0) {
081                            Company company = CompanyLocalServiceUtil.getCompany(_companyId);
082    
083                            _domain = company.getMx();
084                            _userName = PrefsPropsUtil.getString(
085                                    _companyId, PropsKeys.GOOGLE_APPS_USERNAME);
086                            _password = PrefsPropsUtil.getString(
087                                    _companyId, PropsKeys.GOOGLE_APPS_PASSWORD);
088                    }
089    
090                    Http.Options options = new Http.Options();
091    
092                    options.addPart("accountType", "HOSTED");
093    
094                    String emailAddress = _userName;
095    
096                    if (!emailAddress.contains(StringPool.AT)) {
097                            emailAddress = _userName.concat(StringPool.AT).concat(_domain);
098                    }
099    
100                    options.addPart("Email", emailAddress);
101    
102                    options.addPart("Passwd", _password);
103                    options.addPart("service", "apps");
104                    options.setLocation("https://www.google.com/accounts/ClientLogin");
105                    options.setPost(true);
106    
107                    String content = HttpUtil.URLtoString(options);
108    
109                    Properties properties = PropertiesUtil.load(content);
110    
111                    _error = properties.getProperty("Error");
112    
113                    if ((_error != null) && _log.isInfoEnabled()) {
114                            _log.info("Unable to initialize authentication token: " + _error);
115                    }
116    
117                    _authenticationToken = properties.getProperty("Auth");
118    
119                    if ((_authenticationToken != null) && _log.isInfoEnabled()) {
120                            _log.info("Authentication token " + _authenticationToken);
121                    }
122    
123                    _initTime = System.currentTimeMillis();
124            }
125    
126            protected boolean isStale() {
127                    if ((_initTime + _EXPIRE_TIME) > System.currentTimeMillis()) {
128                            return false;
129                    }
130                    else {
131                            return true;
132                    }
133            }
134    
135            private static final long _EXPIRE_TIME = Time.HOUR;
136    
137            private static Log _log = LogFactoryUtil.getLog(GAuthenticator.class);
138    
139            private String _authenticationToken;
140            private long _companyId;
141            private String _domain;
142            private String _error;
143            private long _initTime;
144            private String _password;
145            private String _userName;
146    
147    }