001/*
002 * (C) Copyright 2006-2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nelson Silva
016 */
017package org.nuxeo.ecm.platform.oauth2.tokens;
018
019import java.util.Calendar;
020import java.util.Date;
021import java.util.HashMap;
022import java.util.Map;
023
024import com.google.api.client.auth.oauth2.StoredCredential;
025import org.apache.commons.lang.RandomStringUtils;
026import org.nuxeo.ecm.core.api.DocumentModel;
027
028public class NuxeoOAuth2Token {
029
030    public static final String SCHEMA = "oauth2Token";
031
032    public static final String KEY_SERVICE_LOGIN = "serviceLogin";
033
034    public static final String KEY_NUXEO_LOGIN = "nuxeoLogin";
035
036    protected Long id;
037
038    protected String serviceName;
039
040    protected String nuxeoLogin;
041
042    protected String accessToken;
043
044    protected String clientId;
045
046    protected Calendar creationDate;
047
048    private String refreshToken;
049
050    private Long expirationTimeMilliseconds;
051
052    private boolean isShared;
053
054    protected String serviceLogin;
055
056    public NuxeoOAuth2Token(long expirationTimeMilliseconds, String clientId) {
057        this("", "", expirationTimeMilliseconds);
058        this.clientId = clientId;
059        refresh();
060    }
061
062    public NuxeoOAuth2Token(String accessToken, String refreshToken, Long expirationTimeMilliseconds) {
063        this.accessToken = accessToken;
064        this.refreshToken = refreshToken;
065        this.expirationTimeMilliseconds = expirationTimeMilliseconds;
066        this.creationDate = Calendar.getInstance();
067        this.isShared = false;
068    }
069
070    public NuxeoOAuth2Token(StoredCredential credential) {
071        this(credential.getAccessToken(), credential.getRefreshToken(), credential.getExpirationTimeMilliseconds());
072    }
073
074    public NuxeoOAuth2Token(DocumentModel entry) {
075        this.id = (Long) entry.getProperty(SCHEMA, "id");
076        this.accessToken = (String) entry.getProperty(SCHEMA, "accessToken");
077        this.refreshToken = (String) entry.getProperty(SCHEMA, "refreshToken");
078        this.expirationTimeMilliseconds = (Long) entry.getProperty(SCHEMA, "expirationTimeMilliseconds");
079        this.serviceName = (String) entry.getProperty(SCHEMA, "serviceName");
080        this.nuxeoLogin = (String) entry.getProperty(SCHEMA, "nuxeoLogin");
081        this.clientId = (String) entry.getProperty(SCHEMA, "clientId");
082        this.creationDate = (Calendar) entry.getProperty(SCHEMA, "creationDate");
083        this.isShared = (Boolean) entry.getProperty(SCHEMA, "isShared");
084        this.serviceLogin = (String) entry.getProperty(SCHEMA, "serviceLogin");
085    }
086
087    public static StoredCredential asCredential(DocumentModel entry) {
088        StoredCredential credential = new StoredCredential();
089        String accessToken = (String) entry.getProperty(SCHEMA, "accessToken");
090        String refreshToken = (String) entry.getProperty(SCHEMA, "refreshToken");
091        Long expirationTimeMilliseconds = (Long) entry.getProperty(SCHEMA, "expirationTimeMilliseconds");
092        credential.setAccessToken(accessToken);
093        credential.setRefreshToken(refreshToken);
094        credential.setExpirationTimeMilliseconds(expirationTimeMilliseconds);
095        return credential;
096    }
097
098    public Map<String, Object> toMap() {
099        Map<String, Object> map = new HashMap<String, Object>();
100        map.put("serviceName", serviceName);
101        map.put("nuxeoLogin", nuxeoLogin);
102        map.put("accessToken", accessToken);
103        map.put("refreshToken", refreshToken);
104        map.put("expirationTimeMilliseconds", expirationTimeMilliseconds);
105        map.put("clientId", clientId);
106        map.put("creationDate", creationDate);
107        map.put("isShared", isShared);
108        map.put("serviceLogin", serviceLogin);
109        return map;
110    }
111
112    public Map<String, Object> toJsonObject() {
113        Map<String, Object> m = new HashMap<>();
114        m.put("access_token", accessToken);
115        m.put("refresh_token", refreshToken);
116        m.put("token_type", "bearer");
117        m.put("expires_in",
118                Math.floor((creationDate.getTimeInMillis() + expirationTimeMilliseconds - new Date().getTime()) / 1000));
119        return m;
120    }
121
122    public void updateEntry(DocumentModel entry) {
123        entry.setProperty(SCHEMA, "serviceName", this.serviceName);
124        entry.setProperty(SCHEMA, "nuxeoLogin", this.nuxeoLogin);
125        entry.setProperty(SCHEMA, "accessToken", this.accessToken);
126        entry.setProperty(SCHEMA, "refreshToken", this.refreshToken);
127        entry.setProperty(SCHEMA, "expirationTimeMilliseconds", this.expirationTimeMilliseconds);
128        entry.setProperty(SCHEMA, "clientId", this.clientId);
129        entry.setProperty(SCHEMA, "isShared", this.isShared);
130        entry.setProperty(SCHEMA, "serviceLogin", this.serviceLogin);
131    }
132
133    public void refresh() {
134        accessToken = RandomStringUtils.random(32, true, true);
135        refreshToken = RandomStringUtils.random(64, true, true);
136        creationDate = Calendar.getInstance();
137    }
138
139    public boolean isExpired() {
140        return creationDate != null
141                && creationDate.getTimeInMillis() + expirationTimeMilliseconds < Calendar.getInstance().getTimeInMillis();
142    }
143
144    public void setServiceName(String serviceName) {
145        this.serviceName = serviceName;
146    }
147
148    public void setNuxeoLogin(String userId) {
149        this.nuxeoLogin = userId;
150    }
151
152    public String getNuxeoLogin() {
153        return nuxeoLogin;
154    }
155
156    public String getAccessToken() {
157        return accessToken;
158    }
159
160    public void setAccessToken(String accessToken) {
161        this.accessToken = accessToken;
162    }
163
164    public String getRefreshToken() {
165        return refreshToken;
166    }
167
168    public void setRefreshToken(String refreshToken) {
169        this.refreshToken = refreshToken;
170    }
171
172    public Long getExpirationTimeMilliseconds() {
173        return expirationTimeMilliseconds;
174    }
175
176    public void setExpirationTimeMilliseconds(Long expirationTimeMilliseconds) {
177        this.expirationTimeMilliseconds = expirationTimeMilliseconds;
178    }
179
180    public String getServiceName() {
181        return serviceName;
182    }
183
184    public String getClientId() {
185        return clientId;
186    }
187
188    public void setClientId(String clientId) {
189        this.clientId = clientId;
190    }
191
192    public boolean isShared() {
193        return isShared;
194    }
195
196    public void setIsShared(boolean isShared) {
197        this.isShared = isShared;
198    }
199
200    public String getServiceLogin() {
201        return serviceLogin;
202    }
203
204    public void setServiceLogin(String serviceLogin) {
205        this.serviceLogin = serviceLogin;
206    }
207
208    public Calendar getCreationDate() {
209        return creationDate;
210    }
211
212    public void setCreationDate(Calendar creationDate) {
213        this.creationDate = creationDate;
214    }
215}